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
{
31 public function __construct( $name = 'Recentchanges', $restriction = '' ) {
32 parent
::__construct( $name, $restriction );
36 * Main execution point
38 * @param string $subpage
40 public function execute( $subpage ) {
41 // Backwards-compatibility: redirect to new feed URLs
42 $feedFormat = $this->getRequest()->getVal( 'feed' );
43 if ( !$this->including() && $feedFormat ) {
44 $query = $this->getFeedQuery();
45 $query['feedformat'] = $feedFormat === 'atom' ?
'atom' : 'rss';
46 $this->getOutput()->redirect( wfAppendQuery( wfScript( 'api' ), $query ) );
50 // 10 seconds server-side caching max
51 $this->getOutput()->setSquidMaxage( 10 );
52 // Check if the client has a cached version
53 $lastmod = $this->checkLastModified();
54 if ( $lastmod === false ) {
58 parent
::execute( $subpage );
62 * Get a FormOptions object containing the default options
66 public function getDefaultOptions() {
67 $opts = parent
::getDefaultOptions();
68 $user = $this->getUser();
70 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
71 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
72 $opts->add( 'from', '' );
74 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
75 $opts->add( 'hidebots', true );
76 $opts->add( 'hideanons', false );
77 $opts->add( 'hideliu', false );
78 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
79 $opts->add( 'hidemyself', false );
81 $opts->add( 'categories', '' );
82 $opts->add( 'categories_any', false );
83 $opts->add( 'tagfilter', '' );
89 * Get custom show/hide filters
91 * @return array Map of filter URL param names to properties (msg/default)
93 protected function getCustomFilters() {
94 if ( $this->customFilters
=== null ) {
95 $this->customFilters
= array();
96 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters
) );
99 return $this->customFilters
;
103 * Process $par and put options found in $opts. Used when including the page.
106 * @param FormOptions $opts
108 public function parseParameters( $par, FormOptions
$opts ) {
109 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
110 foreach ( $bits as $bit ) {
111 if ( 'hidebots' === $bit ) {
112 $opts['hidebots'] = true;
114 if ( 'bots' === $bit ) {
115 $opts['hidebots'] = false;
117 if ( 'hideminor' === $bit ) {
118 $opts['hideminor'] = true;
120 if ( 'minor' === $bit ) {
121 $opts['hideminor'] = false;
123 if ( 'hideliu' === $bit ) {
124 $opts['hideliu'] = true;
126 if ( 'hidepatrolled' === $bit ) {
127 $opts['hidepatrolled'] = true;
129 if ( 'hideanons' === $bit ) {
130 $opts['hideanons'] = true;
132 if ( 'hidemyself' === $bit ) {
133 $opts['hidemyself'] = true;
136 if ( is_numeric( $bit ) ) {
137 $opts['limit'] = $bit;
141 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
142 $opts['limit'] = $m[1];
144 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
145 $opts['days'] = $m[1];
147 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
148 $opts['namespace'] = $m[1];
153 public function validateOptions( FormOptions
$opts ) {
154 $opts->validateIntBounds( 'limit', 0, 5000 );
155 parent
::validateOptions( $opts );
159 * Return an array of conditions depending of options set in $opts
161 * @param FormOptions $opts
164 public function buildMainQueryConds( FormOptions
$opts ) {
165 $dbr = $this->getDB();
166 $conds = parent
::buildMainQueryConds( $opts );
169 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
170 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
171 $cutoff = $dbr->timestamp( $cutoff_unixtime );
173 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
174 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW
, $cutoff ) ) {
175 $cutoff = $dbr->timestamp( $opts['from'] );
177 $opts->reset( 'from' );
180 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
188 * @param array $conds
189 * @param FormOptions $opts
190 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
192 public function doMainQuery( $conds, $opts ) {
193 global $wgAllowCategorizedRecentChanges;
195 $dbr = $this->getDB();
196 $user = $this->getUser();
198 $tables = array( 'recentchanges' );
199 $fields = RecentChange
::selectFields();
200 $query_options = array();
201 $join_conds = array();
203 // JOIN on watchlist for users
204 if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
205 $tables[] = 'watchlist';
206 $fields[] = 'wl_user';
207 $fields[] = 'wl_notificationtimestamp';
208 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
209 'wl_user' => $user->getId(),
211 'wl_namespace=rc_namespace'
215 if ( $user->isAllowed( 'rollback' ) ) {
217 $fields[] = 'page_latest';
218 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
221 ChangeTags
::modifyDisplayQuery(
230 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
231 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
236 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
237 // knowledge to use an index merge if it wants (it may use some other index though).
238 $rows = $dbr->select(
241 $conds +
array( 'rc_new' => array( 0, 1 ) ),
243 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $opts['limit'] ) +
$query_options,
247 // Build the final data
248 if ( $wgAllowCategorizedRecentChanges ) {
249 $this->filterByCategories( $rows, $opts );
255 public function outputFeedLinks() {
256 $this->addFeedLinks( $this->getFeedQuery() );
260 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
264 private function getFeedQuery() {
266 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
267 // API handles empty parameters in a different way
268 return $value !== '';
270 $query['action'] = 'feedrecentchanges';
271 if ( $query['limit'] > $wgFeedLimit ) {
272 $query['limit'] = $wgFeedLimit;
278 * Build and output the actual changes list.
280 * @param array $rows Database rows
281 * @param FormOptions $opts
283 public function outputChangesList( $rows, $opts ) {
284 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
286 $limit = $opts['limit'];
288 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
289 $watcherCache = array();
291 $dbr = $this->getDB();
294 $list = ChangesList
::newFromContext( $this->getContext() );
296 $rclistOutput = $list->beginRecentChangesList();
297 foreach ( $rows as $obj ) {
301 $rc = RecentChange
::newFromRow( $obj );
302 $rc->counter
= $counter++
;
303 # Check if the page has been updated since the last visit
304 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp
) ) {
305 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
307 $rc->notificationtimestamp
= false; // Default
309 # Check the number of users watching the page
310 $rc->numberofWatchingusers
= 0; // Default
311 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
312 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
313 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
318 'wl_namespace' => $obj->rc_namespace
,
319 'wl_title' => $obj->rc_title
,
321 __METHOD__
. '-watchers'
324 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
327 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
328 if ( $changeLine !== false ) {
329 $rclistOutput .= $changeLine;
333 $rclistOutput .= $list->endRecentChangesList();
335 if ( $rows->numRows() === 0 ) {
336 $this->getOutput()->addHtml(
337 '<div class="mw-changeslist-empty">' . $this->msg( 'recentchanges-noresult' )->parse() . '</div>'
340 $this->getOutput()->addHTML( $rclistOutput );
345 * Return the text to be displayed above the changes
347 * @param FormOptions $opts
348 * @return string XHTML
350 public function doHeader( $opts ) {
353 $this->setTopText( $opts );
355 $defaults = $opts->getAllValues();
356 $nondefaults = $opts->getChangedValues();
359 $panel[] = self
::makeLegend( $this->getContext() );
360 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
363 $extraOpts = $this->getExtraOptions( $opts );
364 $extraOptsCount = count( $extraOpts );
366 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
368 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
369 foreach ( $extraOpts as $name => $optionRow ) {
370 # Add submit button to the last row only
372 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
374 $out .= Xml
::openElement( 'tr' );
375 if ( is_array( $optionRow ) ) {
378 array( 'class' => 'mw-label mw-' . $name . '-label' ),
383 array( 'class' => 'mw-input' ),
384 $optionRow[1] . $addSubmit
389 array( 'class' => 'mw-input', 'colspan' => 2 ),
390 $optionRow . $addSubmit
393 $out .= Xml
::closeElement( 'tr' );
395 $out .= Xml
::closeElement( 'table' );
397 $unconsumed = $opts->getUnconsumedValues();
398 foreach ( $unconsumed as $key => $value ) {
399 $out .= Html
::hidden( $key, $value );
402 $t = $this->getPageTitle();
403 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
404 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
406 $panelString = implode( "\n", $panel );
408 $this->getOutput()->addHTML(
410 $this->msg( 'recentchanges-legend' )->text(),
412 array( 'class' => 'rcoptions' )
416 $this->setBottomText( $opts );
420 * Send the text to be displayed above the options
422 * @param FormOptions $opts Unused
424 function setTopText( FormOptions
$opts ) {
427 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
428 if ( !$message->isDisabled() ) {
429 $this->getOutput()->addWikiText(
430 Html
::rawElement( 'p',
431 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
432 "\n" . $message->plain() . "\n"
434 /* $lineStart */ false,
435 /* $interface */ false
441 * Get options to be displayed in a form
443 * @param FormOptions $opts
446 function getExtraOptions( $opts ) {
447 $opts->consumeValues( array(
448 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
451 $extraOpts = array();
452 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
454 global $wgAllowCategorizedRecentChanges;
455 if ( $wgAllowCategorizedRecentChanges ) {
456 $extraOpts['category'] = $this->categoryFilterForm( $opts );
459 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
460 if ( count( $tagFilter ) ) {
461 $extraOpts['tagfilter'] = $tagFilter;
464 // Don't fire the hook for subclasses. (Or should we?)
465 if ( $this->getName() === 'Recentchanges' ) {
466 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
473 * Add page-specific modules.
475 protected function addModules() {
476 parent
::addModules();
477 $out = $this->getOutput();
478 $out->addModules( 'mediawiki.special.recentchanges' );
482 * Get last modified date, for client caching
483 * Don't use this if we are using the patrol feature, patrol changes don't
484 * update the timestamp
486 * @return string|bool
488 public function checkLastModified() {
489 $dbr = $this->getDB();
490 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
495 * Creates the choose namespace selection
497 * @param FormOptions $opts
500 protected function namespaceFilterForm( FormOptions
$opts ) {
501 $nsSelect = Html
::namespaceSelector(
502 array( 'selected' => $opts['namespace'], 'all' => '' ),
503 array( 'name' => 'namespace', 'id' => 'namespace' )
505 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
506 $invert = Xml
::checkLabel(
507 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
509 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
511 $associated = Xml
::checkLabel(
512 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
514 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
517 return array( $nsLabel, "$nsSelect $invert $associated" );
521 * Create a input to filter changes by categories
523 * @param FormOptions $opts
526 protected function categoryFilterForm( FormOptions
$opts ) {
527 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
528 'categories', 'mw-categories', false, $opts['categories'] );
530 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
531 'categories_any', 'mw-categories_any', $opts['categories_any'] );
533 return array( $label, $input );
537 * Filter $rows by categories set in $opts
539 * @param ResultWrapper $rows Database rows
540 * @param FormOptions $opts
542 function filterByCategories( &$rows, FormOptions
$opts ) {
543 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
545 if ( !count( $categories ) ) {
551 foreach ( $categories as $cat ) {
563 foreach ( $rows as $k => $r ) {
564 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
565 $id = $nt->getArticleID();
567 continue; # Page might have been deleted...
569 if ( !in_array( $id, $articles ) ) {
572 if ( !isset( $a2r[$id] ) ) {
580 if ( !count( $articles ) ||
!count( $cats ) ) {
585 $c = new Categoryfinder
;
586 $c->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
591 foreach ( $match as $id ) {
592 foreach ( $a2r[$id] as $rev ) {
594 $newrows[$k] = $rowsarr[$k];
601 * Makes change an option link which carries all the other options
603 * @param string $title Title
604 * @param array $override Options to override
605 * @param array $options Current options
606 * @param bool $active Whether to show the link in bold
609 function makeOptionsLink( $title, $override, $options, $active = false ) {
610 $params = $override +
$options;
612 // Bug 36524: false values have be converted to "0" otherwise
613 // wfArrayToCgi() will omit it them.
614 foreach ( $params as &$value ) {
615 if ( $value === false ) {
621 $text = htmlspecialchars( $title );
623 $text = '<strong>' . $text . '</strong>';
626 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
630 * Creates the options panel.
632 * @param array $defaults
633 * @param array $nondefaults
636 function optionsPanel( $defaults, $nondefaults ) {
637 global $wgRCLinkLimits, $wgRCLinkDays;
639 $options = $nondefaults +
$defaults;
642 $msg = $this->msg( 'rclegend' );
643 if ( !$msg->isDisabled() ) {
644 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
647 $lang = $this->getLanguage();
648 $user = $this->getUser();
649 if ( $options['from'] ) {
650 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
651 $lang->userTimeAndDate( $options['from'], $user ),
652 $lang->userDate( $options['from'], $user ),
653 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
656 # Sort data for display and make sure it's unique after we've added user data.
657 $linkLimits = $wgRCLinkLimits;
658 $linkLimits[] = $options['limit'];
660 $linkLimits = array_unique( $linkLimits );
662 $linkDays = $wgRCLinkDays;
663 $linkDays[] = $options['days'];
665 $linkDays = array_unique( $linkDays );
669 foreach ( $linkLimits as $value ) {
670 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
671 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
673 $cl = $lang->pipeList( $cl );
675 // day links, reset 'from' to none
677 foreach ( $linkDays as $value ) {
678 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
679 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
681 $dl = $lang->pipeList( $dl );
685 'hideminor' => 'rcshowhideminor',
686 'hidebots' => 'rcshowhidebots',
687 'hideanons' => 'rcshowhideanons',
688 'hideliu' => 'rcshowhideliu',
689 'hidepatrolled' => 'rcshowhidepatr',
690 'hidemyself' => 'rcshowhidemine'
693 $showhide = array( 'show', 'hide' );
695 foreach ( $this->getCustomFilters() as $key => $params ) {
696 $filters[$key] = $params['msg'];
698 // Disable some if needed
699 if ( !$user->useRCPatrol() ) {
700 unset( $filters['hidepatrolled'] );
704 foreach ( $filters as $key => $msg ) {
705 // The following messages are used here:
706 // rcshowhideminor-show, rcshowhideminor-hide, rcshowhidebots-show, rcshowhidebots-hide,
707 // rcshowhideanons-show, rcshowhideanons-hide, rcshowhideliu-show, rcshowhideliu-hide,
708 // rcshowhidepatr-show, rcshowhidepatr-hide, rcshowhidemine-show, rcshowhidemine-hide.
709 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
710 // Extensions can define additional filters, but don't need to define the corresponding
711 // messages. If they don't exist, just fall back to 'show' and 'hide'.
712 if ( !$linkMessage->exists() ) {
713 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
716 $link = $this->makeOptionsLink( $linkMessage->text(),
717 array( $key => 1 - $options[$key] ), $nondefaults );
718 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
721 // show from this onward link
722 $timestamp = wfTimestampNow();
723 $now = $lang->userTimeAndDate( $timestamp, $user );
724 $timenow = $lang->userTime( $timestamp, $user );
725 $datenow = $lang->userDate( $timestamp, $user );
726 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
728 $rclistfrom = $this->makeOptionsLink(
729 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
730 array( 'from' => $timestamp ),
734 return "{$note}$rclinks<br />$rclistfrom";
737 public function isIncludable() {