Add progress to invindex-rebuild - ready for addition to the master control panel

This commit is contained in:
Starbeamrainbowlabs 2017-07-10 21:53:52 +01:00
parent f9a56272b7
commit c2dcf43bc2
3 changed files with 31 additions and 15 deletions

View File

@ -3178,7 +3178,7 @@ register_module([
// Create the inverted index if it doesn't exist. // Create the inverted index if it doesn't exist.
// todo In the future perhaps a CLI for this would be good? // todo In the future perhaps a CLI for this would be good?
if(!file_exists($paths->searchindex)) if(!file_exists($paths->searchindex))
search::rebuild_invindex(); search::rebuild_invindex(false);
if(!isset($_GET["query"])) if(!isset($_GET["query"]))
exit(page_renderer::render("No Search Terms - Error - $settings->sitename", "<p>You didn't specify any search terms. Try typing some into the box above.</p>")); exit(page_renderer::render("No Search Terms - Error - $settings->sitename", "<p>You didn't specify any search terms. Try typing some into the box above.</p>"));
@ -3558,17 +3558,19 @@ class search
return str_replace([ "[", "]", "\"", "*", "_", " - ", "`" ], "", $source); return str_replace([ "[", "]", "\"", "*", "_", " - ", "`" ], "", $source);
} }
public static function rebuild_invindex() public static function rebuild_invindex($output = true)
{ {
global $pageindex, $env, $paths; global $pageindex, $env, $paths;
header("content-type: text/event-stream"); if($output)
header("content-type: text/event-stream");
// Clear the id index out // Clear the id index out
ids::clear(); ids::clear();
// Reindex each page in turn // Reindex each page in turn
$invindex = []; $invindex = [];
$i = 0; $max = count(get_object_vars($pageindex));
foreach($pageindex as $pagename => $pagedetails) foreach($pageindex as $pagename => $pagedetails)
{ {
$pagesource = utf8_encode(file_get_contents("$env->storage_prefix$pagename.md")); $pagesource = utf8_encode(file_get_contents("$env->storage_prefix$pagename.md"));
@ -3577,12 +3579,18 @@ class search
$pageid = ids::getid($pagename); $pageid = ids::getid($pagename);
self::merge_into_invindex($invindex, $pageid, $index); self::merge_into_invindex($invindex, $pageid, $index);
echo("Added $pagename (id #$pageid) to the new search index.\n\n"); if($output) {
flush(); echo("[" . ($i + 1) . " / $max] Added $pagename (id #$pageid) to the new search index.\n\n");
flush();
}
$i++;
} }
echo("Search index rebuilding complete.\n\n"); if($output) {
echo("Saving new search index to '$paths->searchindex'.\n\n"); echo("Search index rebuilding complete.\n\n");
echo("Saving new search index to '$paths->searchindex'.\n\n");
}
self::save_invindex($paths->searchindex, $invindex); self::save_invindex($paths->searchindex, $invindex);
} }

View File

@ -104,7 +104,7 @@
"author": "Starbeamrainbowlabs", "author": "Starbeamrainbowlabs",
"description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.", "description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
"id": "feature-search", "id": "feature-search",
"lastupdate": 1499102118, "lastupdate": 1499719908,
"optional": false "optional": false
}, },
{ {

View File

@ -102,7 +102,7 @@ register_module([
// Create the inverted index if it doesn't exist. // Create the inverted index if it doesn't exist.
// todo In the future perhaps a CLI for this would be good? // todo In the future perhaps a CLI for this would be good?
if(!file_exists($paths->searchindex)) if(!file_exists($paths->searchindex))
search::rebuild_invindex(); search::rebuild_invindex(false);
if(!isset($_GET["query"])) if(!isset($_GET["query"]))
exit(page_renderer::render("No Search Terms - Error - $settings->sitename", "<p>You didn't specify any search terms. Try typing some into the box above.</p>")); exit(page_renderer::render("No Search Terms - Error - $settings->sitename", "<p>You didn't specify any search terms. Try typing some into the box above.</p>"));
@ -482,17 +482,19 @@ class search
return str_replace([ "[", "]", "\"", "*", "_", " - ", "`" ], "", $source); return str_replace([ "[", "]", "\"", "*", "_", " - ", "`" ], "", $source);
} }
public static function rebuild_invindex() public static function rebuild_invindex($output = true)
{ {
global $pageindex, $env, $paths; global $pageindex, $env, $paths;
header("content-type: text/event-stream"); if($output)
header("content-type: text/event-stream");
// Clear the id index out // Clear the id index out
ids::clear(); ids::clear();
// Reindex each page in turn // Reindex each page in turn
$invindex = []; $invindex = [];
$i = 0; $max = count(get_object_vars($pageindex));
foreach($pageindex as $pagename => $pagedetails) foreach($pageindex as $pagename => $pagedetails)
{ {
$pagesource = utf8_encode(file_get_contents("$env->storage_prefix$pagename.md")); $pagesource = utf8_encode(file_get_contents("$env->storage_prefix$pagename.md"));
@ -501,12 +503,18 @@ class search
$pageid = ids::getid($pagename); $pageid = ids::getid($pagename);
self::merge_into_invindex($invindex, $pageid, $index); self::merge_into_invindex($invindex, $pageid, $index);
echo("Added $pagename (id #$pageid) to the new search index.\n\n"); if($output) {
flush(); echo("[" . ($i + 1) . " / $max] Added $pagename (id #$pageid) to the new search index.\n\n");
flush();
}
$i++;
} }
echo("Search index rebuilding complete.\n\n"); if($output) {
echo("Saving new search index to '$paths->searchindex'.\n\n"); echo("Search index rebuilding complete.\n\n");
echo("Saving new search index to '$paths->searchindex'.\n\n");
}
self::save_invindex($paths->searchindex, $invindex); self::save_invindex($paths->searchindex, $invindex);
} }