From 27acc2420ba92ed6e6cdd797e03a4234ad4e17f5 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 19 Aug 2016 10:00:03 +0100
Subject: [PATCH] Make sure that all files are stored in the data directories.
 Fixes #89.

---
 build/index.php            | 18 ++++++++++--------
 core.php                   |  4 ++--
 module_index.json          |  4 ++--
 modules/feature-search.php |  2 +-
 modules/page-edit.php      | 12 +++++++-----
 5 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/build/index.php b/build/index.php
index 8057baf..c6fcef5 100755
--- a/build/index.php
+++ b/build/index.php
@@ -526,10 +526,10 @@ function check_subpage_parents($pagename)
 
 	$parent_pagename = substr($pagename, 0, strrpos($pagename, "/"));
 	$parent_page_filename = "$parent_pagename.md";
-	if(!file_exists($parent_page_filename))
+	if(!file_exists($env->storage_prefix . $parent_page_filename))
 	{
 		// This parent page doesn't exist! Create it and add it to the page index.
-		touch($parent_page_filename, 0);
+		touch($env->storage_prefix . $parent_page_filename, 0);
 
 		$newentry = new stdClass();
 		$newentry->filename = $parent_page_filename;
@@ -2340,7 +2340,7 @@ class search
 			
 			self::merge_into_invindex($invindex, ids::getid($pagename), $index);
 		}
-		
+		error_log("Saving inverted index to $paths->searchindex");
 		self::save_invindex($paths->searchindex, $invindex);
 	}
 	
@@ -3734,14 +3734,16 @@ DIFFSCRIPT;
 				exit(page_renderer::render_main("Edit Conflict - $env->page - $settings->sitename", $content));
 			}
 			
+			// -----~~~==~~~-----
+			
 			// Update the inverted search index
 			
 			// Construct an index for the old and new page content
 			$oldindex = [];
 			$oldpagedata = ""; // We need the old page data in order to pass it to the preprocessor
-			if(file_exists("$env->page.md"))
+			if(file_exists("$env->storage_prefix$env->page.md"))
 			{
-				$oldpagedata = file_get_contents("$env->page.md");
+				$oldpagedata = file_get_contents("$env->storage_prefix$env->page.md");
 				$oldindex = search::index($oldpagedata);
 			}
 			$newindex = search::index($pagedata);
@@ -3751,13 +3753,13 @@ DIFFSCRIPT;
 			$removals = [];
 			search::compare_indexes($oldindex, $newindex, $additions, $removals);
 			// Load in the inverted index
-			$invindex = search::load_invindex("./invindex.json");
+			$invindex = search::load_invindex($env->storage_prefix . "invindex.json");
 			// Merge the changes into the inverted index
 			search::merge_into_invindex($invindex, ids::getid($env->page), $additions, $removals);
 			// Save the inverted index back to disk
-			search::save_invindex("invindex.json", $invindex);
-			
+			search::save_invindex($env->storage_prefix . "invindex.json", $invindex);
 			
+			// -----~~~==~~~-----
 			
 			if(file_put_contents("$env->storage_prefix$env->page.md", $pagedata) !== false)
 			{
diff --git a/core.php b/core.php
index 17af151..5af5400 100644
--- a/core.php
+++ b/core.php
@@ -227,10 +227,10 @@ function check_subpage_parents($pagename)
 
 	$parent_pagename = substr($pagename, 0, strrpos($pagename, "/"));
 	$parent_page_filename = "$parent_pagename.md";
-	if(!file_exists($parent_page_filename))
+	if(!file_exists($env->storage_prefix . $parent_page_filename))
 	{
 		// This parent page doesn't exist! Create it and add it to the page index.
-		touch($parent_page_filename, 0);
+		touch($env->storage_prefix . $parent_page_filename, 0);
 
 		$newentry = new stdClass();
 		$newentry->filename = $parent_page_filename;
diff --git a/module_index.json b/module_index.json
index a09b22c..b736aa6 100755
--- a/module_index.json
+++ b/module_index.json
@@ -68,7 +68,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": 1465757892,
+        "lastupdate": 1471592829,
         "optional": false
     },
     {
@@ -113,7 +113,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
         "id": "page-edit",
-        "lastupdate": 1471549914,
+        "lastupdate": 1471596690,
         "optional": false
     },
     {
diff --git a/modules/feature-search.php b/modules/feature-search.php
index 6305fc3..f6b74d0 100644
--- a/modules/feature-search.php
+++ b/modules/feature-search.php
@@ -267,7 +267,7 @@ class search
 			
 			self::merge_into_invindex($invindex, ids::getid($pagename), $index);
 		}
-		
+		error_log("Saving inverted index to $paths->searchindex");
 		self::save_invindex($paths->searchindex, $invindex);
 	}
 	
diff --git a/modules/page-edit.php b/modules/page-edit.php
index c911644..a8d6345 100755
--- a/modules/page-edit.php
+++ b/modules/page-edit.php
@@ -233,14 +233,16 @@ DIFFSCRIPT;
 				exit(page_renderer::render_main("Edit Conflict - $env->page - $settings->sitename", $content));
 			}
 			
+			// -----~~~==~~~-----
+			
 			// Update the inverted search index
 			
 			// Construct an index for the old and new page content
 			$oldindex = [];
 			$oldpagedata = ""; // We need the old page data in order to pass it to the preprocessor
-			if(file_exists("$env->page.md"))
+			if(file_exists("$env->storage_prefix$env->page.md"))
 			{
-				$oldpagedata = file_get_contents("$env->page.md");
+				$oldpagedata = file_get_contents("$env->storage_prefix$env->page.md");
 				$oldindex = search::index($oldpagedata);
 			}
 			$newindex = search::index($pagedata);
@@ -250,13 +252,13 @@ DIFFSCRIPT;
 			$removals = [];
 			search::compare_indexes($oldindex, $newindex, $additions, $removals);
 			// Load in the inverted index
-			$invindex = search::load_invindex("./invindex.json");
+			$invindex = search::load_invindex($env->storage_prefix . "invindex.json");
 			// Merge the changes into the inverted index
 			search::merge_into_invindex($invindex, ids::getid($env->page), $additions, $removals);
 			// Save the inverted index back to disk
-			search::save_invindex("invindex.json", $invindex);
-			
+			search::save_invindex($env->storage_prefix . "invindex.json", $invindex);
 			
+			// -----~~~==~~~-----
 			
 			if(file_put_contents("$env->storage_prefix$env->page.md", $pagedata) !== false)
 			{
