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

Implement history_max_revisions setting

This commit is contained in:
Starbeamrainbowlabs 2018-07-03 12:15:16 +01:00
parent 819489a7fb
commit b31301be38
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 28 additions and 6 deletions

View file

@ -404,7 +404,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 = "6e7698b4a9210cd3198ee1bd2a117e7bcf162819"; $commit = "819489a7fb17f8e57fe6af7e01c34e9260d4e9ea";
/// Environment /// /// Environment ///
/** Holds information about the current request environment. */ /** Holds information about the current request environment. */
$env = new stdClass(); $env = new stdClass();
@ -3426,7 +3426,7 @@ register_module([
* @param string $change_type The type of change to record this as in the history revision log * @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") { function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true, $change_type = "edit") {
global $pageindex, $paths, $env; global $env, $paths, $settings, $pageindex;
if(!isset($pageinfo->history)) if(!isset($pageinfo->history))
$pageinfo->history = []; $pageinfo->history = [];
@ -3439,7 +3439,8 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
// this point // this point
// TODO Store tag changes here // TODO Store tag changes here
$nextRid = count($pageinfo->history); // The next revision id end($pageinfo->history); // Calculate the next revision id - we can't jsut count the reivisions here because we might have a revision limit
$nextRid = $pageinfo->history[key($pageinfo->history)]->rid + 1;
$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[] = [
@ -3455,10 +3456,20 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
// Save the new source as a revision // Save the new source as a revision
$result = file_put_contents("$env->storage_prefix$ridFilename", $newsource); $result = file_put_contents("$env->storage_prefix$ridFilename", $newsource);
if($result !== false &&
$settings->history_max_revisions > -1) {
while(count($pageinfo->history) > $settings->history_max_revisions) {
// We've got too many revisions - trim one off & delete it
$oldest_revision = array_shift($pageinfo->history);
unlink("$env->storage_prefix/$oldest_revision->filename");
}
}
// Save the edited pageindex // Save the edited pageindex
if($result !== false && $save_pageindex) if($result !== false && $save_pageindex)
$result = 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; return $result;
} }

View file

@ -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": 1530615427, "lastupdate": 1530616470,
"optional": false "optional": false
}, },
{ {

View file

@ -220,7 +220,7 @@ register_module([
* @param string $change_type The type of change to record this as in the history revision log * @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") { function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pageindex = true, $change_type = "edit") {
global $pageindex, $paths, $env; global $env, $paths, $settings, $pageindex;
if(!isset($pageinfo->history)) if(!isset($pageinfo->history))
$pageinfo->history = []; $pageinfo->history = [];
@ -233,7 +233,8 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
// this point // this point
// TODO Store tag changes here // TODO Store tag changes here
$nextRid = count($pageinfo->history); // The next revision id end($pageinfo->history); // Calculate the next revision id - we can't jsut count the reivisions here because we might have a revision limit
$nextRid = $pageinfo->history[key($pageinfo->history)]->rid + 1;
$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[] = [
@ -249,10 +250,20 @@ function history_add_revision(&$pageinfo, &$newsource, &$oldsource, $save_pagein
// Save the new source as a revision // Save the new source as a revision
$result = file_put_contents("$env->storage_prefix$ridFilename", $newsource); $result = file_put_contents("$env->storage_prefix$ridFilename", $newsource);
if($result !== false &&
$settings->history_max_revisions > -1) {
while(count($pageinfo->history) > $settings->history_max_revisions) {
// We've got too many revisions - trim one off & delete it
$oldest_revision = array_shift($pageinfo->history);
unlink("$env->storage_prefix/$oldest_revision->filename");
}
}
// Save the edited pageindex // Save the edited pageindex
if($result !== false && $save_pageindex) if($result !== false && $save_pageindex)
$result = 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; return $result;
} }