1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-25 05:22:59 +00:00

Add support for subpages to the page index. Opens #11.

This commit is contained in:
Starbeamrainbowlabs 2015-07-13 15:11:20 +01:00
parent 029f777b23
commit b955c1efc0
2 changed files with 142 additions and 15 deletions

View file

@ -65,16 +65,78 @@ if($isloggedin)
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
if(!file_exists("./pageindex.json")) if(!file_exists("./pageindex.json"))
{ {
$existingpages = glob("*.md"); // From http://in.php.net/manual/en/function.glob.php#106595
$pageindex = new stdClass(); function glob_recursive($pattern, $flags = 0)
foreach($existingpages as $pagefilename)
{ {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
{
$prefix = "$dir/";
// Remove the "./" from the beginning if it exists
if(substr($prefix, 0, 2) == "./") $prefix = substr($prefix, 2);
$files = array_merge($files, glob_recursive($prefix . basename($pattern), $flags));
}
return $files;
}
$existingpages = glob_recursive("*.md");
$pageindex = new stdClass();
// We use a for loop here because foreach doesn't loop over new values inserted
// while we were looping
for($i = 0; $i < count($existingpages); $i++)
{
$pagefilename = $existingpages[$i];
// Create a new entry
$newentry = new stdClass(); $newentry = new stdClass();
$newentry->filename = utf8_encode($pagefilename); $newentry->filename = utf8_encode($pagefilename); // Store the filename
$newentry->size = filesize($pagefilename); $newentry->size = filesize($pagefilename); // Store the page size
$newentry->lastmodified = filemtime($pagefilename); $newentry->lastmodified = filemtime($pagefilename); // Store the date last modified
$newentry->lasteditor = utf8_encode("unknown"); // Todo find a way to keep the last editor independent to 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)); $pagekey = utf8_encode(substr($pagefilename, 0, -3));
/// Sub Page finder ///
$newentry->subpages = new stdClass();
// Construct the stem to be used for finding sub pages
$stem = "$pagekey/";
$stem_length = strlen($stem);
foreach($existingpages as $item)
{
// note We *may* need to make this case insensitive on windows systems
if(substr($item, 0, $stem_length) == $stem)
{
// We have found a sub page of the current page!
// Extract the subpage's key
$subpage_relative_key = substr($item, $stem_length, -3);
// Calculate how many times removed the current subpage is from the current page. 0 = direct descendant.
$times_removed = substr_count($subpage_relative_key, "/");
$subpage_full_key = substr($item, 0, -3);
// Store the name of the subpage we found in the subpage object of the current page
$newentry->subpages->$subpage_full_key = $times_removed;
}
}
if(strpos($pagekey, "/") !== false)
{
// We have a sub page people
// Work out what our direct parent's key must be in order to check to
// make sure that it actually exists. If it doesn't, then we need to
// create it.
$subpage_parent_key = substr($pagekey, 0, strrpos($pagekey, "/"));
$subpage_parent_filename = "$subpage_parent_key.md";
if(array_search($subpage_parent_filename, $existingpages) === false)
{
// Our parent page doesn't acutally exist - create it
touch($subpage_parent_filename, 0);
// Furthermore, we should add this page to the list of existing pages
// in order for it to be indexed
$existingpages[] = $subpage_parent_filename;
}
}
// Store the new entry in the new page index
$pageindex->$pagekey = $newentry; $pageindex->$pagekey = $newentry;
} }
file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT)); file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT));

View file

@ -219,16 +219,78 @@ if($isloggedin)
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
if(!file_exists("./pageindex.json")) if(!file_exists("./pageindex.json"))
{ {
$existingpages = glob("*.md"); // From http://in.php.net/manual/en/function.glob.php#106595
$pageindex = new stdClass(); function glob_recursive($pattern, $flags = 0)
foreach($existingpages as $pagefilename)
{ {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
{
$prefix = "$dir/";
// Remove the "./" from the beginning if it exists
if(substr($prefix, 0, 2) == "./") $prefix = substr($prefix, 2);
$files = array_merge($files, glob_recursive($prefix . basename($pattern), $flags));
}
return $files;
}
$existingpages = glob_recursive("*.md");
$pageindex = new stdClass();
// We use a for loop here because foreach doesn't loop over new values inserted
// while we were looping
for($i = 0; $i < count($existingpages); $i++)
{
$pagefilename = $existingpages[$i];
// Create a new entry
$newentry = new stdClass(); $newentry = new stdClass();
$newentry->filename = utf8_encode($pagefilename); $newentry->filename = utf8_encode($pagefilename); // Store the filename
$newentry->size = filesize($pagefilename); $newentry->size = filesize($pagefilename); // Store the page size
$newentry->lastmodified = filemtime($pagefilename); $newentry->lastmodified = filemtime($pagefilename); // Store the date last modified
$newentry->lasteditor = utf8_encode("unknown"); // Todo find a way to keep the last editor independent to 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)); $pagekey = utf8_encode(substr($pagefilename, 0, -3));
/// Sub Page finder ///
$newentry->subpages = new stdClass();
// Construct the stem to be used for finding sub pages
$stem = "$pagekey/";
$stem_length = strlen($stem);
foreach($existingpages as $item)
{
// note We *may* need to make this case insensitive on windows systems
if(substr($item, 0, $stem_length) == $stem)
{
// We have found a sub page of the current page!
// Extract the subpage's key
$subpage_relative_key = substr($item, $stem_length, -3);
// Calculate how many times removed the current subpage is from the current page. 1 = direct descendant.
$times_removed = substr_count($subpage_relative_key, "/");
$subpage_full_key = substr($item, 0, -3);
// Store the name of the subpage we found in the subpage object of the current page
$newentry->subpages->$subpage_full_key = $times_removed;
}
}
if(strpos($pagekey, "/") !== false)
{
// We have a sub page people
// Work out what our direct parent's key must be in order to check to
// make sure that it actually exists. If it doesn't, then we need to
// create it.
$subpage_parent_key = substr($pagekey, 0, strrpos($pagekey, "/"));
$subpage_parent_filename = "$subpage_parent_key.md";
if(array_search($subpage_parent_filename, $existingpages) === false)
{
// Our parent page doesn't acutally exist - create it
touch($subpage_parent_filename, 0);
// Furthermore, we should add this page to the list of existing pages
// in order for it to be indexed
$existingpages[] = $subpage_parent_filename;
}
}
// Store the new entry in the new page index
$pageindex->$pagekey = $newentry; $pageindex->$pagekey = $newentry;
} }
file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT)); file_put_contents("./pageindex.json", json_encode($pageindex, JSON_PRETTY_PRINT));
@ -243,7 +305,10 @@ else
* *
* @details paths may only contain alphanumeric characters, spaces, underscores, and dashes * @details paths may only contain alphanumeric characters, spaces, underscores, and dashes
*/ */
function makepathsafe($string) { return preg_replace("/[^0-9a-zA-Z\_\-\ ]/i", "", $string); } function makepathsafe($string)
{
return preg_replace("/[^0-9a-zA-Z\_\-\ \/]/i", "", $string);
}
/* /*
* @summary Hides an email address from bots by adding random html entities. * @summary Hides an email address from bots by adding random html entities.