Make legend on Special:RecentChanges and Special:Watchlist collapsible
[mediawiki.git] / includes / specials / SpecialRecentchanges.php
blob3b121fb8174d5cc9e9cf47ba4d1b1f8492e48124
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that lists last changes made to the wiki
27 * @ingroup SpecialPage
29 class SpecialRecentChanges extends IncludableSpecialPage {
30 var $rcOptions, $rcSubpage;
31 protected $customFilters;
33 public function __construct( $name = 'Recentchanges' ) {
34 parent::__construct( $name );
37 /**
38 * Get a FormOptions object containing the default options
40 * @return FormOptions
42 public function getDefaultOptions() {
43 $opts = new FormOptions();
44 $user = $this->getUser();
46 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
47 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
48 $opts->add( 'from', '' );
50 $opts->add( 'hideminor', $user->getBoolOption( 'hideminor' ) );
51 $opts->add( 'hidebots', true );
52 $opts->add( 'hideanons', false );
53 $opts->add( 'hideliu', false );
54 $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
55 $opts->add( 'hidemyself', false );
57 $opts->add( 'namespace', '', FormOptions::INTNULL );
58 $opts->add( 'invert', false );
59 $opts->add( 'associated', false );
61 $opts->add( 'categories', '' );
62 $opts->add( 'categories_any', false );
63 $opts->add( 'tagfilter', '' );
65 return $opts;
68 /**
69 * Create a FormOptions object with options as specified by the user
71 * @param array $parameters
73 * @return FormOptions
75 public function setup( $parameters ) {
76 $opts = $this->getDefaultOptions();
78 foreach ( $this->getCustomFilters() as $key => $params ) {
79 $opts->add( $key, $params['default'] );
82 $opts->fetchValuesFromRequest( $this->getRequest() );
84 // Give precedence to subpage syntax
85 if ( $parameters !== null ) {
86 $this->parseParameters( $parameters, $opts );
89 $opts->validateIntBounds( 'limit', 0, 5000 );
91 return $opts;
94 /**
95 * Get custom show/hide filters
97 * @return array Map of filter URL param names to properties (msg/default)
99 protected function getCustomFilters() {
100 if ( $this->customFilters === null ) {
101 $this->customFilters = array();
102 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters ) );
105 return $this->customFilters;
109 * Create a FormOptions object specific for feed requests and return it
111 * @return FormOptions
113 public function feedSetup() {
114 global $wgFeedLimit;
115 $opts = $this->getDefaultOptions();
116 $opts->fetchValuesFromRequest( $this->getRequest() );
117 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
119 return $opts;
123 * Get the current FormOptions for this request
125 public function getOptions() {
126 if ( $this->rcOptions === null ) {
127 if ( $this->including() ) {
128 $isFeed = false;
129 } else {
130 $isFeed = (bool)$this->getRequest()->getVal( 'feed' );
132 $this->rcOptions = $isFeed ? $this->feedSetup() : $this->setup( $this->rcSubpage );
135 return $this->rcOptions;
139 * Main execution point
141 * @param string $subpage
143 public function execute( $subpage ) {
144 $this->rcSubpage = $subpage;
145 $feedFormat = $this->including() ? null : $this->getRequest()->getVal( 'feed' );
147 # 10 seconds server-side caching max
148 $this->getOutput()->setSquidMaxage( 10 );
149 # Check if the client has a cached version
150 $lastmod = $this->checkLastModified( $feedFormat );
151 if ( $lastmod === false ) {
152 return;
155 $opts = $this->getOptions();
156 $this->setHeaders();
157 $this->outputHeader();
158 $this->addModules();
160 // Fetch results, prepare a batch link existence check query
161 $conds = $this->buildMainQueryConds( $opts );
162 $rows = $this->doMainQuery( $conds, $opts );
163 if ( $rows === false ) {
164 if ( !$this->including() ) {
165 $this->doHeader( $opts );
168 return;
171 if ( !$feedFormat ) {
172 $batch = new LinkBatch;
173 foreach ( $rows as $row ) {
174 $batch->add( NS_USER, $row->rc_user_text );
175 $batch->add( NS_USER_TALK, $row->rc_user_text );
176 $batch->add( $row->rc_namespace, $row->rc_title );
178 $batch->execute();
180 if ( $feedFormat ) {
181 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
182 /** @var ChangesFeed $changesFeed */
183 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
184 } else {
185 $this->webOutput( $rows, $opts );
188 $rows->free();
192 * Return an array with a ChangesFeed object and ChannelFeed object
194 * @param string $feedFormat Feed's format (either 'rss' or 'atom')
195 * @return array
197 public function getFeedObject( $feedFormat ) {
198 $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
199 $formatter = $changesFeed->getFeedObject(
200 $this->msg( 'recentchanges' )->inContentLanguage()->text(),
201 $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
202 $this->getTitle()->getFullURL()
205 return array( $changesFeed, $formatter );
209 * Process $par and put options found if $opts
210 * Mainly used when including the page
212 * @param string $par
213 * @param FormOptions $opts
215 public function parseParameters( $par, FormOptions $opts ) {
216 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
217 foreach ( $bits as $bit ) {
218 if ( 'hidebots' === $bit ) {
219 $opts['hidebots'] = true;
221 if ( 'bots' === $bit ) {
222 $opts['hidebots'] = false;
224 if ( 'hideminor' === $bit ) {
225 $opts['hideminor'] = true;
227 if ( 'minor' === $bit ) {
228 $opts['hideminor'] = false;
230 if ( 'hideliu' === $bit ) {
231 $opts['hideliu'] = true;
233 if ( 'hidepatrolled' === $bit ) {
234 $opts['hidepatrolled'] = true;
236 if ( 'hideanons' === $bit ) {
237 $opts['hideanons'] = true;
239 if ( 'hidemyself' === $bit ) {
240 $opts['hidemyself'] = true;
243 if ( is_numeric( $bit ) ) {
244 $opts['limit'] = $bit;
247 $m = array();
248 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
249 $opts['limit'] = $m[1];
251 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
252 $opts['days'] = $m[1];
254 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
255 $opts['namespace'] = $m[1];
261 * Get last modified date, for client caching
262 * Don't use this if we are using the patrol feature, patrol changes don't
263 * update the timestamp
265 * @param string $feedFormat
266 * @return string|bool
268 public function checkLastModified( $feedFormat ) {
269 $dbr = wfGetDB( DB_SLAVE );
270 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
271 if ( $feedFormat || !$this->getUser()->useRCPatrol() ) {
272 if ( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
273 # Client cache fresh and headers sent, nothing more to do.
274 return false;
278 return $lastmod;
282 * Return an array of conditions depending of options set in $opts
284 * @param FormOptions $opts
285 * @return array
287 public function buildMainQueryConds( FormOptions $opts ) {
288 $dbr = wfGetDB( DB_SLAVE );
289 $conds = array();
291 # It makes no sense to hide both anons and logged-in users
292 # Where this occurs, force anons to be shown
293 $forcebot = false;
294 if ( $opts['hideanons'] && $opts['hideliu'] ) {
295 # Check if the user wants to show bots only
296 if ( $opts['hidebots'] ) {
297 $opts['hideanons'] = false;
298 } else {
299 $forcebot = true;
300 $opts['hidebots'] = false;
304 // Calculate cutoff
305 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
306 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime % 86400 );
307 $cutoff = $dbr->timestamp( $cutoff_unixtime );
309 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
310 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW, $cutoff ) ) {
311 $cutoff = $dbr->timestamp( $opts['from'] );
312 } else {
313 $opts->reset( 'from' );
316 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
318 $hidePatrol = $this->getUser()->useRCPatrol() && $opts['hidepatrolled'];
319 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
320 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
322 if ( $opts['hideminor'] ) {
323 $conds['rc_minor'] = 0;
325 if ( $opts['hidebots'] ) {
326 $conds['rc_bot'] = 0;
328 if ( $hidePatrol ) {
329 $conds['rc_patrolled'] = 0;
331 if ( $forcebot ) {
332 $conds['rc_bot'] = 1;
334 if ( $hideLoggedInUsers ) {
335 $conds[] = 'rc_user = 0';
337 if ( $hideAnonymousUsers ) {
338 $conds[] = 'rc_user != 0';
341 if ( $opts['hidemyself'] ) {
342 if ( $this->getUser()->getId() ) {
343 $conds[] = 'rc_user != ' . $dbr->addQuotes( $this->getUser()->getId() );
344 } else {
345 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $this->getUser()->getName() );
349 # Namespace filtering
350 if ( $opts['namespace'] !== '' ) {
351 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
352 $operator = $opts['invert'] ? '!=' : '=';
353 $boolean = $opts['invert'] ? 'AND' : 'OR';
355 # namespace association (bug 2429)
356 if ( !$opts['associated'] ) {
357 $condition = "rc_namespace $operator $selectedNS";
358 } else {
359 # Also add the associated namespace
360 $associatedNS = $dbr->addQuotes(
361 MWNamespace::getAssociated( $opts['namespace'] )
363 $condition = "(rc_namespace $operator $selectedNS "
364 . $boolean
365 . " rc_namespace $operator $associatedNS)";
368 $conds[] = $condition;
371 return $conds;
375 * Process the query
377 * @param array $conds
378 * @param FormOptions $opts
379 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
381 public function doMainQuery( $conds, $opts ) {
382 $tables = array( 'recentchanges' );
383 $join_conds = array();
384 $query_options = array();
386 $uid = $this->getUser()->getId();
387 $dbr = wfGetDB( DB_SLAVE );
388 $limit = $opts['limit'];
389 $namespace = $opts['namespace'];
390 $invert = $opts['invert'];
391 $associated = $opts['associated'];
393 $fields = RecentChange::selectFields();
394 // JOIN on watchlist for users
395 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
396 $tables[] = 'watchlist';
397 $fields[] = 'wl_user';
398 $fields[] = 'wl_notificationtimestamp';
399 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
400 'wl_user' => $uid,
401 'wl_title=rc_title',
402 'wl_namespace=rc_namespace'
403 ) );
405 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
406 $tables[] = 'page';
407 $fields[] = 'page_latest';
408 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
410 // Tag stuff.
411 ChangeTags::modifyDisplayQuery(
412 $tables,
413 $fields,
414 $conds,
415 $join_conds,
416 $query_options,
417 $opts['tagfilter']
420 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
421 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
423 return false;
426 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
427 // knowledge to use an index merge if it wants (it may use some other index though).
428 return $dbr->select(
429 $tables,
430 $fields,
431 $conds + array( 'rc_new' => array( 0, 1 ) ),
432 __METHOD__,
433 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) + $query_options,
434 $join_conds
439 * Send output to the OutputPage object, only called if not used feeds
441 * @param array $rows Database rows
442 * @param FormOptions $opts
444 public function webOutput( $rows, $opts ) {
445 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker, $wgAllowCategorizedRecentChanges;
447 // Build the final data
449 if ( $wgAllowCategorizedRecentChanges ) {
450 $this->filterByCategories( $rows, $opts );
453 $limit = $opts['limit'];
455 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
456 $watcherCache = array();
458 $dbr = wfGetDB( DB_SLAVE );
460 $counter = 1;
461 $list = ChangesList::newFromContext( $this->getContext() );
463 $rclistOutput = $list->beginRecentChangesList();
464 foreach ( $rows as $obj ) {
465 if ( $limit == 0 ) {
466 break;
468 $rc = RecentChange::newFromRow( $obj );
469 $rc->counter = $counter++;
470 # Check if the page has been updated since the last visit
471 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp ) ) {
472 $rc->notificationtimestamp = ( $obj->rc_timestamp >= $obj->wl_notificationtimestamp );
473 } else {
474 $rc->notificationtimestamp = false; // Default
476 # Check the number of users watching the page
477 $rc->numberofWatchingusers = 0; // Default
478 if ( $showWatcherCount && $obj->rc_namespace >= 0 ) {
479 if ( !isset( $watcherCache[$obj->rc_namespace][$obj->rc_title] ) ) {
480 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
481 $dbr->selectField(
482 'watchlist',
483 'COUNT(*)',
484 array(
485 'wl_namespace' => $obj->rc_namespace,
486 'wl_title' => $obj->rc_title,
488 __METHOD__ . '-watchers'
491 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
494 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
495 if ( $changeLine !== false ) {
496 $rclistOutput .= $changeLine;
497 --$limit;
500 $rclistOutput .= $list->endRecentChangesList();
502 // Print things out
504 if ( !$this->including() ) {
505 // Output options box
506 $this->doHeader( $opts );
509 // And now for the content
510 $feedQuery = $this->getFeedQuery();
511 if ( $feedQuery !== '' ) {
512 $this->getOutput()->setFeedAppendQuery( $feedQuery );
513 } else {
514 $this->getOutput()->setFeedAppendQuery( false );
517 if ( $rows->numRows() === 0 ) {
518 $this->getOutput()->addHtml(
519 '<div class="mw-changeslist-empty">' . $this->msg( 'recentchanges-noresult' )->parse() . '</div>'
521 } else {
522 $this->getOutput()->addHTML( $rclistOutput );
527 * Get the query string to append to feed link URLs.
529 * @return string
531 public function getFeedQuery() {
532 global $wgFeedLimit;
534 $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
535 $options = $this->getOptions()->getChangedValues();
537 // wfArrayToCgi() omits options set to null or false
538 foreach ( $options as &$value ) {
539 if ( $value === false ) {
540 $value = '0';
543 unset( $value );
545 return wfArrayToCgi( $options );
549 * Return the text to be displayed above the changes
551 * @param FormOptions $opts
552 * @return string XHTML
554 public function doHeader( $opts ) {
555 global $wgScript;
557 $this->setTopText( $opts );
559 $defaults = $opts->getAllValues();
560 $nondefaults = $opts->getChangedValues();
562 $panel = array();
563 $panel[] = self::makeLegend( $this->getContext() );
564 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
565 $panel[] = '<hr />';
567 $extraOpts = $this->getExtraOptions( $opts );
568 $extraOptsCount = count( $extraOpts );
569 $count = 0;
570 $submit = ' ' . Xml::submitbutton( $this->msg( 'allpagessubmit' )->text() );
572 $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
573 foreach ( $extraOpts as $name => $optionRow ) {
574 # Add submit button to the last row only
575 ++$count;
576 $addSubmit = ( $count === $extraOptsCount ) ? $submit : '';
578 $out .= Xml::openElement( 'tr' );
579 if ( is_array( $optionRow ) ) {
580 $out .= Xml::tags(
581 'td',
582 array( 'class' => 'mw-label mw-' . $name . '-label' ),
583 $optionRow[0]
585 $out .= Xml::tags(
586 'td',
587 array( 'class' => 'mw-input' ),
588 $optionRow[1] . $addSubmit
590 } else {
591 $out .= Xml::tags(
592 'td',
593 array( 'class' => 'mw-input', 'colspan' => 2 ),
594 $optionRow . $addSubmit
597 $out .= Xml::closeElement( 'tr' );
599 $out .= Xml::closeElement( 'table' );
601 $unconsumed = $opts->getUnconsumedValues();
602 foreach ( $unconsumed as $key => $value ) {
603 $out .= Html::hidden( $key, $value );
606 $t = $this->getTitle();
607 $out .= Html::hidden( 'title', $t->getPrefixedText() );
608 $form = Xml::tags( 'form', array( 'action' => $wgScript ), $out );
609 $panel[] = $form;
610 $panelString = implode( "\n", $panel );
612 $this->getOutput()->addHTML(
613 Xml::fieldset(
614 $this->msg( 'recentchanges-legend' )->text(),
615 $panelString,
616 array( 'class' => 'rcoptions' )
620 $this->setBottomText( $opts );
624 * Get options to be displayed in a form
626 * @param FormOptions $opts
627 * @return array
629 function getExtraOptions( $opts ) {
630 $opts->consumeValues( array(
631 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
632 ) );
634 $extraOpts = array();
635 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
637 global $wgAllowCategorizedRecentChanges;
638 if ( $wgAllowCategorizedRecentChanges ) {
639 $extraOpts['category'] = $this->categoryFilterForm( $opts );
642 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
643 if ( count( $tagFilter ) ) {
644 $extraOpts['tagfilter'] = $tagFilter;
647 // Don't fire the hook for subclasses. (Or should we?)
648 if ( $this->getName() === 'Recentchanges' ) {
649 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
652 return $extraOpts;
656 * Return the legend displayed within the fieldset.
658 * This method is also called from SpecialWatchlist.
660 * @param $context the object available as $this in non-static functions
661 * @return string
663 public static function makeLegend( IContextSource $context ) {
664 global $wgRecentChangesFlags;
665 $user = $context->getUser();
666 # The legend showing what the letters and stuff mean
667 $legend = Xml::openElement( 'dl' ) . "\n";
668 # Iterates through them and gets the messages for both letter and tooltip
669 $legendItems = $wgRecentChangesFlags;
670 if ( !$user->useRCPatrol() ) {
671 unset( $legendItems['unpatrolled'] );
673 foreach ( $legendItems as $key => $legendInfo ) { # generate items of the legend
674 $label = $legendInfo['title'];
675 $letter = $legendInfo['letter'];
676 $cssClass = isset( $legendInfo['class'] ) ? $legendInfo['class'] : $key;
678 $legend .= Xml::element( 'dt',
679 array( 'class' => $cssClass ), $context->msg( $letter )->text()
680 ) . "\n";
681 if ( $key === 'newpage' ) {
682 $legend .= Xml::openElement( 'dd' );
683 $legend .= $context->msg( $label )->escaped();
684 $legend .= ' ' . $context->msg( 'recentchanges-legend-newpage' )->parse();
685 $legend .= Xml::closeElement( 'dd' ) . "\n";
686 } else {
687 $legend .= Xml::element( 'dd', array(),
688 $context->msg( $label )->text()
689 ) . "\n";
692 # (+-123)
693 $legend .= Xml::tags( 'dt',
694 array( 'class' => 'mw-plusminus-pos' ),
695 $context->msg( 'recentchanges-legend-plusminus' )->parse()
696 ) . "\n";
697 $legend .= Xml::element(
698 'dd',
699 array( 'class' => 'mw-changeslist-legend-plusminus' ),
700 $context->msg( 'recentchanges-label-plusminus' )->text()
701 ) . "\n";
702 $legend .= Xml::closeElement( 'dl' ) . "\n";
704 # Collapsibility
705 $legend =
706 '<div class="mw-changeslist-legend">' .
707 $context->msg( 'recentchanges-legend-heading' )->parse() .
708 '<div class="mw-collapsible-content">' . $legend . '</div>' .
709 '</div>';
711 return $legend;
715 * Send the text to be displayed above the options
717 * @param FormOptions $opts Unused
719 function setTopText( FormOptions $opts ) {
720 global $wgContLang;
722 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
723 if ( !$message->isDisabled() ) {
724 $this->getOutput()->addWikiText(
725 Html::rawElement( 'p',
726 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
727 "\n" . $message->plain() . "\n"
729 /* $lineStart */ false,
730 /* $interface */ false
736 * Send the text to be displayed after the options, for use in subclasses.
738 * @param FormOptions $opts
740 function setBottomText( FormOptions $opts ) {
744 * Creates the choose namespace selection
746 * @param FormOptions $opts
747 * @return string
749 protected function namespaceFilterForm( FormOptions $opts ) {
750 $nsSelect = Html::namespaceSelector(
751 array( 'selected' => $opts['namespace'], 'all' => '' ),
752 array( 'name' => 'namespace', 'id' => 'namespace' )
754 $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
755 $invert = Xml::checkLabel(
756 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
757 $opts['invert'],
758 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
760 $associated = Xml::checkLabel(
761 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
762 $opts['associated'],
763 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
766 return array( $nsLabel, "$nsSelect $invert $associated" );
770 * Create a input to filter changes by categories
772 * @param FormOptions $opts
773 * @return array
775 protected function categoryFilterForm( FormOptions $opts ) {
776 list( $label, $input ) = Xml::inputLabelSep( $this->msg( 'rc_categories' )->text(),
777 'categories', 'mw-categories', false, $opts['categories'] );
779 $input .= ' ' . Xml::checkLabel( $this->msg( 'rc_categories_any' )->text(),
780 'categories_any', 'mw-categories_any', $opts['categories_any'] );
782 return array( $label, $input );
786 * Filter $rows by categories set in $opts
788 * @param array $rows Database rows
789 * @param FormOptions $opts
791 function filterByCategories( &$rows, FormOptions $opts ) {
792 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
794 if ( !count( $categories ) ) {
795 return;
798 # Filter categories
799 $cats = array();
800 foreach ( $categories as $cat ) {
801 $cat = trim( $cat );
802 if ( $cat == '' ) {
803 continue;
805 $cats[] = $cat;
808 # Filter articles
809 $articles = array();
810 $a2r = array();
811 $rowsarr = array();
812 foreach ( $rows as $k => $r ) {
813 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
814 $id = $nt->getArticleID();
815 if ( $id == 0 ) {
816 continue; # Page might have been deleted...
818 if ( !in_array( $id, $articles ) ) {
819 $articles[] = $id;
821 if ( !isset( $a2r[$id] ) ) {
822 $a2r[$id] = array();
824 $a2r[$id][] = $k;
825 $rowsarr[$k] = $r;
828 # Shortcut?
829 if ( !count( $articles ) || !count( $cats ) ) {
830 return;
833 # Look up
834 $c = new Categoryfinder;
835 $c->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
836 $match = $c->run();
838 # Filter
839 $newrows = array();
840 foreach ( $match as $id ) {
841 foreach ( $a2r[$id] as $rev ) {
842 $k = $rev;
843 $newrows[$k] = $rowsarr[$k];
846 $rows = $newrows;
850 * Makes change an option link which carries all the other options
852 * @param string $title Title
853 * @param array $override Options to override
854 * @param array $options Current options
855 * @param bool $active Whether to show the link in bold
856 * @return string
858 function makeOptionsLink( $title, $override, $options, $active = false ) {
859 $params = $override + $options;
861 // Bug 36524: false values have be converted to "0" otherwise
862 // wfArrayToCgi() will omit it them.
863 foreach ( $params as &$value ) {
864 if ( $value === false ) {
865 $value = '0';
868 unset( $value );
870 $text = htmlspecialchars( $title );
871 if ( $active ) {
872 $text = '<strong>' . $text . '</strong>';
875 return Linker::linkKnown( $this->getTitle(), $text, array(), $params );
879 * Creates the options panel.
881 * @param array $defaults
882 * @param array $nondefaults
883 * @return string
885 function optionsPanel( $defaults, $nondefaults ) {
886 global $wgRCLinkLimits, $wgRCLinkDays;
888 $options = $nondefaults + $defaults;
890 $note = '';
891 $msg = $this->msg( 'rclegend' );
892 if ( !$msg->isDisabled() ) {
893 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
896 $lang = $this->getLanguage();
897 $user = $this->getUser();
898 if ( $options['from'] ) {
899 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
900 $lang->userTimeAndDate( $options['from'], $user ),
901 $lang->userDate( $options['from'], $user ),
902 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
905 # Sort data for display and make sure it's unique after we've added user data.
906 $linkLimits = $wgRCLinkLimits;
907 $linkLimits[] = $options['limit'];
908 sort( $linkLimits );
909 $linkLimits = array_unique( $linkLimits );
911 $linkDays = $wgRCLinkDays;
912 $linkDays[] = $options['days'];
913 sort( $linkDays );
914 $linkDays = array_unique( $linkDays );
916 // limit links
917 $cl = array();
918 foreach ( $linkLimits as $value ) {
919 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
920 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
922 $cl = $lang->pipeList( $cl );
924 // day links, reset 'from' to none
925 $dl = array();
926 foreach ( $linkDays as $value ) {
927 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
928 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
930 $dl = $lang->pipeList( $dl );
932 // show/hide links
933 $showhide = array( $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() );
934 $filters = array(
935 'hideminor' => 'rcshowhideminor',
936 'hidebots' => 'rcshowhidebots',
937 'hideanons' => 'rcshowhideanons',
938 'hideliu' => 'rcshowhideliu',
939 'hidepatrolled' => 'rcshowhidepatr',
940 'hidemyself' => 'rcshowhidemine'
942 foreach ( $this->getCustomFilters() as $key => $params ) {
943 $filters[$key] = $params['msg'];
945 // Disable some if needed
946 if ( !$user->useRCPatrol() ) {
947 unset( $filters['hidepatrolled'] );
950 $links = array();
951 foreach ( $filters as $key => $msg ) {
952 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
953 array( $key => 1 - $options[$key] ), $nondefaults );
954 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
957 // show from this onward link
958 $timestamp = wfTimestampNow();
959 $now = $lang->userTimeAndDate( $timestamp, $user );
960 $tl = $this->makeOptionsLink(
961 $now, array( 'from' => $timestamp ), $nondefaults
964 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
965 ->parse();
966 $rclistfrom = $this->msg( 'rclistfrom' )->rawParams( $tl )->parse();
968 return "{$note}$rclinks<br />$rclistfrom";
972 * Add page-specific modules.
974 protected function addModules() {
975 $this->getOutput()->addModules( array(
976 'mediawiki.special.recentchanges',
977 ) );
980 protected function getGroupName() {
981 return 'changes';