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
= parent
::getCustomFilters();
98 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters
), '1.23' );
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 ),
239 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
240 // knowledge to use an index merge if it wants (it may use some other index though).
241 $rows = $dbr->select(
244 $conds +
array( 'rc_new' => array( 0, 1 ) ),
246 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $opts['limit'] ) +
$query_options,
250 // Build the final data
251 if ( $wgAllowCategorizedRecentChanges ) {
252 $this->filterByCategories( $rows, $opts );
258 public function outputFeedLinks() {
259 $this->addFeedLinks( $this->getFeedQuery() );
263 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
267 private function getFeedQuery() {
269 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
270 // API handles empty parameters in a different way
271 return $value !== '';
273 $query['action'] = 'feedrecentchanges';
274 if ( $query['limit'] > $wgFeedLimit ) {
275 $query['limit'] = $wgFeedLimit;
282 * Build and output the actual changes list.
284 * @param array $rows Database rows
285 * @param FormOptions $opts
287 public function outputChangesList( $rows, $opts ) {
288 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
290 $limit = $opts['limit'];
292 $showWatcherCount = $wgRCShowWatchingUsers
293 && $this->getUser()->getOption( 'shownumberswatching' );
294 $watcherCache = array();
296 $dbr = $this->getDB();
299 $list = ChangesList
::newFromContext( $this->getContext() );
300 $list->initChangesListRows( $rows );
302 $rclistOutput = $list->beginRecentChangesList();
303 foreach ( $rows as $obj ) {
307 $rc = RecentChange
::newFromRow( $obj );
308 $rc->counter
= $counter++
;
309 # Check if the page has been updated since the last visit
310 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp
) ) {
311 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
313 $rc->notificationtimestamp
= false; // Default
315 # Check the number of users watching the page
316 $rc->numberofWatchingusers
= 0; // Default
317 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
318 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
319 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
324 'wl_namespace' => $obj->rc_namespace
,
325 'wl_title' => $obj->rc_title
,
327 __METHOD__
. '-watchers'
330 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
333 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
334 if ( $changeLine !== false ) {
335 $rclistOutput .= $changeLine;
339 $rclistOutput .= $list->endRecentChangesList();
341 if ( $rows->numRows() === 0 ) {
342 $this->getOutput()->addHtml(
343 '<div class="mw-changeslist-empty">' .
344 $this->msg( 'recentchanges-noresult' )->parse() .
348 $this->getOutput()->addHTML( $rclistOutput );
353 * Set the text to be displayed above the changes
355 * @param FormOptions $opts
357 public function doHeader( $opts ) {
360 $this->setTopText( $opts );
362 $defaults = $opts->getAllValues();
363 $nondefaults = $opts->getChangedValues();
366 $panel[] = self
::makeLegend( $this->getContext() );
367 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
370 $extraOpts = $this->getExtraOptions( $opts );
371 $extraOptsCount = count( $extraOpts );
373 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
375 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
376 foreach ( $extraOpts as $name => $optionRow ) {
377 # Add submit button to the last row only
379 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
381 $out .= Xml
::openElement( 'tr' );
382 if ( is_array( $optionRow ) ) {
385 array( 'class' => 'mw-label mw-' . $name . '-label' ),
390 array( 'class' => 'mw-input' ),
391 $optionRow[1] . $addSubmit
396 array( 'class' => 'mw-input', 'colspan' => 2 ),
397 $optionRow . $addSubmit
400 $out .= Xml
::closeElement( 'tr' );
402 $out .= Xml
::closeElement( 'table' );
404 $unconsumed = $opts->getUnconsumedValues();
405 foreach ( $unconsumed as $key => $value ) {
406 $out .= Html
::hidden( $key, $value );
409 $t = $this->getPageTitle();
410 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
411 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
413 $panelString = implode( "\n", $panel );
415 $this->getOutput()->addHTML(
417 $this->msg( 'recentchanges-legend' )->text(),
419 array( 'class' => 'rcoptions' )
423 $this->setBottomText( $opts );
427 * Send the text to be displayed above the options
429 * @param FormOptions $opts Unused
431 function setTopText( FormOptions
$opts ) {
434 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
435 if ( !$message->isDisabled() ) {
436 $this->getOutput()->addWikiText(
437 Html
::rawElement( 'p',
438 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
439 "\n" . $message->plain() . "\n"
441 /* $lineStart */ false,
442 /* $interface */ false
448 * Get options to be displayed in a form
450 * @param FormOptions $opts
453 function getExtraOptions( $opts ) {
454 $opts->consumeValues( array(
455 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
458 $extraOpts = array();
459 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
461 global $wgAllowCategorizedRecentChanges;
462 if ( $wgAllowCategorizedRecentChanges ) {
463 $extraOpts['category'] = $this->categoryFilterForm( $opts );
466 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
467 if ( count( $tagFilter ) ) {
468 $extraOpts['tagfilter'] = $tagFilter;
471 // Don't fire the hook for subclasses. (Or should we?)
472 if ( $this->getName() === 'Recentchanges' ) {
473 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
480 * Add page-specific modules.
482 protected function addModules() {
483 parent
::addModules();
484 $out = $this->getOutput();
485 $out->addModules( 'mediawiki.special.recentchanges' );
489 * Get last modified date, for client caching
490 * Don't use this if we are using the patrol feature, patrol changes don't
491 * update the timestamp
493 * @return string|bool
495 public function checkLastModified() {
496 $dbr = $this->getDB();
497 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
503 * Creates the choose namespace selection
505 * @param FormOptions $opts
508 protected function namespaceFilterForm( FormOptions
$opts ) {
509 $nsSelect = Html
::namespaceSelector(
510 array( 'selected' => $opts['namespace'], 'all' => '' ),
511 array( 'name' => 'namespace', 'id' => 'namespace' )
513 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
514 $invert = Xml
::checkLabel(
515 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
517 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
519 $associated = Xml
::checkLabel(
520 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
522 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
525 return array( $nsLabel, "$nsSelect $invert $associated" );
529 * Create an input to filter changes by categories
531 * @param FormOptions $opts
534 protected function categoryFilterForm( FormOptions
$opts ) {
535 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
536 'categories', 'mw-categories', false, $opts['categories'] );
538 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
539 'categories_any', 'mw-categories_any', $opts['categories_any'] );
541 return array( $label, $input );
545 * Filter $rows by categories set in $opts
547 * @param ResultWrapper $rows Database rows
548 * @param FormOptions $opts
550 function filterByCategories( &$rows, FormOptions
$opts ) {
551 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
553 if ( !count( $categories ) ) {
559 foreach ( $categories as $cat ) {
571 foreach ( $rows as $k => $r ) {
572 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
573 $id = $nt->getArticleID();
575 continue; # Page might have been deleted...
577 if ( !in_array( $id, $articles ) ) {
580 if ( !isset( $a2r[$id] ) ) {
588 if ( !count( $articles ) ||
!count( $cats ) ) {
593 $c = new Categoryfinder
;
594 $c->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
599 foreach ( $match as $id ) {
600 foreach ( $a2r[$id] as $rev ) {
602 $newrows[$k] = $rowsarr[$k];
609 * Makes change an option link which carries all the other options
611 * @param string $title Title
612 * @param array $override Options to override
613 * @param array $options Current options
614 * @param bool $active Whether to show the link in bold
617 function makeOptionsLink( $title, $override, $options, $active = false ) {
618 $params = $override +
$options;
620 // Bug 36524: false values have be converted to "0" otherwise
621 // wfArrayToCgi() will omit it them.
622 foreach ( $params as &$value ) {
623 if ( $value === false ) {
629 $text = htmlspecialchars( $title );
631 $text = '<strong>' . $text . '</strong>';
634 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
638 * Creates the options panel.
640 * @param array $defaults
641 * @param array $nondefaults
644 function optionsPanel( $defaults, $nondefaults ) {
645 global $wgRCLinkLimits, $wgRCLinkDays;
647 $options = $nondefaults +
$defaults;
650 $msg = $this->msg( 'rclegend' );
651 if ( !$msg->isDisabled() ) {
652 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
655 $lang = $this->getLanguage();
656 $user = $this->getUser();
657 if ( $options['from'] ) {
658 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
659 $lang->userTimeAndDate( $options['from'], $user ),
660 $lang->userDate( $options['from'], $user ),
661 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
664 # Sort data for display and make sure it's unique after we've added user data.
665 $linkLimits = $wgRCLinkLimits;
666 $linkLimits[] = $options['limit'];
668 $linkLimits = array_unique( $linkLimits );
670 $linkDays = $wgRCLinkDays;
671 $linkDays[] = $options['days'];
673 $linkDays = array_unique( $linkDays );
677 foreach ( $linkLimits as $value ) {
678 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
679 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
681 $cl = $lang->pipeList( $cl );
683 // day links, reset 'from' to none
685 foreach ( $linkDays as $value ) {
686 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
687 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
689 $dl = $lang->pipeList( $dl );
693 'hideminor' => 'rcshowhideminor',
694 'hidebots' => 'rcshowhidebots',
695 'hideanons' => 'rcshowhideanons',
696 'hideliu' => 'rcshowhideliu',
697 'hidepatrolled' => 'rcshowhidepatr',
698 'hidemyself' => 'rcshowhidemine'
701 $showhide = array( 'show', 'hide' );
703 foreach ( $this->getCustomFilters() as $key => $params ) {
704 $filters[$key] = $params['msg'];
706 // Disable some if needed
707 if ( !$user->useRCPatrol() ) {
708 unset( $filters['hidepatrolled'] );
712 foreach ( $filters as $key => $msg ) {
713 // The following messages are used here:
714 // rcshowhideminor-show, rcshowhideminor-hide, rcshowhidebots-show, rcshowhidebots-hide,
715 // rcshowhideanons-show, rcshowhideanons-hide, rcshowhideliu-show, rcshowhideliu-hide,
716 // rcshowhidepatr-show, rcshowhidepatr-hide, rcshowhidemine-show, rcshowhidemine-hide.
717 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
718 // Extensions can define additional filters, but don't need to define the corresponding
719 // messages. If they don't exist, just fall back to 'show' and 'hide'.
720 if ( !$linkMessage->exists() ) {
721 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
724 $link = $this->makeOptionsLink( $linkMessage->text(),
725 array( $key => 1 - $options[$key] ), $nondefaults );
726 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
729 // show from this onward link
730 $timestamp = wfTimestampNow();
731 $now = $lang->userTimeAndDate( $timestamp, $user );
732 $timenow = $lang->userTime( $timestamp, $user );
733 $datenow = $lang->userDate( $timestamp, $user );
734 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
736 $rclistfrom = $this->makeOptionsLink(
737 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
738 array( 'from' => $timestamp ),
742 return "{$note}$rclinks<br />$rclistfrom";
745 public function isIncludable() {