From b539cb4107b3190e143f3f6f370d3dbb6e32364e Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 14 Nov 2015 17:01:23 +0000 Subject: [PATCH] Also delete from searchindex when deleting a page. Speculative fix for #35, but it seems #41 needs fixing first. --- build/index.php | 31 ++++++++++++++++++++++++++++++- module_index.json | 4 ++-- modules/feature-search.php | 19 +++++++++++++++++++ modules/page-delete.php | 12 +++++++++++- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/build/index.php b/build/index.php index 90be3b6..a95ae2e 100644 --- a/build/index.php +++ b/build/index.php @@ -1763,6 +1763,25 @@ class search }); } + /** + * Deletes the given pageid from the given pageindex. + * @param inverted_index &$invindex The inverted index. + * @param number $pageid The pageid to remove. + */ + public static function delete_entry(&$invindex, $pageid) + { + $str_pageid = (string)$pageid; + foreach($invindex as $nterm => &$entry) + { + if(isset($entry[$pageid])) + unset($entry[$pageid]); + if(isset($entry[$str_pageid])) + unset($entry[$str_pageid]); + if(count($entry) === 0) + unset($invindex[$nterm]); + } + } + public static function save_invindex($filename, &$invindex) { file_put_contents($filename, json_encode($invindex)); @@ -2375,7 +2394,7 @@ register_module([ "id" => "page-delete", "code" => function() { add_action("delete", function() { - global $pageindex, $settings, $env, $paths; + global $pageindex, $settings, $env, $paths, $modules; if(!$settings->editing) { exit(page_renderer::render_main("Deleting $env->page - error", "

You tried to delete $env->page, but editing is disabled on this wiki.

@@ -2400,6 +2419,7 @@ register_module([ { unlink($env->storage_prefix . $pageindex->$page->uploadedfilepath); } + // Delete the page from the page index unset($pageindex->$page); @@ -2409,6 +2429,15 @@ register_module([ // Remove the page's name from the id index ids::deletepagename($env->page); + // Delete the page from the search index, if that module is installed + if(isset($modules["feature-search"])) + { + $pageid = ids::getid($env->page); + $invindex = search::load_invindex($paths->searchindex); + search::delete_entry($invindex, $pageid); + search::save_invindex($paths->searchindex, $invindex); + } + // Delete the page from the disk unlink("$env->storage_prefix$env->page.md"); diff --git a/module_index.json b/module_index.json index e11435a..75bf0c5 100644 --- a/module_index.json +++ b/module_index.json @@ -50,7 +50,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds proper search functionality to Pepperminty Wiki. Note that this module, at the moment, just contains test code while I figure out how best to write a search engine.", "id": "feature-search", - "lastupdate": 1447267196, + "lastupdate": 1447520172, "optional": false }, { @@ -77,7 +77,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds an action to allow administrators to delete pages.", "id": "page-delete", - "lastupdate": 1447269891, + "lastupdate": 1447520092, "optional": false }, { diff --git a/modules/feature-search.php b/modules/feature-search.php index e0199a7..34d4b18 100644 --- a/modules/feature-search.php +++ b/modules/feature-search.php @@ -278,6 +278,25 @@ class search }); } + /** + * Deletes the given pageid from the given pageindex. + * @param inverted_index &$invindex The inverted index. + * @param number $pageid The pageid to remove. + */ + public static function delete_entry(&$invindex, $pageid) + { + $str_pageid = (string)$pageid; + foreach($invindex as $nterm => &$entry) + { + if(isset($entry[$pageid])) + unset($entry[$pageid]); + if(isset($entry[$str_pageid])) + unset($entry[$str_pageid]); + if(count($entry) === 0) + unset($invindex[$nterm]); + } + } + public static function save_invindex($filename, &$invindex) { file_put_contents($filename, json_encode($invindex)); diff --git a/modules/page-delete.php b/modules/page-delete.php index 59b5632..92a1dc5 100644 --- a/modules/page-delete.php +++ b/modules/page-delete.php @@ -7,7 +7,7 @@ register_module([ "id" => "page-delete", "code" => function() { add_action("delete", function() { - global $pageindex, $settings, $env, $paths; + global $pageindex, $settings, $env, $paths, $modules; if(!$settings->editing) { exit(page_renderer::render_main("Deleting $env->page - error", "

You tried to delete $env->page, but editing is disabled on this wiki.

@@ -32,6 +32,7 @@ register_module([ { unlink($env->storage_prefix . $pageindex->$page->uploadedfilepath); } + // Delete the page from the page index unset($pageindex->$page); @@ -41,6 +42,15 @@ register_module([ // Remove the page's name from the id index ids::deletepagename($env->page); + // Delete the page from the search index, if that module is installed + if(isset($modules["feature-search"])) + { + $pageid = ids::getid($env->page); + $invindex = search::load_invindex($paths->searchindex); + search::delete_entry($invindex, $pageid); + search::save_invindex($paths->searchindex, $invindex); + } + // Delete the page from the disk unlink("$env->storage_prefix$env->page.md");