From b31301be38bef89587f22afb7ce2bc092c9dd45c Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 3 Jul 2018 12:15:16 +0100
Subject: [PATCH] Implement `history_max_revisions` setting

---
 build/index.php             | 17 ++++++++++++++---
 module_index.json           |  2 +-
 modules/feature-history.php | 15 +++++++++++++--
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/build/index.php b/build/index.php
index ae54276..36291a1 100644
--- a/build/index.php
+++ b/build/index.php
@@ -404,7 +404,7 @@ if($settings->sessionprefix == "auto")
 /////////////////////////////////////////////////////////////////////////////
 /** The version of Pepperminty Wiki currently running. */
 $version = "v0.17-dev";
-$commit = "6e7698b4a9210cd3198ee1bd2a117e7bcf162819";
+$commit = "819489a7fb17f8e57fe6af7e01c34e9260d4e9ea";
 /// Environment ///
 /** Holds information about the current request environment. */
 $env = new stdClass();
@@ -3426,7 +3426,7 @@ register_module([
  * @param string  $change_type    The type of change to record this as in the history revision log
  */
 function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true, $change_type = "edit") {
-	global $pageindex, $paths, $env;
+	global $env, $paths, $settings, $pageindex;
 	
 	if(!isset($pageinfo->history))
 		$pageinfo->history = [];
@@ -3439,7 +3439,8 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
 	// this point
 	
 	// TODO Store tag changes here
-	$nextRid = count($pageinfo->history); // The next revision id
+	end($pageinfo->history); // Calculate the next revision id - we can't jsut count the reivisions here because we might have a revision limit
+	$nextRid = $pageinfo->history[key($pageinfo->history)]->rid + 1;
 	$ridFilename = "$pageinfo->filename.r$nextRid";
 	// Insert a new entry into the history
 	$pageinfo->history[] = [
@@ -3455,10 +3456,20 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
 	// Save the new source as a revision
 	$result = file_put_contents("$env->storage_prefix$ridFilename", $newsource);
 	
+	if($result !== false &&
+		$settings->history_max_revisions > -1) {
+		while(count($pageinfo->history) > $settings->history_max_revisions) {
+			// We've got too many revisions - trim one off & delete it
+			$oldest_revision = array_shift($pageinfo->history);
+			unlink("$env->storage_prefix/$oldest_revision->filename");
+		}
+	}
+	
 	// Save the edited pageindex
 	if($result !== false && $save_pageindex)
 		$result = file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
 	
+	
 	return $result;
 }
 
diff --git a/module_index.json b/module_index.json
index 9760873..3bf562f 100755
--- a/module_index.json
+++ b/module_index.json
@@ -77,7 +77,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": 1530615427,
+        "lastupdate": 1530616470,
         "optional": false
     },
     {
diff --git a/modules/feature-history.php b/modules/feature-history.php
index d65f8c2..14a1895 100644
--- a/modules/feature-history.php
+++ b/modules/feature-history.php
@@ -220,7 +220,7 @@ register_module([
  * @param string  $change_type    The type of change to record this as in the history revision log
  */
 function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true, $change_type = "edit") {
-	global $pageindex, $paths, $env;
+	global $env, $paths, $settings, $pageindex;
 	
 	if(!isset($pageinfo->history))
 		$pageinfo->history = [];
@@ -233,7 +233,8 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
 	// this point
 	
 	// TODO Store tag changes here
-	$nextRid = count($pageinfo->history); // The next revision id
+	end($pageinfo->history); // Calculate the next revision id - we can't jsut count the reivisions here because we might have a revision limit
+	$nextRid = $pageinfo->history[key($pageinfo->history)]->rid + 1;
 	$ridFilename = "$pageinfo->filename.r$nextRid";
 	// Insert a new entry into the history
 	$pageinfo->history[] = [
@@ -249,10 +250,20 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
 	// Save the new source as a revision
 	$result = file_put_contents("$env->storage_prefix$ridFilename", $newsource);
 	
+	if($result !== false &&
+		$settings->history_max_revisions > -1) {
+		while(count($pageinfo->history) > $settings->history_max_revisions) {
+			// We've got too many revisions - trim one off & delete it
+			$oldest_revision = array_shift($pageinfo->history);
+			unlink("$env->storage_prefix/$oldest_revision->filename");
+		}
+	}
+	
 	// Save the edited pageindex
 	if($result !== false && $save_pageindex)
 		$result = file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
 	
+	
 	return $result;
 }
 
