62 lines
1.5 KiB
PHP

<?php declare(strict_types=1);
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\Custom;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Mapper\CountMapper;
use FeedReaderCentral\Data;
require __DIR__ . '/../cli-start.php';
cli_title('SEARCH MAINTENANCE');
if ($argc < 2) display_help();
switch ($argv[1]) {
case 'rebuild':
rebuild_index();
break;
default:
printfn('Unrecognized option "%s"', $argv[1]);
display_help();
}
/**
* Display the options for this utility and exit
*/
function display_help(): never
{
printfn('Options:');
printfn(' - rebuild');
printfn(' Rebuilds search index');
exit(0);
}
/**
* Rebuild the search index, creating it if it does not already exist
*/
function rebuild_index(): void
{
try {
$pdo = Configuration::dbConn();
} catch (DocumentException $ex) {
printfn("ERR: Cannot obtain a connection to the database\n $ex");
return;
}
try {
$hasIndex = Custom::scalar("SELECT COUNT(*) FROM sqlite_master WHERE name = 'item_ai'", [], new CountMapper(),
$pdo);
if ($hasIndex == 0) {
printfn('Creating search index....');
Data::createSearchIndex($pdo);
}
printfn('Rebuilding search index...');
Custom::nonQuery("INSERT INTO item_search (item_search) VALUES ('rebuild')", [], $pdo);
printfn(PHP_EOL . 'Search index rebuilt');
} catch (DocumentException $ex) {
printfn("$ex");
}
}