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

Created new page-export module

This commit is contained in:
Starbeamrainbowlabs 2015-07-18 10:58:42 +01:00
parent daabf74406
commit 2c2325ad40
5 changed files with 175 additions and 28 deletions

View file

@ -84,6 +84,8 @@ function human_filesize($bytes, $decimals = 2)
* @source http://snippets.pro/snippet/137-php-convert-the-timestamp-to-human-readable-format/ * @source http://snippets.pro/snippet/137-php-convert-the-timestamp-to-human-readable-format/
* *
* @param $time - The timestamp to convert. * @param $time - The timestamp to convert.
*
* @returns {string} - The time since the given timestamp pas a human-readable string.
*/ */
function human_time_since($time) function human_time_since($time)
{ {
@ -104,6 +106,28 @@ function human_time_since($time)
} }
} }
/*
* @summary A recursive glob() function.
*
* @param $pattern - The glob pattern to use to find filenames.
* @param $flags - The glob flags to use when finding filenames.
*
* @returns {array} - An array of the filepaths that match the given glob.
*/
// From http://in.php.net/manual/en/function.glob.php#106595
function glob_recursive($pattern, $flags = 0)
{
$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;
}
/* /*
* @summary Gets a list of all the sub pagess of the current page. * @summary Gets a list of all the sub pagess of the current page.
* *
@ -218,19 +242,6 @@ function hide_email($str)
*/ */
if(!file_exists("./pageindex.json")) if(!file_exists("./pageindex.json"))
{ {
// From http://in.php.net/manual/en/function.glob.php#106595
function glob_recursive($pattern, $flags = 0)
{
$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"); $existingpages = glob_recursive("*.md");
$pageindex = new stdClass(); $pageindex = new stdClass();
// We use a for loop here because foreach doesn't loop over new values inserted // We use a for loop here because foreach doesn't loop over new values inserted
@ -349,10 +360,13 @@ class page_renderer
<p><em>Timed at {generation-date}</em> <p><em>Timed at {generation-date}</em>
<p><em>Powered by Pepperminty Wiki.</em></p>"; <p><em>Powered by Pepperminty Wiki.</em></p>";
public static function render($title, $content, $body_template) public static function render($title, $content, $body_template = false)
{ {
global $settings, $start_time; global $settings, $start_time;
if($body_template === false)
$body_template = page_renderer::$main_content_template;
$result = self::$html_template; $result = self::$html_template;
$result = str_replace("{body}", $body_template, $result); $result = str_replace("{body}", $body_template, $result);
$result = str_replace([ $result = str_replace([

View file

@ -86,6 +86,10 @@ $settings->admindetails = [
"email" => "admin@localhost" "email" => "admin@localhost"
]; ];
// Whether to only allow adminstrators to export the your wiki as a zip using
// the page-export module.
$settings->export_allow_only_admins = false;
// Array of links and display text to display at the top of the site. // Array of links and display text to display at the top of the site.
// Format: // Format:
// [ "Display Text", "Link" ] // [ "Display Text", "Link" ]
@ -286,6 +290,8 @@ function human_filesize($bytes, $decimals = 2)
* @source http://snippets.pro/snippet/137-php-convert-the-timestamp-to-human-readable-format/ * @source http://snippets.pro/snippet/137-php-convert-the-timestamp-to-human-readable-format/
* *
* @param $time - The timestamp to convert. * @param $time - The timestamp to convert.
*
* @returns {string} - The time since the given timestamp pas a human-readable string.
*/ */
function human_time_since($time) function human_time_since($time)
{ {
@ -306,6 +312,28 @@ function human_time_since($time)
} }
} }
/*
* @summary A recursive glob() function.
*
* @param $pattern - The glob pattern to use to find filenames.
* @param $flags - The glob flags to use when finding filenames.
*
* @returns {array} - An array of the filepaths that match the given glob.
*/
// From http://in.php.net/manual/en/function.glob.php#106595
function glob_recursive($pattern, $flags = 0)
{
$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;
}
/* /*
* @summary Gets a list of all the sub pagess of the current page. * @summary Gets a list of all the sub pagess of the current page.
* *
@ -420,19 +448,6 @@ function hide_email($str)
*/ */
if(!file_exists("./pageindex.json")) if(!file_exists("./pageindex.json"))
{ {
// From http://in.php.net/manual/en/function.glob.php#106595
function glob_recursive($pattern, $flags = 0)
{
$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"); $existingpages = glob_recursive("*.md");
$pageindex = new stdClass(); $pageindex = new stdClass();
// We use a for loop here because foreach doesn't loop over new values inserted // We use a for loop here because foreach doesn't loop over new values inserted
@ -551,10 +566,13 @@ class page_renderer
<p><em>Timed at {generation-date}</em> <p><em>Timed at {generation-date}</em>
<p><em>Powered by Pepperminty Wiki.</em></p>"; <p><em>Powered by Pepperminty Wiki.</em></p>";
public static function render($title, $content, $body_template) public static function render($title, $content, $body_template = false)
{ {
global $settings, $start_time; global $settings, $start_time;
if($body_template === false)
$body_template = page_renderer::$main_content_template;
$result = self::$html_template; $result = self::$html_template;
$result = str_replace("{body}", $body_template, $result); $result = str_replace("{body}", $body_template, $result);
$result = str_replace([ $result = str_replace([
@ -1021,6 +1039,58 @@ register_module([
register_module([
"name" => "Export",
"version" => "0.1",
"author" => "Starbeamrainbowlabs",
"description" => "Adds a page that you can use to export your wiki as a .zip file. Uses \$settings->export_only_allow_admins, which controls whether only admins are allowed to export the wiki.",
"id" => "page-export",
"code" => function() {
add_action("export", function() {
global $settings, $pageindex, $isadmin;
if($settings->export_allow_only_admins && !$isadmin)
{
http_response_code(401);
exit(page_renderer::render("Export error - $settings->sitename", "Only administrators of $settings->sitename are allowed to export the wiki as a zip. <a href='?action=$settings->defaultaction&page='>Return to the $settings->defaultpage</a>."));
}
$tmpfilename = tempnam(sys_get_temp_dir(), "pepperminty-wiki-");
$zip = new ZipArchive();
if($zip->open($tmpfilename, ZipArchive::CREATE) !== true)
{
http_response_code(507);
exit(page_renderer::render("Export error - $settings->sitename", "Pepperminty Wiki was unable to open a temporary file to store the exported data in. Please contact $settings->sitename's administrator (" . $settings->admindetails["name"] . " at " . hide_email($settings->admindetails["email"]) . ") for assistance."));
}
foreach($pageindex as $entry)
{
$zip->addFile("./$entry->filename", $entry->filename);
}
if($zip->close() !== true)
{
http_response_code(500);
exit(page_renderer::render("Export error - $settings->sitename", "Pepperminty wiki was unable to close the temporary zip file after creating it. Please contact $settings->sitename's administrator (" . $settings->admindetails["name"] . " at " . hide_email($settings->admindetails["email"]) . ") for assistance."));
}
header("content-type: application/zip");
header("content-disposition: attachment; filename=$settings->sitename-export.zip");
header("content-length: " . filesize($tmpfilename));
$zip_handle = fopen($tmpfilename, "rb");
fpassthru($zip_handle);
fclose($zip_handle);
unlink($tmpfilename);
});
}
]);
register_module([ register_module([
"name" => "Help page", "name" => "Help page",
"version" => "0.6", "version" => "0.6",

View file

@ -31,6 +31,14 @@
"id": "page-edit", "id": "page-edit",
"lastupdate": 1437037997 "lastupdate": 1437037997
}, },
{
"name": "Export",
"version": "0.1",
"author": "Starbeamrainbowlabs",
"description": "Adds a page that you can use to export your wiki as a .zip file. Uses $settings->export_only_allow_admins, which controls whether only admins are allowed to export the wiki.",
"id": "page-export",
"lastupdate": 1437213501
},
{ {
"name": "Help page", "name": "Help page",
"version": "0.6", "version": "0.6",

51
modules/page-export.php Normal file
View file

@ -0,0 +1,51 @@
<?php
register_module([
"name" => "Export",
"version" => "0.1",
"author" => "Starbeamrainbowlabs",
"description" => "Adds a page that you can use to export your wiki as a .zip file. Uses \$settings->export_only_allow_admins, which controls whether only admins are allowed to export the wiki.",
"id" => "page-export",
"code" => function() {
add_action("export", function() {
global $settings, $pageindex, $isadmin;
if($settings->export_allow_only_admins && !$isadmin)
{
http_response_code(401);
exit(page_renderer::render("Export error - $settings->sitename", "Only administrators of $settings->sitename are allowed to export the wiki as a zip. <a href='?action=$settings->defaultaction&page='>Return to the $settings->defaultpage</a>."));
}
$tmpfilename = tempnam(sys_get_temp_dir(), "pepperminty-wiki-");
$zip = new ZipArchive();
if($zip->open($tmpfilename, ZipArchive::CREATE) !== true)
{
http_response_code(507);
exit(page_renderer::render("Export error - $settings->sitename", "Pepperminty Wiki was unable to open a temporary file to store the exported data in. Please contact $settings->sitename's administrator (" . $settings->admindetails["name"] . " at " . hide_email($settings->admindetails["email"]) . ") for assistance."));
}
foreach($pageindex as $entry)
{
$zip->addFile("./$entry->filename", $entry->filename);
}
if($zip->close() !== true)
{
http_response_code(500);
exit(page_renderer::render("Export error - $settings->sitename", "Pepperminty wiki was unable to close the temporary zip file after creating it. Please contact $settings->sitename's administrator (" . $settings->admindetails["name"] . " at " . hide_email($settings->admindetails["email"]) . ") for assistance."));
}
header("content-type: application/zip");
header("content-disposition: attachment; filename=$settings->sitename-export.zip");
header("content-length: " . filesize($tmpfilename));
$zip_handle = fopen($tmpfilename, "rb");
fpassthru($zip_handle);
fclose($zip_handle);
unlink($tmpfilename);
});
}
]);
?>

View file

@ -83,6 +83,10 @@ $settings->admindetails = [
"email" => "admin@localhost" "email" => "admin@localhost"
]; ];
// Whether to only allow adminstrators to export the your wiki as a zip using
// the page-export module.
$settings->export_allow_only_admins = false;
// Array of links and display text to display at the top of the site. // Array of links and display text to display at the top of the site.
// Format: // Format:
// [ "Display Text", "Link" ] // [ "Display Text", "Link" ]