mirror of
https://github.com/sbrl/Pepperminty-Wiki.git
synced 2024-12-27 03:14:56 +00:00
157 lines
6.3 KiB
PHP
157 lines
6.3 KiB
PHP
<?php
|
|
register_module([
|
|
"name" => "Recent Changes",
|
|
"version" => "0.3.2",
|
|
"author" => "Starbeamrainbowlabs",
|
|
"description" => "Adds recent changes. Access through the 'recent-changes' action.",
|
|
"id" => "feature-recent-changes",
|
|
"code" => function() {
|
|
global $settings, $env, $paths;
|
|
// Add the recent changes json file to $paths for convenience.
|
|
$paths->recentchanges = $env->storage_prefix . "recent-changes.json";
|
|
// Create the recent changes json file if it doesn't exist
|
|
if(!file_exists($paths->recentchanges))
|
|
file_put_contents($paths->recentchanges, "[]");
|
|
|
|
/*
|
|
* ██████ ███████ ██████ ███████ ███ ██ ████████
|
|
* ██ ██ ██ ██ ██ ████ ██ ██
|
|
* ██████ █████ ██ █████ ██ ██ ██ ██
|
|
* ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
* ██ ██ ███████ ██████ ███████ ██ ████ ██
|
|
*
|
|
* ██████ ██ ██ █████ ███ ██ ██████ ███████ ███████
|
|
* ██ ██ ██ ██ ██ ████ ██ ██ ██ ██
|
|
* ██ ███████ ███████ ██ ██ ██ ██ ███ █████ ███████
|
|
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
* ██████ ██ ██ ██ ██ ██ ████ ██████ ███████ ███████
|
|
*/
|
|
add_action("recent-changes", function() {
|
|
global $settings, $paths, $pageindex;
|
|
|
|
$content = "\t\t<h1>Recent Changes</h1>\n";
|
|
|
|
$recent_changes = json_decode(file_get_contents($paths->recentchanges));
|
|
|
|
if(count($recent_changes) > 0)
|
|
{
|
|
$content .= render_recent_changes($recent_changes);
|
|
}
|
|
else
|
|
{
|
|
// No changes yet :(
|
|
$content .= "<p><em>None yet! Try making a few changes and then check back here.</em></p>\n";
|
|
}
|
|
|
|
echo(page_renderer::render("Recent Changes - $settings->sitename", $content));
|
|
});
|
|
|
|
register_save_preprocessor(function(&$pageinfo, &$newsource, &$oldsource) {
|
|
global $env, $settings, $paths;
|
|
|
|
// Work out the old and new page lengths
|
|
$oldsize = strlen($oldsource);
|
|
$newsize = strlen($newsource);
|
|
// Calculate the page length difference
|
|
$size_diff = $newsize - $oldsize;
|
|
|
|
$newchange = [
|
|
"type" => "edit",
|
|
"timestamp" => time(),
|
|
"page" => $env->page,
|
|
"user" => $env->user,
|
|
"newsize" => $newsize,
|
|
"sizediff" => $size_diff
|
|
];
|
|
if($oldsize == 0)
|
|
$newchange["newpage"] = true;
|
|
|
|
add_recent_change($newchange);
|
|
});
|
|
|
|
add_help_section("800-raw-page-content", "Recent Changes", "<p>The <a href='?action=recent-changes'>recent changes</a> page displays a list of all the most recent changes that have happened around $settings->sitename, arranged in chronological order. It can be found in the \"More...\" menu in the top right by default.</p>
|
|
<p>Each entry displays the name of the page in question, who edited it, how long ago they did so, and the number of characters added or removed. Pages that <em>currently</em> redirect to another page are shown in italics, and hovering over the time since the edit wil show the exact time that the edit was made.</p>");
|
|
}
|
|
]);
|
|
|
|
/**
|
|
* Adds a new recent change to the recent changes file.
|
|
* @param array $rchange The new change to add.
|
|
*/
|
|
function add_recent_change($rchange)
|
|
{
|
|
global $settings, $paths;
|
|
|
|
$recentchanges = json_decode(file_get_contents($paths->recentchanges), true);
|
|
array_unshift($recentchanges, $rchange);
|
|
|
|
// Limit the number of entries in the recent changes file if we've
|
|
// been asked to.
|
|
if(isset($settings->max_recent_changes))
|
|
$recentchanges = array_slice($recentchanges, -$settings->max_recent_changes);
|
|
|
|
// Save the recent changes file back to disk
|
|
file_put_contents($paths->recentchanges, json_encode($recentchanges, JSON_PRETTY_PRINT));
|
|
}
|
|
|
|
function render_recent_changes($recentchanges)
|
|
{
|
|
$content = "<ul class='page-list'>\n";
|
|
foreach($recentchanges as $rchange)
|
|
{
|
|
$content .= render_recent_change($rchange);
|
|
}
|
|
$content .= "\t\t</ul>";
|
|
|
|
return $content;
|
|
}
|
|
|
|
function render_recent_change($rchange)
|
|
{
|
|
// Render the page's name
|
|
$pageDisplayName = $rchange->page;
|
|
if(isset($pageindex->$pageDisplayName) and !empty($pageindex->$pageDisplayName->redirect))
|
|
$pageDisplayName = "<em>$pageDisplayName</em>";
|
|
$pageDisplayLink = "<a href='?page=" . rawurlencode($rchange->page) . "'>$pageDisplayName</a>";
|
|
|
|
$editorDisplayHtml = "<span class='editor'>✎ $rchange->user</span>";
|
|
$timeDisplayHtml = "<time class='cursor-query' title='" . date("l jS \of F Y \a\\t h:ia T", $rchange->timestamp) . "'>" . human_time_since($rchange->timestamp) . "</time>";
|
|
|
|
$result = "";
|
|
$resultClasses = [];
|
|
switch(isset($rchange->type) ? $rchange->type : "edit")
|
|
{
|
|
case "edit":
|
|
// The number (and the sign) of the size difference to display
|
|
$size_display = ($rchange->sizediff > 0 ? "+" : "") . $rchange->sizediff;
|
|
$size_display_class = $rchange->sizediff > 0 ? "larger" : ($rchange->sizediff < 0 ? "smaller" : "nochange");
|
|
if($rchange->sizediff > 500 or $rchange->sizediff < -500)
|
|
$size_display_class .= " significant";
|
|
|
|
|
|
$title_display = human_filesize($rchange->newsize - $rchange->sizediff) . " -> " . human_filesize($rchange->newsize);
|
|
|
|
if(!empty($rchange->newpage))
|
|
$resultClasses[] = "newpage";
|
|
|
|
$result .= "$pageDisplayLink $editorDisplayHtml $timeDisplayHtml <span class='$size_display_class' title='$title_display'>($size_display)</span>";
|
|
break;
|
|
|
|
case "deletion":
|
|
$resultClasses[] = "deletion";
|
|
$result .= "$pageDisplayName $editorDisplayHtml $timeDisplayHtml";
|
|
break;
|
|
|
|
case "upload":
|
|
$resultClasses[] = "upload";
|
|
$result .= "$pageDisplayLink $editorDisplayHtml $timeDisplayHtml (" . human_filesize($rchange->filesize) . ")";
|
|
break;
|
|
}
|
|
|
|
$resultAttributes = " " . (count($resultClasses) > 0 ? "class='" . implode(" ", $resultClasses) . "'" : "");
|
|
$result = "\t\t\t<li$resultAttributes>$result</li>\n";
|
|
|
|
return $result;
|
|
}
|
|
|
|
?>
|