mirror of
https://github.com/sbrl/Pepperminty-Wiki.git
synced 2024-11-22 16:33:00 +00:00
Add function to make sure parent pages exist. Fixes #12
This commit is contained in:
parent
ff397244c3
commit
52821d1a87
5 changed files with 118 additions and 6 deletions
37
core.php
37
core.php
|
@ -94,9 +94,44 @@ function get_subpages($pageindex, $pagename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unset($pagenames);
|
||||||
return $result;
|
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"))
|
if(!file_exists("./pageindex.json"))
|
||||||
{
|
{
|
||||||
// From http://in.php.net/manual/en/function.glob.php#106595
|
// 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->filename = utf8_encode($pagefilename); // Store the filename
|
||||||
$newentry->size = filesize($pagefilename); // Store the page size
|
$newentry->size = filesize($pagefilename); // Store the page size
|
||||||
$newentry->lastmodified = filemtime($pagefilename); // Store the date last modified
|
$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"
|
$newentry->lasteditor = utf8_encode("unknown"); // Set the editor to "unknown"
|
||||||
// Extract the name of the (sub)page without the ".md"
|
// Extract the name of the (sub)page without the ".md"
|
||||||
$pagekey = utf8_encode(substr($pagefilename, 0, -3));
|
$pagekey = utf8_encode(substr($pagefilename, 0, -3));
|
||||||
|
|
43
index.php
43
index.php
|
@ -248,9 +248,44 @@ function get_subpages($pageindex, $pagename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unset($pagenames);
|
||||||
return $result;
|
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"))
|
if(!file_exists("./pageindex.json"))
|
||||||
{
|
{
|
||||||
// From http://in.php.net/manual/en/function.glob.php#106595
|
// 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->filename = utf8_encode($pagefilename); // Store the filename
|
||||||
$newentry->size = filesize($pagefilename); // Store the page size
|
$newentry->size = filesize($pagefilename); // Store the page size
|
||||||
$newentry->lastmodified = filemtime($pagefilename); // Store the date last modified
|
$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"
|
$newentry->lasteditor = utf8_encode("unknown"); // Set the editor to "unknown"
|
||||||
// Extract the name of the (sub)page without the ".md"
|
// Extract the name of the (sub)page without the ".md"
|
||||||
$pagekey = utf8_encode(substr($pagefilename, 0, -3));
|
$pagekey = utf8_encode(substr($pagefilename, 0, -3));
|
||||||
|
@ -713,7 +748,7 @@ register_module([
|
||||||
|
|
||||||
register_module([
|
register_module([
|
||||||
"name" => "Page editor",
|
"name" => "Page editor",
|
||||||
"version" => "0.7",
|
"version" => "0.8",
|
||||||
"author" => "Starbeamrainbowlabs",
|
"author" => "Starbeamrainbowlabs",
|
||||||
"description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
|
"description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
|
||||||
"id" => "page-edit",
|
"id" => "page-edit",
|
||||||
|
@ -809,8 +844,12 @@ register_module([
|
||||||
mkdir(dirname("$page.md"), null, true);
|
mkdir(dirname("$page.md"), null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(file_put_contents("$page.md", htmlentities($_POST["content"]), ENT_QUOTES) !== false)
|
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
|
//update the page index
|
||||||
if(!isset($pageindex->$page))
|
if(!isset($pageindex->$page))
|
||||||
{
|
{
|
||||||
|
|
|
@ -25,11 +25,11 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Page editor",
|
"name": "Page editor",
|
||||||
"version": "0.7",
|
"version": "0.8",
|
||||||
"author": "Starbeamrainbowlabs",
|
"author": "Starbeamrainbowlabs",
|
||||||
"description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
|
"description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
|
||||||
"id": "page-edit",
|
"id": "page-edit",
|
||||||
"lastupdate": 1436797664
|
"lastupdate": 1436885187
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Help page",
|
"name": "Help page",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
register_module([
|
register_module([
|
||||||
"name" => "Page editor",
|
"name" => "Page editor",
|
||||||
"version" => "0.7",
|
"version" => "0.8",
|
||||||
"author" => "Starbeamrainbowlabs",
|
"author" => "Starbeamrainbowlabs",
|
||||||
"description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
|
"description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
|
||||||
"id" => "page-edit",
|
"id" => "page-edit",
|
||||||
|
@ -97,8 +97,12 @@ register_module([
|
||||||
mkdir(dirname("$page.md"), null, true);
|
mkdir(dirname("$page.md"), null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(file_put_contents("$page.md", htmlentities($_POST["content"]), ENT_QUOTES) !== false)
|
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
|
//update the page index
|
||||||
if(!isset($pageindex->$page))
|
if(!isset($pageindex->$page))
|
||||||
{
|
{
|
||||||
|
|
34
testcheck.php
Normal file
34
testcheck.php
Normal 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");
|
Loading…
Reference in a new issue