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