$App::CSE::Command::Update::VERSION
=
'0.015'
;
my
$LOGGER
= Log::Log4perl->get_logger();
sub
execute{
my
(
$self
) =
@_
;
my
$colorizer
=
$self
->cse()->colorizer();
my
$colored
=
sub
{
$colorizer
->colored(
@_
);};
my
$check
= App::CSE::Command::Check->new({
cse
=>
$self
->cse() });
if
(
$check
->execute() ){
$LOGGER
->info(
&$colored
(
"Rebuilding the index.."
,
'green bold'
));
my
$index_cmd
= App::CSE::Command::Index->new( {
cse
=>
$self
->cse() });
if
(
$index_cmd
->execute() ){
$LOGGER
->error(
&$colored
(
"Building index failed"
,
'red'
));
return
1;
}
return
0;
}
my
@dirty_files
=
sort
keys
%{
$self
->cse->dirty_files()};
unless
(
@dirty_files
){
$LOGGER
->info(
&$colored
(
"No dirty files"
,
'green bold'
));
return
0;
}
my
$searcher
= Lucy::Search::IndexSearcher->new(
index
=>
$self
->cse()->index_dir().
''
);
my
$indexer
= Lucy::Index::Indexer->new(
schema
=>
$searcher
->get_schema(),
index
=>
$self
->cse()->index_dir().
''
);
my
$NFILES
= 0;
foreach
my
$dirty_file
(
@dirty_files
){
$indexer
->delete_by_term(
field
=>
'path.raw'
,
term
=>
$dirty_file
);
unless
( -e
$dirty_file
){
delete
$self
->cse()->dirty_files()->{
$dirty_file
};
next
;
}
my
$mime_type
= File::MimeInfo::Magic::mimetype(
$dirty_file
.
''
) ||
'application/octet-stream'
;
my
$file_class
= App::CSE::File->class_for_mime(
$mime_type
,
$dirty_file
.
''
);
unless
(
$file_class
){
$LOGGER
->
warn
(
"Cannot reindex Mimetype $mime_type for $dirty_file is not supported anymore?"
);
next
;
}
my
$file
=
$file_class
->new({
cse
=>
$self
->cse(),
mime_type
=>
$mime_type
,
file_path
=>
$dirty_file
.
''
})->effective_object();
$LOGGER
->info(
"Reindexing file $dirty_file as "
.
$file
->mime_type());
my
$content
=
$file
->content();
$indexer
->add_doc({
path
=>
$file
->file_path(),
'path.raw'
=>
$file
->file_path(),
dir
=>
$file
->dir(),
mime
=>
$file
->mime_type(),
mtime
=>
$file
->mtime->iso8601(),
$content
? (
content
=>
$content
) : ()
});
delete
$self
->cse()->dirty_files()->{
$dirty_file
};
$NFILES
++;
}
$indexer
->commit();
$self
->cse()->save_dirty_files();
$LOGGER
->info(
&$colored
(
'Re-indexed '
.
$NFILES
.
' files'
,
'green bold'
));
return
0;
}
__PACKAGE__->meta->make_immutable();