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()->setCdnMaxage( 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();
75 $config = $this->getConfig();
77 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
78 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
79 $opts->add( 'from', '' );
81 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
82 $opts->add( 'hidebots', true );
83 $opts->add( 'hideanons', false );
84 $opts->add( 'hideliu', false );
85 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
86 $opts->add( 'hidemyself', false );
88 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
89 $opts->add( 'hidecategorization', $user->getBoolOption( 'hidecategorization' ) );
92 $opts->add( 'categories', '' );
93 $opts->add( 'categories_any', false );
94 $opts->add( 'tagfilter', '' );
100 * Get custom show/hide filters
102 * @return array Map of filter URL param names to properties (msg/default)
104 protected function getCustomFilters() {
105 if ( $this->customFilters
=== null ) {
106 $this->customFilters
= parent
::getCustomFilters();
107 Hooks
::run( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters
), '1.23' );
110 return $this->customFilters
;
114 * Process $par and put options found in $opts. Used when including the page.
117 * @param FormOptions $opts
119 public function parseParameters( $par, FormOptions
$opts ) {
120 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
121 foreach ( $bits as $bit ) {
122 if ( 'hidebots' === $bit ) {
123 $opts['hidebots'] = true;
125 if ( 'bots' === $bit ) {
126 $opts['hidebots'] = false;
128 if ( 'hideminor' === $bit ) {
129 $opts['hideminor'] = true;
131 if ( 'minor' === $bit ) {
132 $opts['hideminor'] = false;
134 if ( 'hideliu' === $bit ) {
135 $opts['hideliu'] = true;
137 if ( 'hidepatrolled' === $bit ) {
138 $opts['hidepatrolled'] = true;
140 if ( 'hideanons' === $bit ) {
141 $opts['hideanons'] = true;
143 if ( 'hidemyself' === $bit ) {
144 $opts['hidemyself'] = true;
146 if ( 'hidecategorization' === $bit ) {
147 $opts['hidecategorization'] = true;
150 if ( is_numeric( $bit ) ) {
151 $opts['limit'] = $bit;
155 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
156 $opts['limit'] = $m[1];
158 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
159 $opts['days'] = $m[1];
161 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
162 $opts['namespace'] = $m[1];
167 public function validateOptions( FormOptions
$opts ) {
168 $opts->validateIntBounds( 'limit', 0, 5000 );
169 parent
::validateOptions( $opts );
173 * Return an array of conditions depending of options set in $opts
175 * @param FormOptions $opts
178 public function buildMainQueryConds( FormOptions
$opts ) {
179 $dbr = $this->getDB();
180 $conds = parent
::buildMainQueryConds( $opts );
183 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
184 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
185 $cutoff = $dbr->timestamp( $cutoff_unixtime );
187 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
188 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW
, $cutoff ) ) {
189 $cutoff = $dbr->timestamp( $opts['from'] );
191 $opts->reset( 'from' );
194 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
202 * @param array $conds
203 * @param FormOptions $opts
204 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
206 public function doMainQuery( $conds, $opts ) {
207 $dbr = $this->getDB();
208 $user = $this->getUser();
210 $tables = array( 'recentchanges' );
211 $fields = RecentChange
::selectFields();
212 $query_options = array();
213 $join_conds = array();
215 // JOIN on watchlist for users
216 if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
217 $tables[] = 'watchlist';
218 $fields[] = 'wl_user';
219 $fields[] = 'wl_notificationtimestamp';
220 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
221 'wl_user' => $user->getId(),
223 'wl_namespace=rc_namespace'
227 if ( $user->isAllowed( 'rollback' ) ) {
229 $fields[] = 'page_latest';
230 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
233 ChangeTags
::modifyDisplayQuery(
242 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
248 // array_merge() is used intentionally here so that hooks can, should
249 // they so desire, override the ORDER BY / LIMIT condition(s); prior to
250 // MediaWiki 1.26 this used to use the plus operator instead, which meant
251 // that extensions weren't able to change these conditions
252 $query_options = array_merge( array(
253 'ORDER BY' => 'rc_timestamp DESC',
254 'LIMIT' => $opts['limit'] ), $query_options );
255 $rows = $dbr->select(
258 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
259 // knowledge to use an index merge if it wants (it may use some other index though).
260 $conds +
array( 'rc_new' => array( 0, 1 ) ),
266 // Build the final data
267 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
268 $this->filterByCategories( $rows, $opts );
274 protected function runMainQueryHook( &$tables, &$fields, &$conds,
275 &$query_options, &$join_conds, $opts
277 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
279 'SpecialRecentChangesQuery',
280 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ),
285 protected function getDB() {
286 return wfGetDB( DB_SLAVE
, 'recentchanges' );
289 public function outputFeedLinks() {
290 $this->addFeedLinks( $this->getFeedQuery() );
294 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
298 protected function getFeedQuery() {
299 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
300 // API handles empty parameters in a different way
301 return $value !== '';
303 $query['action'] = 'feedrecentchanges';
304 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
305 if ( $query['limit'] > $feedLimit ) {
306 $query['limit'] = $feedLimit;
313 * Build and output the actual changes list.
315 * @param array $rows Database rows
316 * @param FormOptions $opts
318 public function outputChangesList( $rows, $opts ) {
319 $limit = $opts['limit'];
321 $showWatcherCount = $this->getConfig()->get( 'RCShowWatchingUsers' )
322 && $this->getUser()->getOption( 'shownumberswatching' );
323 $watcherCache = array();
325 $dbr = $this->getDB();
328 $list = ChangesList
::newFromContext( $this->getContext() );
329 $list->initChangesListRows( $rows );
331 $rclistOutput = $list->beginRecentChangesList();
332 foreach ( $rows as $obj ) {
336 $rc = RecentChange
::newFromRow( $obj );
337 $rc->counter
= $counter++
;
338 # Check if the page has been updated since the last visit
339 if ( $this->getConfig()->get( 'ShowUpdatedMarker' )
340 && !empty( $obj->wl_notificationtimestamp
)
342 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
344 $rc->notificationtimestamp
= false; // Default
346 # Check the number of users watching the page
347 $rc->numberofWatchingusers
= 0; // Default
348 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
349 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
350 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
355 'wl_namespace' => $obj->rc_namespace
,
356 'wl_title' => $obj->rc_title
,
358 __METHOD__
. '-watchers'
361 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
364 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
365 if ( $changeLine !== false ) {
366 $rclistOutput .= $changeLine;
370 $rclistOutput .= $list->endRecentChangesList();
372 if ( $rows->numRows() === 0 ) {
373 $this->getOutput()->addHtml(
374 '<div class="mw-changeslist-empty">' .
375 $this->msg( 'recentchanges-noresult' )->parse() .
378 if ( !$this->including() ) {
379 $this->getOutput()->setStatusCode( 404 );
382 $this->getOutput()->addHTML( $rclistOutput );
387 * Set the text to be displayed above the changes
389 * @param FormOptions $opts
390 * @param int $numRows Number of rows in the result to show after this header
392 public function doHeader( $opts, $numRows ) {
393 $this->setTopText( $opts );
395 $defaults = $opts->getAllValues();
396 $nondefaults = $opts->getChangedValues();
399 $panel[] = self
::makeLegend( $this->getContext() );
400 $panel[] = $this->optionsPanel( $defaults, $nondefaults, $numRows );
403 $extraOpts = $this->getExtraOptions( $opts );
404 $extraOptsCount = count( $extraOpts );
406 $submit = ' ' . Xml
::submitbutton( $this->msg( 'recentchanges-submit' )->text() );
408 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
409 foreach ( $extraOpts as $name => $optionRow ) {
410 # Add submit button to the last row only
412 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
414 $out .= Xml
::openElement( 'tr' );
415 if ( is_array( $optionRow ) ) {
418 array( 'class' => 'mw-label mw-' . $name . '-label' ),
423 array( 'class' => 'mw-input' ),
424 $optionRow[1] . $addSubmit
429 array( 'class' => 'mw-input', 'colspan' => 2 ),
430 $optionRow . $addSubmit
433 $out .= Xml
::closeElement( 'tr' );
435 $out .= Xml
::closeElement( 'table' );
437 $unconsumed = $opts->getUnconsumedValues();
438 foreach ( $unconsumed as $key => $value ) {
439 $out .= Html
::hidden( $key, $value );
442 $t = $this->getPageTitle();
443 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
444 $form = Xml
::tags( 'form', array( 'action' => wfScript() ), $out );
446 $panelString = implode( "\n", $panel );
448 $this->getOutput()->addHTML(
450 $this->msg( 'recentchanges-legend' )->text(),
452 array( 'class' => 'rcoptions' )
456 $this->setBottomText( $opts );
460 * Send the text to be displayed above the options
462 * @param FormOptions $opts Unused
464 function setTopText( FormOptions
$opts ) {
467 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
468 if ( !$message->isDisabled() ) {
469 $this->getOutput()->addWikiText(
470 Html
::rawElement( 'div',
471 array( 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ),
472 "\n" . $message->plain() . "\n"
474 /* $lineStart */ true,
475 /* $interface */ false
481 * Get options to be displayed in a form
483 * @param FormOptions $opts
486 function getExtraOptions( $opts ) {
487 $opts->consumeValues( array(
488 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
491 $extraOpts = array();
492 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
494 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
495 $extraOpts['category'] = $this->categoryFilterForm( $opts );
498 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
499 if ( count( $tagFilter ) ) {
500 $extraOpts['tagfilter'] = $tagFilter;
503 // Don't fire the hook for subclasses. (Or should we?)
504 if ( $this->getName() === 'Recentchanges' ) {
505 Hooks
::run( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
512 * Add page-specific modules.
514 protected function addModules() {
515 parent
::addModules();
516 $out = $this->getOutput();
517 $out->addModules( 'mediawiki.special.recentchanges' );
521 * Get last modified date, for client caching
522 * Don't use this if we are using the patrol feature, patrol changes don't
523 * update the timestamp
525 * @return string|bool
527 public function checkLastModified() {
528 $dbr = $this->getDB();
529 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
535 * Creates the choose namespace selection
537 * @param FormOptions $opts
540 protected function namespaceFilterForm( FormOptions
$opts ) {
541 $nsSelect = Html
::namespaceSelector(
542 array( 'selected' => $opts['namespace'], 'all' => '' ),
543 array( 'name' => 'namespace', 'id' => 'namespace' )
545 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
546 $invert = Xml
::checkLabel(
547 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
549 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
551 $associated = Xml
::checkLabel(
552 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
554 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
557 return array( $nsLabel, "$nsSelect $invert $associated" );
561 * Create an input to filter changes by categories
563 * @param FormOptions $opts
566 protected function categoryFilterForm( FormOptions
$opts ) {
567 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
568 'categories', 'mw-categories', false, $opts['categories'] );
570 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
571 'categories_any', 'mw-categories_any', $opts['categories_any'] );
573 return array( $label, $input );
577 * Filter $rows by categories set in $opts
579 * @param ResultWrapper $rows Database rows
580 * @param FormOptions $opts
582 function filterByCategories( &$rows, FormOptions
$opts ) {
583 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
585 if ( !count( $categories ) ) {
591 foreach ( $categories as $cat ) {
603 foreach ( $rows as $k => $r ) {
604 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
605 $id = $nt->getArticleID();
607 continue; # Page might have been deleted...
609 if ( !in_array( $id, $articles ) ) {
612 if ( !isset( $a2r[$id] ) ) {
620 if ( !count( $articles ) ||
!count( $cats ) ) {
625 $catFind = new CategoryFinder
;
626 $catFind->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
627 $match = $catFind->run();
631 foreach ( $match as $id ) {
632 foreach ( $a2r[$id] as $rev ) {
634 $newrows[$k] = $rowsarr[$k];
641 * Makes change an option link which carries all the other options
643 * @param string $title Title
644 * @param array $override Options to override
645 * @param array $options Current options
646 * @param bool $active Whether to show the link in bold
649 function makeOptionsLink( $title, $override, $options, $active = false ) {
650 $params = $override +
$options;
652 // Bug 36524: false values have be converted to "0" otherwise
653 // wfArrayToCgi() will omit it them.
654 foreach ( $params as &$value ) {
655 if ( $value === false ) {
661 $text = htmlspecialchars( $title );
663 $text = '<strong>' . $text . '</strong>';
666 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
670 * Creates the options panel.
672 * @param array $defaults
673 * @param array $nondefaults
674 * @param int $numRows Number of rows in the result to show after this header
677 function optionsPanel( $defaults, $nondefaults, $numRows ) {
678 $options = $nondefaults +
$defaults;
681 $msg = $this->msg( 'rclegend' );
682 if ( !$msg->isDisabled() ) {
683 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
686 $lang = $this->getLanguage();
687 $user = $this->getUser();
688 $config = $this->getConfig();
689 if ( $options['from'] ) {
690 $note .= $this->msg( 'rcnotefrom' )
691 ->numParams( $options['limit'] )
693 $lang->userTimeAndDate( $options['from'], $user ),
694 $lang->userDate( $options['from'], $user ),
695 $lang->userTime( $options['from'], $user )
697 ->numParams( $numRows )
698 ->parse() . '<br />';
701 # Sort data for display and make sure it's unique after we've added user data.
702 $linkLimits = $config->get( 'RCLinkLimits' );
703 $linkLimits[] = $options['limit'];
705 $linkLimits = array_unique( $linkLimits );
707 $linkDays = $config->get( 'RCLinkDays' );
708 $linkDays[] = $options['days'];
710 $linkDays = array_unique( $linkDays );
714 foreach ( $linkLimits as $value ) {
715 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
716 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
718 $cl = $lang->pipeList( $cl );
720 // day links, reset 'from' to none
722 foreach ( $linkDays as $value ) {
723 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
724 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
726 $dl = $lang->pipeList( $dl );
730 'hideminor' => 'rcshowhideminor',
731 'hidebots' => 'rcshowhidebots',
732 'hideanons' => 'rcshowhideanons',
733 'hideliu' => 'rcshowhideliu',
734 'hidepatrolled' => 'rcshowhidepatr',
735 'hidemyself' => 'rcshowhidemine'
738 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
739 $filters['hidecategorization'] = 'rcshowhidecategorization';
742 $showhide = array( 'show', 'hide' );
744 foreach ( $this->getCustomFilters() as $key => $params ) {
745 $filters[$key] = $params['msg'];
747 // Disable some if needed
748 if ( !$user->useRCPatrol() ) {
749 unset( $filters['hidepatrolled'] );
753 foreach ( $filters as $key => $msg ) {
754 // The following messages are used here:
755 // rcshowhideminor-show, rcshowhideminor-hide, rcshowhidebots-show, rcshowhidebots-hide,
756 // rcshowhideanons-show, rcshowhideanons-hide, rcshowhideliu-show, rcshowhideliu-hide,
757 // rcshowhidepatr-show, rcshowhidepatr-hide, rcshowhidemine-show, rcshowhidemine-hide,
758 // rcshowhidecategorization-show, rcshowhidecategorization-hide.
759 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
760 // Extensions can define additional filters, but don't need to define the corresponding
761 // messages. If they don't exist, just fall back to 'show' and 'hide'.
762 if ( !$linkMessage->exists() ) {
763 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
766 $link = $this->makeOptionsLink( $linkMessage->text(),
767 array( $key => 1 - $options[$key] ), $nondefaults );
768 $links[] = "<span class=\"$msg rcshowhideoption\">"
769 . $this->msg( $msg )->rawParams( $link )->escaped() . '</span>';
772 // show from this onward link
773 $timestamp = wfTimestampNow();
774 $now = $lang->userTimeAndDate( $timestamp, $user );
775 $timenow = $lang->userTime( $timestamp, $user );
776 $datenow = $lang->userDate( $timestamp, $user );
777 $pipedLinks = '<span class="rcshowhide">' . $lang->pipeList( $links ) . '</span>';
779 $rclinks = '<span class="rclinks">' . $this->msg( 'rclinks' )->rawParams( $cl, $dl, $pipedLinks )
780 ->parse() . '</span>';
782 $rclistfrom = '<span class="rclistfrom">' . $this->makeOptionsLink(
783 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
784 array( 'from' => $timestamp ),
788 return "{$note}$rclinks<br />$rclistfrom";
791 public function isIncludable() {