"Page History", "version" => "0.4", "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.", "id" => "feature-history", "code" => function() { /** * @api {get} ?action=history&page={pageName}[&format={format}] Get a list of revisions for a page * @apiName History * @apiGroup Page * @apiPermission Anonymous * * @apiUse PageParameter * @apiParam {string} format The format to return the list of pages in. available values: html, json, text. Default: html */ /* * ██ ██ ██ ███████ ████████ ██████ ██████ ██ ██ * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ * ███████ ██ ███████ ██ ██ ██ ██████ ████ * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ * ██ ██ ██ ███████ ██ ██████ ██ ██ ██ */ add_action("history", function() { global $settings, $env, $pageindex; $supported_formats = [ "html", "json", "text" ]; $format = $_GET["format"] ?? "html"; switch($format) { case "html": $content = "
(None yet! Try editing this page and then coming back here.)
\n"; } exit(page_renderer::render_main("$env->page - History - $settings->sitename", $content)); case "json": $page_history = $pageindex->{$env->page}->history ?? []; foreach($page_history as &$history_entry) { unset($history_entry->filename); } header("content-type: application/json"); exit(json_encode($page_history, JSON_PRETTY_PRINT)); case "csv": $page_history = $pageindex->{$env->page}->history ?? []; header("content-type: text/csv"); echo("revision_id,timestamp,type,editor,newsize,sizediff\n"); foreach($page_history as $hentry) { echo("$hentry->rid,$hentry->timestamp,$hentry->type,$hentry->editor,$hentry->newsize,$hentry->sizediff\n"); } exit(); default: http_response_code(400); exit(page_renderer::render_main("Format Error - $env->page - History - $settings->sitename", "The format " . htmlentities($format) . "
isn't currently supported. Supported formats: html, json, csv"));
}
});
/**
* @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", "
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 logging out and then" : "you aren't logged in. You can try") . " 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", " A server error occurred when $settings->sitename tried to save the reversion of " . htmlentities($env->page) . " has been reverted back to revision {$env->history->revision_number} successfully. Go back to the page, or continue reviewing its history." . htmlentities($env->page) . "
. Please contact $settings->sitename's administrator $settings->admindetails_name, whose email address can be found at the bottom of every page (including this one).