From 52821d1a87179586cdfa509bf2ee4cf59cc8463f Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 14 Jul 2015 15:47:05 +0100 Subject: [PATCH] Add function to make sure parent pages exist. Fixes #12 --- core.php | 37 ++++++++++++++++++++++++++++++++++++- index.php | 43 +++++++++++++++++++++++++++++++++++++++++-- module_index.json | 4 ++-- modules/page-edit.php | 6 +++++- testcheck.php | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 testcheck.php diff --git a/core.php b/core.php index cc7e4be..b1cd4ae 100644 --- a/core.php +++ b/core.php @@ -94,9 +94,44 @@ function get_subpages($pageindex, $pagename) } } + unset($pagenames); return $result; } +/* + * @summary Makes sure that a subpage's parents exist. Note this doesn't check the pagename itself. + * + * @param The pagename to check. + * + */ +function check_subpage_parents($pagename) +{ + global $pageindex; + // Save the new pageindex and return if there aren't any more parent pages to check + if(strpos($pagename, "/") === false) + { + file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT)); + return; + } + + $parent_pagename = substr($pagename, 0, strrpos($pagename, "/")); + $parent_page_filename = "$parent_pagename.md"; + if(!file_exists($parent_page_filename)) + { + // This parent page doesn't exist! Create it and add it to the page index. + touch($parent_page_filename, 0); + + $newentry = new stdClass(); + $newentry->filename = $parent_page_filename; + $newentry->size = 0; + $newentry->lastmodified = 0; + $newentry->lasteditor = "none"; + $pageindex->$parent_pagename = $newentry; + } + + check_subpage_parents($parent_pagename); +} + if(!file_exists("./pageindex.json")) { // From http://in.php.net/manual/en/function.glob.php#106595 @@ -125,7 +160,7 @@ if(!file_exists("./pageindex.json")) $newentry->filename = utf8_encode($pagefilename); // Store the filename $newentry->size = filesize($pagefilename); // Store the page size $newentry->lastmodified = filemtime($pagefilename); // Store the date last modified - // Todo find a way to keep the last editor independent to the page index + // Todo find a way to keep the last editor independent of the page index $newentry->lasteditor = utf8_encode("unknown"); // Set the editor to "unknown" // Extract the name of the (sub)page without the ".md" $pagekey = utf8_encode(substr($pagefilename, 0, -3)); diff --git a/index.php b/index.php index 1aa5e9a..b96a744 100644 --- a/index.php +++ b/index.php @@ -248,9 +248,44 @@ function get_subpages($pageindex, $pagename) } } + unset($pagenames); return $result; } +/* + * @summary Makes sure that a subpage's parents exist. Note this doesn't check the pagename itself. + * + * @param The pagename to check. + * + */ +function check_subpage_parents($pagename) +{ + global $pageindex; + // Save the new pageindex and return if there aren't any more parent pages to check + if(strpos($pagename, "/") === false) + { + file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT)); + return; + } + + $parent_pagename = substr($pagename, 0, strrpos($pagename, "/")); + $parent_page_filename = "$parent_pagename.md"; + if(!file_exists($parent_page_filename)) + { + // This parent page doesn't exist! Create it and add it to the page index. + touch($parent_page_filename, 0); + + $newentry = new stdClass(); + $newentry->filename = $parent_page_filename; + $newentry->size = 0; + $newentry->lastmodified = 0; + $newentry->lasteditor = "none"; + $pageindex->$parent_pagename = $newentry; + } + + check_subpage_parents($parent_pagename); +} + if(!file_exists("./pageindex.json")) { // From http://in.php.net/manual/en/function.glob.php#106595 @@ -279,7 +314,7 @@ if(!file_exists("./pageindex.json")) $newentry->filename = utf8_encode($pagefilename); // Store the filename $newentry->size = filesize($pagefilename); // Store the page size $newentry->lastmodified = filemtime($pagefilename); // Store the date last modified - // Todo find a way to keep the last editor independent to the page index + // Todo find a way to keep the last editor independent of the page index $newentry->lasteditor = utf8_encode("unknown"); // Set the editor to "unknown" // Extract the name of the (sub)page without the ".md" $pagekey = utf8_encode(substr($pagefilename, 0, -3)); @@ -713,7 +748,7 @@ register_module([ register_module([ "name" => "Page editor", - "version" => "0.7", + "version" => "0.8", "author" => "Starbeamrainbowlabs", "description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.", "id" => "page-edit", @@ -809,8 +844,12 @@ register_module([ mkdir(dirname("$page.md"), null, true); } + if(file_put_contents("$page.md", htmlentities($_POST["content"]), ENT_QUOTES) !== false) { + // Make sure that this page's parents exist + check_subpage_parents($page); + //update the page index if(!isset($pageindex->$page)) { diff --git a/module_index.json b/module_index.json index 3870b55..56f9954 100644 --- a/module_index.json +++ b/module_index.json @@ -25,11 +25,11 @@ }, { "name": "Page editor", - "version": "0.7", + "version": "0.8", "author": "Starbeamrainbowlabs", "description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.", "id": "page-edit", - "lastupdate": 1436797664 + "lastupdate": 1436885187 }, { "name": "Help page", diff --git a/modules/page-edit.php b/modules/page-edit.php index 288022b..e685e45 100644 --- a/modules/page-edit.php +++ b/modules/page-edit.php @@ -1,7 +1,7 @@ "Page editor", - "version" => "0.7", + "version" => "0.8", "author" => "Starbeamrainbowlabs", "description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.", "id" => "page-edit", @@ -97,8 +97,12 @@ register_module([ mkdir(dirname("$page.md"), null, true); } + if(file_put_contents("$page.md", htmlentities($_POST["content"]), ENT_QUOTES) !== false) { + // Make sure that this page's parents exist + check_subpage_parents($page); + //update the page index if(!isset($pageindex->$page)) { diff --git a/testcheck.php b/testcheck.php new file mode 100644 index 0000000..32727b3 --- /dev/null +++ b/testcheck.php @@ -0,0 +1,34 @@ +filename = $parent_page_filename; + $newentry->size = 0; + $newentry->lastmodified = 0; + $newentry->lasteditor = "none"; + + } + + check_subpage_parents($parent_pagename); +} + +check_subpage_parents("New Test\/New"); \ No newline at end of file