1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-10-31 21:33:00 +00:00

Add history-revert action, but it isn't tested yet.

This commit is contained in:
Starbeamrainbowlabs 2018-07-02 23:44:10 +01:00
parent 2853cf4da5
commit fffadb829a
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
7 changed files with 307 additions and 82 deletions

View file

@ -8,6 +8,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
- [Rest API] Add `user-add` and `set-password` moderator actions - [Rest API] Add `user-add` and `set-password` moderator actions
- Added `random_page_exclude_redirects` setting that prevents the `random` action from returning redirect pages. - Added `random_page_exclude_redirects` setting that prevents the `random` action from returning redirect pages.
- Added link to user table on the credits page - Added link to user table on the credits page
- Added history reversion via the `history-revert` action
### Fixed ### Fixed
- Updated the search system to transliterate characters to better support searching pages that are written in other languages. - Updated the search system to transliterate characters to better support searching pages that are written in other languages.

View file

@ -402,7 +402,7 @@ if($settings->sessionprefix == "auto")
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
/** The version of Pepperminty Wiki currently running. */ /** The version of Pepperminty Wiki currently running. */
$version = "v0.17-dev"; $version = "v0.17-dev";
$commit = "10b374871faaaebe84139ac36c7708c2598313ca"; $commit = "2853cf4da595832f9d7e3519c3fb96cfb714d745";
/// Environment /// /// Environment ///
/** Holds information about the current request environment. */ /** Holds information about the current request environment. */
$env = new stdClass(); $env = new stdClass();
@ -503,15 +503,15 @@ if($env->is_logged_in)
/** /**
* @apiDefine Admin Only the wiki administrator may use this call. * @apiDefine Admin Only the wiki administrator may use this call.
*/ */
/** /**
* @apiDefine Moderator Only users loggged with a moderator account may use this call. * @apiDefine Moderator Only users loggged with a moderator account may use this call.
*/ */
/** /**
* @apiDefine User Only users loggged in may use this call. * @apiDefine User Only users loggged in may use this call.
*/ */
/** /**
* @apiDefine Anonymous Anybody may use this call. * @apiDefine Anonymous Anybody may use this call.
*/ */
/** /**
* @apiDefine UserNotLoggedInError * @apiDefine UserNotLoggedInError
* @apiError UserNotLoggedInError You didn't log in before sending this request. * @apiError UserNotLoggedInError You didn't log in before sending this request.
@ -2347,7 +2347,7 @@ register_module([
"name" => "API status", "name" => "API status",
"version" => "0.1", "version" => "0.1",
"author" => "Starbeamrainbowlabs", "author" => "Starbeamrainbowlabs",
"description" => "Provides a basic JSON status action that provices a few useful bits of information for API consumption.", "description" => "Provides a basic JSON status action that provides a few useful bits of information for API consumption.",
"id" => "api-status", "id" => "api-status",
"code" => function() { "code" => function() {
global $settings; global $settings;
@ -2360,7 +2360,13 @@ register_module([
* @apiParam {boolean} Whether or not the result should be minified JSON. Default: false * @apiParam {boolean} Whether or not the result should be minified JSON. Default: false
*/ */
/*
* ███████ ████████ █████ ████████ ██ ██ ███████
* ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ██ ██ ███████
* ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ██ ██████ ███████
*/
add_action("status", function() { add_action("status", function() {
global $version, $env, $settings, $actions; global $version, $env, $settings, $actions;
@ -3210,7 +3216,7 @@ register_module([
* @apiGroup Page * @apiGroup Page
* @apiPermission Anonymous * @apiPermission Anonymous
* *
* @apiParam {string} page The page name to return a revision list for. * @apiUse PageParameter
* @apiParam {string} format The format to return the list of pages in. available values: html, json, text. Default: html * @apiParam {string} format The format to return the list of pages in. available values: html, json, text. Default: html
*/ */
@ -3235,8 +3241,8 @@ register_module([
$content .= "\t\t<ul class='page-list'>\n"; $content .= "\t\t<ul class='page-list'>\n";
foreach(array_reverse($pageindex->{$env->page}->history) as $revisionData) foreach(array_reverse($pageindex->{$env->page}->history) as $revisionData)
{ {
// Only display edits for now // Only display edits & reverts for now
if($revisionData->type != "edit") if($revisionData->type != "edit" || $revisionData->type != "revert")
continue; continue;
// The number (and the sign) of the size difference to display // The number (and the sign) of the size difference to display
@ -3246,7 +3252,11 @@ register_module([
$size_display_class .= " significant"; $size_display_class .= " significant";
$size_title_display = human_filesize($revisionData->newsize - $revisionData->sizediff) . " -> " . human_filesize($revisionData->newsize); $size_title_display = human_filesize($revisionData->newsize - $revisionData->sizediff) . " -> " . human_filesize($revisionData->newsize);
$content .= "<li><a href='?page=" . rawurlencode($env->page) . "&revision=$revisionData->rid'>#$revisionData->rid</a> " . render_editor(page_renderer::render_username($revisionData->editor)) . " " . render_timestamp($revisionData->timestamp) . " <span class='cursor-query $size_display_class' title='$size_title_display'>($size_display)</span>"; $content .= "<li>";
$content .= "<a href='?page=" . rawurlencode($env->page) . "&revision=$revisionData->rid'>#$revisionData->rid</a> " . render_editor(page_renderer::render_username($revisionData->editor)) . " " . render_timestamp($revisionData->timestamp) . " <span class='cursor-query $size_display_class' title='$size_title_display'>($size_display)</span>";
if($env->is_logged_in || ($settings->history_revert_require_moderator && $env->is_admin && $env->is_logged_in))
$content .= " <small>(<a class='revert-button' href='?action=history-revert&page=" . rawurlencode($env->page) . "&revision=$revisionData->rid'>restore this revision</a>)</small>";
$content .= "</li>";
} }
} }
else else
@ -3281,12 +3291,105 @@ register_module([
}); });
/**
* @api {get} ?action=history-revert&page={pageName}&revision={rid} Revert a page to a previous version
* @apiName HistoryRevert
* @apiGroup Editing
* @apiPermission User
* @apiUse PageParameter
* @apiUse UserNotLoggedInError
* @apiUse UserNotModeratorError
*
* @apiParam {string} revision The page revision number to revert to.
*/
/*
* ██ ██ ██ ███████ ████████ ██████ ██████ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ██ ██ ██████ ████ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ███████ ██ ██████ ██ ██ ██
*
* ██████ ███████ ██ ██ ███████ ██████ ████████
* ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ █████ ██ ██ █████ ██████ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ███████ ████ ███████ ██ ██ ██
*/
add_action("history-revert", function() {
global $env, $settings, $pageindex;
if((!$env->is_admin && $settings->history_revert_require_moderator) ||
$env->is_logged_in) {
http_response_code(401);
exit(page_renderer::render_main("Unauthorised - $settings->sitename", "<p>You can't revert pages to a previous revision because " . ($settings->history_revert_require_moderator && $env->is_logged_in ? "you aren't logged in as a moderator. You can try <a href='?action=logout'>logging out</a> and then" : "you aren't logged in. You can try") . " <a href='?action=login&returnto=" . rawurlencode("?action=history-revert&revision={$env->history->revision_number}&page=" . rawurlencode($env->page)) . "logging in</a>."));
}
$current_revision_filepath = "$env->storage_prefix/{$pageindex->{$env->page}->filename}";
// Figure out what we're saving
$newsource = file_get_contents($env->page_filename); // The old revision content - the Pepperminty Wiki core sorts this out for us
$oldsource = file_get_contents($current_revision_filepath); // The current revision's content
// Save the old content over the current content
file_put_contents($current_revision_filepath, $newsource);
// NOTE: We don't run the save preprocessors here because they are run when a page is edited - reversion is special and requires different treatment.
// FUTURE: We may want ot refactor the save preprocessor system ot take a single object instead - then we can add as many params as we like and we could execute the save preprocessors as normal :P
// Add the old content as a new revision
$result = history_add_revision(
$pageindex->{$env->page},
$newsource,
$oldsource,
true, // Yep, go ahead and save the page index
"revert" // It's a revert, not an edit
);
// Update the redirect metadata, if the redirect module is installed
if(module_exists("feature-redirect"))
update_redirect_metadata($pageindex->{$env->page}, $newsource);
// Add an entry to the recent changes log, if the module exists
if($result !== false && module_exists("feature-recent-changes"))
add_recent_change([
"type" => "revert",
"timestamp" => time(),
"page" => $env->page,
"user" => $env->user,
"newsize" => strlen($newsource),
"sizediff" => strlen($newsource) - strlen($oldsource)
]);
if($result === false) {
http_response_code(503);
exit(page_renderer::render_main("Server Error - Revert - $settings->sitename", "<p>A server error occurred when $settings->sitename tried to save the reversion of <code>" . htmlentities($env->page) . "</code>. Please contact $settings->sitename's administrator $settings->admindetails_name, whose email address can be found at the bottom of every page (including this one).</p>"));
}
http_response_code(201);
exit(page_renderer::render_main("Reverting " . htmlentities($env->page) . " - $settings->sitename", "<p>" . htmlentities($env->page) . " has been reverted back to revision {$env->history->revision_number} successfully.</p>
<p><a href='?page=" . rawurlencode($env->page) . "'>Go back</a> to the page, or continue <a href='?action=history&page = " . rawurlencode($env->page) . "'>reviewing its history</a>.</p>"));
// $env->page_filename
//
});
register_save_preprocessor("history_add_revision"); register_save_preprocessor("history_add_revision");
} }
]); ]);
function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true) { /**
* Adds a history revision against a page.
* Note: Does not updaate the current page content! This function _only_
* records a new revision against a page name. Thus it is possible to have a
* disparaty between the history revisions and the actual content displayed in
* the current revision if you're not careful!
* @param object $pageinfo The pageindex object of the page to operate on.
* @param string $newsource The page content to save as the new revision.
* @param string $oldsource The old page content that is the current revision (before the update).
* @param boolean $save_pageindex Whether the page index should be saved to disk.
* @param string $change_type The type of change to record this as in the history revision log
*/
function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true, $change_type = "edit") {
global $pageindex, $paths, $env; global $pageindex, $paths, $env;
if(!isset($pageinfo->history)) if(!isset($pageinfo->history))
@ -3294,8 +3397,7 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
// Save the *new source* as a revision // Save the *new source* as a revision
// This results in 2 copies of the current source, but this is ok // This results in 2 copies of the current source, but this is ok
// since any time someone changes something, it create a new // since any time someone changes something, it creates a new revision
// revision
// Note that we can't save the old source here because we'd have no // Note that we can't save the old source here because we'd have no
// clue who edited it since $pageinfo has already been updated by // clue who edited it since $pageinfo has already been updated by
// this point // this point
@ -3305,7 +3407,7 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
$ridFilename = "$pageinfo->filename.r$nextRid"; $ridFilename = "$pageinfo->filename.r$nextRid";
// Insert a new entry into the history // Insert a new entry into the history
$pageinfo->history[] = [ $pageinfo->history[] = [
"type" => "edit", // We might want to store other types later (e.g. page moves) "type" => $change_type, // We might want to store other types later (e.g. page moves)
"rid" => $nextRid, "rid" => $nextRid,
"timestamp" => time(), "timestamp" => time(),
"filename" => $ridFilename, "filename" => $ridFilename,
@ -3315,11 +3417,13 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
]; ];
// Save the new source as a revision // Save the new source as a revision
file_put_contents("$env->storage_prefix$ridFilename", $newsource); $result = file_put_contents("$env->storage_prefix$ridFilename", $newsource);
// Save the edited pageindex // Save the edited pageindex
if($save_pageindex) if($result !== false && $save_pageindex)
file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT)); $result = file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
return $result;
} }
@ -3619,7 +3723,20 @@ register_module([
"code" => function() { "code" => function() {
global $settings; global $settings;
register_save_preprocessor(function(&$index_entry, &$pagedata) { register_save_preprocessor("update_redirect_metadata");
// Register a help section
add_help_section("25-redirect", "Redirect Pages", "<p>$settings->sitename supports redirect pages. To create a redirect page, enter something like <code># REDIRECT [[pagename]]</code> on the first line of the redirect page's content. This <em>must</em> appear as the first line of the page, with no whitespace before it. You can include content beneath the redirect if you want, too (such as a reason for redirecting the page).</p>");
}
]);
/**
* Updates the metadata associated with redirects in the pageindex entry
* specified utilising the provided page content.
* @param object $index_entry The page index entry object to update.
* @param string $pagedata The page content to operate on.
*/
function update_redirect_metadata(&$index_entry, &$pagedata) {
$matches = []; $matches = [];
if(preg_match("/^# ?REDIRECT ?\[\[([^\]]+)\]\]/i", $pagedata, $matches) === 1) if(preg_match("/^# ?REDIRECT ?\[\[([^\]]+)\]\]/i", $pagedata, $matches) === 1)
{ {
@ -3637,12 +3754,7 @@ register_module([
if(isset($index_entry->redirect_target)) if(isset($index_entry->redirect_target))
unset($index_entry->redirect_target); unset($index_entry->redirect_target);
} }
}); }
// Register a help section
add_help_section("25-redirect", "Redirect Pages", "<p>$settings->sitename supports redirect pages. To create a redirect page, enter something like <code># REDIRECT [[pagename]]</code> on the first line of the redirect page's content. This <em>must</em> appear as the first line of the page, with no whitespace before it. You can include content beneath the redirect if you want, too (such as a reason for redirecting the page).</p>");
}
]);

