From 9f5e52d76f565286d968334886ba58f1e766515b Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 5 Jun 2016 14:35:09 +0100
Subject: [PATCH] Make move action aware of page revisions

---
 build/index.php       | 20 +++++++++++++++++++-
 module_index.json     |  2 +-
 modules/page-move.php | 22 ++++++++++++++++++++--
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/build/index.php b/build/index.php
index 65efc6a..d707e2e 100644
--- a/build/index.php
+++ b/build/index.php
@@ -4225,7 +4225,7 @@ register_module([
 				exit(page_renderer::render_main("Moving $env->page - Error - $settings->sitename", "<p>Whilst moving the file associated with $env->page, $settings->sitename detected a pre-existing file on the server's file system. Because $settings->sitename can't determine whether the existing file is important to another component of $settings->sitename or it's host web server, the move have been aborted - just in case.</p>
 				<p>If you know that this move is actually safe, please get your site administrator (" . $settings->admindetails["name"] . ") to perform the move manually. Their contact address can be found at the bottom of every page (including this one).</p>"));
 			
-			//move the page in the page index
+			// Move the page in the page index
 			$pageindex->$new_name = new stdClass();
 			foreach($pageindex->$page as $key => $value)
 			{
@@ -4233,6 +4233,7 @@ register_module([
 			}
 			unset($pageindex->$page);
 			$pageindex->$new_name->filename = $new_name;
+			
 			// If this page has an associated file, then we should move that too
 			if(!empty($pageindex->$new_name->uploadedfile))
 			{
@@ -4243,6 +4244,23 @@ register_module([
 				// Move the file on disk
 				rename($env->storage_prefix . $env->page, $env->storage_prefix . $new_name);
 			}
+			
+			// Come to think about it, we should probably move the history while we're at it
+			foreach($pageindex->$new_name->history as &$revisionData)
+			{
+				// We're only interested in edits
+				if($revisionData->type !== "edit") continue;
+				$newRevisionName = $pageindex->$new_name->filename . ".r$revisionData->rid";
+				// Move the revision to it's new name
+				rename(
+					$env->storage_prefix . $revisionData->filename,
+					$env->storage_prefix . $newRevisionName
+				);
+				// Update the pageindex entry
+				$revisionData->filename = $newRevisionName;
+			}
+			
+			// Save the updated pageindex
 			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
 			
 			// Move the page on the disk
diff --git a/module_index.json b/module_index.json
index 39f00bd..e6d4cb9 100644
--- a/module_index.json
+++ b/module_index.json
@@ -167,7 +167,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds an action to allow administrators to move pages.",
         "id": "page-move",
-        "lastupdate": 1459628404,
+        "lastupdate": 1465133466,
         "optional": false
     },
     {
diff --git a/modules/page-move.php b/modules/page-move.php
index e3a53de..6832ba7 100644
--- a/modules/page-move.php
+++ b/modules/page-move.php
@@ -1,7 +1,7 @@
 <?php
 register_module([
 	"name" => "Page mover",
-	"version" => "0.8.1",
+	"version" => "0.9",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Adds an action to allow administrators to move pages.",
 	"id" => "page-move",
@@ -60,7 +60,7 @@ register_module([
 				exit(page_renderer::render_main("Moving $env->page - Error - $settings->sitename", "<p>Whilst moving the file associated with $env->page, $settings->sitename detected a pre-existing file on the server's file system. Because $settings->sitename can't determine whether the existing file is important to another component of $settings->sitename or it's host web server, the move have been aborted - just in case.</p>
 				<p>If you know that this move is actually safe, please get your site administrator (" . $settings->admindetails["name"] . ") to perform the move manually. Their contact address can be found at the bottom of every page (including this one).</p>"));
 			
-			//move the page in the page index
+			// Move the page in the page index
 			$pageindex->$new_name = new stdClass();
 			foreach($pageindex->$page as $key => $value)
 			{
@@ -68,6 +68,7 @@ register_module([
 			}
 			unset($pageindex->$page);
 			$pageindex->$new_name->filename = $new_name;
+			
 			// If this page has an associated file, then we should move that too
 			if(!empty($pageindex->$new_name->uploadedfile))
 			{
@@ -78,6 +79,23 @@ register_module([
 				// Move the file on disk
 				rename($env->storage_prefix . $env->page, $env->storage_prefix . $new_name);
 			}
+			
+			// Come to think about it, we should probably move the history while we're at it
+			foreach($pageindex->$new_name->history as &$revisionData)
+			{
+				// We're only interested in edits
+				if($revisionData->type !== "edit") continue;
+				$newRevisionName = $pageindex->$new_name->filename . ".r$revisionData->rid";
+				// Move the revision to it's new name
+				rename(
+					$env->storage_prefix . $revisionData->filename,
+					$env->storage_prefix . $newRevisionName
+				);
+				// Update the pageindex entry
+				$revisionData->filename = $newRevisionName;
+			}
+			
+			// Save the updated pageindex
 			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
 			
 			// Move the page on the disk
