2016-01-16 13:24:13 +00:00
< ? php
register_module ([
" name " => " Recent Changes " ,
2016-04-03 15:10:37 +00:00
" version " => " 0.3.2 " ,
2016-01-16 13:24:13 +00:00
" author " => " Starbeamrainbowlabs " ,
" description " => " Adds recent changes. Access through the 'recent-changes' action. " ,
" id " => " feature-recent-changes " ,
" code " => function () {
global $settings , $env , $paths ;
2016-06-12 20:15:43 +00:00
/**
* @ api { get } ? action = recentchanges Get a list of recent changes
* @ apiName RecentChanges
* @ apiGroup Stats
* @ apiPermission Anonymous
*/
2016-01-16 13:24:13 +00:00
// 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 () {
2016-03-21 16:21:09 +00:00
global $settings , $paths , $pageindex ;
2016-01-16 13:24:13 +00:00
2016-01-16 13:51:25 +00:00
$content = " \t \t <h1>Recent Changes</h1> \n " ;
2016-01-16 13:24:13 +00:00
2016-04-03 14:58:49 +00:00
$recent_changes = json_decode ( file_get_contents ( $paths -> recentchanges ));
2016-01-16 13:51:25 +00:00
2016-04-03 14:58:49 +00:00
if ( count ( $recent_changes ) > 0 )
2016-01-16 13:24:13 +00:00
{
2016-04-03 14:58:49 +00:00
$content .= render_recent_changes ( $recent_changes );
2016-01-16 13:51:25 +00:00
}
else
{
// No changes yet :(
$content .= " <p><em>None yet! Try making a few changes and then check back here.</em></p> \n " ;
2016-01-16 13:24:13 +00:00
}
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 ;
2016-04-03 14:27:50 +00:00
$newchange = [
" type " => " edit " ,
2016-01-16 13:24:13 +00:00
" timestamp " => time (),
" page " => $env -> page ,
" user " => $env -> user ,
" newsize " => $newsize ,
" sizediff " => $size_diff
2016-04-03 14:27:50 +00:00
];
if ( $oldsize == 0 )
$newchange [ " newpage " ] = true ;
2016-01-16 13:24:13 +00:00
2016-04-03 14:31:50 +00:00
add_recent_change ( $newchange );
2016-01-16 13:24:13 +00:00
});
2016-03-21 16:21:09 +00:00
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 > " );
2016-01-16 13:24:13 +00:00
}
]);
2016-04-03 14:31:50 +00:00
/**
* 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 ;
2016-04-03 14:43:40 +00:00
2016-04-03 14:31:50 +00:00
$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 ));
}
2016-04-03 19:36:01 +00:00
function render_recent_changes ( $recent_changes )
2016-04-03 14:58:49 +00:00
{
2016-04-04 12:55:43 +00:00
global $pageindex ;
2016-04-03 19:36:01 +00:00
// 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 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 )
{
// 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 ++ ;
}
}
}
2016-04-03 14:58:49 +00:00
$content = " <ul class='page-list'> \n " ;
2016-04-03 17:11:34 +00:00
$last_time = 0 ;
2016-04-03 19:36:01 +00:00
for ( $i = 0 ; $i < $rchange_count ; $i ++ )
2016-04-03 14:58:49 +00:00
{
2016-04-03 19:36:01 +00:00
$rchange = $recent_changes [ $i ];
2016-04-03 17:11:34 +00:00
if ( $last_time !== date ( " dmY " , $rchange -> timestamp ))
2016-04-04 12:55:43 +00:00
$content .= " <li class='header'><h2> " . date ( " jS F " , $rchange -> timestamp ) . " </h2></li> \n " ;
2016-04-03 17:11:34 +00:00
2016-04-04 12:55:43 +00:00
$rchange_results = [];
for ( $s = $i ; $s < $rchange_count ; $s ++ )
{
if ( $recent_changes [ $s ] -> page !== $rchange -> page )
break ;
$rchange_results [ $s ] = render_recent_change ( $recent_changes [ $s ]);
$i ++ ;
}
//$content .= render_recent_change($rchange);
$next_entry = implode ( " \n " , $rchange_results );
if ( count ( $rchange_results ) > 1 )
{
reset ( $rchange_results );
$rchange_first = $recent_changes [ key ( $rchange_results )];
end ( $rchange_results );
$rchange_last = $recent_changes [ key ( $rchange_results )];
2016-06-08 16:41:30 +00:00
$pageDisplayHtml = render_pagename ( $rchange_first );
$timeDisplayHtml = render_timestamp ( $rchange_first -> timestamp );
2016-04-04 12:55:43 +00:00
$users = [];
foreach ( $rchange_results as $key => $rchange_result )
{
if ( ! in_array ( $recent_changes [ $key ] -> user , $users ))
$users [] = $recent_changes [ $key ] -> user ;
}
2016-06-08 16:41:30 +00:00
$userDisplayHtml = render_editor ( implode ( " , " , $users ));
2016-04-04 12:55:43 +00:00
// TODO: COllect up and render a list of participating users
$next_entry = " <li><details><summary> $pageDisplayHtml $userDisplayHtml $timeDisplayHtml </summary><ul class='page-list'> $next_entry </ul></details></li> " ;
$content .= " $next_entry\n " ;
}
2016-04-06 14:24:49 +00:00
else
{
$content .= implode ( " \n " , $rchange_results );
}
2016-04-03 17:11:34 +00:00
$last_time = date ( " dmY " , $rchange -> timestamp );
2016-04-03 14:58:49 +00:00
}
$content .= " \t \t </ul> " ;
return $content ;
}
function render_recent_change ( $rchange )
{
2016-06-08 16:41:30 +00:00
$pageDisplayHtml = render_pagename ( $rchange );
$editorDisplayHtml = render_editor ( $rchange -> user );
$timeDisplayHtml = render_timestamp ( $rchange -> timestamp );
2016-04-03 14:58:49 +00:00
$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 " ;
2016-04-04 12:55:43 +00:00
$size_title_display = human_filesize ( $rchange -> newsize - $rchange -> sizediff ) . " -> " . human_filesize ( $rchange -> newsize );
2016-04-03 14:58:49 +00:00
if ( ! empty ( $rchange -> newpage ))
$resultClasses [] = " newpage " ;
2016-04-06 15:20:53 +00:00
$result .= " <a href='?page= $rchange->page '> $pageDisplayHtml </a> $editorDisplayHtml $timeDisplayHtml <span class=' $size_display_class ' title=' $size_title_display '>( $size_display )</span> " ;
2016-04-03 14:58:49 +00:00
break ;
case " deletion " :
$resultClasses [] = " deletion " ;
2016-04-04 12:55:43 +00:00
$result .= " $pageDisplayHtml $editorDisplayHtml $timeDisplayHtml " ;
2016-04-03 15:10:37 +00:00
break ;
case " upload " :
$resultClasses [] = " upload " ;
2016-04-06 15:20:53 +00:00
$result .= " <a href='?page= $rchange->page '> $pageDisplayHtml </a> $editorDisplayHtml $timeDisplayHtml ( " . human_filesize ( $rchange -> filesize ) . " ) " ;
2016-04-03 15:10:37 +00:00
break ;
2016-04-03 14:58:49 +00:00
}
$resultAttributes = " " . ( count ( $resultClasses ) > 0 ? " class=' " . implode ( " " , $resultClasses ) . " ' " : " " );
$result = " \t \t \t <li $resultAttributes > $result </li> \n " ;
return $result ;
}
2016-01-16 13:24:13 +00:00
?>