From 0253ab6c0edea0613452f1d6204b9200567475d9 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 18 Oct 2016 18:34:27 +0100 Subject: [PATCH] Moved history logic to core & tied it in with $env. Also fixed a few bugs in view & upgraded the display mode. --- Changelog.md | 12 ++++- build/index.php | 103 +++++++++++++++++++++----------------- core.php | 37 ++++++++++++-- module_index.json | 24 ++++----- modules/page-view.php | 64 +++++++++-------------- peppermint.guiconfig.json | 2 +- 6 files changed, 139 insertions(+), 103 deletions(-) diff --git a/Changelog.md b/Changelog.md index a055260..348d206 100755 --- a/Changelog.md +++ b/Changelog.md @@ -1,4 +1,14 @@ -data# Changelog +# Changelog + +## v0.13-dev + +### Changed + - Overhauled internal history logic - history logic is now done in core. + - Added `$env->page_filename`, which points to the current page on disk. + - Changed the way different display modes are accessed. You can now use the new `mode` parameter to the `view` action. It supports 4 different modes at present: `normal`, `printable`, `contentonly`, and `parsedsourceonly`. + +### Fixed + - Fxed huge issue with `contentonly` display mode. ## v0.12.1-dev diff --git a/build/index.php b/build/index.php index 15a2484..c4f8969 100755 --- a/build/index.php +++ b/build/index.php @@ -103,7 +103,7 @@ $guiConfig = <<<'GUICONFIG' "nav_links_bottom": {"type": "text", "description": "An array of links in the above format that will be shown at the bottom of the page.", "default": [ [ "🖶 Printable version", - "index.php?action=view&printable=yes&page={page}" + "index.php?action=view&mode=printable&page={page}" ], [ "Credits", @@ -313,6 +313,11 @@ $version = "v0.12.1-dev"; $env = new stdClass(); $env->action = $settings->defaultaction; $env->page = ""; +$env->page_filename = ""; +$env->is_history_revision = false; +$env->history = new stdClass(); +$env->history->revision_number = -1; +$env->history->revision_data = false; $env->user = "Anonymous"; $env->is_logged_in = false; $env->is_admin = false; @@ -329,7 +334,7 @@ foreach ($paths as &$path) { $path = $env->storage_prefix . $path; } -$paths->upload_file_prefix = "Files/"; // The prefix to append to uploaded files +$paths->upload_file_prefix = "Files/"; // The prefix to add to uploaded files session_start(); ///////// Login System ///////// @@ -1010,9 +1015,6 @@ if(makepathsafe($_GET["page"]) !== $_GET["page"]) exit(); } -// Finish setting up the environment object -$env->page = $_GET["page"]; -$env->action = strtolower($_GET["action"]); //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// @@ -1311,6 +1313,33 @@ class page_renderer } } +/// Finish setting up the environment object /// +$env->page = $_GET["page"]; +if(isset($_GET["revision"]) and is_numeric($_GET["revision"])) +{ + // We have a revision number! + $env->is_history_revision = true; + $env->history->revision_number = intval($_GET["revision"]); + $env->history->revision_data = $pageindex->{$env->page}->history[$revisionNumber]; + + // Make sure that the revision exists for later on + if(!isset($pageindex->{$env->page}->history[$revisionNumber])) + { + http_response_code(404); + exit(page_renderer::render_main("404: Revision Not Found - $env->page - $settings->sitename", "

Revision #{$env->history->revision_number} of $env->page doesn't appear to exist. Try viewing the list of revisions for $env->page, or viewing the latest revision instead.

")); + } +} +// Construct the page's filename +$env->page_filename = $env->storage_prefix; +if($env->is_history_revision) + $env->page_filename .= $pageindex->{$env->page}->history[$env->history->revision_number]->filename; +else + $env->page_filename .= $pageindex->{$env->page}->filename; + +$env->action = strtolower($_GET["action"]); + +//////////////////////////////////////////////// + ////////////////////////////////////// ///// Extra consistency measures ///// ////////////////////////////////////// @@ -2161,7 +2190,7 @@ register_module([ /** - * @api {get} ?action=idindex-show Show the id index. + * @api {get} ?action=idindex-show Show the id index * @apiDescription Outputs the id index. Useful if you need to verify that it's working as expected. * @apiName SearchShowIdIndex * @apiGroup Search @@ -4673,7 +4702,7 @@ register_module([ * * @apiUse PageParameter * @apiParam {number} revision The revision number to display. - * @apiParam {string} printable Set to 'yes' to get a printable version of the specified page instead. + * @apiParam {string} mode Optional. The display mode to use. Can hld the following values: 'normal' - The default. Sends a normal page. 'printable' - Sends a printable version of the page. 'contentonly' - Sends only the content of the page, not the extra stuff around it. 'parsedsourceonly' - Sends only the raw rendered source of the page, as it appears just after it has come out of the page parser. Useful for writing external tools (see also the `raw` action). * * @apiError NonExistentPageError The page doesn't exist and editing is disabled in the wiki's settings. If editing isn't disabled, you will be redirected to the edit page instead. * @apiError NonExistentRevisionError The specified revision was not found. @@ -4743,51 +4772,26 @@ register_module([ } } - $isHistoryRevision = false; - if(isset($_GET["revision"]) and is_numeric($_GET["revision"])) - { - // We have a revision number! - $isHistoryRevision = true; - $revisionNumber = intval($_GET["revision"]); - $revisionData = $pageindex->{$env->page}->history[$revisionNumber]; - - // Make sure that the revision exists for later on - if(!isset($pageindex->{$env->page}->history[$revisionNumber])) - { - http_response_code(404); - exit(page_renderer::render_main("404: Revision Not Found - $env->page - $settings->sitename", "

