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
152 * @param FormOptions $opts
155 public function buildMainQueryConds( FormOptions
$opts ) {
156 $dbr = wfGetDB( DB_SLAVE
);
159 # It makes no sense to hide both anons and logged-in users
160 # Where this occurs, force anons to be shown
162 if ( $opts['hideanons'] && $opts['hideliu'] ) {
163 # Check if the user wants to show bots only
164 if ( $opts['hidebots'] ) {
165 $opts['hideanons'] = false;
168 $opts['hidebots'] = false;
173 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
174 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
175 $cutoff = $dbr->timestamp( $cutoff_unixtime );
177 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
178 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW
, $cutoff ) ) {
179 $cutoff = $dbr->timestamp( $opts['from'] );
181 $opts->reset( 'from' );
184 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
186 $hidePatrol = $this->getUser()->useRCPatrol() && $opts['hidepatrolled'];
187 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
188 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
190 if ( $opts['hideminor'] ) {
191 $conds['rc_minor'] = 0;
193 if ( $opts['hidebots'] ) {
194 $conds['rc_bot'] = 0;
197 $conds['rc_patrolled'] = 0;
200 $conds['rc_bot'] = 1;
202 if ( $hideLoggedInUsers ) {
203 $conds[] = 'rc_user = 0';
205 if ( $hideAnonymousUsers ) {
206 $conds[] = 'rc_user != 0';
209 if ( $opts['hidemyself'] ) {
210 if ( $this->getUser()->getId() ) {
211 $conds[] = 'rc_user != ' . $dbr->addQuotes( $this->getUser()->getId() );
213 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $this->getUser()->getName() );
217 # Namespace filtering
218 if ( $opts['namespace'] !== '' ) {
219 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
220 $operator = $opts['invert'] ?
'!=' : '=';
221 $boolean = $opts['invert'] ?
'AND' : 'OR';
223 # namespace association (bug 2429)
224 if ( !$opts['associated'] ) {
225 $condition = "rc_namespace $operator $selectedNS";
227 # Also add the associated namespace
228 $associatedNS = $dbr->addQuotes(
229 MWNamespace
::getAssociated( $opts['namespace'] )
231 $condition = "(rc_namespace $operator $selectedNS "
233 . " rc_namespace $operator $associatedNS)";
236 $conds[] = $condition;
245 * @param array $conds
246 * @param FormOptions $opts
247 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
249 public function doMainQuery( $conds, $opts ) {
250 $tables = array( 'recentchanges' );
251 $join_conds = array();
252 $query_options = array();
254 $uid = $this->getUser()->getId();
255 $dbr = wfGetDB( DB_SLAVE
);
256 $limit = $opts['limit'];
257 $namespace = $opts['namespace'];
258 $invert = $opts['invert'];
259 $associated = $opts['associated'];
261 $fields = RecentChange
::selectFields();
262 // JOIN on watchlist for users
263 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
264 $tables[] = 'watchlist';
265 $fields[] = 'wl_user';
266 $fields[] = 'wl_notificationtimestamp';
267 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
270 'wl_namespace=rc_namespace'
273 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
275 $fields[] = 'page_latest';
276 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
279 ChangeTags
::modifyDisplayQuery(
288 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
289 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
294 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
295 // knowledge to use an index merge if it wants (it may use some other index though).
299 $conds +
array( 'rc_new' => array( 0, 1 ) ),
301 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
$query_options,
307 * Send output to the OutputPage object, only called if not used feeds
309 * @param array $rows Database rows
310 * @param FormOptions $opts
312 public function webOutput( $rows, $opts ) {
313 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker, $wgAllowCategorizedRecentChanges;
315 // Build the final data
317 if ( $wgAllowCategorizedRecentChanges ) {
318 $this->filterByCategories( $rows, $opts );
321 $limit = $opts['limit'];
323 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
324 $watcherCache = array();
326 $dbr = wfGetDB( DB_SLAVE
);
329 $list = ChangesList
::newFromContext( $this->getContext() );
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 ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp
) ) {
340 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
342 $rc->notificationtimestamp
= false; // Default
344 # Check the number of users watching the page
345 $rc->numberofWatchingusers
= 0; // Default
346 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
347 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
348 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
353 'wl_namespace' => $obj->rc_namespace
,
354 'wl_title' => $obj->rc_title
,
356 __METHOD__
. '-watchers'
359 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
362 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
363 if ( $changeLine !== false ) {
364 $rclistOutput .= $changeLine;
368 $rclistOutput .= $list->endRecentChangesList();
372 if ( !$this->including() ) {
373 // Output options box
374 $this->doHeader( $opts );
377 // And now for the content
378 $feedQuery = $this->getFeedQuery();
379 if ( $feedQuery !== '' ) {
380 $this->getOutput()->setFeedAppendQuery( $feedQuery );
382 $this->getOutput()->setFeedAppendQuery( false );
385 if ( $rows->numRows() === 0 ) {
386 $this->getOutput()->addHtml(
387 '<div class="mw-changeslist-empty">' . $this->msg( 'recentchanges-noresult' )->parse() . '</div>'
390 $this->getOutput()->addHTML( $rclistOutput );
395 * Return the text to be displayed above the changes
397 * @param FormOptions $opts
398 * @return string XHTML
400 public function doHeader( $opts ) {
403 $this->setTopText( $opts );
405 $defaults = $opts->getAllValues();
406 $nondefaults = $opts->getChangedValues();
409 $panel[] = self
::makeLegend( $this->getContext() );
410 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
413 $extraOpts = $this->getExtraOptions( $opts );
414 $extraOptsCount = count( $extraOpts );
416 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
418 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
419 foreach ( $extraOpts as $name => $optionRow ) {
420 # Add submit button to the last row only
422 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
424 $out .= Xml
::openElement( 'tr' );
425 if ( is_array( $optionRow ) ) {
428 array( 'class' => 'mw-label mw-' . $name . '-label' ),
433 array( 'class' => 'mw-input' ),
434 $optionRow[1] . $addSubmit
439 array( 'class' => 'mw-input', 'colspan' => 2 ),
440 $optionRow . $addSubmit
443 $out .= Xml
::closeElement( 'tr' );
445 $out .= Xml
::closeElement( 'table' );
447 $unconsumed = $opts->getUnconsumedValues();
448 foreach ( $unconsumed as $key => $value ) {
449 $out .= Html
::hidden( $key, $value );
452 $t = $this->getPageTitle();
453 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
454 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
456 $panelString = implode( "\n", $panel );
458 $this->getOutput()->addHTML(
460 $this->msg( 'recentchanges-legend' )->text(),
462 array( 'class' => 'rcoptions' )
466 $this->setBottomText( $opts );
470 * Send the text to be displayed above the options
472 * @param FormOptions $opts Unused
474 function setTopText( FormOptions
$opts ) {
477 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
478 if ( !$message->isDisabled() ) {
479 $this->getOutput()->addWikiText(
480 Html
::rawElement( 'p',
481 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
482 "\n" . $message->plain() . "\n"
484 /* $lineStart */ false,
485 /* $interface */ false
491 * Get options to be displayed in a form
493 * @param FormOptions $opts
496 function getExtraOptions( $opts ) {
497 $opts->consumeValues( array(
498 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
501 $extraOpts = array();
502 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
504 global $wgAllowCategorizedRecentChanges;
505 if ( $wgAllowCategorizedRecentChanges ) {
506 $extraOpts['category'] = $this->categoryFilterForm( $opts );
509 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
510 if ( count( $tagFilter ) ) {
511 $extraOpts['tagfilter'] = $tagFilter;
514 // Don't fire the hook for subclasses. (Or should we?)
515 if ( $this->getName() === 'Recentchanges' ) {
516 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
523 * Add page-specific modules.
525 protected function addModules() {
526 parent
::addModules();
527 $out = $this->getOutput();
528 $out->addModules( 'mediawiki.special.recentchanges' );
532 * Get last modified date, for client caching
533 * Don't use this if we are using the patrol feature, patrol changes don't
534 * update the timestamp
536 * @param string $feedFormat
537 * @return string|bool
539 public function checkLastModified( $feedFormat ) {
540 $dbr = wfGetDB( DB_SLAVE
);
541 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
542 if ( $feedFormat ||
!$this->getUser()->useRCPatrol() ) {
543 if ( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
544 # Client cache fresh and headers sent, nothing more to do.
553 * Return an array with a ChangesFeed object and ChannelFeed object.
555 * @param string $feedFormat Feed's format (either 'rss' or 'atom')
558 public function getFeedObject( $feedFormat ) {
559 $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
560 $formatter = $changesFeed->getFeedObject(
561 $this->msg( 'recentchanges' )->inContentLanguage()->text(),
562 $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
563 $this->getPageTitle()->getFullURL()
566 return array( $changesFeed, $formatter );
570 * Get the query string to append to feed link URLs.
574 public function getFeedQuery() {
577 $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
578 $options = $this->getOptions()->getChangedValues();
580 // wfArrayToCgi() omits options set to null or false
581 foreach ( $options as &$value ) {
582 if ( $value === false ) {
588 return wfArrayToCgi( $options );
592 * Creates the choose namespace selection
594 * @param FormOptions $opts
597 protected function namespaceFilterForm( FormOptions
$opts ) {
598 $nsSelect = Html
::namespaceSelector(
599 array( 'selected' => $opts['namespace'], 'all' => '' ),
600 array( 'name' => 'namespace', 'id' => 'namespace' )
602 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
603 $invert = Xml
::checkLabel(
604 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
606 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
608 $associated = Xml
::checkLabel(
609 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
611 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
614 return array( $nsLabel, "$nsSelect $invert $associated" );
618 * Create a input to filter changes by categories
620 * @param FormOptions $opts
623 protected function categoryFilterForm( FormOptions
$opts ) {
624 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
625 'categories', 'mw-categories', false, $opts['categories'] );
627 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
628 'categories_any', 'mw-categories_any', $opts['categories_any'] );
630 return array( $label, $input );
634 * Filter $rows by categories set in $opts
636 * @param ResultWrapper $rows Database rows
637 * @param FormOptions $opts
639 function filterByCategories( &$rows, FormOptions
$opts ) {
640 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
642 if ( !count( $categories ) ) {
648 foreach ( $categories as $cat ) {
660 foreach ( $rows as $k => $r ) {
661 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
662 $id = $nt->getArticleID();
664 continue; # Page might have been deleted...
666 if ( !in_array( $id, $articles ) ) {
669 if ( !isset( $a2r[$id] ) ) {
677 if ( !count( $articles ) ||
!count( $cats ) ) {
682 $c = new Categoryfinder
;
683 $c->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
688 foreach ( $match as $id ) {
689 foreach ( $a2r[$id] as $rev ) {
691 $newrows[$k] = $rowsarr[$k];
698 * Makes change an option link which carries all the other options
700 * @param string $title Title
701 * @param array $override Options to override
702 * @param array $options Current options
703 * @param bool $active Whether to show the link in bold
706 function makeOptionsLink( $title, $override, $options, $active = false ) {
707 $params = $override +
$options;
709 // Bug 36524: false values have be converted to "0" otherwise
710 // wfArrayToCgi() will omit it them.
711 foreach ( $params as &$value ) {
712 if ( $value === false ) {
718 $text = htmlspecialchars( $title );
720 $text = '<strong>' . $text . '</strong>';
723 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
727 * Creates the options panel.
729 * @param array $defaults
730 * @param array $nondefaults
733 function optionsPanel( $defaults, $nondefaults ) {
734 global $wgRCLinkLimits, $wgRCLinkDays;
736 $options = $nondefaults +
$defaults;
739 $msg = $this->msg( 'rclegend' );
740 if ( !$msg->isDisabled() ) {
741 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
744 $lang = $this->getLanguage();
745 $user = $this->getUser();
746 if ( $options['from'] ) {
747 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
748 $lang->userTimeAndDate( $options['from'], $user ),
749 $lang->userDate( $options['from'], $user ),
750 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
753 # Sort data for display and make sure it's unique after we've added user data.
754 $linkLimits = $wgRCLinkLimits;
755 $linkLimits[] = $options['limit'];
757 $linkLimits = array_unique( $linkLimits );
759 $linkDays = $wgRCLinkDays;
760 $linkDays[] = $options['days'];
762 $linkDays = array_unique( $linkDays );
766 foreach ( $linkLimits as $value ) {
767 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
768 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
770 $cl = $lang->pipeList( $cl );
772 // day links, reset 'from' to none
774 foreach ( $linkDays as $value ) {
775 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
776 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
778 $dl = $lang->pipeList( $dl );
781 $showhide = array( $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() );
783 'hideminor' => 'rcshowhideminor',
784 'hidebots' => 'rcshowhidebots',
785 'hideanons' => 'rcshowhideanons',
786 'hideliu' => 'rcshowhideliu',
787 'hidepatrolled' => 'rcshowhidepatr',
788 'hidemyself' => 'rcshowhidemine'
790 foreach ( $this->getCustomFilters() as $key => $params ) {
791 $filters[$key] = $params['msg'];
793 // Disable some if needed
794 if ( !$user->useRCPatrol() ) {
795 unset( $filters['hidepatrolled'] );
799 foreach ( $filters as $key => $msg ) {
800 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
801 array( $key => 1 - $options[$key] ), $nondefaults );
802 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
805 // show from this onward link
806 $timestamp = wfTimestampNow();
807 $now = $lang->userTimeAndDate( $timestamp, $user );
808 $tl = $this->makeOptionsLink(
809 $now, array( 'from' => $timestamp ), $nondefaults
812 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
814 $rclistfrom = $this->msg( 'rclistfrom' )->rawParams( $tl )->parse();
816 return "{$note}$rclinks<br />$rclistfrom";
819 public function isIncludable() {