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 );
150 * Return an array of conditions depending of options set in $opts
151 * @todo Whyyyy is this mutating $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 $tables = array( 'recentchanges' );
186 $join_conds = array();
187 $query_options = array();
189 $uid = $this->getUser()->getId();
190 $dbr = $this->getDB();
191 $limit = $opts['limit'];
192 $namespace = $opts['namespace'];
193 $invert = $opts['invert'];
194 $associated = $opts['associated'];
196 $fields = RecentChange
::selectFields();
197 // JOIN on watchlist for users
198 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
199 $tables[] = 'watchlist';
200 $fields[] = 'wl_user';
201 $fields[] = 'wl_notificationtimestamp';
202 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
205 'wl_namespace=rc_namespace'
208 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
210 $fields[] = 'page_latest';
211 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
214 ChangeTags
::modifyDisplayQuery(
223 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
224 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
229 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
230 // knowledge to use an index merge if it wants (it may use some other index though).
234 $conds +
array( 'rc_new' => array( 0, 1 ) ),
236 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
$query_options,
242 * Send output to the OutputPage object, only called if not used feeds
244 * @param array $rows Database rows
245 * @param FormOptions $opts
247 public function webOutput( $rows, $opts ) {
248 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker, $wgAllowCategorizedRecentChanges;
250 // Build the final data
252 if ( $wgAllowCategorizedRecentChanges ) {
253 $this->filterByCategories( $rows, $opts );
256 $limit = $opts['limit'];
258 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
259 $watcherCache = array();
261 $dbr = $this->getDB();
264 $list = ChangesList
::newFromContext( $this->getContext() );
266 $rclistOutput = $list->beginRecentChangesList();
267 foreach ( $rows as $obj ) {
271 $rc = RecentChange
::newFromRow( $obj );
272 $rc->counter
= $counter++
;
273 # Check if the page has been updated since the last visit
274 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp
) ) {
275 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
277 $rc->notificationtimestamp
= false; // Default
279 # Check the number of users watching the page
280 $rc->numberofWatchingusers
= 0; // Default
281 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
282 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
283 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
288 'wl_namespace' => $obj->rc_namespace
,
289 'wl_title' => $obj->rc_title
,
291 __METHOD__
. '-watchers'
294 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
297 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
298 if ( $changeLine !== false ) {
299 $rclistOutput .= $changeLine;
303 $rclistOutput .= $list->endRecentChangesList();
307 if ( !$this->including() ) {
308 // Output options box
309 $this->doHeader( $opts );
312 // And now for the content
313 $feedQuery = $this->getFeedQuery();
314 if ( $feedQuery !== '' ) {
315 $this->getOutput()->setFeedAppendQuery( $feedQuery );
317 $this->getOutput()->setFeedAppendQuery( false );
320 if ( $rows->numRows() === 0 ) {
321 $this->getOutput()->addHtml(
322 '<div class="mw-changeslist-empty">' . $this->msg( 'recentchanges-noresult' )->parse() . '</div>'
325 $this->getOutput()->addHTML( $rclistOutput );
330 * Return the text to be displayed above the changes
332 * @param FormOptions $opts
333 * @return string XHTML
335 public function doHeader( $opts ) {
338 $this->setTopText( $opts );
340 $defaults = $opts->getAllValues();
341 $nondefaults = $opts->getChangedValues();
344 $panel[] = self
::makeLegend( $this->getContext() );
345 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
348 $extraOpts = $this->getExtraOptions( $opts );
349 $extraOptsCount = count( $extraOpts );
351 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
353 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
354 foreach ( $extraOpts as $name => $optionRow ) {
355 # Add submit button to the last row only
357 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
359 $out .= Xml
::openElement( 'tr' );
360 if ( is_array( $optionRow ) ) {
363 array( 'class' => 'mw-label mw-' . $name . '-label' ),
368 array( 'class' => 'mw-input' ),
369 $optionRow[1] . $addSubmit
374 array( 'class' => 'mw-input', 'colspan' => 2 ),
375 $optionRow . $addSubmit
378 $out .= Xml
::closeElement( 'tr' );
380 $out .= Xml
::closeElement( 'table' );
382 $unconsumed = $opts->getUnconsumedValues();
383 foreach ( $unconsumed as $key => $value ) {
384 $out .= Html
::hidden( $key, $value );
387 $t = $this->getPageTitle();
388 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
389 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
391 $panelString = implode( "\n", $panel );
393 $this->getOutput()->addHTML(
395 $this->msg( 'recentchanges-legend' )->text(),
397 array( 'class' => 'rcoptions' )
401 $this->setBottomText( $opts );
405 * Send the text to be displayed above the options
407 * @param FormOptions $opts Unused
409 function setTopText( FormOptions
$opts ) {
412 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
413 if ( !$message->isDisabled() ) {
414 $this->getOutput()->addWikiText(
415 Html
::rawElement( 'p',
416 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
417 "\n" . $message->plain() . "\n"
419 /* $lineStart */ false,
420 /* $interface */ false
426 * Get options to be displayed in a form
428 * @param FormOptions $opts
431 function getExtraOptions( $opts ) {
432 $opts->consumeValues( array(
433 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
436 $extraOpts = array();
437 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
439 global $wgAllowCategorizedRecentChanges;
440 if ( $wgAllowCategorizedRecentChanges ) {
441 $extraOpts['category'] = $this->categoryFilterForm( $opts );
444 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
445 if ( count( $tagFilter ) ) {
446 $extraOpts['tagfilter'] = $tagFilter;
449 // Don't fire the hook for subclasses. (Or should we?)
450 if ( $this->getName() === 'Recentchanges' ) {
451 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
458 * Add page-specific modules.
460 protected function addModules() {
461 parent
::addModules();
462 $out = $this->getOutput();
463 $out->addModules( 'mediawiki.special.recentchanges' );
467 * Get last modified date, for client caching
468 * Don't use this if we are using the patrol feature, patrol changes don't
469 * update the timestamp
471 * @param string $feedFormat
472 * @return string|bool
474 public function checkLastModified( $feedFormat ) {
475 $dbr = $this->getDB();
476 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
477 if ( $feedFormat ||
!$this->getUser()->useRCPatrol() ) {
478 if ( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
479 # Client cache fresh and headers sent, nothing more to do.
488 * Return an array with a ChangesFeed object and ChannelFeed object.
490 * @param string $feedFormat Feed's format (either 'rss' or 'atom')
493 public function getFeedObject( $feedFormat ) {
494 $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
495 $formatter = $changesFeed->getFeedObject(
496 $this->msg( 'recentchanges' )->inContentLanguage()->text(),
497 $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
498 $this->getPageTitle()->getFullURL()
501 return array( $changesFeed, $formatter );
505 * Get the query string to append to feed link URLs.
509 public function getFeedQuery() {
512 $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
513 $options = $this->getOptions()->getChangedValues();
515 // wfArrayToCgi() omits options set to null or false
516 foreach ( $options as &$value ) {
517 if ( $value === false ) {
523 return wfArrayToCgi( $options );
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 a 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 $c = new Categoryfinder
;
618 $c->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
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
668 function optionsPanel( $defaults, $nondefaults ) {
669 global $wgRCLinkLimits, $wgRCLinkDays;
671 $options = $nondefaults +
$defaults;
674 $msg = $this->msg( 'rclegend' );
675 if ( !$msg->isDisabled() ) {
676 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
679 $lang = $this->getLanguage();
680 $user = $this->getUser();
681 if ( $options['from'] ) {
682 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
683 $lang->userTimeAndDate( $options['from'], $user ),
684 $lang->userDate( $options['from'], $user ),
685 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
688 # Sort data for display and make sure it's unique after we've added user data.
689 $linkLimits = $wgRCLinkLimits;
690 $linkLimits[] = $options['limit'];
692 $linkLimits = array_unique( $linkLimits );
694 $linkDays = $wgRCLinkDays;
695 $linkDays[] = $options['days'];
697 $linkDays = array_unique( $linkDays );
701 foreach ( $linkLimits as $value ) {
702 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
703 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
705 $cl = $lang->pipeList( $cl );
707 // day links, reset 'from' to none
709 foreach ( $linkDays as $value ) {
710 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
711 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
713 $dl = $lang->pipeList( $dl );
716 $showhide = array( $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() );
718 'hideminor' => 'rcshowhideminor',
719 'hidebots' => 'rcshowhidebots',
720 'hideanons' => 'rcshowhideanons',
721 'hideliu' => 'rcshowhideliu',
722 'hidepatrolled' => 'rcshowhidepatr',
723 'hidemyself' => 'rcshowhidemine'
725 foreach ( $this->getCustomFilters() as $key => $params ) {
726 $filters[$key] = $params['msg'];
728 // Disable some if needed
729 if ( !$user->useRCPatrol() ) {
730 unset( $filters['hidepatrolled'] );
734 foreach ( $filters as $key => $msg ) {
735 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
736 array( $key => 1 - $options[$key] ), $nondefaults );
737 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
740 // show from this onward link
741 $timestamp = wfTimestampNow();
742 $now = $lang->userTimeAndDate( $timestamp, $user );
743 $tl = $this->makeOptionsLink(
744 $now, array( 'from' => $timestamp ), $nondefaults
747 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
749 $rclistfrom = $this->msg( 'rclistfrom' )->rawParams( $tl )->parse();
751 return "{$note}$rclinks<br />$rclistfrom";
754 public function isIncludable() {