Revision #$revisionNumer of $env->page doesn't appear to exist. Try viewing the list of revisions for $env->page, or viewing the latest revision instead.

")); - } - } - - $title = "$env->page - $settings->sitename"; if(isset($pageindex->$page->protect) && $pageindex->$page->protect === true) $title = $settings->protectedpagechar . $title; $content = ""; - if(!$isHistoryRevision) + if(!$env->is_history_revision) $content .= "

$env->page

\n"; else { - $content .= "

Revision #$revisionNumber of $env->page

\n"; - $content .= "

(Revision saved by $revisionData->editor " . render_timestamp($revisionData->timestamp) . ". Jump to the current revision or see a list of all revisions for this page.)

\n"; + $content .= "

Revision #{$env->history->revision_number} of $env->page

\n"; + $content .= "

(Revision saved by $revisionData->editor " . render_timestamp($env->history->revision_data->timestamp) . ". Jump to the current revision or see a list of all revisions for this page.)

\n"; } // Add an extra message if the requester was redirected from another page if(isset($_GET["redirected_from"])) $content .= "

Redirected from " . $_GET["redirected_from"] . ".

"; - // Construct the filename - $pageFilename = "$env->storage_prefix"; - if($isHistoryRevision) - { - $pageFilename .= $pageindex->{$env->page}->history[$revisionNumber]->filename; - } - else - $pageFilename .= $pageindex->{$env->page}->filename; - $parsing_start = microtime(true); - $content .= parse_page_source(file_get_contents($pageFilename)); + $rawRenderedSource = parse_page_source(file_get_contents($env->page_filename)); + $content .= $rawRenderedSource; if(!empty($pageindex->$page->tags)) { @@ -4831,15 +4835,24 @@ register_module([ time() - $pageindex->{$env->page}->lastmodified < $settings->delayed_indexing_time) header("x-robots-tag: noindex"); - // Content only mode: Send only the raw rendered page - if(isset($_GET["contentonly"]) and $_GET["contentonly"] === "yes") - exit(parse_page_source($content)); - // Printable: Sends a printable version of the page - if(isset($_GET["printable"]) and $_GET["printable"] === "yes") - exit(page_renderer::render_minimal($title, $content)); - // Normal page $settings->footer_message = "Last edited at " . date('h:ia T \o\n j F Y', $pageindex->{$env->page}->lastmodified) . ".

\n

"; // Add the last edited time to the footer - exit(page_renderer::render_main($title, $content)); + + switch(strtolower(trim($_GET["mode"]))) + { + case "contentonly": + // Content only mode: Send only the content of the page + exit($content); + case "parsedsourceonly": + // Parsed source only mode: Send only the raw rendered source + exit($rawRenderedSource); + case "printable": + // Printable mode: Sends a printable version of the page + exit(page_renderer::render_minimal($title, $content)); + case "normal": + default: + // Normal mode: Send a normal page + exit(page_renderer::render_main($title, $content)); + } }); } ]); diff --git a/core.php b/core.php index 93ee0fe..45998e2 100644 --- a/core.php +++ b/core.php @@ -12,6 +12,11 @@ $version = "{version}"; $env = new stdClass(); $env->action = $settings->defaultaction; $env->page = ""; +$env->page_filename = ""; +$env->is_history_revision = false; +$env->history = new stdClass(); +$env->history->revision_number = -1; +$env->history->revision_data = false; $env->user = "Anonymous"; $env->is_logged_in = false; $env->is_admin = false; @@ -28,7 +33,7 @@ foreach ($paths as &$path) { $path = $env->storage_prefix . $path; } -$paths->upload_file_prefix = "Files/"; // The prefix to append to uploaded files +$paths->upload_file_prefix = "Files/"; // The prefix to add to uploaded files session_start(); ///////// Login System ///////// @@ -709,9 +714,6 @@ if(makepathsafe($_GET["page"]) !== $_GET["page"]) exit(); } -// Finish setting up the environment object -$env->page = $_GET["page"]; -$env->action = strtolower($_GET["action"]); //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// @@ -1010,6 +1012,33 @@ class page_renderer } } +/// Finish setting up the environment object /// +$env->page = $_GET["page"]; +if(isset($_GET["revision"]) and is_numeric($_GET["revision"])) +{ + // We have a revision number! + $env->is_history_revision = true; + $env->history->revision_number = intval($_GET["revision"]); + $env->history->revision_data = $pageindex->{$env->page}->history[$revisionNumber]; + + // Make sure that the revision exists for later on + if(!isset($pageindex->{$env->page}->history[$revisionNumber])) + { + http_response_code(404); + exit(page_renderer::render_main("404: Revision Not Found - $env->page - $settings->sitename", "

