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