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 ) {
61 '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Recent_changes',
64 parent
::execute( $subpage );
68 * Get a FormOptions object containing the default options
72 public function getDefaultOptions() {
73 $opts = parent
::getDefaultOptions();
74 $user = $this->getUser();
76 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
77 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
78 $opts->add( 'from', '' );
80 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
81 $opts->add( 'hidebots', true );
82 $opts->add( 'hideanons', false );
83 $opts->add( 'hideliu', false );
84 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
85 $opts->add( 'hidemyself', false );
87 $opts->add( 'categories', '' );
88 $opts->add( 'categories_any', false );
89 $opts->add( 'tagfilter', '' );
95 * Get custom show/hide filters
97 * @return array Map of filter URL param names to properties (msg/default)
99 protected function getCustomFilters() {
100 if ( $this->customFilters
=== null ) {
101 $this->customFilters
= parent
::getCustomFilters();
102 Hooks
::run( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters
), '1.23' );
105 return $this->customFilters
;
109 * Process $par and put options found in $opts. Used when including the page.
112 * @param FormOptions $opts
114 public function parseParameters( $par, FormOptions
$opts ) {
115 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
116 foreach ( $bits as $bit ) {
117 if ( 'hidebots' === $bit ) {
118 $opts['hidebots'] = true;
120 if ( 'bots' === $bit ) {
121 $opts['hidebots'] = false;
123 if ( 'hideminor' === $bit ) {
124 $opts['hideminor'] = true;
126 if ( 'minor' === $bit ) {
127 $opts['hideminor'] = false;
129 if ( 'hideliu' === $bit ) {
130 $opts['hideliu'] = true;
132 if ( 'hidepatrolled' === $bit ) {
133 $opts['hidepatrolled'] = true;
135 if ( 'hideanons' === $bit ) {
136 $opts['hideanons'] = true;
138 if ( 'hidemyself' === $bit ) {
139 $opts['hidemyself'] = true;
142 if ( is_numeric( $bit ) ) {
143 $opts['limit'] = $bit;
147 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
148 $opts['limit'] = $m[1];
150 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
151 $opts['days'] = $m[1];
153 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
154 $opts['namespace'] = $m[1];
159 public function validateOptions( FormOptions
$opts ) {
160 $opts->validateIntBounds( 'limit', 0, 5000 );
161 parent
::validateOptions( $opts );
165 * Return an array of conditions depending of options set in $opts
167 * @param FormOptions $opts
170 public function buildMainQueryConds( FormOptions
$opts ) {
171 $dbr = $this->getDB();
172 $conds = parent
::buildMainQueryConds( $opts );
175 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
176 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
177 $cutoff = $dbr->timestamp( $cutoff_unixtime );
179 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
180 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW
, $cutoff ) ) {
181 $cutoff = $dbr->timestamp( $opts['from'] );
183 $opts->reset( 'from' );
186 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
194 * @param array $conds
195 * @param FormOptions $opts
196 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
198 public function doMainQuery( $conds, $opts ) {
199 $dbr = $this->getDB();
200 $user = $this->getUser();
202 $tables = array( 'recentchanges' );
203 $fields = RecentChange
::selectFields();
204 $query_options = array();
205 $join_conds = array();
207 // JOIN on watchlist for users
208 if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
209 $tables[] = 'watchlist';
210 $fields[] = 'wl_user';
211 $fields[] = 'wl_notificationtimestamp';
212 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
213 'wl_user' => $user->getId(),
215 'wl_namespace=rc_namespace'
219 if ( $user->isAllowed( 'rollback' ) ) {
221 $fields[] = 'page_latest';
222 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
225 ChangeTags
::modifyDisplayQuery(
234 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
240 // array_merge() is used intentionally here so that hooks can, should
241 // they so desire, override the ORDER BY / LIMIT condition(s); prior to
242 // MediaWiki 1.26 this used to use the plus operator instead, which meant
243 // that extensions weren't able to change these conditions
244 $query_options = array_merge( array(
245 'ORDER BY' => 'rc_timestamp DESC',
246 'LIMIT' => $opts['limit'] ), $query_options );
247 $rows = $dbr->select(
250 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
251 // knowledge to use an index merge if it wants (it may use some other index though).
252 $conds +
array( 'rc_new' => array( 0, 1 ) ),
258 // Build the final data
259 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
260 $this->filterByCategories( $rows, $opts );
266 protected function runMainQueryHook( &$tables, &$fields, &$conds,
267 &$query_options, &$join_conds, $opts
269 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
271 'SpecialRecentChangesQuery',
272 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ),
277 protected function getDB() {
278 return wfGetDB( DB_SLAVE
, 'recentchanges' );
281 public function outputFeedLinks() {
282 $this->addFeedLinks( $this->getFeedQuery() );
286 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
290 private function getFeedQuery() {
291 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
292 // API handles empty parameters in a different way
293 return $value !== '';
295 $query['action'] = 'feedrecentchanges';
296 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
297 if ( $query['limit'] > $feedLimit ) {
298 $query['limit'] = $feedLimit;
305 * Build and output the actual changes list.
307 * @param array $rows Database rows
308 * @param FormOptions $opts
310 public function outputChangesList( $rows, $opts ) {
311 $limit = $opts['limit'];
313 $showWatcherCount = $this->getConfig()->get( 'RCShowWatchingUsers' )
314 && $this->getUser()->getOption( 'shownumberswatching' );
315 $watcherCache = array();
317 $dbr = $this->getDB();
320 $list = ChangesList
::newFromContext( $this->getContext() );
321 $list->initChangesListRows( $rows );
323 $rclistOutput = $list->beginRecentChangesList();
324 foreach ( $rows as $obj ) {
328 $rc = RecentChange
::newFromRow( $obj );
329 $rc->counter
= $counter++
;
330 # Check if the page has been updated since the last visit
331 if ( $this->getConfig()->get( 'ShowUpdatedMarker' )
332 && !empty( $obj->wl_notificationtimestamp
)
334 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
336 $rc->notificationtimestamp
= false; // Default
338 # Check the number of users watching the page
339 $rc->numberofWatchingusers
= 0; // Default
340 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
341 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
342 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
347 'wl_namespace' => $obj->rc_namespace
,
348 'wl_title' => $obj->rc_title
,
350 __METHOD__
. '-watchers'
353 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
356 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
357 if ( $changeLine !== false ) {
358 $rclistOutput .= $changeLine;
362 $rclistOutput .= $list->endRecentChangesList();
364 if ( $rows->numRows() === 0 ) {
365 $this->getOutput()->addHtml(
366 '<div class="mw-changeslist-empty">' .
367 $this->msg( 'recentchanges-noresult' )->parse() .
370 if ( !$this->including() ) {
371 $this->getOutput()->setStatusCode( 404 );
374 $this->getOutput()->addHTML( $rclistOutput );
379 * Set the text to be displayed above the changes
381 * @param FormOptions $opts
382 * @param int $numRows Number of rows in the result to show after this header
384 public function doHeader( $opts, $numRows ) {
385 $this->setTopText( $opts );
387 $defaults = $opts->getAllValues();
388 $nondefaults = $opts->getChangedValues();
391 $panel[] = self
::makeLegend( $this->getContext() );
392 $panel[] = $this->optionsPanel( $defaults, $nondefaults, $numRows );
395 $extraOpts = $this->getExtraOptions( $opts );
396 $extraOptsCount = count( $extraOpts );
398 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
400 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
401 foreach ( $extraOpts as $name => $optionRow ) {
402 # Add submit button to the last row only
404 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
406 $out .= Xml
::openElement( 'tr' );
407 if ( is_array( $optionRow ) ) {
410 array( 'class' => 'mw-label mw-' . $name . '-label' ),
415 array( 'class' => 'mw-input' ),
416 $optionRow[1] . $addSubmit
421 array( 'class' => 'mw-input', 'colspan' => 2 ),
422 $optionRow . $addSubmit
425 $out .= Xml
::closeElement( 'tr' );
427 $out .= Xml
::closeElement( 'table' );
429 $unconsumed = $opts->getUnconsumedValues();
430 foreach ( $unconsumed as $key => $value ) {
431 $out .= Html
::hidden( $key, $value );
434 $t = $this->getPageTitle();
435 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
436 $form = Xml
::tags( 'form', array( 'action' => wfScript() ), $out );
438 $panelString = implode( "\n", $panel );
440 $this->getOutput()->addHTML(
442 $this->msg( 'recentchanges-legend' )->text(),
444 array( 'class' => 'rcoptions' )
448 $this->setBottomText( $opts );
452 * Send the text to be displayed above the options
454 * @param FormOptions $opts Unused
456 function setTopText( FormOptions
$opts ) {
459 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
460 if ( !$message->isDisabled() ) {
461 $this->getOutput()->addWikiText(
462 Html
::rawElement( 'div',
463 array( 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ),
464 "\n" . $message->plain() . "\n"
466 /* $lineStart */ true,
467 /* $interface */ false
473 * Get options to be displayed in a form
475 * @param FormOptions $opts
478 function getExtraOptions( $opts ) {
479 $opts->consumeValues( array(
480 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
483 $extraOpts = array();
484 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
486 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
487 $extraOpts['category'] = $this->categoryFilterForm( $opts );
490 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
491 if ( count( $tagFilter ) ) {
492 $extraOpts['tagfilter'] = $tagFilter;
495 // Don't fire the hook for subclasses. (Or should we?)
496 if ( $this->getName() === 'Recentchanges' ) {
497 Hooks
::run( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
504 * Add page-specific modules.
506 protected function addModules() {
507 parent
::addModules();
508 $out = $this->getOutput();
509 $out->addModules( 'mediawiki.special.recentchanges' );
513 * Get last modified date, for client caching
514 * Don't use this if we are using the patrol feature, patrol changes don't
515 * update the timestamp
517 * @return string|bool
519 public function checkLastModified() {
520 $dbr = $this->getDB();
521 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
527 * Creates the choose namespace selection
529 * @param FormOptions $opts
532 protected function namespaceFilterForm( FormOptions
$opts ) {
533 $nsSelect = Html
::namespaceSelector(
534 array( 'selected' => $opts['namespace'], 'all' => '' ),
535 array( 'name' => 'namespace', 'id' => 'namespace' )
537 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
538 $invert = Xml
::checkLabel(
539 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
541 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
543 $associated = Xml
::checkLabel(
544 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
546 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
549 return array( $nsLabel, "$nsSelect $invert $associated" );
553 * Create an input to filter changes by categories
555 * @param FormOptions $opts
558 protected function categoryFilterForm( FormOptions
$opts ) {
559 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
560 'categories', 'mw-categories', false, $opts['categories'] );
562 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
563 'categories_any', 'mw-categories_any', $opts['categories_any'] );
565 return array( $label, $input );
569 * Filter $rows by categories set in $opts
571 * @param ResultWrapper $rows Database rows
572 * @param FormOptions $opts
574 function filterByCategories( &$rows, FormOptions
$opts ) {
575 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
577 if ( !count( $categories ) ) {
583 foreach ( $categories as $cat ) {
595 foreach ( $rows as $k => $r ) {
596 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
597 $id = $nt->getArticleID();
599 continue; # Page might have been deleted...
601 if ( !in_array( $id, $articles ) ) {
604 if ( !isset( $a2r[$id] ) ) {
612 if ( !count( $articles ) ||
!count( $cats ) ) {
617 $catFind = new CategoryFinder
;
618 $catFind->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
619 $match = $catFind->run();
623 foreach ( $match as $id ) {
624 foreach ( $a2r[$id] as $rev ) {
626 $newrows[$k] = $rowsarr[$k];
633 * Makes change an option link which carries all the other options
635 * @param string $title Title
636 * @param array $override Options to override
637 * @param array $options Current options
638 * @param bool $active Whether to show the link in bold
641 function makeOptionsLink( $title, $override, $options, $active = false ) {
642 $params = $override +
$options;
644 // Bug 36524: false values have be converted to "0" otherwise
645 // wfArrayToCgi() will omit it them.
646 foreach ( $params as &$value ) {
647 if ( $value === false ) {
653 $text = htmlspecialchars( $title );
655 $text = '<strong>' . $text . '</strong>';
658 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
662 * Creates the options panel.
664 * @param array $defaults
665 * @param array $nondefaults
666 * @param int $numRows Number of rows in the result to show after this header
669 function optionsPanel( $defaults, $nondefaults, $numRows ) {
670 $options = $nondefaults +
$defaults;
673 $msg = $this->msg( 'rclegend' );
674 if ( !$msg->isDisabled() ) {
675 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
678 $lang = $this->getLanguage();
679 $user = $this->getUser();
680 if ( $options['from'] ) {
681 $note .= $this->msg( 'rcnotefrom' )
682 ->numParams( $options['limit'] )
684 $lang->userTimeAndDate( $options['from'], $user ),
685 $lang->userDate( $options['from'], $user ),
686 $lang->userTime( $options['from'], $user )
688 ->numParams( $numRows )
689 ->parse() . '<br />';
692 # Sort data for display and make sure it's unique after we've added user data.
693 $linkLimits = $this->getConfig()->get( 'RCLinkLimits' );
694 $linkLimits[] = $options['limit'];
696 $linkLimits = array_unique( $linkLimits );
698 $linkDays = $this->getConfig()->get( 'RCLinkDays' );
699 $linkDays[] = $options['days'];
701 $linkDays = array_unique( $linkDays );
705 foreach ( $linkLimits as $value ) {
706 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
707 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
709 $cl = $lang->pipeList( $cl );
711 // day links, reset 'from' to none
713 foreach ( $linkDays as $value ) {
714 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
715 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
717 $dl = $lang->pipeList( $dl );
721 'hideminor' => 'rcshowhideminor',
722 'hidebots' => 'rcshowhidebots',
723 'hideanons' => 'rcshowhideanons',
724 'hideliu' => 'rcshowhideliu',
725 'hidepatrolled' => 'rcshowhidepatr',
726 'hidemyself' => 'rcshowhidemine'
729 $showhide = array( 'show', 'hide' );
731 foreach ( $this->getCustomFilters() as $key => $params ) {
732 $filters[$key] = $params['msg'];
734 // Disable some if needed
735 if ( !$user->useRCPatrol() ) {
736 unset( $filters['hidepatrolled'] );
740 foreach ( $filters as $key => $msg ) {
741 // The following messages are used here:
742 // rcshowhideminor-show, rcshowhideminor-hide, rcshowhidebots-show, rcshowhidebots-hide,
743 // rcshowhideanons-show, rcshowhideanons-hide, rcshowhideliu-show, rcshowhideliu-hide,
744 // rcshowhidepatr-show, rcshowhidepatr-hide, rcshowhidemine-show, rcshowhidemine-hide.
745 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
746 // Extensions can define additional filters, but don't need to define the corresponding
747 // messages. If they don't exist, just fall back to 'show' and 'hide'.
748 if ( !$linkMessage->exists() ) {
749 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
752 $link = $this->makeOptionsLink( $linkMessage->text(),
753 array( $key => 1 - $options[$key] ), $nondefaults );
754 $links[] = "<span class=\"$msg rcshowhideoption\">"
755 . $this->msg( $msg )->rawParams( $link )->escaped() . '</span>';
758 // show from this onward link
759 $timestamp = wfTimestampNow();
760 $now = $lang->userTimeAndDate( $timestamp, $user );
761 $timenow = $lang->userTime( $timestamp, $user );
762 $datenow = $lang->userDate( $timestamp, $user );
763 $pipedLinks = '<span class="rcshowhide">' . $lang->pipeList( $links ) . '</span>';
765 $rclinks = '<span class="rclinks">' . $this->msg( 'rclinks' )->rawParams( $cl, $dl, $pipedLinks )
766 ->parse() . '</span>';
768 $rclistfrom = '<span class="rclistfrom">' . $this->makeOptionsLink(
769 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
770 array( 'from' => $timestamp ),
774 return "{$note}$rclinks<br />$rclistfrom";
777 public function isIncludable() {