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 ) );
52 // 10 seconds server-side caching max
53 $this->getOutput()->setSquidMaxage( 10 );
54 // Check if the client has a cached version
55 $lastmod = $this->checkLastModified();
56 if ( $lastmod === false ) {
60 parent
::execute( $subpage );
64 * Get a FormOptions object containing the default options
68 public function getDefaultOptions() {
69 $opts = parent
::getDefaultOptions();
70 $user = $this->getUser();
72 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
73 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
74 $opts->add( 'from', '' );
76 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
77 $opts->add( 'hidebots', true );
78 $opts->add( 'hideanons', false );
79 $opts->add( 'hideliu', false );
80 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
81 $opts->add( 'hidemyself', false );
83 $opts->add( 'categories', '' );
84 $opts->add( 'categories_any', false );
85 $opts->add( 'tagfilter', '' );
91 * Get custom show/hide filters
93 * @return array Map of filter URL param names to properties (msg/default)
95 protected function getCustomFilters() {
96 if ( $this->customFilters
=== null ) {
97 $this->customFilters
= array();
98 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters
) );
101 return $this->customFilters
;
105 * Process $par and put options found in $opts. Used when including the page.
108 * @param FormOptions $opts
110 public function parseParameters( $par, FormOptions
$opts ) {
111 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
112 foreach ( $bits as $bit ) {
113 if ( 'hidebots' === $bit ) {
114 $opts['hidebots'] = true;
116 if ( 'bots' === $bit ) {
117 $opts['hidebots'] = false;
119 if ( 'hideminor' === $bit ) {
120 $opts['hideminor'] = true;
122 if ( 'minor' === $bit ) {
123 $opts['hideminor'] = false;
125 if ( 'hideliu' === $bit ) {
126 $opts['hideliu'] = true;
128 if ( 'hidepatrolled' === $bit ) {
129 $opts['hidepatrolled'] = true;
131 if ( 'hideanons' === $bit ) {
132 $opts['hideanons'] = true;
134 if ( 'hidemyself' === $bit ) {
135 $opts['hidemyself'] = true;
138 if ( is_numeric( $bit ) ) {
139 $opts['limit'] = $bit;
143 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
144 $opts['limit'] = $m[1];
146 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
147 $opts['days'] = $m[1];
149 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
150 $opts['namespace'] = $m[1];
155 public function validateOptions( FormOptions
$opts ) {
156 $opts->validateIntBounds( 'limit', 0, 5000 );
157 parent
::validateOptions( $opts );
161 * Return an array of conditions depending of options set in $opts
163 * @param FormOptions $opts
166 public function buildMainQueryConds( FormOptions
$opts ) {
167 $dbr = $this->getDB();
168 $conds = parent
::buildMainQueryConds( $opts );
171 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
172 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
173 $cutoff = $dbr->timestamp( $cutoff_unixtime );
175 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
176 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW
, $cutoff ) ) {
177 $cutoff = $dbr->timestamp( $opts['from'] );
179 $opts->reset( 'from' );
182 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
190 * @param array $conds
191 * @param FormOptions $opts
192 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
194 public function doMainQuery( $conds, $opts ) {
195 global $wgAllowCategorizedRecentChanges;
197 $dbr = $this->getDB();
198 $user = $this->getUser();
200 $tables = array( 'recentchanges' );
201 $fields = RecentChange
::selectFields();
202 $query_options = array();
203 $join_conds = array();
205 // JOIN on watchlist for users
206 if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
207 $tables[] = 'watchlist';
208 $fields[] = 'wl_user';
209 $fields[] = 'wl_notificationtimestamp';
210 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
211 'wl_user' => $user->getId(),
213 'wl_namespace=rc_namespace'
217 if ( $user->isAllowed( 'rollback' ) ) {
219 $fields[] = 'page_latest';
220 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
223 ChangeTags
::modifyDisplayQuery(
232 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
233 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
238 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
239 // knowledge to use an index merge if it wants (it may use some other index though).
240 $rows = $dbr->select(
243 $conds +
array( 'rc_new' => array( 0, 1 ) ),
245 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $opts['limit'] ) +
$query_options,
249 // Build the final data
250 if ( $wgAllowCategorizedRecentChanges ) {
251 $this->filterByCategories( $rows, $opts );
257 public function outputFeedLinks() {
258 $this->addFeedLinks( $this->getFeedQuery() );
262 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
266 private function getFeedQuery() {
268 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
269 // API handles empty parameters in a different way
270 return $value !== '';
272 $query['action'] = 'feedrecentchanges';
273 if ( $query['limit'] > $wgFeedLimit ) {
274 $query['limit'] = $wgFeedLimit;
281 * Build and output the actual changes list.
283 * @param array $rows Database rows
284 * @param FormOptions $opts
286 public function outputChangesList( $rows, $opts ) {
287 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
289 $limit = $opts['limit'];
291 $showWatcherCount = $wgRCShowWatchingUsers
292 && $this->getUser()->getOption( 'shownumberswatching' );
293 $watcherCache = array();
295 $dbr = $this->getDB();
298 $list = ChangesList
::newFromContext( $this->getContext() );
300 $rclistOutput = $list->beginRecentChangesList();
301 foreach ( $rows as $obj ) {
305 $rc = RecentChange
::newFromRow( $obj );
306 $rc->counter
= $counter++
;
307 # Check if the page has been updated since the last visit
308 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp
) ) {
309 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
311 $rc->notificationtimestamp
= false; // Default
313 # Check the number of users watching the page
314 $rc->numberofWatchingusers
= 0; // Default
315 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
316 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
317 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
322 'wl_namespace' => $obj->rc_namespace
,
323 'wl_title' => $obj->rc_title
,
325 __METHOD__
. '-watchers'
328 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
331 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
332 if ( $changeLine !== false ) {
333 $rclistOutput .= $changeLine;
337 $rclistOutput .= $list->endRecentChangesList();
339 if ( $rows->numRows() === 0 ) {
340 $this->getOutput()->addHtml(
341 '<div class="mw-changeslist-empty">' .
342 $this->msg( 'recentchanges-noresult' )->parse() .
346 $this->getOutput()->addHTML( $rclistOutput );
351 * Return the text to be displayed above the changes
353 * @param FormOptions $opts
354 * @return string XHTML
356 public function doHeader( $opts ) {
359 $this->setTopText( $opts );
361 $defaults = $opts->getAllValues();
362 $nondefaults = $opts->getChangedValues();
365 $panel[] = self
::makeLegend( $this->getContext() );
366 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
369 $extraOpts = $this->getExtraOptions( $opts );
370 $extraOptsCount = count( $extraOpts );
372 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
374 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
375 foreach ( $extraOpts as $name => $optionRow ) {
376 # Add submit button to the last row only
378 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
380 $out .= Xml
::openElement( 'tr' );
381 if ( is_array( $optionRow ) ) {
384 array( 'class' => 'mw-label mw-' . $name . '-label' ),
389 array( 'class' => 'mw-input' ),
390 $optionRow[1] . $addSubmit
395 array( 'class' => 'mw-input', 'colspan' => 2 ),
396 $optionRow . $addSubmit
399 $out .= Xml
::closeElement( 'tr' );
401 $out .= Xml
::closeElement( 'table' );
403 $unconsumed = $opts->getUnconsumedValues();
404 foreach ( $unconsumed as $key => $value ) {
405 $out .= Html
::hidden( $key, $value );
408 $t = $this->getPageTitle();
409 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
410 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
412 $panelString = implode( "\n", $panel );
414 $this->getOutput()->addHTML(
416 $this->msg( 'recentchanges-legend' )->text(),
418 array( 'class' => 'rcoptions' )
422 $this->setBottomText( $opts );
426 * Send the text to be displayed above the options
428 * @param FormOptions $opts Unused
430 function setTopText( FormOptions
$opts ) {
433 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
434 if ( !$message->isDisabled() ) {
435 $this->getOutput()->addWikiText(
436 Html
::rawElement( 'p',
437 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
438 "\n" . $message->plain() . "\n"
440 /* $lineStart */ false,
441 /* $interface */ false
447 * Get options to be displayed in a form
449 * @param FormOptions $opts
452 function getExtraOptions( $opts ) {
453 $opts->consumeValues( array(
454 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
457 $extraOpts = array();
458 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
460 global $wgAllowCategorizedRecentChanges;
461 if ( $wgAllowCategorizedRecentChanges ) {
462 $extraOpts['category'] = $this->categoryFilterForm( $opts );
465 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
466 if ( count( $tagFilter ) ) {
467 $extraOpts['tagfilter'] = $tagFilter;
470 // Don't fire the hook for subclasses. (Or should we?)
471 if ( $this->getName() === 'Recentchanges' ) {
472 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
479 * Add page-specific modules.
481 protected function addModules() {
482 parent
::addModules();
483 $out = $this->getOutput();
484 $out->addModules( 'mediawiki.special.recentchanges' );
488 * Get last modified date, for client caching
489 * Don't use this if we are using the patrol feature, patrol changes don't
490 * update the timestamp
492 * @return string|bool
494 public function checkLastModified() {
495 $dbr = $this->getDB();
496 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
502 * Creates the choose namespace selection
504 * @param FormOptions $opts
507 protected function namespaceFilterForm( FormOptions
$opts ) {
508 $nsSelect = Html
::namespaceSelector(
509 array( 'selected' => $opts['namespace'], 'all' => '' ),
510 array( 'name' => 'namespace', 'id' => 'namespace' )
512 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
513 $invert = Xml
::checkLabel(
514 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
516 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
518 $associated = Xml
::checkLabel(
519 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
521 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
524 return array( $nsLabel, "$nsSelect $invert $associated" );
528 * Create a input to filter changes by categories
530 * @param FormOptions $opts
533 protected function categoryFilterForm( FormOptions
$opts ) {
534 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
535 'categories', 'mw-categories', false, $opts['categories'] );
537 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
538 'categories_any', 'mw-categories_any', $opts['categories_any'] );
540 return array( $label, $input );
544 * Filter $rows by categories set in $opts
546 * @param ResultWrapper $rows Database rows
547 * @param FormOptions $opts
549 function filterByCategories( &$rows, FormOptions
$opts ) {
550 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
552 if ( !count( $categories ) ) {
558 foreach ( $categories as $cat ) {
570 foreach ( $rows as $k => $r ) {
571 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
572 $id = $nt->getArticleID();
574 continue; # Page might have been deleted...
576 if ( !in_array( $id, $articles ) ) {
579 if ( !isset( $a2r[$id] ) ) {
587 if ( !count( $articles ) ||
!count( $cats ) ) {
592 $c = new Categoryfinder
;
593 $c->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
598 foreach ( $match as $id ) {
599 foreach ( $a2r[$id] as $rev ) {
601 $newrows[$k] = $rowsarr[$k];
608 * Makes change an option link which carries all the other options
610 * @param string $title Title
611 * @param array $override Options to override
612 * @param array $options Current options
613 * @param bool $active Whether to show the link in bold
616 function makeOptionsLink( $title, $override, $options, $active = false ) {
617 $params = $override +
$options;
619 // Bug 36524: false values have be converted to "0" otherwise
620 // wfArrayToCgi() will omit it them.
621 foreach ( $params as &$value ) {
622 if ( $value === false ) {
628 $text = htmlspecialchars( $title );
630 $text = '<strong>' . $text . '</strong>';
633 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
637 * Creates the options panel.
639 * @param array $defaults
640 * @param array $nondefaults
643 function optionsPanel( $defaults, $nondefaults ) {
644 global $wgRCLinkLimits, $wgRCLinkDays;
646 $options = $nondefaults +
$defaults;
649 $msg = $this->msg( 'rclegend' );
650 if ( !$msg->isDisabled() ) {
651 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
654 $lang = $this->getLanguage();
655 $user = $this->getUser();
656 if ( $options['from'] ) {
657 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
658 $lang->userTimeAndDate( $options['from'], $user ),
659 $lang->userDate( $options['from'], $user ),
660 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
663 # Sort data for display and make sure it's unique after we've added user data.
664 $linkLimits = $wgRCLinkLimits;
665 $linkLimits[] = $options['limit'];
667 $linkLimits = array_unique( $linkLimits );
669 $linkDays = $wgRCLinkDays;
670 $linkDays[] = $options['days'];
672 $linkDays = array_unique( $linkDays );
676 foreach ( $linkLimits as $value ) {
677 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
678 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
680 $cl = $lang->pipeList( $cl );
682 // day links, reset 'from' to none
684 foreach ( $linkDays as $value ) {
685 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
686 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
688 $dl = $lang->pipeList( $dl );
692 'hideminor' => 'rcshowhideminor',
693 'hidebots' => 'rcshowhidebots',
694 'hideanons' => 'rcshowhideanons',
695 'hideliu' => 'rcshowhideliu',
696 'hidepatrolled' => 'rcshowhidepatr',
697 'hidemyself' => 'rcshowhidemine'
700 $showhide = array( 'show', 'hide' );
702 foreach ( $this->getCustomFilters() as $key => $params ) {
703 $filters[$key] = $params['msg'];
705 // Disable some if needed
706 if ( !$user->useRCPatrol() ) {
707 unset( $filters['hidepatrolled'] );
711 foreach ( $filters as $key => $msg ) {
712 // The following messages are used here:
713 // rcshowhideminor-show, rcshowhideminor-hide, rcshowhidebots-show, rcshowhidebots-hide,
714 // rcshowhideanons-show, rcshowhideanons-hide, rcshowhideliu-show, rcshowhideliu-hide,
715 // rcshowhidepatr-show, rcshowhidepatr-hide, rcshowhidemine-show, rcshowhidemine-hide.
716 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
717 // Extensions can define additional filters, but don't need to define the corresponding
718 // messages. If they don't exist, just fall back to 'show' and 'hide'.
719 if ( !$linkMessage->exists() ) {
720 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
723 $link = $this->makeOptionsLink( $linkMessage->text(),
724 array( $key => 1 - $options[$key] ), $nondefaults );
725 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
728 // show from this onward link
729 $timestamp = wfTimestampNow();
730 $now = $lang->userTimeAndDate( $timestamp, $user );
731 $timenow = $lang->userTime( $timestamp, $user );
732 $datenow = $lang->userDate( $timestamp, $user );
733 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
735 $rclistfrom = $this->makeOptionsLink(
736 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
737 array( 'from' => $timestamp ),
741 return "{$note}$rclinks<br />$rclistfrom";
744 public function isIncludable() {