3 * Implements Special:Recentchanges
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * A special page that lists last changes made to the wiki
27 * @ingroup SpecialPage
29 class SpecialRecentChanges
extends ChangesListSpecialPage
{
30 // @codingStandardsIgnoreStart Needed "useless" override to change parameters.
31 public function __construct( $name = 'Recentchanges', $restriction = '' ) {
32 parent
::__construct( $name, $restriction );
34 // @codingStandardsIgnoreEnd
37 * Main execution point
39 * @param string $subpage
41 public function execute( $subpage ) {
42 // Backwards-compatibility: redirect to new feed URLs
43 $feedFormat = $this->getRequest()->getVal( 'feed' );
44 if ( !$this->including() && $feedFormat ) {
45 $query = $this->getFeedQuery();
46 $query['feedformat'] = $feedFormat === 'atom' ?
'atom' : 'rss';
47 $this->getOutput()->redirect( wfAppendQuery( wfScript( 'api' ), $query ) );
51 // 10 seconds server-side caching max
52 $this->getOutput()->setSquidMaxage( 10 );
53 // Check if the client has a cached version
54 $lastmod = $this->checkLastModified();
55 if ( $lastmod === false ) {
59 parent
::execute( $subpage );
63 * Get a FormOptions object containing the default options
67 public function getDefaultOptions() {
68 $opts = parent
::getDefaultOptions();
69 $user = $this->getUser();
71 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
72 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
73 $opts->add( 'from', '' );
75 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
76 $opts->add( 'hidebots', true );
77 $opts->add( 'hideanons', false );
78 $opts->add( 'hideliu', false );
79 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
80 $opts->add( 'hidemyself', false );
82 $opts->add( 'categories', '' );
83 $opts->add( 'categories_any', false );
84 $opts->add( 'tagfilter', '' );
90 * Get custom show/hide filters
92 * @return array Map of filter URL param names to properties (msg/default)
94 protected function getCustomFilters() {
95 if ( $this->customFilters
=== null ) {
96 $this->customFilters
= array();
97 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters
) );
100 return $this->customFilters
;
104 * Process $par and put options found in $opts. Used when including the page.
107 * @param FormOptions $opts
109 public function parseParameters( $par, FormOptions
$opts ) {
110 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
111 foreach ( $bits as $bit ) {
112 if ( 'hidebots' === $bit ) {
113 $opts['hidebots'] = true;
115 if ( 'bots' === $bit ) {
116 $opts['hidebots'] = false;
118 if ( 'hideminor' === $bit ) {
119 $opts['hideminor'] = true;
121 if ( 'minor' === $bit ) {
122 $opts['hideminor'] = false;
124 if ( 'hideliu' === $bit ) {
125 $opts['hideliu'] = true;
127 if ( 'hidepatrolled' === $bit ) {
128 $opts['hidepatrolled'] = true;
130 if ( 'hideanons' === $bit ) {
131 $opts['hideanons'] = true;
133 if ( 'hidemyself' === $bit ) {
134 $opts['hidemyself'] = true;
137 if ( is_numeric( $bit ) ) {
138 $opts['limit'] = $bit;
142 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
143 $opts['limit'] = $m[1];
145 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
146 $opts['days'] = $m[1];
148 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
149 $opts['namespace'] = $m[1];
154 public function validateOptions( FormOptions
$opts ) {
155 $opts->validateIntBounds( 'limit', 0, 5000 );
156 parent
::validateOptions( $opts );
160 * Return an array of conditions depending of options set in $opts
162 * @param FormOptions $opts
165 public function buildMainQueryConds( FormOptions
$opts ) {
166 $dbr = $this->getDB();
167 $conds = parent
::buildMainQueryConds( $opts );
170 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
171 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
172 $cutoff = $dbr->timestamp( $cutoff_unixtime );
174 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
175 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW
, $cutoff ) ) {
176 $cutoff = $dbr->timestamp( $opts['from'] );
178 $opts->reset( 'from' );
181 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
189 * @param array $conds
190 * @param FormOptions $opts
191 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
193 public function doMainQuery( $conds, $opts ) {
194 global $wgAllowCategorizedRecentChanges;
196 $dbr = $this->getDB();
197 $user = $this->getUser();
199 $tables = array( 'recentchanges' );
200 $fields = RecentChange
::selectFields();
201 $query_options = array();
202 $join_conds = array();
204 // JOIN on watchlist for users
205 if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
206 $tables[] = 'watchlist';
207 $fields[] = 'wl_user';
208 $fields[] = 'wl_notificationtimestamp';
209 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
210 'wl_user' => $user->getId(),
212 'wl_namespace=rc_namespace'
216 if ( $user->isAllowed( 'rollback' ) ) {
218 $fields[] = 'page_latest';
219 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
222 ChangeTags
::modifyDisplayQuery(
231 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
232 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
237 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
238 // knowledge to use an index merge if it wants (it may use some other index though).
239 $rows = $dbr->select(
242 $conds +
array( 'rc_new' => array( 0, 1 ) ),
244 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $opts['limit'] ) +
$query_options,
248 // Build the final data
249 if ( $wgAllowCategorizedRecentChanges ) {
250 $this->filterByCategories( $rows, $opts );
256 public function outputFeedLinks() {
257 $this->addFeedLinks( $this->getFeedQuery() );
261 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
265 private function getFeedQuery() {
267 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
268 // API handles empty parameters in a different way
269 return $value !== '';
271 $query['action'] = 'feedrecentchanges';
272 if ( $query['limit'] > $wgFeedLimit ) {
273 $query['limit'] = $wgFeedLimit;
279 * Build and output the actual changes list.
281 * @param array $rows Database rows
282 * @param FormOptions $opts
284 public function outputChangesList( $rows, $opts ) {
285 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
287 $limit = $opts['limit'];
289 $showWatcherCount = $wgRCShowWatchingUsers
290 && $this->getUser()->getOption( 'shownumberswatching' );
291 $watcherCache = array();
293 $dbr = $this->getDB();
296 $list = ChangesList
::newFromContext( $this->getContext() );
298 $rclistOutput = $list->beginRecentChangesList();
299 foreach ( $rows as $obj ) {
303 $rc = RecentChange
::newFromRow( $obj );
304 $rc->counter
= $counter++
;
305 # Check if the page has been updated since the last visit
306 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp
) ) {
307 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
309 $rc->notificationtimestamp
= false; // Default
311 # Check the number of users watching the page
312 $rc->numberofWatchingusers
= 0; // Default
313 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
314 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
315 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
320 'wl_namespace' => $obj->rc_namespace
,
321 'wl_title' => $obj->rc_title
,
323 __METHOD__
. '-watchers'
326 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
329 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
330 if ( $changeLine !== false ) {
331 $rclistOutput .= $changeLine;
335 $rclistOutput .= $list->endRecentChangesList();
337 if ( $rows->numRows() === 0 ) {
338 $this->getOutput()->addHtml(
339 '<div class="mw-changeslist-empty">' .
340 $this->msg( 'recentchanges-noresult' )->parse() .
344 $this->getOutput()->addHTML( $rclistOutput );
349 * Return the text to be displayed above the changes
351 * @param FormOptions $opts
352 * @return string XHTML
354 public function doHeader( $opts ) {
357 $this->setTopText( $opts );
359 $defaults = $opts->getAllValues();
360 $nondefaults = $opts->getChangedValues();
363 $panel[] = self
::makeLegend( $this->getContext() );
364 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
367 $extraOpts = $this->getExtraOptions( $opts );
368 $extraOptsCount = count( $extraOpts );
370 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
372 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
373 foreach ( $extraOpts as $name => $optionRow ) {
374 # Add submit button to the last row only
376 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
378 $out .= Xml
::openElement( 'tr' );
379 if ( is_array( $optionRow ) ) {
382 array( 'class' => 'mw-label mw-' . $name . '-label' ),
387 array( 'class' => 'mw-input' ),
388 $optionRow[1] . $addSubmit
393 array( 'class' => 'mw-input', 'colspan' => 2 ),
394 $optionRow . $addSubmit
397 $out .= Xml
::closeElement( 'tr' );
399 $out .= Xml
::closeElement( 'table' );
401 $unconsumed = $opts->getUnconsumedValues();
402 foreach ( $unconsumed as $key => $value ) {
403 $out .= Html
::hidden( $key, $value );
406 $t = $this->getPageTitle();
407 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
408 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
410 $panelString = implode( "\n", $panel );
412 $this->getOutput()->addHTML(
414 $this->msg( 'recentchanges-legend' )->text(),
416 array( 'class' => 'rcoptions' )
420 $this->setBottomText( $opts );
424 * Send the text to be displayed above the options
426 * @param FormOptions $opts Unused
428 function setTopText( FormOptions
$opts ) {
431 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
432 if ( !$message->isDisabled() ) {
433 $this->getOutput()->addWikiText(
434 Html
::rawElement( 'p',
435 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
436 "\n" . $message->plain() . "\n"
438 /* $lineStart */ false,
439 /* $interface */ false
445 * Get options to be displayed in a form
447 * @param FormOptions $opts
450 function getExtraOptions( $opts ) {
451 $opts->consumeValues( array(
452 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
455 $extraOpts = array();
456 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
458 global $wgAllowCategorizedRecentChanges;
459 if ( $wgAllowCategorizedRecentChanges ) {
460 $extraOpts['category'] = $this->categoryFilterForm( $opts );
463 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
464 if ( count( $tagFilter ) ) {
465 $extraOpts['tagfilter'] = $tagFilter;
468 // Don't fire the hook for subclasses. (Or should we?)
469 if ( $this->getName() === 'Recentchanges' ) {
470 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
477 * Add page-specific modules.
479 protected function addModules() {
480 parent
::addModules();
481 $out = $this->getOutput();
482 $out->addModules( 'mediawiki.special.recentchanges' );
486 * Get last modified date, for client caching
487 * Don't use this if we are using the patrol feature, patrol changes don't
488 * update the timestamp
490 * @return string|bool
492 public function checkLastModified() {
493 $dbr = $this->getDB();
494 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
499 * Creates the choose namespace selection
501 * @param FormOptions $opts
504 protected function namespaceFilterForm( FormOptions
$opts ) {
505 $nsSelect = Html
::namespaceSelector(
506 array( 'selected' => $opts['namespace'], 'all' => '' ),
507 array( 'name' => 'namespace', 'id' => 'namespace' )
509 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
510 $invert = Xml
::checkLabel(
511 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
513 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
515 $associated = Xml
::checkLabel(
516 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
518 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
521 return array( $nsLabel, "$nsSelect $invert $associated" );
525 * Create a input to filter changes by categories
527 * @param FormOptions $opts
530 protected function categoryFilterForm( FormOptions
$opts ) {
531 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
532 'categories', 'mw-categories', false, $opts['categories'] );
534 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
535 'categories_any', 'mw-categories_any', $opts['categories_any'] );
537 return array( $label, $input );
541 * Filter $rows by categories set in $opts
543 * @param ResultWrapper $rows Database rows
544 * @param FormOptions $opts
546 function filterByCategories( &$rows, FormOptions
$opts ) {
547 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
549 if ( !count( $categories ) ) {
555 foreach ( $categories as $cat ) {
567 foreach ( $rows as $k => $r ) {
568 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
569 $id = $nt->getArticleID();
571 continue; # Page might have been deleted...
573 if ( !in_array( $id, $articles ) ) {
576 if ( !isset( $a2r[$id] ) ) {
584 if ( !count( $articles ) ||
!count( $cats ) ) {
589 $c = new Categoryfinder
;
590 $c->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
595 foreach ( $match as $id ) {
596 foreach ( $a2r[$id] as $rev ) {
598 $newrows[$k] = $rowsarr[$k];
605 * Makes change an option link which carries all the other options
607 * @param string $title Title
608 * @param array $override Options to override
609 * @param array $options Current options
610 * @param bool $active Whether to show the link in bold
613 function makeOptionsLink( $title, $override, $options, $active = false ) {
614 $params = $override +
$options;
616 // Bug 36524: false values have be converted to "0" otherwise
617 // wfArrayToCgi() will omit it them.
618 foreach ( $params as &$value ) {
619 if ( $value === false ) {
625 $text = htmlspecialchars( $title );
627 $text = '<strong>' . $text . '</strong>';
630 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
634 * Creates the options panel.
636 * @param array $defaults
637 * @param array $nondefaults
640 function optionsPanel( $defaults, $nondefaults ) {
641 global $wgRCLinkLimits, $wgRCLinkDays;
643 $options = $nondefaults +
$defaults;
646 $msg = $this->msg( 'rclegend' );
647 if ( !$msg->isDisabled() ) {
648 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
651 $lang = $this->getLanguage();
652 $user = $this->getUser();
653 if ( $options['from'] ) {
654 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
655 $lang->userTimeAndDate( $options['from'], $user ),
656 $lang->userDate( $options['from'], $user ),
657 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
660 # Sort data for display and make sure it's unique after we've added user data.
661 $linkLimits = $wgRCLinkLimits;
662 $linkLimits[] = $options['limit'];
664 $linkLimits = array_unique( $linkLimits );
666 $linkDays = $wgRCLinkDays;
667 $linkDays[] = $options['days'];
669 $linkDays = array_unique( $linkDays );
673 foreach ( $linkLimits as $value ) {
674 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
675 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
677 $cl = $lang->pipeList( $cl );
679 // day links, reset 'from' to none
681 foreach ( $linkDays as $value ) {
682 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
683 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
685 $dl = $lang->pipeList( $dl );
689 'hideminor' => 'rcshowhideminor',
690 'hidebots' => 'rcshowhidebots',
691 'hideanons' => 'rcshowhideanons',
692 'hideliu' => 'rcshowhideliu',
693 'hidepatrolled' => 'rcshowhidepatr',
694 'hidemyself' => 'rcshowhidemine'
697 $showhide = array( 'show', 'hide' );
699 foreach ( $this->getCustomFilters() as $key => $params ) {
700 $filters[$key] = $params['msg'];
702 // Disable some if needed
703 if ( !$user->useRCPatrol() ) {
704 unset( $filters['hidepatrolled'] );
708 foreach ( $filters as $key => $msg ) {
709 // The following messages are used here:
710 // rcshowhideminor-show, rcshowhideminor-hide, rcshowhidebots-show, rcshowhidebots-hide,
711 // rcshowhideanons-show, rcshowhideanons-hide, rcshowhideliu-show, rcshowhideliu-hide,
712 // rcshowhidepatr-show, rcshowhidepatr-hide, rcshowhidemine-show, rcshowhidemine-hide.
713 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
714 // Extensions can define additional filters, but don't need to define the corresponding
715 // messages. If they don't exist, just fall back to 'show' and 'hide'.
716 if ( !$linkMessage->exists() ) {
717 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
720 $link = $this->makeOptionsLink( $linkMessage->text(),
721 array( $key => 1 - $options[$key] ), $nondefaults );
722 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
725 // show from this onward link
726 $timestamp = wfTimestampNow();
727 $now = $lang->userTimeAndDate( $timestamp, $user );
728 $timenow = $lang->userTime( $timestamp, $user );
729 $datenow = $lang->userDate( $timestamp, $user );
730 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
732 $rclistfrom = $this->makeOptionsLink(
733 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
734 array( 'from' => $timestamp ),
738 return "{$note}$rclinks<br />$rclistfrom";
741 public function isIncludable() {