Add function to make sure parent pages exist. Fixes #12

This commit is contained in:
Starbeamrainbowlabs 2015-07-14 15:47:05 +01:00
parent ff397244c3
commit 52821d1a87
5 changed files with 118 additions and 6 deletions

View File

@ -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));

View File

@ -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))
{

View File

@ -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",

View File

@ -1,7 +1,7 @@
<?php
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",
@ -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))
{

34
testcheck.php Normal file
View File

@ -0,0 +1,34 @@
<?php
$pageindex = json_decode(file_get_contents("./pageindex.json"));
function check_subpage_parents($pagename)
{
global $pageindex;
echo("pagename: $pagename\n");
// 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";
echo("parent page name: $parent_pagename, filename: $parent_page_filename\n");
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";
}
check_subpage_parents($parent_pagename);
}
check_subpage_parents("New Test\/New");