From ee1224bbe921ed802c8152934e5bc9f7d9f12e3a Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 4 Jun 2016 13:52:45 +0100
Subject: [PATCH] Generate history revisions upon uploading a file

---
 build/index.php             | 77 +++++++++++++++++++++----------------
 module_index.json           |  4 +-
 modules/feature-history.php | 70 +++++++++++++++++----------------
 modules/feature-upload.php  |  7 ++++
 4 files changed, 90 insertions(+), 68 deletions(-)

diff --git a/build/index.php b/build/index.php
index 25922a7..81c52e8 100644
--- a/build/index.php
+++ b/build/index.php
@@ -1791,42 +1791,46 @@ register_module([
 		});
 		
 		
-		register_save_preprocessor(function(&$pageinfo, &$newsource, &$oldsource) {
-			global $pageindex, $paths, $env;
-			if(!isset($pageinfo->history))
-				$pageinfo->history = [];
-			
-			// Save the *new source* as a revision
-			// This results in 2 copies of the current source, but this is ok
-			// since any time someone changes something, it create a new
-			// revision
-			// Note that we can't save the old source here because we'd have no
-			// clue who edited it since $pageinfo has already been updated by
-			// this point
-			
-			// TODO Store tag changes here
-			$nextRid = count($pageinfo->history); // The next revision id
-			$ridFilename = "$pageinfo->filename.r$nextRid";
-			// Insert a new entry into the history
-			$pageinfo->history[] = [
-				"type" => "edit", // We might want to store other types later (e.g. page moves)
-				"rid" => $nextRid,
-				"timestamp" => time(),
-				"filename" => $ridFilename,
-				"newsize" => strlen($newsource),
-				"sizediff" => strlen($newsource) - strlen($oldsource),
-				"editor" => $pageinfo->lasteditor
-			];
-			
-			// Save the new source as a revision
-			file_put_contents("$env->storage_prefix$ridFilename", $newsource);
-			
-			// Save the edited pageindex
-			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
-		});
+		register_save_preprocessor("history_add_revision");
 	}
 ]);
 
+function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true) {
+	global $pageindex, $paths, $env;
+	
+	if(!isset($pageinfo->history))
+		$pageinfo->history = [];
+	
+	// Save the *new source* as a revision
+	// This results in 2 copies of the current source, but this is ok
+	// since any time someone changes something, it create a new
+	// revision
+	// Note that we can't save the old source here because we'd have no
+	// clue who edited it since $pageinfo has already been updated by
+	// this point
+	
+	// TODO Store tag changes here
+	$nextRid = count($pageinfo->history); // The next revision id
+	$ridFilename = "$pageinfo->filename.r$nextRid";
+	// Insert a new entry into the history
+	$pageinfo->history[] = [
+		"type" => "edit", // We might want to store other types later (e.g. page moves)
+		"rid" => $nextRid,
+		"timestamp" => time(),
+		"filename" => $ridFilename,
+		"newsize" => strlen($newsource),
+		"sizediff" => strlen($newsource) - strlen($oldsource),
+		"editor" => $pageinfo->lasteditor
+	];
+	
+	// Save the new source as a revision
+	file_put_contents("$env->storage_prefix$ridFilename", $newsource);
+	
+	// Save the edited pageindex
+	if($save_pageindex)
+		file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
+}
+
 
 
 