View file

@ -111,15 +111,15 @@ if($env->is_logged_in)
/** /**
* @apiDefine Admin Only the wiki administrator may use this call. * @apiDefine Admin Only the wiki administrator may use this call.
*/ */
/** /**
* @apiDefine Moderator Only users loggged with a moderator account may use this call. * @apiDefine Moderator Only users loggged with a moderator account may use this call.
*/ */
/** /**
* @apiDefine User Only users loggged in may use this call. * @apiDefine User Only users loggged in may use this call.
*/ */
/** /**
* @apiDefine Anonymous Anybody may use this call. * @apiDefine Anonymous Anybody may use this call.
*/ */
/** /**
* @apiDefine UserNotLoggedInError * @apiDefine UserNotLoggedInError
* @apiError UserNotLoggedInError You didn't log in before sending this request. * @apiError UserNotLoggedInError You didn't log in before sending this request.

View file

@ -39,9 +39,9 @@
"name": "API status", "name": "API status",
"version": "0.1", "version": "0.1",
"author": "Starbeamrainbowlabs", "author": "Starbeamrainbowlabs",
"description": "Provides a basic JSON status action that provices a few useful bits of information for API consumption.", "description": "Provides a basic JSON status action that provides a few useful bits of information for API consumption.",
"id": "api-status", "id": "api-status",
"lastupdate": 1523055259, "lastupdate": 1530565418,
"optional": false "optional": false
}, },
{ {
@ -77,7 +77,7 @@
"author": "Starbeamrainbowlabs", "author": "Starbeamrainbowlabs",
"description": "Adds the ability to keep unlimited page history, limited only by your disk space. Note that this doesn't store file history (yet). Currently depends on feature-recent-changes for rendering of the history page.", "description": "Adds the ability to keep unlimited page history, limited only by your disk space. Note that this doesn't store file history (yet). Currently depends on feature-recent-changes for rendering of the history page.",
"id": "feature-history", "id": "feature-history",
"lastupdate": 1518645062, "lastupdate": 1530571420,
"optional": false "optional": false
}, },
{ {
@ -95,7 +95,7 @@
"author": "Starbeamrainbowlabs", "author": "Starbeamrainbowlabs",
"description": "Adds support for redirect pages. Uses the same syntax that Mediawiki does.", "description": "Adds support for redirect pages. Uses the same syntax that Mediawiki does.",
"id": "feature-redirect", "id": "feature-redirect",
"lastupdate": 1498646092, "lastupdate": 1530570852,
"optional": false "optional": false
}, },
{ {

View file

@ -16,7 +16,13 @@ register_module([
* @apiParam {boolean} Whether or not the result should be minified JSON. Default: false * @apiParam {boolean} Whether or not the result should be minified JSON. Default: false
*/ */
/*
* ███████ ████████ █████ ████████ ██ ██ ███████
* ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ██ ██ ███████
* ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ██ ██████ ███████
*/
add_action("status", function() { add_action("status", function() {
global $version, $env, $settings, $actions; global $version, $env, $settings, $actions;

View file

@ -12,7 +12,7 @@ register_module([
* @apiGroup Page * @apiGroup Page
* @apiPermission Anonymous * @apiPermission Anonymous
* *
* @apiParam {string} page The page name to return a revision list for. * @apiUse PageParameter
* @apiParam {string} format The format to return the list of pages in. available values: html, json, text. Default: html * @apiParam {string} format The format to return the list of pages in. available values: html, json, text. Default: html
*/ */
@ -37,8 +37,8 @@ register_module([
$content .= "\t\t<ul class='page-list'>\n"; $content .= "\t\t<ul class='page-list'>\n";
foreach(array_reverse($pageindex->{$env->page}->history) as $revisionData) foreach(array_reverse($pageindex->{$env->page}->history) as $revisionData)
{ {
// Only display edits for now // Only display edits & reverts for now
if($revisionData->type != "edit") if($revisionData->type != "edit" || $revisionData->type != "revert")
continue; continue;
// The number (and the sign) of the size difference to display // The number (and the sign) of the size difference to display
@ -48,7 +48,11 @@ register_module([
$size_display_class .= " significant"; $size_display_class .= " significant";
$size_title_display = human_filesize($revisionData->newsize - $revisionData->sizediff) . " -> " . human_filesize($revisionData->newsize); $size_title_display = human_filesize($revisionData->newsize - $revisionData->sizediff) . " -> " . human_filesize($revisionData->newsize);
$content .= "<li><a href='?page=" . rawurlencode($env->page) . "&revision=$revisionData->rid'>#$revisionData->rid</a> " . render_editor(page_renderer::render_username($revisionData->editor)) . " " . render_timestamp($revisionData->timestamp) . " <span class='cursor-query $size_display_class' title='$size_title_display'>($size_display)</span>"; $content .= "<li>";
$content .= "<a href='?page=" . rawurlencode($env->page) . "&revision=$revisionData->rid'>#$revisionData->rid</a> " . render_editor(page_renderer::render_username($revisionData->editor)) . " " . render_timestamp($revisionData->timestamp) . " <span class='cursor-query $size_display_class' title='$size_title_display'>($size_display)</span>";
if($env->is_logged_in || ($settings->history_revert_require_moderator && $env->is_admin && $env->is_logged_in))
$content .= " <small>(<a class='revert-button' href='?action=history-revert&page=" . rawurlencode($env->page) . "&revision=$revisionData->rid'>restore this revision</a>)</small>";
$content .= "</li>";
} }
} }
else else
@ -83,12 +87,105 @@ register_module([
}); });
/**
* @api {get} ?action=history-revert&page={pageName}&revision={rid} Revert a page to a previous version
* @apiName HistoryRevert
* @apiGroup Editing
* @apiPermission User
* @apiUse PageParameter
* @apiUse UserNotLoggedInError
* @apiUse UserNotModeratorError
*
* @apiParam {string} revision The page revision number to revert to.
*/
/*
* ██ ██ ██ ███████ ████████ ██████ ██████ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ██ ██ ██████ ████ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ███████ ██ ██████ ██ ██ ██
*
* ██████ ███████ ██ ██ ███████ ██████ ████████
* ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ █████ ██ ██ █████ ██████ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ███████ ████ ███████ ██ ██ ██
*/
add_action("history-revert", function() {
global $env, $settings, $pageindex;
if((!$env->is_admin && $settings->history_revert_require_moderator) ||
$env->is_logged_in) {
http_response_code(401);
exit(page_renderer::render_main("Unauthorised - $settings->sitename", "<p>You can't revert pages to a previous revision because " . ($settings->history_revert_require_moderator && $env->is_logged_in ? "you aren't logged in as a moderator. You can try <a href='?action=logout'>logging out</a> and then" : "you aren't logged in. You can try") . " <a href='?action=login&returnto=" . rawurlencode("?action=history-revert&revision={$env->history->revision_number}&page=" . rawurlencode($env->page)) . "logging in</a>."));
}
$current_revision_filepath = "$env->storage_prefix/{$pageindex->{$env->page}->filename}";
// Figure out what we're saving
$newsource = file_get_contents($env->page_filename); // The old revision content - the Pepperminty Wiki core sorts this out for us
$oldsource = file_get_contents($current_revision_filepath); // The current revision's content
// Save the old content over the current content
file_put_contents($current_revision_filepath, $newsource);
// NOTE: We don't run the save preprocessors here because they are run when a page is edited - reversion is special and requires different treatment.
// FUTURE: We may want ot refactor the save preprocessor system ot take a single object instead - then we can add as many params as we like and we could execute the save preprocessors as normal :P
// Add the old content as a new revision
$result = history_add_revision(
$pageindex->{$env->page},
$newsource,
$oldsource,
true, // Yep, go ahead and save the page index
"revert" // It's a revert, not an edit
);
// Update the redirect metadata, if the redirect module is installed
if(module_exists("feature-redirect"))
update_redirect_metadata($pageindex->{$env->page}, $newsource);
// Add an entry to the recent changes log, if the module exists
if($result !== false && module_exists("feature-recent-changes"))
add_recent_change([
"type" => "revert",
"timestamp" => time(),
"page" => $env->page,
"user" => $env->user,
"newsize" => strlen($newsource),
"sizediff" => strlen($newsource) - strlen($oldsource)
]);
if($result === false) {
http_response_code(503);
exit(page_renderer::render_main("Server Error - Revert - $settings->sitename", "<p>A server error occurred when $settings->sitename tried to save the reversion of <code>" . htmlentities($env->page) . "</code>. Please contact $settings->sitename's administrator $settings->admindetails_name, whose email address can be found at the bottom of every page (including this one).</p>"));
}
http_response_code(201);
exit(page_renderer::render_main("Reverting " . htmlentities($env->page) . " - $settings->sitename", "<p>" . htmlentities($env->page) . " has been reverted back to revision {$env->history->revision_number} successfully.</p>
<p><a href='?page=" . rawurlencode($env->page) . "'>Go back</a> to the page, or continue <a href='?action=history&page = " . rawurlencode($env->page) . "'>reviewing its history</a>.</p>"));
// $env->page_filename
//
});
register_save_preprocessor("history_add_revision"); register_save_preprocessor("history_add_revision");
} }
]); ]);
function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true) { /**
* Adds a history revision against a page.
* Note: Does not updaate the current page content! This function _only_
* records a new revision against a page name. Thus it is possible to have a
* disparaty between the history revisions and the actual content displayed in
* the current revision if you're not careful!
* @param object $pageinfo The pageindex object of the page to operate on.
* @param string $newsource The page content to save as the new revision.
* @param string $oldsource The old page content that is the current revision (before the update).
* @param boolean $save_pageindex Whether the page index should be saved to disk.
* @param string $change_type The type of change to record this as in the history revision log
*/
function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true, $change_type = "edit") {
global $pageindex, $paths, $env; global $pageindex, $paths, $env;
if(!isset($pageinfo->history)) if(!isset($pageinfo->history))
@ -96,8 +193,7 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
// Save the *new source* as a revision // Save the *new source* as a revision
// This results in 2 copies of the current source, but this is ok // This results in 2 copies of the current source, but this is ok
// since any time someone changes something, it create a new // since any time someone changes something, it creates a new revision
// revision
// Note that we can't save the old source here because we'd have no // Note that we can't save the old source here because we'd have no
// clue who edited it since $pageinfo has already been updated by // clue who edited it since $pageinfo has already been updated by
// this point // this point
@ -107,7 +203,7 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
$ridFilename = "$pageinfo->filename.r$nextRid"; $ridFilename = "$pageinfo->filename.r$nextRid";
// Insert a new entry into the history // Insert a new entry into the history
$pageinfo->history[] = [ $pageinfo->history[] = [
"type" => "edit", // We might want to store other types later (e.g. page moves) "type" => $change_type, // We might want to store other types later (e.g. page moves)
"rid" => $nextRid, "rid" => $nextRid,
"timestamp" => time(), "timestamp" => time(),
"filename" => $ridFilename, "filename" => $ridFilename,
@ -117,11 +213,13 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
]; ];
// Save the new source as a revision // Save the new source as a revision
file_put_contents("$env->storage_prefix$ridFilename", $newsource); $result = file_put_contents("$env->storage_prefix$ridFilename", $newsource);
// Save the edited pageindex // Save the edited pageindex
if($save_pageindex) if($result !== false && $save_pageindex)
file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT)); $result = file_put_contents($paths->pageindex, json_encode($pageindex, JSON_PRETTY_PRINT));
return $result;
} }
?> ?>

