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