Merge "Make setUp and tearDown protected in tests"
[lhc/web/wiklou.git] / includes / specials / SpecialRecentchanges.php
1 <?php
2 /**
3 * Implements Special:Recentchanges
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that lists last changes made to the wiki
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialRecentChanges extends IncludableSpecialPage {
30 var $rcOptions, $rcSubpage;
31 protected $customFilters;
32
33 public function __construct( $name = 'Recentchanges' ) {
34 parent::__construct( $name );
35 }
36
37 /**
38 * Get a FormOptions object containing the default options
39 *
40 * @return FormOptions
41 */
42 public function getDefaultOptions() {
43 $opts = new FormOptions();
44 $user = $this->getUser();
45
46 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
47 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
48 $opts->add( 'from', '' );
49
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 );
56
57 $opts->add( 'namespace', '', FormOptions::INTNULL );
58 $opts->add( 'invert', false );
59 $opts->add( 'associated', false );
60
61 $opts->add( 'categories', '' );
62 $opts->add( 'categories_any', false );
63 $opts->add( 'tagfilter', '' );
64
65 return $opts;
66 }
67
68 /**
69 * Create a FormOptions object with options as specified by the user
70 *
71 * @param array $parameters
72 *
73 * @return FormOptions
74 */
75 public function setup( $parameters ) {
76 $opts = $this->getDefaultOptions();
77
78 foreach ( $this->getCustomFilters() as $key => $params ) {
79 $opts->add( $key, $params['default'] );
80 }
81
82 $opts->fetchValuesFromRequest( $this->getRequest() );
83
84 // Give precedence to subpage syntax
85 if ( $parameters !== null ) {
86 $this->parseParameters( $parameters, $opts );
87 }
88
89 $opts->validateIntBounds( 'limit', 0, 5000 );
90
91 return $opts;
92 }
93
94 /**
95 * Get custom show/hide filters
96 *
97 * @return array Map of filter URL param names to properties (msg/default)
98 */
99 protected function getCustomFilters() {
100 if ( $this->customFilters === null ) {
101 $this->customFilters = array();
102 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters ) );
103 }
104
105 return $this->customFilters;
106 }
107
108 /**
109 * Create a FormOptions object specific for feed requests and return it
110 *
111 * @return FormOptions
112 */
113 public function feedSetup() {
114 global $wgFeedLimit;
115 $opts = $this->getDefaultOptions();
116 $opts->fetchValuesFromRequest( $this->getRequest() );
117 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
118
119 return $opts;
120 }
121
122 /**
123 * Get the current FormOptions for this request
124 */
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' );
131 }
132 $this->rcOptions = $isFeed ? $this->feedSetup() : $this->setup( $this->rcSubpage );
133 }
134
135 return $this->rcOptions;
136 }
137
138 /**
139 * Main execution point
140 *
141 * @param string $subpage
142 */
143 public function execute( $subpage ) {
144 $this->rcSubpage = $subpage;
145 $feedFormat = $this->including() ? null : $this->getRequest()->getVal( 'feed' );
146
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;
153 }
154
155 $opts = $this->getOptions();
156 $this->setHeaders();
157 $this->outputHeader();
158 $this->addModules();
159
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 );
166 }
167
168 return;
169 }
170
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 );
177 }
178 $batch->execute();
179 }
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 );
186 }
187
188 $rows->free();
189 }
190
191 /**
192 * Return an array with a ChangesFeed object and ChannelFeed object
193 *
194 * @param string $feedFormat Feed's format (either 'rss' or 'atom')
195 * @return array
196 */
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()
203 );
204
205 return array( $changesFeed, $formatter );
206 }
207
208 /**
209 * Process $par and put options found if $opts
210 * Mainly used when including the page
211 *
212 * @param string $par
213 * @param FormOptions $opts
214 */
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;
220 }
221 if ( 'bots' === $bit ) {
222 $opts['hidebots'] = false;
223 }
224 if ( 'hideminor' === $bit ) {
225 $opts['hideminor'] = true;
226 }
227 if ( 'minor' === $bit ) {
228 $opts['hideminor'] = false;
229 }
230 if ( 'hideliu' === $bit ) {
231 $opts['hideliu'] = true;
232 }
233 if ( 'hidepatrolled' === $bit ) {
234 $opts['hidepatrolled'] = true;
235 }
236 if ( 'hideanons' === $bit ) {
237 $opts['hideanons'] = true;
238 }
239 if ( 'hidemyself' === $bit ) {
240 $opts['hidemyself'] = true;
241 }
242
243 if ( is_numeric( $bit ) ) {
244 $opts['limit'] = $bit;
245 }
246
247 $m = array();
248 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
249 $opts['limit'] = $m[1];
250 }
251 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
252 $opts['days'] = $m[1];
253 }
254 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
255 $opts['namespace'] = $m[1];
256 }
257 }
258 }
259
260 /**
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
264 *
265 * @param string $feedFormat
266 * @return string|bool
267 */
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;
275 }
276 }
277
278 return $lastmod;
279 }
280
281 /**
282 * Return an array of conditions depending of options set in $opts
283 *
284 * @param FormOptions $opts
285 * @return array
286 */
287 public function buildMainQueryConds( FormOptions $opts ) {
288 $dbr = wfGetDB( DB_SLAVE );
289 $conds = array();
290
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;
301 }
302 }
303
304 // Calculate cutoff
305 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
306 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime % 86400 );
307 $cutoff = $dbr->timestamp( $cutoff_unixtime );
308
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' );
314 }
315
316 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
317
318 $hidePatrol = $this->getUser()->useRCPatrol() && $opts['hidepatrolled'];
319 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
320 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
321
322 if ( $opts['hideminor'] ) {
323 $conds['rc_minor'] = 0;
324 }
325 if ( $opts['hidebots'] ) {
326 $conds['rc_bot'] = 0;
327 }
328 if ( $hidePatrol ) {
329 $conds['rc_patrolled'] = 0;
330 }
331 if ( $forcebot ) {
332 $conds['rc_bot'] = 1;
333 }
334 if ( $hideLoggedInUsers ) {
335 $conds[] = 'rc_user = 0';
336 }
337 if ( $hideAnonymousUsers ) {
338 $conds[] = 'rc_user != 0';
339 }
340
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() );
346 }
347 }
348
349 # Namespace filtering
350 if ( $opts['namespace'] !== '' ) {
351 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
352 $operator = $opts['invert'] ? '!=' : '=';
353 $boolean = $opts['invert'] ? 'AND' : 'OR';
354
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'] )
362 );
363 $condition = "(rc_namespace $operator $selectedNS "
364 . $boolean
365 . " rc_namespace $operator $associatedNS)";
366 }
367
368 $conds[] = $condition;
369 }
370
371 return $conds;
372 }
373
374 /**
375 * Process the query
376 *
377 * @param array $conds
378 * @param FormOptions $opts
379 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
380 */
381 public function doMainQuery( $conds, $opts ) {
382 $tables = array( 'recentchanges' );
383 $join_conds = array();
384 $query_options = array(
385 'USE INDEX' => array( 'recentchanges' => 'rc_timestamp' )
386 );
387
388 $uid = $this->getUser()->getId();
389 $dbr = wfGetDB( DB_SLAVE );
390 $limit = $opts['limit'];
391 $namespace = $opts['namespace'];
392 $invert = $opts['invert'];
393 $associated = $opts['associated'];
394
395 $fields = RecentChange::selectFields();
396 // JOIN on watchlist for users
397 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
398 $tables[] = 'watchlist';
399 $fields[] = 'wl_user';
400 $fields[] = 'wl_notificationtimestamp';
401 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
402 'wl_user' => $uid,
403 'wl_title=rc_title',
404 'wl_namespace=rc_namespace'
405 ) );
406 }
407 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
408 $tables[] = 'page';
409 $fields[] = 'page_latest';
410 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
411 }
412 // Tag stuff.
413 ChangeTags::modifyDisplayQuery(
414 $tables,
415 $fields,
416 $conds,
417 $join_conds,
418 $query_options,
419 $opts['tagfilter']
420 );
421
422 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
423 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) )
424 ) {
425 return false;
426 }
427
428 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
429 // knowledge to use an index merge if it wants (it may use some other index though).
430 return $dbr->select(
431 $tables,
432 $fields,
433 $conds + array( 'rc_new' => array( 0, 1 ) ),
434 __METHOD__,
435 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) + $query_options,
436 $join_conds
437 );
438 }
439
440 /**
441 * Send output to the OutputPage object, only called if not used feeds
442 *
443 * @param array $rows Database rows
444 * @param FormOptions $opts
445 */
446 public function webOutput( $rows, $opts ) {
447 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker, $wgAllowCategorizedRecentChanges;
448
449 // Build the final data
450
451 if ( $wgAllowCategorizedRecentChanges ) {
452 $this->filterByCategories( $rows, $opts );
453 }
454
455 $limit = $opts['limit'];
456
457 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
458 $watcherCache = array();
459
460 $dbr = wfGetDB( DB_SLAVE );
461
462 $counter = 1;
463 $list = ChangesList::newFromContext( $this->getContext() );
464
465 $rclistOutput = $list->beginRecentChangesList();
466 foreach ( $rows as $obj ) {
467 if ( $limit == 0 ) {
468 break;
469 }
470 $rc = RecentChange::newFromRow( $obj );
471 $rc->counter = $counter++;
472 # Check if the page has been updated since the last visit
473 if ( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp ) ) {
474 $rc->notificationtimestamp = ( $obj->rc_timestamp >= $obj->wl_notificationtimestamp );
475 } else {
476 $rc->notificationtimestamp = false; // Default
477 }
478 # Check the number of users watching the page
479 $rc->numberofWatchingusers = 0; // Default
480 if ( $showWatcherCount && $obj->rc_namespace >= 0 ) {
481 if ( !isset( $watcherCache[$obj->rc_namespace][$obj->rc_title] ) ) {
482 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
483 $dbr->selectField(
484 'watchlist',
485 'COUNT(*)',
486 array(
487 'wl_namespace' => $obj->rc_namespace,
488 'wl_title' => $obj->rc_title,
489 ),
490 __METHOD__ . '-watchers'
491 );
492 }
493 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
494 }
495
496 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
497 if ( $changeLine !== false ) {
498 $rclistOutput .= $changeLine;
499 --$limit;
500 }
501 }
502 $rclistOutput .= $list->endRecentChangesList();
503
504 // Print things out
505
506 if ( !$this->including() ) {
507 // Output options box
508 $this->doHeader( $opts );
509 }
510
511 // And now for the content
512 $feedQuery = $this->getFeedQuery();
513 if ( $feedQuery !== '' ) {
514 $this->getOutput()->setFeedAppendQuery( $feedQuery );
515 } else {
516 $this->getOutput()->setFeedAppendQuery( false );
517 }
518
519 if ( $rows->numRows() === 0 ) {
520 $this->getOutput()->addHtml(
521 '<div class="mw-changeslist-empty">' . $this->msg( 'recentchanges-noresult' )->parse() . '</div>'
522 );
523 } else {
524 $this->getOutput()->addHTML( $rclistOutput );
525 }
526 }
527
528 /**
529 * Get the query string to append to feed link URLs.
530 *
531 * @return string
532 */
533 public function getFeedQuery() {
534 global $wgFeedLimit;
535
536 $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
537 $options = $this->getOptions()->getChangedValues();
538
539 // wfArrayToCgi() omits options set to null or false
540 foreach ( $options as &$value ) {
541 if ( $value === false ) {
542 $value = '0';
543 }
544 }
545 unset( $value );
546
547 return wfArrayToCgi( $options );
548 }
549
550 /**
551 * Return the text to be displayed above the changes
552 *
553 * @param FormOptions $opts
554 * @return string XHTML
555 */
556 public function doHeader( $opts ) {
557 global $wgScript;
558
559 $this->setTopText( $opts );
560
561 $defaults = $opts->getAllValues();
562 $nondefaults = $opts->getChangedValues();
563
564 $panel = array();
565 $panel[] = self::makeLegend( $this->getContext() );
566 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
567 $panel[] = '<hr />';
568
569 $extraOpts = $this->getExtraOptions( $opts );
570 $extraOptsCount = count( $extraOpts );
571 $count = 0;
572 $submit = ' ' . Xml::submitbutton( $this->msg( 'allpagessubmit' )->text() );
573
574 $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
575 foreach ( $extraOpts as $name => $optionRow ) {
576 # Add submit button to the last row only
577 ++$count;
578 $addSubmit = ( $count === $extraOptsCount ) ? $submit : '';
579
580 $out .= Xml::openElement( 'tr' );
581 if ( is_array( $optionRow ) ) {
582 $out .= Xml::tags(
583 'td',
584 array( 'class' => 'mw-label mw-' . $name . '-label' ),
585 $optionRow[0]
586 );
587 $out .= Xml::tags(
588 'td',
589 array( 'class' => 'mw-input' ),
590 $optionRow[1] . $addSubmit
591 );
592 } else {
593 $out .= Xml::tags(
594 'td',
595 array( 'class' => 'mw-input', 'colspan' => 2 ),
596 $optionRow . $addSubmit
597 );
598 }
599 $out .= Xml::closeElement( 'tr' );
600 }
601 $out .= Xml::closeElement( 'table' );
602
603 $unconsumed = $opts->getUnconsumedValues();
604 foreach ( $unconsumed as $key => $value ) {
605 $out .= Html::hidden( $key, $value );
606 }
607
608 $t = $this->getTitle();
609 $out .= Html::hidden( 'title', $t->getPrefixedText() );
610 $form = Xml::tags( 'form', array( 'action' => $wgScript ), $out );
611 $panel[] = $form;
612 $panelString = implode( "\n", $panel );
613
614 $this->getOutput()->addHTML(
615 Xml::fieldset(
616 $this->msg( 'recentchanges-legend' )->text(),
617 $panelString,
618 array( 'class' => 'rcoptions' )
619 )
620 );
621
622 $this->setBottomText( $opts );
623 }
624
625 /**
626 * Get options to be displayed in a form
627 *
628 * @param FormOptions $opts
629 * @return array
630 */
631 function getExtraOptions( $opts ) {
632 $opts->consumeValues( array(
633 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
634 ) );
635
636 $extraOpts = array();
637 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
638
639 global $wgAllowCategorizedRecentChanges;
640 if ( $wgAllowCategorizedRecentChanges ) {
641 $extraOpts['category'] = $this->categoryFilterForm( $opts );
642 }
643
644 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
645 if ( count( $tagFilter ) ) {
646 $extraOpts['tagfilter'] = $tagFilter;
647 }
648
649 // Don't fire the hook for subclasses. (Or should we?)
650 if ( $this->getName() === 'Recentchanges' ) {
651 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
652 }
653
654 return $extraOpts;
655 }
656
657 /**
658 * Return the legend displayed within the fieldset
659 *
660 * @param $context the object available as $this in non-static functions
661 * @return string
662 */
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', array( 'class' => 'mw-changeslist-legend' ) ) . "\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'] );
672 }
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;
677
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";
690 }
691 }
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";
703 return $legend;
704 }
705
706 /**
707 * Send the text to be displayed above the options
708 *
709 * @param FormOptions $opts Unused
710 */
711 function setTopText( FormOptions $opts ) {
712 global $wgContLang;
713
714 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
715 if ( !$message->isDisabled() ) {
716 $this->getOutput()->addWikiText(
717 Html::rawElement( 'p',
718 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
719 "\n" . $message->plain() . "\n"
720 ),
721 /* $lineStart */ false,
722 /* $interface */ false
723 );
724 }
725 }
726
727 /**
728 * Send the text to be displayed after the options, for use in subclasses.
729 *
730 * @param FormOptions $opts
731 */
732 function setBottomText( FormOptions $opts ) {
733 }
734
735 /**
736 * Creates the choose namespace selection
737 *
738 * @param FormOptions $opts
739 * @return string
740 */
741 protected function namespaceFilterForm( FormOptions $opts ) {
742 $nsSelect = Html::namespaceSelector(
743 array( 'selected' => $opts['namespace'], 'all' => '' ),
744 array( 'name' => 'namespace', 'id' => 'namespace' )
745 );
746 $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
747 $invert = Xml::checkLabel(
748 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
749 $opts['invert'],
750 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
751 );
752 $associated = Xml::checkLabel(
753 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
754 $opts['associated'],
755 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
756 );
757
758 return array( $nsLabel, "$nsSelect $invert $associated" );
759 }
760
761 /**
762 * Create a input to filter changes by categories
763 *
764 * @param FormOptions $opts
765 * @return array
766 */
767 protected function categoryFilterForm( FormOptions $opts ) {
768 list( $label, $input ) = Xml::inputLabelSep( $this->msg( 'rc_categories' )->text(),
769 'categories', 'mw-categories', false, $opts['categories'] );
770
771 $input .= ' ' . Xml::checkLabel( $this->msg( 'rc_categories_any' )->text(),
772 'categories_any', 'mw-categories_any', $opts['categories_any'] );
773
774 return array( $label, $input );
775 }
776
777 /**
778 * Filter $rows by categories set in $opts
779 *
780 * @param array $rows Database rows
781 * @param FormOptions $opts
782 */
783 function filterByCategories( &$rows, FormOptions $opts ) {
784 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
785
786 if ( !count( $categories ) ) {
787 return;
788 }
789
790 # Filter categories
791 $cats = array();
792 foreach ( $categories as $cat ) {
793 $cat = trim( $cat );
794 if ( $cat == '' ) {
795 continue;
796 }
797 $cats[] = $cat;
798 }
799
800 # Filter articles
801 $articles = array();
802 $a2r = array();
803 $rowsarr = array();
804 foreach ( $rows as $k => $r ) {
805 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
806 $id = $nt->getArticleID();
807 if ( $id == 0 ) {
808 continue; # Page might have been deleted...
809 }
810 if ( !in_array( $id, $articles ) ) {
811 $articles[] = $id;
812 }
813 if ( !isset( $a2r[$id] ) ) {
814 $a2r[$id] = array();
815 }
816 $a2r[$id][] = $k;
817 $rowsarr[$k] = $r;
818 }
819
820 # Shortcut?
821 if ( !count( $articles ) || !count( $cats ) ) {
822 return;
823 }
824
825 # Look up
826 $c = new Categoryfinder;
827 $c->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
828 $match = $c->run();
829
830 # Filter
831 $newrows = array();
832 foreach ( $match as $id ) {
833 foreach ( $a2r[$id] as $rev ) {
834 $k = $rev;
835 $newrows[$k] = $rowsarr[$k];
836 }
837 }
838 $rows = $newrows;
839 }
840
841 /**
842 * Makes change an option link which carries all the other options
843 *
844 * @param string $title Title
845 * @param array $override Options to override
846 * @param array $options Current options
847 * @param bool $active Whether to show the link in bold
848 * @return string
849 */
850 function makeOptionsLink( $title, $override, $options, $active = false ) {
851 $params = $override + $options;
852
853 // Bug 36524: false values have be converted to "0" otherwise
854 // wfArrayToCgi() will omit it them.
855 foreach ( $params as &$value ) {
856 if ( $value === false ) {
857 $value = '0';
858 }
859 }
860 unset( $value );
861
862 $text = htmlspecialchars( $title );
863 if ( $active ) {
864 $text = '<strong>' . $text . '</strong>';
865 }
866
867 return Linker::linkKnown( $this->getTitle(), $text, array(), $params );
868 }
869
870 /**
871 * Creates the options panel.
872 *
873 * @param array $defaults
874 * @param array $nondefaults
875 * @return string
876 */
877 function optionsPanel( $defaults, $nondefaults ) {
878 global $wgRCLinkLimits, $wgRCLinkDays;
879
880 $options = $nondefaults + $defaults;
881
882 $note = '';
883 $msg = $this->msg( 'rclegend' );
884 if ( !$msg->isDisabled() ) {
885 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
886 }
887
888 $lang = $this->getLanguage();
889 $user = $this->getUser();
890 if ( $options['from'] ) {
891 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
892 $lang->userTimeAndDate( $options['from'], $user ),
893 $lang->userDate( $options['from'], $user ),
894 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
895 }
896
897 # Sort data for display and make sure it's unique after we've added user data.
898 $linkLimits = $wgRCLinkLimits;
899 $linkLimits[] = $options['limit'];
900 sort( $linkLimits );
901 $linkLimits = array_unique( $linkLimits );
902
903 $linkDays = $wgRCLinkDays;
904 $linkDays[] = $options['days'];
905 sort( $linkDays );
906 $linkDays = array_unique( $linkDays );
907
908 // limit links
909 $cl = array();
910 foreach ( $linkLimits as $value ) {
911 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
912 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
913 }
914 $cl = $lang->pipeList( $cl );
915
916 // day links, reset 'from' to none
917 $dl = array();
918 foreach ( $linkDays as $value ) {
919 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
920 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
921 }
922 $dl = $lang->pipeList( $dl );
923
924 // show/hide links
925 $showhide = array( $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() );
926 $filters = array(
927 'hideminor' => 'rcshowhideminor',
928 'hidebots' => 'rcshowhidebots',
929 'hideanons' => 'rcshowhideanons',
930 'hideliu' => 'rcshowhideliu',
931 'hidepatrolled' => 'rcshowhidepatr',
932 'hidemyself' => 'rcshowhidemine'
933 );
934 foreach ( $this->getCustomFilters() as $key => $params ) {
935 $filters[$key] = $params['msg'];
936 }
937 // Disable some if needed
938 if ( !$user->useRCPatrol() ) {
939 unset( $filters['hidepatrolled'] );
940 }
941
942 $links = array();
943 foreach ( $filters as $key => $msg ) {
944 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
945 array( $key => 1 - $options[$key] ), $nondefaults );
946 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
947 }
948
949 // show from this onward link
950 $timestamp = wfTimestampNow();
951 $now = $lang->userTimeAndDate( $timestamp, $user );
952 $tl = $this->makeOptionsLink(
953 $now, array( 'from' => $timestamp ), $nondefaults
954 );
955
956 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )
957 ->parse();
958 $rclistfrom = $this->msg( 'rclistfrom' )->rawParams( $tl )->parse();
959
960 return "{$note}$rclinks<br />$rclistfrom";
961 }
962
963 /**
964 * Add page-specific modules.
965 */
966 protected function addModules() {
967 $this->getOutput()->addModules( array(
968 'mediawiki.special.recentchanges',
969 ) );
970 }
971
972 protected function getGroupName() {
973 return 'changes';
974 }
975 }