From 6eea1700d6a45f0bdc5a85dc2cd6c5cd8c48ae4d Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Wed, 11 Nov 2015 19:24:40 +0000
Subject: [PATCH] Delete page names from the id index upon page deletion.

---
 build/index.php         | 36 ++++++++++++++++++++++++++++++++----
 core.php                | 21 +++++++++++++++++++++
 module_index.json       |  2 +-
 modules/page-delete.php | 15 +++++++++++----
 4 files changed, 65 insertions(+), 9 deletions(-)

diff --git a/build/index.php b/build/index.php
index d014271..44459ae 100644
--- a/build/index.php
+++ b/build/index.php
@@ -771,6 +771,11 @@ class ids
 			return $idindex->$id;
 	}
     
+    /*
+     * @summary Moves a page in the id index from $oldpagename to $newpagename.
+     *          Note that this function doesn't perform any special checks to
+     *          make sure that the destination name doesn't already exist.
+     */
     public static function movepagename($oldpagename, $newpagename)
     {
         global $idindex, $paths;
@@ -780,6 +785,22 @@ class ids
         
         file_put_contents($paths->idindex, json_encode($idindex));
     }
+    
+    /*
+     * @summary Removes the given page name from the id index. Note that this
+     *          function doesn't handle multiple entries with the same name.
+     */
+    public static function deletepagename($pagename)
+    {
+        global $idindex, $paths;
+        
+        // Get the id of the specified page
+        $pageid = self::getid($pagename);
+        // Remove it from the pageindex
+        unset($idindex->$pageid);
+        // Save the id index
+        file_put_contents($paths->idindex, json_encode($idindex));
+    }
 
 	/*
 	 * @summary Assigns an id to a pagename. Doesn't check to make sure that
@@ -2329,10 +2350,17 @@ register_module([
 			{
 				unlink($env->storage_prefix . $pageindex->$page->uploadedfilepath);
 			}
-			unset($pageindex->$page); //delete the page from the page index
-			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT)); //save the new page index
-			unlink("$env->storage_prefix$env->page.md"); //delete the page from the disk
-
+			// Delete the page from the page index
+			unset($pageindex->$page);
+			// Save the new page index
+			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT)); 
+			
+			// Remove the page's name from the id index
+			ids::deletepagename($env->page);
+			
+			// Delete the page from the disk
+			unlink("$env->storage_prefix$env->page.md");
+			
 			exit(page_renderer::render_main("Deleting $env->page - $settings->sitename", "<p>$env->page has been deleted. <a href='index.php'>Go back to the main page</a>.</p>"));
 		});
 	}
diff --git a/core.php b/core.php
index c70718f..70596dc 100644
--- a/core.php
+++ b/core.php
@@ -433,6 +433,11 @@ class ids
 			return $idindex->$id;
 	}
     
+    /*
+     * @summary Moves a page in the id index from $oldpagename to $newpagename.
+     *          Note that this function doesn't perform any special checks to
+     *          make sure that the destination name doesn't already exist.
+     */
     public static function movepagename($oldpagename, $newpagename)
     {
         global $idindex, $paths;
@@ -442,6 +447,22 @@ class ids
         
         file_put_contents($paths->idindex, json_encode($idindex));
     }
+    
+    /*
+     * @summary Removes the given page name from the id index. Note that this
+     *          function doesn't handle multiple entries with the same name.
+     */
+    public static function deletepagename($pagename)
+    {
+        global $idindex, $paths;
+        
+        // Get the id of the specified page
+        $pageid = self::getid($pagename);
+        // Remove it from the pageindex
+        unset($idindex->$pageid);
+        // Save the id index
+        file_put_contents($paths->idindex, json_encode($idindex));
+    }
 
 	/*
 	 * @summary Assigns an id to a pagename. Doesn't check to make sure that
diff --git a/module_index.json b/module_index.json
index 0fe844f..5da63b6 100644
--- a/module_index.json
+++ b/module_index.json
@@ -77,7 +77,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds an action to allow administrators to delete pages.",
         "id": "page-delete",
-        "lastupdate": 1447053826,
+        "lastupdate": 1447269808,
         "optional": false
     },
     {
diff --git a/modules/page-delete.php b/modules/page-delete.php
index 3f5f57d..f4e0cda 100644
--- a/modules/page-delete.php
+++ b/modules/page-delete.php
@@ -32,10 +32,17 @@ register_module([
 			{
 				unlink($env->storage_prefix . $pageindex->$page->uploadedfilepath);
 			}
-			unset($pageindex->$page); //delete the page from the page index
-			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT)); //save the new page index
-			unlink("$env->storage_prefix$env->page.md"); //delete the page from the disk
-
+			// Delete the page from the page index
+			unset($pageindex->$page);
+			// Save the new page index
+			file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT)); 
+			
+			// Remove the page's name from the id index
+			ids::deletepagename($env->page);
+			
+			// Delete the page from the disk
+			unlink("$env->storage_prefix$env->page.md");
+			
 			exit(page_renderer::render_main("Deleting $env->page - $settings->sitename", "<p>$env->page has been deleted. <a href='index.php'>Go back to the main page</a>.</p>"));
 		});
 	}
