"Recent Changes", "version" => "0.3.5", "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, "[]"); /** * @api {get} ?action=recent-changes[&offset={number}][&count={number}][&format={code}] Get a list of recent changes * @apiName RecentChanges * @apiGroup Stats * @apiPermission Anonymous * * @apiParam {number} offset If specified, start returning changes from this many changes in. 0 is the beginning. * @apiParam {number} count If specified, return at most this many changes. A value of 0 means no limit (the default) - apart from the limit on the number of changes stored by the server (configurable in pepppermint.json). * @apiParam {string} format The format to return the recent changes in. Valid values: html, json, csv. Default: html. */ /* * ██████ ███████ ██████ ███████ ███ ██ ████████ * ██ ██ ██ ██ ██ ████ ██ ██ * ██████ █████ ██ █████ ██ ██ ██ ██ * ██ ██ ██ ██ ██ ██ ██ ██ ██ * ██ ██ ███████ ██████ ███████ ██ ████ ██ * * ██████ ██ ██ █████ ███ ██ ██████ ███████ ███████ * ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ * ██ ███████ ███████ ██ ██ ██ ██ ███ █████ ███████ * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ * ██████ ██ ██ ██ ██ ██ ████ ██████ ███████ ███████ */ add_action("recent-changes", function() { global $settings, $paths, $pageindex; $format = $_GET["format"] ?? "html"; $offset = intval($_GET["offset"] ?? 0); $count = intval($_GET["count"] ?? 0); $recent_changes = json_decode(file_get_contents($paths->recentchanges)); // Limit the number of changes displayed if requested if($count > 0) $recent_changes = array_slice($recent_changes, $offset, $count); switch($format) { case "html": $content = "\t\t
None yet! Try making a few changes and then check back here.
\n"; exit(page_renderer::render("Recent Changes - $settings->sitename", $content)); break; case "json": $result = json_encode($recent_changes); header("content-type: application/json"); header("content-length: " . strlen($result)); exit($result); break; case "csv": if(empty($recent_changes)) { http_response_code(404); header("content-type: text/plain"); exit("No changes made been recorded yet. Make some changes and then come back later!"); } $result = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+'); fputcsv($result, array_keys(get_object_vars($recent_changes[0]))); foreach($recent_changes as $recent_change) fputcsv($result, array_values(get_object_vars($recent_change))); rewind($result); header("content-type: text/csv"); header("content-length: " . fstat($result)["size"]); echo(stream_get_contents($result)); } }); 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", "The recent changes 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.
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 currently 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.
"); } ]); /** * Adds a new recent change to the recent changes file. * @package feature-recent-changes * @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, 0, $settings->max_recent_changes); // Save the recent changes file back to disk file_put_contents($paths->recentchanges, json_encode($recentchanges, JSON_PRETTY_PRINT)); } /** * Renders a list of recent changes to HTML. * @package feature-recent-changes * @param array $recent_changes The recent changes to render. * @return string The given recent changes as HTML. */ function render_recent_changes($recent_changes) { global $pageindex; // Cache the number of recent changes we are dealing with $rchange_count = count($recent_changes); // Group changes made on the same page and the same day together for($i = 0; $i < $rchange_count; $i++) { for($s = $i + 1; $s < $rchange_count; $s++) { // Break out if we have reached the end of the day we are scanning if(date("dmY", $recent_changes[$i]->timestamp) !== date("dmY", $recent_changes[$s]->timestamp)) break; // If we have found a change that has been made on the same page and // on the same day as the one that we are scanning for, move it up // next to the change we are scanning for. if($recent_changes[$i]->page == $recent_changes[$s]->page && date("j", $recent_changes[$i]->timestamp) === date("j", $recent_changes[$s]->timestamp)) { // FUTURE: We may need to remove and insert instead of swapping changes around if this causes some changes to appear out of order. $temp = $recent_changes[$i + 1]; $recent_changes[$i + 1] = $recent_changes[$s]; $recent_changes[$s] = $temp; $i++; } } } $content = "