Revision #{$env->history->revision_number} of $env->page doesn't appear to exist. Try viewing the list of revisions for $env->page, or viewing the latest revision instead.

")); + } +} +// Construct the page's filename +$env->page_filename = $env->storage_prefix; +if($env->is_history_revision) + $env->page_filename .= $pageindex->{$env->page}->history[$env->history->revision_number]->filename; +else + $env->page_filename .= $pageindex->{$env->page}->filename; + +$env->action = strtolower($_GET["action"]); + +//////////////////////////////////////////////// + ////////////////////////////////////// ///// Extra consistency measures ///// ////////////////////////////////////// diff --git a/module_index.json b/module_index.json index 19b6298..3439c5f 100755 --- a/module_index.json +++ b/module_index.json @@ -50,7 +50,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds recent changes. Access through the 'recent-changes' action.", "id": "feature-recent-changes", - "lastupdate": 1465757910, + "lastupdate": 1476809773, "optional": false }, { @@ -68,7 +68,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.", "id": "feature-search", - "lastupdate": 1472229999, + "lastupdate": 1476809773, "optional": false }, { @@ -77,7 +77,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File\/' prefix.", "id": "feature-upload", - "lastupdate": 1471884345, + "lastupdate": 1476809773, "optional": false }, { @@ -86,7 +86,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds the credits page. You *must* have this module :D", "id": "page-credits", - "lastupdate": 1471632910, + "lastupdate": 1472230366, "optional": false }, { @@ -113,7 +113,7 @@ "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": 1471809336, + "lastupdate": 1476809773, "optional": false }, { @@ -122,7 +122,7 @@ "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": 1466582751, + "lastupdate": 1472230366, "optional": false }, { @@ -131,7 +131,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds a rather useful help page. Access through the 'help' action. This module also exposes help content added to Pepperminty Wiki's inbuilt invisible help section system.", "id": "page-help", - "lastupdate": 1471697534, + "lastupdate": 1472230366, "optional": false }, { @@ -140,7 +140,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds a page that lists all the pages in the index along with their metadata.", "id": "page-list", - "lastupdate": 1471633011, + "lastupdate": 1472230366, "optional": false }, { @@ -149,7 +149,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds a pair of actions (login and checklogin) that allow users to login. You need this one if you want your users to be able to login.", "id": "page-login", - "lastupdate": 1472061908, + "lastupdate": 1472230366, "optional": false }, { @@ -167,7 +167,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds an action to allow administrators to move pages.", "id": "page-move", - "lastupdate": 1471632807, + "lastupdate": 1476809773, "optional": false }, { @@ -185,7 +185,7 @@ "author": "Starbeamrainbowlabs", "description": "Allows you to view pages. You really should include this one.", "id": "page-view", - "lastupdate": 1471784271, + "lastupdate": 1476811748, "optional": false }, { @@ -203,7 +203,7 @@ "author": "Emanuil Rusev & Starbeamrainbowlabs", "description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.", "id": "parser-parsedown", - "lastupdate": 1471954922, + "lastupdate": 1476809773, "optional": false } ] \ No newline at end of file diff --git a/modules/page-view.php b/modules/page-view.php index 2d72005..ff69e80 100644 --- a/modules/page-view.php +++ b/modules/page-view.php @@ -1,7 +1,7 @@ "Page viewer", - "version" => "0.15", + "version" => "0.16", "author" => "Starbeamrainbowlabs", "description" => "Allows you to view pages. You really should include this one.", "id" => "page-view", @@ -14,7 +14,7 @@ register_module([ * * @apiUse PageParameter * @apiParam {number} revision The revision number to display. - * @apiParam {string} printable Set to 'yes' to get a printable version of the specified page instead. + * @apiParam {string} mode Optional. The display mode to use. Can hld the following values: 'normal' - The default. Sends a normal page. 'printable' - Sends a printable version of the page. 'contentonly' - Sends only the content of the page, not the extra stuff around it. 'parsedsourceonly' - Sends only the raw rendered source of the page, as it appears just after it has come out of the page parser. Useful for writing external tools (see also the `raw` action). * * @apiError NonExistentPageError The page doesn't exist and editing is disabled in the wiki's settings. If editing isn't disabled, you will be redirected to the edit page instead. * @apiError NonExistentRevisionError The specified revision was not found. @@ -84,51 +84,26 @@ register_module([ } } - $isHistoryRevision = false; - if(isset($_GET["revision"]) and is_numeric($_GET["revision"])) - { - // We have a revision number! - $isHistoryRevision = true; - $revisionNumber = intval($_GET["revision"]); - $revisionData = $pageindex->{$env->page}->history[$revisionNumber]; - - // Make sure that the revision exists for later on - if(!isset($pageindex->{$env->page}->history[$revisionNumber])) - { - http_response_code(404); - exit(page_renderer::render_main("404: Revision Not Found - $env->page - $settings->sitename", "

Revision #$revisionNumer of $env->page doesn't appear to exist. Try viewing the list of revisions for $env->page, or viewing the latest revision instead.

")); - } - } - - $title = "$env->page - $settings->sitename"; if(isset($pageindex->$page->protect) && $pageindex->$page->protect === true) $title = $settings->protectedpagechar . $title; $content = ""; - if(!$isHistoryRevision) + if(!$env->is_history_revision) $content .= "

$env->page

\n"; else { - $content .= "

Revision #$revisionNumber of $env->page

\n"; - $content .= "

(Revision saved by $revisionData->editor " . render_timestamp($revisionData->timestamp) . ". Jump to the current revision or see a list of all revisions for this page.)

\n"; + $content .= "

Revision #{$env->history->revision_number} of $env->page

\n"; + $content .= "

(Revision saved by $revisionData->editor " . render_timestamp($env->history->revision_data->timestamp) . ". Jump to the current revision or see a list of all revisions for this page.)

\n"; } // Add an extra message if the requester was redirected from another page if(isset($_GET["redirected_from"])) $content .= "

Redirected from " . $_GET["redirected_from"] . ".

"; - // Construct the filename - $pageFilename = "$env->storage_prefix"; - if($isHistoryRevision) - { - $pageFilename .= $pageindex->{$env->page}->history[$revisionNumber]->filename; - } - else - $pageFilename .= $pageindex->{$env->page}->filename; - $parsing_start = microtime(true); - $content .= parse_page_source(file_get_contents($pageFilename)); + $rawRenderedSource = parse_page_source(file_get_contents($env->page_filename)); + $content .= $rawRenderedSource; if(!empty($pageindex->$page->tags)) { @@ -172,15 +147,24 @@ register_module([ time() - $pageindex->{$env->page}->lastmodified < $settings->delayed_indexing_time) header("x-robots-tag: noindex"); - // Content only mode: Send only the raw rendered page - if(isset($_GET["contentonly"]) and $_GET["contentonly"] === "yes") - exit(parse_page_source($content)); - // Printable: Sends a printable version of the page - if(isset($_GET["printable"]) and $_GET["printable"] === "yes") - exit(page_renderer::render_minimal($title, $content)); - // Normal page $settings->footer_message = "Last edited at " . date('h:ia T \o\n j F Y', $pageindex->{$env->page}->lastmodified) . ".

\n

"; // Add the last edited time to the footer - exit(page_renderer::render_main($title, $content)); + + switch(strtolower(trim($_GET["mode"]))) + { + case "contentonly": + // Content only mode: Send only the content of the page + exit($content); + case "parsedsourceonly": + // Parsed source only mode: Send only the raw rendered source + exit($rawRenderedSource); + case "printable": + // Printable mode: Sends a printable version of the page + exit(page_renderer::render_minimal($title, $content)); + case "normal": + default: + // Normal mode: Send a normal page + exit(page_renderer::render_main($title, $content)); + } }); } ]); diff --git a/peppermint.guiconfig.json b/peppermint.guiconfig.json index 8fe23ee..af1d257 100644 --- a/peppermint.guiconfig.json +++ b/peppermint.guiconfig.json @@ -81,7 +81,7 @@ "nav_links_bottom": {"type": "text", "description": "An array of links in the above format that will be shown at the bottom of the page.", "default": [ [ "🖶 Printable version", - "index.php?action=view&printable=yes&page={page}" + "index.php?action=view&mode=printable&page={page}" ], [ "Credits",