View file

@ -8,7 +8,20 @@ register_module([
"code" => function() { "code" => function() {
global $settings; global $settings;
register_save_preprocessor(function(&$index_entry, &$pagedata) { register_save_preprocessor("update_redirect_metadata");
// Register a help section
add_help_section("25-redirect", "Redirect Pages", "<p>$settings->sitename supports redirect pages. To create a redirect page, enter something like <code># REDIRECT [[pagename]]</code> on the first line of the redirect page's content. This <em>must</em> appear as the first line of the page, with no whitespace before it. You can include content beneath the redirect if you want, too (such as a reason for redirecting the page).</p>");
}
]);
/**
* Updates the metadata associated with redirects in the pageindex entry
* specified utilising the provided page content.
* @param object $index_entry The page index entry object to update.
* @param string $pagedata The page content to operate on.
*/
function update_redirect_metadata(&$index_entry, &$pagedata) {
$matches = []; $matches = [];
if(preg_match("/^# ?REDIRECT ?\[\[([^\]]+)\]\]/i", $pagedata, $matches) === 1) if(preg_match("/^# ?REDIRECT ?\[\[([^\]]+)\]\]/i", $pagedata, $matches) === 1)
{ {
@ -26,11 +39,6 @@ register_module([
if(isset($index_entry->redirect_target)) if(isset($index_entry->redirect_target))
unset($index_entry->redirect_target); unset($index_entry->redirect_target);
} }
}); }
// Register a help section
add_help_section("25-redirect", "Redirect Pages", "<p>$settings->sitename supports redirect pages. To create a redirect page, enter something like <code># REDIRECT [[pagename]]</code> on the first line of the redirect page's content. This <em>must</em> appear as the first line of the page, with no whitespace before it. You can include content beneath the redirect if you want, too (such as a reason for redirecting the page).</p>");
}
]);
?> ?>