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 // 10 seconds server-side caching max
42 $this->getOutput()->setSquidMaxage( 10 );
43 // Check if the client has a cached version
44 $lastmod = $this->checkLastModified( $this->feedFormat
);
45 if ( $lastmod === false ) {
49 parent
::execute( $subpage );
53 * Get a FormOptions object containing the default options
57 public function getDefaultOptions() {
58 $opts = parent
::getDefaultOptions();
59 $user = $this->getUser();
61 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
62 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
63 $opts->add( 'from', '' );
65 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
66 $opts->add( 'hidebots', true );
67 $opts->add( 'hideanons', false );
68 $opts->add( 'hideliu', false );
69 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
70 $opts->add( 'hidemyself', false );
72 $opts->add( 'categories', '' );
73 $opts->add( 'categories_any', false );
74 $opts->add( 'tagfilter', '' );
80 * Get custom show/hide filters
82 * @return array Map of filter URL param names to properties (msg/default)
84 protected function getCustomFilters() {
85 if ( $this->customFilters
=== null ) {
86 $this->customFilters
= array();
87 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters
) );
90 return $this->customFilters
;
94 * Process $par and put options found in $opts. Used when including the page.
97 * @param FormOptions $opts
99 public function parseParameters( $par, FormOptions
$opts ) {
100 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
101 foreach ( $bits as $bit ) {
102 if ( 'hidebots' === $bit ) {
103 $opts['hidebots'] = true;
105 if ( 'bots' === $bit ) {
106 $opts['hidebots'] = false;
108 if ( 'hideminor' === $bit ) {
109 $opts['hideminor'] = true;
111 if ( 'minor' === $bit ) {
112 $opts['hideminor'] = false;
114 if ( 'hideliu' === $bit ) {
115 $opts['hideliu'] = true;
117 if ( 'hidepatrolled' === $bit ) {
118 $opts['hidepatrolled'] = true;
120 if ( 'hideanons' === $bit ) {
121 $opts['hideanons'] = true;
123 if ( 'hidemyself' === $bit ) {
124 $opts['hidemyself'] = true;
127 if ( is_numeric( $bit ) ) {
128 $opts['limit'] = $bit;
132 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
133 $opts['limit'] = $m[1];
135 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
136 $opts['days'] = $m[1];
138 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
139 $opts['namespace'] = $m[1];
144 public function validateOptions( FormOptions
$opts ) {
146 $opts->validateIntBounds( 'limit', 0, $this->feedFormat ?
$wgFeedLimit : 5000 );
147 parent
::validateOptions( $opts );
151 * Return an array of conditions depending of options set in $opts
153 * @param FormOptions $opts
156 public function buildMainQueryConds( FormOptions
$opts ) {
157 $dbr = $this->getDB();
158 $conds = parent
::buildMainQueryConds( $opts );
161 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
162 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
163 $cutoff = $dbr->timestamp( $cutoff_unixtime );
165 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
166 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW
, $cutoff ) ) {
167 $cutoff = $dbr->timestamp( $opts['from'] );
169 $opts->reset( 'from' );
172 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
180 * @param array $conds
181 * @param FormOptions $opts
182 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
184 public function doMainQuery( $conds, $opts ) {
185 global $wgAllowCategorizedRecentChanges;
187 $dbr = $this->getDB();
188 $user = $this->getUser();
190 $tables = array( 'recentchanges' );
191 $fields = RecentChange
::selectFields();
192 $query_options = array();
193 $join_conds = array();
195 // JOIN on watchlist for users
196 if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
197 $tables[] = 'watchlist';
198 $fields[] = 'wl_user';
199 $fields[] = 'wl_notificationtimestamp';
200 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
201 'wl_user' => $user->getId(),
203 'wl_namespace=rc_namespace'
207 if ( $user->isAllowed( 'rollback' ) ) {
209 $fields[] = 'page_latest';
210 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
213 ChangeTags
::modifyDisplayQuery(
222 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
223 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
228 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
229 // knowledge to use an index merge if it wants (it may use some other index though).
230 $rows = $dbr->select(
233 $conds +
array( 'rc_new' => array( 0, 1 ) ),
235 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $opts['limit'] ) +
$query_options,
239 // Build the final data
240 if ( $wgAllowCategorizedRecentChanges ) {
241 $this->filterByCategories( $rows, $opts );
250 public function outputFeedLinks() {
251 $feedQuery = $this->getFeedQuery();
252 if ( $feedQuery !== '' ) {
253 $this->getOutput()->setFeedAppendQuery( $feedQuery );
255 $this->getOutput()->setFeedAppendQuery( false );
260 * Build and output the actual changes list.
262 * @param array $rows Database rows
263 * @param FormOptions $opts
265 public function outputChangesList( $rows, $opts ) {
266 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
268 $limit = $opts['limit'];
270 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
271 $watcherCache = array();
273 $dbr = $this->getDB();
276 $list = ChangesList
::newFromContext( $this->getContext() );
278 $rclistOutput = $list->beginRecentChangesList();
279 foreach ( $rows as $obj ) {
283 $rc = RecentChange
::newFromRow( $obj );
284 $rc->counter
= $counter++
;
285 # Check if the page has been updated since the last visit
286 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp
) ) {
287 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
289 $rc->notificationtimestamp
= false; // Default
291 # Check the number of users watching the page
292 $rc->numberofWatchingusers
= 0; // Default
293 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
294 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
295 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
300 'wl_namespace' => $obj->rc_namespace
,
301 'wl_title' => $obj->rc_title
,
303 __METHOD__
. '-watchers'
306 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
309 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
310 if ( $changeLine !== false ) {
311 $rclistOutput .= $changeLine;
315 $rclistOutput .= $list->endRecentChangesList();
317 if ( $rows->numRows() === 0 ) {
318 $this->getOutput()->addHtml(
319 '<div class="mw-changeslist-empty">' . $this->msg( 'recentchanges-noresult' )->parse() . '</div>'
322 $this->getOutput()->addHTML( $rclistOutput );
327 * Return the text to be displayed above the changes
329 * @param FormOptions $opts
330 * @return string XHTML
332 public function doHeader( $opts ) {
335 $this->setTopText( $opts );
337 $defaults = $opts->getAllValues();
338 $nondefaults = $opts->getChangedValues();
341 $panel[] = self
::makeLegend( $this->getContext() );
342 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
345 $extraOpts = $this->getExtraOptions( $opts );
346 $extraOptsCount = count( $extraOpts );
348 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
350 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
351 foreach ( $extraOpts as $name => $optionRow ) {
352 # Add submit button to the last row only
354 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
356 $out .= Xml
::openElement( 'tr' );
357 if ( is_array( $optionRow ) ) {
360 array( 'class' => 'mw-label mw-' . $name . '-label' ),
365 array( 'class' => 'mw-input' ),
366 $optionRow[1] . $addSubmit
371 array( 'class' => 'mw-input', 'colspan' => 2 ),
372 $optionRow . $addSubmit
375 $out .= Xml
::closeElement( 'tr' );
377 $out .= Xml
::closeElement( 'table' );
379 $unconsumed = $opts->getUnconsumedValues();
380 foreach ( $unconsumed as $key => $value ) {
381 $out .= Html
::hidden( $key, $value );
384 $t = $this->getPageTitle();
385 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
386 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
388 $panelString = implode( "\n", $panel );
390 $this->getOutput()->addHTML(
392 $this->msg( 'recentchanges-legend' )->text(),
394 array( 'class' => 'rcoptions' )
398 $this->setBottomText( $opts );
402 * Send the text to be displayed above the options
404 * @param FormOptions $opts Unused
406 function setTopText( FormOptions
$opts ) {
409 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
410 if ( !$message->isDisabled() ) {
411 $this->getOutput()->addWikiText(
412 Html
::rawElement( 'p',
413 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
414 "\n" . $message->plain() . "\n"
416 /* $lineStart */ false,
417 /* $interface */ false
423 * Get options to be displayed in a form
425 * @param FormOptions $opts
428 function getExtraOptions( $opts ) {
429 $opts->consumeValues( array(
430 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
433 $extraOpts = array();
434 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
436 global $wgAllowCategorizedRecentChanges;
437 if ( $wgAllowCategorizedRecentChanges ) {
438 $extraOpts['category'] = $this->categoryFilterForm( $opts );
441 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
442 if ( count( $tagFilter ) ) {
443 $extraOpts['tagfilter'] = $tagFilter;
446 // Don't fire the hook for subclasses. (Or should we?)
447 if ( $this->getName() === 'Recentchanges' ) {
448 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
455 * Add page-specific modules.
457 protected function addModules() {
458 parent
::addModules();
459 $out = $this->getOutput();
460 $out->addModules( 'mediawiki.special.recentchanges' );
464 * Get last modified date, for client caching
465 * Don't use this if we are using the patrol feature, patrol changes don't
466 * update the timestamp
468 * @param string $feedFormat
469 * @return string|bool
471 public function checkLastModified( $feedFormat ) {
472 $dbr = $this->getDB();
473 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
474 if ( $feedFormat ||
!$this->getUser()->useRCPatrol() ) {
475 if ( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
476 # Client cache fresh and headers sent, nothing more to do.
485 * Return an array with a ChangesFeed object and ChannelFeed object.
487 * @param string $feedFormat Feed's format (either 'rss' or 'atom')
490 public function getFeedObject( $feedFormat ) {
491 $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
492 $formatter = $changesFeed->getFeedObject(
493 $this->msg( 'recentchanges' )->inContentLanguage()->text(),
494 $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
495 $this->getPageTitle()->getFullURL()
498 return array( $changesFeed, $formatter );
502 * Get the query string to append to feed link URLs.
506 public function getFeedQuery() {
509 $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
510 $options = $this->getOptions()->getChangedValues();
512 // wfArrayToCgi() omits options set to null or false
513 foreach ( $options as &$value ) {
514 if ( $value === false ) {
520 return wfArrayToCgi( $options );
524 * Creates the choose namespace selection
526 * @param FormOptions $opts
529 protected function namespaceFilterForm( FormOptions
$opts ) {
530 $nsSelect = Html
::namespaceSelector(
531 array( 'selected' => $opts['namespace'], 'all' => '' ),
532 array( 'name' => 'namespace', 'id' => 'namespace' )
534 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
535 $invert = Xml
::checkLabel(
536 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
538 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
540 $associated = Xml
::checkLabel(
541 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
543 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
546 return array( $nsLabel, "$nsSelect $invert $associated" );
550 * Create a input to filter changes by categories
552 * @param FormOptions $opts
555 protected function categoryFilterForm( FormOptions
$opts ) {
556 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
557 'categories', 'mw-categories', false, $opts['categories'] );
559 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
560 'categories_any', 'mw-categories_any', $opts['categories_any'] );
562 return array( $label, $input );
566 * Filter $rows by categories set in $opts
568 * @param ResultWrapper $rows Database rows
569 * @param FormOptions $opts
571 function filterByCategories( &$rows, FormOptions
$opts ) {
572 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
574 if ( !count( $categories ) ) {
580 foreach ( $categories as $cat ) {
592 foreach ( $rows as $k => $r ) {
593 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
594 $id = $nt->getArticleID();
596 continue; # Page might have been deleted...
598 if ( !in_array( $id, $articles ) ) {
601 if ( !isset( $a2r[$id] ) ) {
609 if ( !count( $articles ) ||
!count( $cats ) ) {
614 $c = new Categoryfinder
;
615 $c->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
620 foreach ( $match as $id ) {
621 foreach ( $a2r[$id] as $rev ) {
623 $newrows[$k] = $rowsarr[$k];
630 * Makes change an option link which carries all the other options
632 * @param string $title Title
633 * @param array $override Options to override
634 * @param array $options Current options
635 * @param bool $active Whether to show the link in bold
638 function makeOptionsLink( $title, $override, $options, $active = false ) {
639 $params = $override +
$options;
641 // Bug 36524: false values have be converted to "0" otherwise
642 // wfArrayToCgi() will omit it them.
643 foreach ( $params as &$value ) {
644 if ( $value === false ) {
650 $text = htmlspecialchars( $title );
652 $text = '<strong>' . $text . '</strong>';
655 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
659 * Creates the options panel.
661 * @param array $defaults
662 * @param array $nondefaults
665 function optionsPanel( $defaults, $nondefaults ) {
666 global $wgRCLinkLimits, $wgRCLinkDays;
668 $options = $nondefaults +
$defaults;
671 $msg = $this->msg( 'rclegend' );
672 if ( !$msg->isDisabled() ) {
673 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
676 $lang = $this->getLanguage();
677 $user = $this->getUser();
678 if ( $options['from'] ) {
679 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
680 $lang->userTimeAndDate( $options['from'], $user ),
681 $lang->userDate( $options['from'], $user ),
682 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
685 # Sort data for display and make sure it's unique after we've added user data.
686 $linkLimits = $wgRCLinkLimits;
687 $linkLimits[] = $options['limit'];
689 $linkLimits = array_unique( $linkLimits );
691 $linkDays = $wgRCLinkDays;
692 $linkDays[] = $options['days'];
694 $linkDays = array_unique( $linkDays );
698 foreach ( $linkLimits as $value ) {
699 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
700 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
702 $cl = $lang->pipeList( $cl );
704 // day links, reset 'from' to none
706 foreach ( $linkDays as $value ) {
707 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
708 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
710 $dl = $lang->pipeList( $dl );
713 $showhide = array( $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() );
715 'hideminor' => 'rcshowhideminor',
716 'hidebots' => 'rcshowhidebots',
717 'hideanons' => 'rcshowhideanons',
718 'hideliu' => 'rcshowhideliu',
719 'hidepatrolled' => 'rcshowhidepatr',
720 'hidemyself' => 'rcshowhidemine'
722 foreach ( $this->getCustomFilters() as $key => $params ) {
723 $filters[$key] = $params['msg'];
725 // Disable some if needed
726 if ( !$user->useRCPatrol() ) {
727 unset( $filters['hidepatrolled'] );
731 foreach ( $filters as $key => $msg ) {
732 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
733 array( $key => 1 - $options[$key] ), $nondefaults );
734 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
737 // show from this onward link
738 $timestamp = wfTimestampNow();
739 $now = $lang->userTimeAndDate( $timestamp, $user );
740 $tl = $this->makeOptionsLink(
741 $now, array( 'from' => $timestamp ), $nondefaults
744 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
746 $rclistfrom = $this->msg( 'rclistfrom' )->rawParams( $tl )->parse();
748 return "{$note}$rclinks<br />$rclistfrom";
751 public function isIncludable() {