From c2dcf43bc28a0aeb7f78825f49667f62ec74d687 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Mon, 10 Jul 2017 21:53:52 +0100
Subject: [PATCH] Add progress to invindex-rebuild - ready for addition to the
 master control panel

---
 build/index.php            | 22 +++++++++++++++-------
 module_index.json          |  2 +-
 modules/feature-search.php | 22 +++++++++++++++-------
 3 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/build/index.php b/build/index.php
index e3f0a64..a67df11 100644
--- a/build/index.php
+++ b/build/index.php
@@ -3178,7 +3178,7 @@ register_module([
 			// Create the inverted index if it doesn't exist.
 			// todo In the future perhaps a CLI for this would be good?
 			if(!file_exists($paths->searchindex))
-				search::rebuild_invindex();
+				search::rebuild_invindex(false);
 			
 			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>"));
@@ -3558,17 +3558,19 @@ class search
 		return str_replace([ "[", "]", "\"", "*", "_", " - ", "`" ], "", $source);
 	}
 	
-	public static function rebuild_invindex()
+	public static function rebuild_invindex($output = true)
 	{
 		global $pageindex, $env, $paths;
 		
-		header("content-type: text/event-stream");
+		if($output)
+			header("content-type: text/event-stream");
 		
 		// Clear the id index out
 		ids::clear();
 		
 		// Reindex each page in turn
 		$invindex = [];
+		$i = 0; $max = count(get_object_vars($pageindex));
 		foreach($pageindex as $pagename => $pagedetails)
 		{
 			$pagesource = utf8_encode(file_get_contents("$env->storage_prefix$pagename.md"));
@@ -3577,12 +3579,18 @@ class search
 			$pageid = ids::getid($pagename);
 			self::merge_into_invindex($invindex, $pageid, $index);
 			
-			echo("Added $pagename (id #$pageid) to the new search index.\n\n");
-			flush();
+			if($output) {
+				echo("[" . ($i + 1) . " / $max] Added $pagename (id #$pageid) to the new search index.\n\n");
+				flush();
+			}
+			
+			$i++;
 		}
 		
-		echo("Search index rebuilding complete.\n\n");
-		echo("Saving new search index to '$paths->searchindex'.\n\n");
+		if($output) {
+			echo("Search index rebuilding complete.\n\n");
+			echo("Saving new search index to '$paths->searchindex'.\n\n");
+		}
 		
 		self::save_invindex($paths->searchindex, $invindex);
 	}
diff --git a/module_index.json b/module_index.json
index 20ecfa4..8b4d669 100755
--- a/module_index.json
+++ b/module_index.json
@@ -104,7 +104,7 @@
         "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.",
         "id": "feature-search",
-        "lastupdate": 1499102118,
+        "lastupdate": 1499719908,
         "optional": false
     },
     {
diff --git a/modules/feature-search.php b/modules/feature-search.php
index 795e2c0..97eb219 100644
--- a/modules/feature-search.php
+++ b/modules/feature-search.php
@@ -102,7 +102,7 @@ register_module([
 			// Create the inverted index if it doesn't exist.
 			// todo In the future perhaps a CLI for this would be good?
 			if(!file_exists($paths->searchindex))
-				search::rebuild_invindex();
+				search::rebuild_invindex(false);
 			
 			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>"));
@@ -482,17 +482,19 @@ class search
 		return str_replace([ "[", "]", "\"", "*", "_", " - ", "`" ], "", $source);
 	}
 	
-	public static function rebuild_invindex()
+	public static function rebuild_invindex($output = true)
 	{
 		global $pageindex, $env, $paths;
 		
-		header("content-type: text/event-stream");
+		if($output)
+			header("content-type: text/event-stream");
 		
 		// Clear the id index out
 		ids::clear();
 		
 		// Reindex each page in turn
 		$invindex = [];
+		$i = 0; $max = count(get_object_vars($pageindex));
 		foreach($pageindex as $pagename => $pagedetails)
 		{
 			$pagesource = utf8_encode(file_get_contents("$env->storage_prefix$pagename.md"));
@@ -501,12 +503,18 @@ class search
 			$pageid = ids::getid($pagename);
 			self::merge_into_invindex($invindex, $pageid, $index);
 			
-			echo("Added $pagename (id #$pageid) to the new search index.\n\n");
-			flush();
+			if($output) {
+				echo("[" . ($i + 1) . " / $max] Added $pagename (id #$pageid) to the new search index.\n\n");
+				flush();
+			}
+			
+			$i++;
 		}
 		
-		echo("Search index rebuilding complete.\n\n");
-		echo("Saving new search index to '$paths->searchindex'.\n\n");
+		if($output) {
+			echo("Search index rebuilding complete.\n\n");
+			echo("Saving new search index to '$paths->searchindex'.\n\n");
+		}
 		
 		self::save_invindex($paths->searchindex, $invindex);
 	}
