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 * The feed format to output as (either 'rss' or 'atom'), or null if no
32 * feed output was requested
34 * @var string $feedFormat
36 protected $feedFormat;
38 public function __construct( $name = 'Recentchanges', $restriction = '' ) {
39 parent
::__construct( $name, $restriction );
42 public function isIncludable() {
47 * Get a FormOptions object containing the default options
51 public function getDefaultOptions() {
52 $opts = parent
::getDefaultOptions();
53 $user = $this->getUser();
55 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
56 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
57 $opts->add( 'from', '' );
59 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
60 $opts->add( 'hidebots', true );
61 $opts->add( 'hideanons', false );
62 $opts->add( 'hideliu', false );
63 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
64 $opts->add( 'hidemyself', false );
66 $opts->add( 'categories', '' );
67 $opts->add( 'categories_any', false );
68 $opts->add( 'tagfilter', '' );
73 public function validateOptions( FormOptions
$opts ) {
75 $opts->validateIntBounds( 'limit', 0, $this->feedFormat ?
$wgFeedLimit : 5000 );
79 * Get custom show/hide filters
81 * @return array Map of filter URL param names to properties (msg/default)
83 protected function getCustomFilters() {
84 if ( $this->customFilters
=== null ) {
85 $this->customFilters
= array();
86 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters
) );
89 return $this->customFilters
;
93 * Main execution point
95 * @param string $subpage
97 public function execute( $subpage ) {
98 $this->rcSubpage
= $subpage;
99 $this->feedFormat
= $this->including() ?
null : $this->getRequest()->getVal( 'feed' );
101 # 10 seconds server-side caching max
102 $this->getOutput()->setSquidMaxage( 10 );
103 # Check if the client has a cached version
104 $lastmod = $this->checkLastModified( $this->feedFormat
);
105 if ( $lastmod === false ) {
109 $opts = $this->getOptions();
111 $this->outputHeader();
114 // Fetch results, prepare a batch link existence check query
115 $conds = $this->buildMainQueryConds( $opts );
116 $rows = $this->doMainQuery( $conds, $opts );
117 if ( $rows === false ) {
118 if ( !$this->including() ) {
119 $this->doHeader( $opts );
125 if ( !$this->feedFormat
) {
126 $batch = new LinkBatch
;
127 foreach ( $rows as $row ) {
128 $batch->add( NS_USER
, $row->rc_user_text
);
129 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
130 $batch->add( $row->rc_namespace
, $row->rc_title
);
134 if ( $this->feedFormat
) {
135 list( $changesFeed, $formatter ) = $this->getFeedObject( $this->feedFormat
);
136 /** @var ChangesFeed $changesFeed */
137 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
139 $this->webOutput( $rows, $opts );
146 * Return an array with a ChangesFeed object and ChannelFeed object
148 * @param string $feedFormat Feed's format (either 'rss' or 'atom')
151 public function getFeedObject( $feedFormat ) {
152 $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
153 $formatter = $changesFeed->getFeedObject(
154 $this->msg( 'recentchanges' )->inContentLanguage()->text(),
155 $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
156 $this->getPageTitle()->getFullURL()
159 return array( $changesFeed, $formatter );
163 * Process $par and put options found in $opts. Used when including the page.
166 * @param FormOptions $opts
168 public function parseParameters( $par, FormOptions
$opts ) {
169 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
170 foreach ( $bits as $bit ) {
171 if ( 'hidebots' === $bit ) {
172 $opts['hidebots'] = true;
174 if ( 'bots' === $bit ) {
175 $opts['hidebots'] = false;
177 if ( 'hideminor' === $bit ) {
178 $opts['hideminor'] = true;
180 if ( 'minor' === $bit ) {
181 $opts['hideminor'] = false;
183 if ( 'hideliu' === $bit ) {
184 $opts['hideliu'] = true;
186 if ( 'hidepatrolled' === $bit ) {
187 $opts['hidepatrolled'] = true;
189 if ( 'hideanons' === $bit ) {
190 $opts['hideanons'] = true;
192 if ( 'hidemyself' === $bit ) {
193 $opts['hidemyself'] = true;
196 if ( is_numeric( $bit ) ) {
197 $opts['limit'] = $bit;
201 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
202 $opts['limit'] = $m[1];
204 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
205 $opts['days'] = $m[1];
207 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
208 $opts['namespace'] = $m[1];
214 * Get last modified date, for client caching
215 * Don't use this if we are using the patrol feature, patrol changes don't
216 * update the timestamp
218 * @param string $feedFormat
219 * @return string|bool
221 public function checkLastModified( $feedFormat ) {
222 $dbr = wfGetDB( DB_SLAVE
);
223 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
224 if ( $feedFormat ||
!$this->getUser()->useRCPatrol() ) {
225 if ( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
226 # Client cache fresh and headers sent, nothing more to do.
235 * Return an array of conditions depending of options set in $opts
237 * @param FormOptions $opts
240 public function buildMainQueryConds( FormOptions
$opts ) {
241 $dbr = wfGetDB( DB_SLAVE
);
244 # It makes no sense to hide both anons and logged-in users
245 # Where this occurs, force anons to be shown
247 if ( $opts['hideanons'] && $opts['hideliu'] ) {
248 # Check if the user wants to show bots only
249 if ( $opts['hidebots'] ) {
250 $opts['hideanons'] = false;
253 $opts['hidebots'] = false;
258 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
259 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime %
86400 );
260 $cutoff = $dbr->timestamp( $cutoff_unixtime );
262 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
263 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW
, $cutoff ) ) {
264 $cutoff = $dbr->timestamp( $opts['from'] );
266 $opts->reset( 'from' );
269 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
271 $hidePatrol = $this->getUser()->useRCPatrol() && $opts['hidepatrolled'];
272 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
273 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
275 if ( $opts['hideminor'] ) {
276 $conds['rc_minor'] = 0;
278 if ( $opts['hidebots'] ) {
279 $conds['rc_bot'] = 0;
282 $conds['rc_patrolled'] = 0;
285 $conds['rc_bot'] = 1;
287 if ( $hideLoggedInUsers ) {
288 $conds[] = 'rc_user = 0';
290 if ( $hideAnonymousUsers ) {
291 $conds[] = 'rc_user != 0';
294 if ( $opts['hidemyself'] ) {
295 if ( $this->getUser()->getId() ) {
296 $conds[] = 'rc_user != ' . $dbr->addQuotes( $this->getUser()->getId() );
298 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $this->getUser()->getName() );
302 # Namespace filtering
303 if ( $opts['namespace'] !== '' ) {
304 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
305 $operator = $opts['invert'] ?
'!=' : '=';
306 $boolean = $opts['invert'] ?
'AND' : 'OR';
308 # namespace association (bug 2429)
309 if ( !$opts['associated'] ) {
310 $condition = "rc_namespace $operator $selectedNS";
312 # Also add the associated namespace
313 $associatedNS = $dbr->addQuotes(
314 MWNamespace
::getAssociated( $opts['namespace'] )
316 $condition = "(rc_namespace $operator $selectedNS "
318 . " rc_namespace $operator $associatedNS)";
321 $conds[] = $condition;
330 * @param array $conds
331 * @param FormOptions $opts
332 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
334 public function doMainQuery( $conds, $opts ) {
335 $tables = array( 'recentchanges' );
336 $join_conds = array();
337 $query_options = array();
339 $uid = $this->getUser()->getId();
340 $dbr = wfGetDB( DB_SLAVE
);
341 $limit = $opts['limit'];
342 $namespace = $opts['namespace'];
343 $invert = $opts['invert'];
344 $associated = $opts['associated'];
346 $fields = RecentChange
::selectFields();
347 // JOIN on watchlist for users
348 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
349 $tables[] = 'watchlist';
350 $fields[] = 'wl_user';
351 $fields[] = 'wl_notificationtimestamp';
352 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
355 'wl_namespace=rc_namespace'
358 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
360 $fields[] = 'page_latest';
361 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
364 ChangeTags
::modifyDisplayQuery(
373 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
374 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
379 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
380 // knowledge to use an index merge if it wants (it may use some other index though).
384 $conds +
array( 'rc_new' => array( 0, 1 ) ),
386 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
$query_options,
392 * Send output to the OutputPage object, only called if not used feeds
394 * @param array $rows Database rows
395 * @param FormOptions $opts
397 public function webOutput( $rows, $opts ) {
398 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker, $wgAllowCategorizedRecentChanges;
400 // Build the final data
402 if ( $wgAllowCategorizedRecentChanges ) {
403 $this->filterByCategories( $rows, $opts );
406 $limit = $opts['limit'];
408 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
409 $watcherCache = array();
411 $dbr = wfGetDB( DB_SLAVE
);
414 $list = ChangesList
::newFromContext( $this->getContext() );
416 $rclistOutput = $list->beginRecentChangesList();
417 foreach ( $rows as $obj ) {
421 $rc = RecentChange
::newFromRow( $obj );
422 $rc->counter
= $counter++
;
423 # Check if the page has been updated since the last visit
424 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp
) ) {
425 $rc->notificationtimestamp
= ( $obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
427 $rc->notificationtimestamp
= false; // Default
429 # Check the number of users watching the page
430 $rc->numberofWatchingusers
= 0; // Default
431 if ( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
432 if ( !isset( $watcherCache[$obj->rc_namespace
][$obj->rc_title
] ) ) {
433 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
438 'wl_namespace' => $obj->rc_namespace
,
439 'wl_title' => $obj->rc_title
,
441 __METHOD__
. '-watchers'
444 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
447 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
448 if ( $changeLine !== false ) {
449 $rclistOutput .= $changeLine;
453 $rclistOutput .= $list->endRecentChangesList();
457 if ( !$this->including() ) {
458 // Output options box
459 $this->doHeader( $opts );
462 // And now for the content
463 $feedQuery = $this->getFeedQuery();
464 if ( $feedQuery !== '' ) {
465 $this->getOutput()->setFeedAppendQuery( $feedQuery );
467 $this->getOutput()->setFeedAppendQuery( false );
470 if ( $rows->numRows() === 0 ) {
471 $this->getOutput()->addHtml(
472 '<div class="mw-changeslist-empty">' . $this->msg( 'recentchanges-noresult' )->parse() . '</div>'
475 $this->getOutput()->addHTML( $rclistOutput );
480 * Get the query string to append to feed link URLs.
484 public function getFeedQuery() {
487 $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
488 $options = $this->getOptions()->getChangedValues();
490 // wfArrayToCgi() omits options set to null or false
491 foreach ( $options as &$value ) {
492 if ( $value === false ) {
498 return wfArrayToCgi( $options );
502 * Return the text to be displayed above the changes
504 * @param FormOptions $opts
505 * @return string XHTML
507 public function doHeader( $opts ) {
510 $this->setTopText( $opts );
512 $defaults = $opts->getAllValues();
513 $nondefaults = $opts->getChangedValues();
516 $panel[] = self
::makeLegend( $this->getContext() );
517 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
520 $extraOpts = $this->getExtraOptions( $opts );
521 $extraOptsCount = count( $extraOpts );
523 $submit = ' ' . Xml
::submitbutton( $this->msg( 'allpagessubmit' )->text() );
525 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
526 foreach ( $extraOpts as $name => $optionRow ) {
527 # Add submit button to the last row only
529 $addSubmit = ( $count === $extraOptsCount ) ?
$submit : '';
531 $out .= Xml
::openElement( 'tr' );
532 if ( is_array( $optionRow ) ) {
535 array( 'class' => 'mw-label mw-' . $name . '-label' ),
540 array( 'class' => 'mw-input' ),
541 $optionRow[1] . $addSubmit
546 array( 'class' => 'mw-input', 'colspan' => 2 ),
547 $optionRow . $addSubmit
550 $out .= Xml
::closeElement( 'tr' );
552 $out .= Xml
::closeElement( 'table' );
554 $unconsumed = $opts->getUnconsumedValues();
555 foreach ( $unconsumed as $key => $value ) {
556 $out .= Html
::hidden( $key, $value );
559 $t = $this->getPageTitle();
560 $out .= Html
::hidden( 'title', $t->getPrefixedText() );
561 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
563 $panelString = implode( "\n", $panel );
565 $this->getOutput()->addHTML(
567 $this->msg( 'recentchanges-legend' )->text(),
569 array( 'class' => 'rcoptions' )
573 $this->setBottomText( $opts );
577 * Get options to be displayed in a form
579 * @param FormOptions $opts
582 function getExtraOptions( $opts ) {
583 $opts->consumeValues( array(
584 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
587 $extraOpts = array();
588 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
590 global $wgAllowCategorizedRecentChanges;
591 if ( $wgAllowCategorizedRecentChanges ) {
592 $extraOpts['category'] = $this->categoryFilterForm( $opts );
595 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
596 if ( count( $tagFilter ) ) {
597 $extraOpts['tagfilter'] = $tagFilter;
600 // Don't fire the hook for subclasses. (Or should we?)
601 if ( $this->getName() === 'Recentchanges' ) {
602 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
609 * Send the text to be displayed above the options
611 * @param FormOptions $opts Unused
613 function setTopText( FormOptions
$opts ) {
616 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
617 if ( !$message->isDisabled() ) {
618 $this->getOutput()->addWikiText(
619 Html
::rawElement( 'p',
620 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
621 "\n" . $message->plain() . "\n"
623 /* $lineStart */ false,
624 /* $interface */ false
630 * Creates the choose namespace selection
632 * @param FormOptions $opts
635 protected function namespaceFilterForm( FormOptions
$opts ) {
636 $nsSelect = Html
::namespaceSelector(
637 array( 'selected' => $opts['namespace'], 'all' => '' ),
638 array( 'name' => 'namespace', 'id' => 'namespace' )
640 $nsLabel = Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' );
641 $invert = Xml
::checkLabel(
642 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
644 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
646 $associated = Xml
::checkLabel(
647 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
649 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
652 return array( $nsLabel, "$nsSelect $invert $associated" );
656 * Create a input to filter changes by categories
658 * @param FormOptions $opts
661 protected function categoryFilterForm( FormOptions
$opts ) {
662 list( $label, $input ) = Xml
::inputLabelSep( $this->msg( 'rc_categories' )->text(),
663 'categories', 'mw-categories', false, $opts['categories'] );
665 $input .= ' ' . Xml
::checkLabel( $this->msg( 'rc_categories_any' )->text(),
666 'categories_any', 'mw-categories_any', $opts['categories_any'] );
668 return array( $label, $input );
672 * Filter $rows by categories set in $opts
674 * @param array $rows Database rows
675 * @param FormOptions $opts
677 function filterByCategories( &$rows, FormOptions
$opts ) {
678 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
680 if ( !count( $categories ) ) {
686 foreach ( $categories as $cat ) {
698 foreach ( $rows as $k => $r ) {
699 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
700 $id = $nt->getArticleID();
702 continue; # Page might have been deleted...
704 if ( !in_array( $id, $articles ) ) {
707 if ( !isset( $a2r[$id] ) ) {
715 if ( !count( $articles ) ||
!count( $cats ) ) {
720 $c = new Categoryfinder
;
721 $c->seed( $articles, $cats, $opts['categories_any'] ?
'OR' : 'AND' );
726 foreach ( $match as $id ) {
727 foreach ( $a2r[$id] as $rev ) {
729 $newrows[$k] = $rowsarr[$k];
736 * Makes change an option link which carries all the other options
738 * @param string $title Title
739 * @param array $override Options to override
740 * @param array $options Current options
741 * @param bool $active Whether to show the link in bold
744 function makeOptionsLink( $title, $override, $options, $active = false ) {
745 $params = $override +
$options;
747 // Bug 36524: false values have be converted to "0" otherwise
748 // wfArrayToCgi() will omit it them.
749 foreach ( $params as &$value ) {
750 if ( $value === false ) {
756 $text = htmlspecialchars( $title );
758 $text = '<strong>' . $text . '</strong>';
761 return Linker
::linkKnown( $this->getPageTitle(), $text, array(), $params );
765 * Creates the options panel.
767 * @param array $defaults
768 * @param array $nondefaults
771 function optionsPanel( $defaults, $nondefaults ) {
772 global $wgRCLinkLimits, $wgRCLinkDays;
774 $options = $nondefaults +
$defaults;
777 $msg = $this->msg( 'rclegend' );
778 if ( !$msg->isDisabled() ) {
779 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
782 $lang = $this->getLanguage();
783 $user = $this->getUser();
784 if ( $options['from'] ) {
785 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
786 $lang->userTimeAndDate( $options['from'], $user ),
787 $lang->userDate( $options['from'], $user ),
788 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
791 # Sort data for display and make sure it's unique after we've added user data.
792 $linkLimits = $wgRCLinkLimits;
793 $linkLimits[] = $options['limit'];
795 $linkLimits = array_unique( $linkLimits );
797 $linkDays = $wgRCLinkDays;
798 $linkDays[] = $options['days'];
800 $linkDays = array_unique( $linkDays );
804 foreach ( $linkLimits as $value ) {
805 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
806 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
808 $cl = $lang->pipeList( $cl );
810 // day links, reset 'from' to none
812 foreach ( $linkDays as $value ) {
813 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
814 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
816 $dl = $lang->pipeList( $dl );
819 $showhide = array( $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() );
821 'hideminor' => 'rcshowhideminor',
822 'hidebots' => 'rcshowhidebots',
823 'hideanons' => 'rcshowhideanons',
824 'hideliu' => 'rcshowhideliu',
825 'hidepatrolled' => 'rcshowhidepatr',
826 'hidemyself' => 'rcshowhidemine'
828 foreach ( $this->getCustomFilters() as $key => $params ) {
829 $filters[$key] = $params['msg'];
831 // Disable some if needed
832 if ( !$user->useRCPatrol() ) {
833 unset( $filters['hidepatrolled'] );
837 foreach ( $filters as $key => $msg ) {
838 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
839 array( $key => 1 - $options[$key] ), $nondefaults );
840 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
843 // show from this onward link
844 $timestamp = wfTimestampNow();
845 $now = $lang->userTimeAndDate( $timestamp, $user );
846 $tl = $this->makeOptionsLink(
847 $now, array( 'from' => $timestamp ), $nondefaults
850 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
852 $rclistfrom = $this->msg( 'rclistfrom' )->rawParams( $tl )->parse();
854 return "{$note}$rclinks<br />$rclistfrom";
858 * Add page-specific modules.
860 protected function addModules() {
861 parent
::addModules();
862 $out = $this->getOutput();
863 $out->addModules( 'mediawiki.special.recentchanges' );