@@ -2799,6 +2803,13 @@ register_module([
 					// should be the page name.
 					$pageindex->$new_filename = $entry;
 					
+					// Generate a revision to keep the apge history up to date
+					if(module_exists("feature-history"))
+					{
+						$oldsource = ""; // Only variables can be passed by reference, not literals
+						history_add_revision($entry, $description, $oldsource, false);
+					}
+					
 					// Save the pageindex
 					file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
 					
diff --git a/module_index.json b/module_index.json
index 9b2d42f..1830589 100644
--- a/module_index.json
+++ b/module_index.json
@@ -41,7 +41,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds the ability to keep unlimited page history, limited only by your disk space. Note that this doesn't store file history (yet). Currently depends on feature-recent-changes for rendering of the history page.",
         "id": "feature-history",
-        "lastupdate": 1464941758,
+        "lastupdate": 1465044619,
         "optional": false
     },
     {
@@ -77,7 +77,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File\/' prefix.",
         "id": "feature-upload",
-        "lastupdate": 1465043901,
+        "lastupdate": 1465044741,
         "optional": false
     },
     {
diff --git a/modules/feature-history.php b/modules/feature-history.php
index 6724ae5..51d7423 100644
--- a/modules/feature-history.php
+++ b/modules/feature-history.php
@@ -46,40 +46,44 @@ register_module([
 		});
 		
 		
-		register_save_preprocessor(function(&$pageinfo, &$newsource, &$oldsource) {
-			global $pageindex, $paths, $env;
-			if(!isset($pageinfo->history))
-				$pageinfo->history = [];
-			
-			// Save the *new source* as a revision
-			// This results in 2 copies of the current source, but this is ok
-			// since any time someone changes something, it create a new
-			// revision
-			// Note that we can't save the old source here because we'd have no
-			// clue who edited it since $pageinfo has already been updated by
-			// this point
-			
-			// TODO Store tag changes here
-			$nextRid = count($pageinfo->history); // The next revision id
-			$ridFilename = "$pageinfo->filename.r$nextRid";
-			// Insert a new entry into the history
-			$pageinfo->history[] = [
-				"type" => "edit", // We might want to store other types later (e.g. page moves)
-				"rid" => $nextRid,
-				"timestamp" => time(),
-				"filename" => $ridFilename,
-				"newsize" => strlen($newsource),
-				"sizediff" => strlen($newsource) - strlen($oldsource),
-				"editor" => $pageinfo->lasteditor
-			];
-			
-			// Save the new source as a revision
-			file_put_contents("$env->storage_prefix$ridFilename", $newsource);
-			
-			// Save the edited pageindex
-			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
-		});
+		register_save_preprocessor("history_add_revision");
 	}
 ]);
 
+function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true) {
+	global $pageindex, $paths, $env;
+	
+	if(!isset($pageinfo->history))
+		$pageinfo->history = [];
+	
+	// Save the *new source* as a revision
+	// This results in 2 copies of the current source, but this is ok
+	// since any time someone changes something, it create a new
+	// revision
+	// Note that we can't save the old source here because we'd have no
+	// clue who edited it since $pageinfo has already been updated by
+	// this point
+	
+	// TODO Store tag changes here
+	$nextRid = count($pageinfo->history); // The next revision id
+	$ridFilename = "$pageinfo->filename.r$nextRid";
+	// Insert a new entry into the history
+	$pageinfo->history[] = [
+		"type" => "edit", // We might want to store other types later (e.g. page moves)
+		"rid" => $nextRid,
+		"timestamp" => time(),
+		"filename" => $ridFilename,
+		"newsize" => strlen($newsource),
+		"sizediff" => strlen($newsource) - strlen($oldsource),
+		"editor" => $pageinfo->lasteditor
+	];
+	
+	// Save the new source as a revision
+	file_put_contents("$env->storage_prefix$ridFilename", $newsource);
+	
+	// Save the edited pageindex
+	if($save_pageindex)
+		file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
+}
+
 ?>
diff --git a/modules/feature-upload.php b/modules/feature-upload.php
index 5678a3c..1cdbb70 100644
--- a/modules/feature-upload.php
+++ b/modules/feature-upload.php
@@ -153,6 +153,13 @@ register_module([
 					// should be the page name.
 					$pageindex->$new_filename = $entry;
 					
+					// Generate a revision to keep the apge history up to date
+					if(module_exists("feature-history"))
+					{
+						$oldsource = ""; // Only variables can be passed by reference, not literals
+						history_add_revision($entry, $description, $oldsource, false);
+					}
+					
 					// Save the pageindex
 					file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
 					
