merged master
[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', (int)$this->getUser()->getOption( 'rcdays' ) );
46 $opts->add( 'limit', (int)$this->getUser()->getOption( '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 # Feed is cached on limit,hideminor,namespace; other params would randomly not work
113 $opts->fetchValuesFromRequest( $this->getRequest(), array( 'limit', 'hideminor', 'namespace' ) );
114 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
115 return $opts;
116 }
117
118 /**
119 * Get the current FormOptions for this request
120 */
121 public function getOptions() {
122 if ( $this->rcOptions === null ) {
123 if ( $this->including() ) {
124 $isFeed = false;
125 } else {
126 $isFeed = (bool)$this->getRequest()->getVal( 'feed' );
127 }
128 $this->rcOptions = $isFeed ? $this->feedSetup() : $this->setup( $this->rcSubpage );
129 }
130 return $this->rcOptions;
131 }
132
133
134 /**
135 * Main execution point
136 *
137 * @param $subpage String
138 */
139 public function execute( $subpage ) {
140 $this->rcSubpage = $subpage;
141 $feedFormat = $this->including() ? null : $this->getRequest()->getVal( 'feed' );
142
143 # 10 seconds server-side caching max
144 $this->getOutput()->setSquidMaxage( 10 );
145 # Check if the client has a cached version
146 $lastmod = $this->checkLastModified( $feedFormat );
147 if( $lastmod === false ) {
148 return;
149 }
150
151 $opts = $this->getOptions();
152 $this->setHeaders();
153 $this->outputHeader();
154 $this->addRecentChangesJS();
155
156 // Fetch results, prepare a batch link existence check query
157 $conds = $this->buildMainQueryConds( $opts );
158 $rows = $this->doMainQuery( $conds, $opts );
159 if( $rows === false ){
160 if( !$this->including() ) {
161 $this->doHeader( $opts );
162 }
163 return;
164 }
165
166 if( !$feedFormat ) {
167 $batch = new LinkBatch;
168 foreach( $rows as $row ) {
169 $batch->add( NS_USER, $row->rc_user_text );
170 $batch->add( NS_USER_TALK, $row->rc_user_text );
171 $batch->add( $row->rc_namespace, $row->rc_title );
172 }
173 $batch->execute();
174 }
175 if( $feedFormat ) {
176 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
177 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
178 } else {
179 $this->webOutput( $rows, $opts );
180 }
181
182 $rows->free();
183 }
184
185 /**
186 * Return an array with a ChangesFeed object and ChannelFeed object
187 *
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 $par String
205 * @param $opts FormOptions
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 $feedFormat String
258 * @return String or false
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 $opts FormOptions
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 $conds Array
368 * @param $opts FormOptions
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 = array( $dbr->tableName( 'recentchanges' ) . '.*' ); // all rc columns
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',
392 "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace");
393 }
394 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
395 $tables[] = 'page';
396 $fields[] = 'page_latest';
397 $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
398 }
399 // Tag stuff.
400 ChangeTags::modifyDisplayQuery(
401 $tables,
402 $fields,
403 $conds,
404 $join_conds,
405 $query_options,
406 $opts['tagfilter']
407 );
408
409 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
410 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ) ) )
411 {
412 return false;
413 }
414
415 // Don't use the new_namespace_time timestamp index if:
416 // (a) "All namespaces" selected
417 // (b) We want pages in more than one namespace (inverted/associated)
418 // (c) There is a tag to filter on (use tag index instead)
419 // (d) UNION + sort/limit is not an option for the DBMS
420 if( $namespace === ''
421 || ( $invert || $associated )
422 || $opts['tagfilter'] != ''
423 || !$dbr->unionSupportsOrderAndLimit() )
424 {
425 $res = $dbr->select( $tables, $fields, $conds, __METHOD__,
426 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
427 $query_options,
428 $join_conds );
429 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
430 } else {
431 // New pages
432 $sqlNew = $dbr->selectSQLText(
433 $tables,
434 $fields,
435 array( 'rc_new' => 1 ) + $conds,
436 __METHOD__,
437 array(
438 'ORDER BY' => 'rc_timestamp DESC',
439 'LIMIT' => $limit,
440 'USE INDEX' => array( 'recentchanges' => 'new_name_timestamp' )
441 ),
442 $join_conds
443 );
444 // Old pages
445 $sqlOld = $dbr->selectSQLText(
446 $tables,
447 $fields,
448 array( 'rc_new' => 0 ) + $conds,
449 __METHOD__,
450 array(
451 'ORDER BY' => 'rc_timestamp DESC',
452 'LIMIT' => $limit,
453 'USE INDEX' => array( 'recentchanges' => 'new_name_timestamp' )
454 ),
455 $join_conds
456 );
457 # Join the two fast queries, and sort the result set
458 $sql = $dbr->unionQueries( array( $sqlNew, $sqlOld ), false ) .
459 ' ORDER BY rc_timestamp DESC';
460 $sql = $dbr->limitResult( $sql, $limit, false );
461 $res = $dbr->query( $sql, __METHOD__ );
462 }
463
464 return $res;
465 }
466
467 /**
468 * Send output to the OutputPage object, only called if not used feeds
469 *
470 * @param $rows Array of database rows
471 * @param $opts FormOptions
472 */
473 public function webOutput( $rows, $opts ) {
474 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker, $wgAllowCategorizedRecentChanges;
475
476 $limit = $opts['limit'];
477
478 if( !$this->including() ) {
479 // Output options box
480 $this->doHeader( $opts );
481 }
482
483 // And now for the content
484 $this->getOutput()->setFeedAppendQuery( $this->getFeedQuery() );
485
486 if( $wgAllowCategorizedRecentChanges ) {
487 $this->filterByCategories( $rows, $opts );
488 }
489
490 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
491 $watcherCache = array();
492
493 $dbr = wfGetDB( DB_SLAVE );
494
495 $counter = 1;
496 $list = ChangesList::newFromContext( $this->getContext() );
497
498 $s = $list->beginRecentChangesList();
499 foreach( $rows as $obj ) {
500 if( $limit == 0 ) {
501 break;
502 }
503 $rc = RecentChange::newFromRow( $obj );
504 $rc->counter = $counter++;
505 # Check if the page has been updated since the last visit
506 if( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp ) ) {
507 $rc->notificationtimestamp = ( $obj->rc_timestamp >= $obj->wl_notificationtimestamp );
508 } else {
509 $rc->notificationtimestamp = false; // Default
510 }
511 # Check the number of users watching the page
512 $rc->numberofWatchingusers = 0; // Default
513 if( $showWatcherCount && $obj->rc_namespace >= 0 ) {
514 if( !isset( $watcherCache[$obj->rc_namespace][$obj->rc_title] ) ) {
515 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
516 $dbr->selectField(
517 'watchlist',
518 'COUNT(*)',
519 array(
520 'wl_namespace' => $obj->rc_namespace,
521 'wl_title' => $obj->rc_title,
522 ),
523 __METHOD__ . '-watchers'
524 );
525 }
526 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
527 }
528 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
529 --$limit;
530 }
531 $s .= $list->endRecentChangesList();
532 $this->getOutput()->addHTML( $s );
533 }
534
535 /**
536 * Get the query string to append to feed link URLs.
537 * This is overridden by RCL to add the target parameter
538 * @return bool
539 */
540 public function getFeedQuery() {
541 return false;
542 }
543
544 /**
545 * Return the text to be displayed above the changes
546 *
547 * @param $opts FormOptions
548 * @return String: XHTML
549 */
550 public function doHeader( $opts ) {
551 global $wgScript;
552
553 $this->setTopText( $opts );
554
555 $defaults = $opts->getAllValues();
556 $nondefaults = $opts->getChangedValues();
557 $opts->consumeValues( array(
558 'namespace', 'invert', 'associated', 'tagfilter',
559 'categories', 'categories_any'
560 ) );
561
562 $panel = array();
563 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
564 $panel[] = '<hr />';
565
566 $extraOpts = $this->getExtraOptions( $opts );
567 $extraOptsCount = count( $extraOpts );
568 $count = 0;
569 $submit = ' ' . Xml::submitbutton( $this->msg( 'allpagessubmit' )->text() );
570
571 $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
572 foreach( $extraOpts as $name => $optionRow ) {
573 # Add submit button to the last row only
574 ++$count;
575 $addSubmit = ( $count === $extraOptsCount ) ? $submit : '';
576
577 $out .= Xml::openElement( 'tr' );
578 if( is_array( $optionRow ) ) {
579 $out .= Xml::tags( 'td', array( 'class' => 'mw-label mw-' . $name . '-label' ), $optionRow[0] );
580 $out .= Xml::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
581 } else {
582 $out .= Xml::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
583 }
584 $out .= Xml::closeElement( 'tr' );
585 }
586 $out .= Xml::closeElement( 'table' );
587
588 $unconsumed = $opts->getUnconsumedValues();
589 foreach( $unconsumed as $key => $value ) {
590 $out .= Html::hidden( $key, $value );
591 }
592
593 $t = $this->getTitle();
594 $out .= Html::hidden( 'title', $t->getPrefixedText() );
595 $form = Xml::tags( 'form', array( 'action' => $wgScript ), $out );
596 $panel[] = $form;
597 $panelString = implode( "\n", $panel );
598
599 $this->getOutput()->addHTML(
600 Xml::fieldset( $this->msg( 'recentchanges-legend' )->text(), $panelString, array( 'class' => 'rcoptions' ) )
601 );
602
603 $this->setBottomText( $opts );
604 }
605
606 /**
607 * Get options to be displayed in a form
608 *
609 * @param $opts FormOptions
610 * @return Array
611 */
612 function getExtraOptions( $opts ) {
613 $extraOpts = array();
614 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
615
616 global $wgAllowCategorizedRecentChanges;
617 if( $wgAllowCategorizedRecentChanges ) {
618 $extraOpts['category'] = $this->categoryFilterForm( $opts );
619 }
620
621 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
622 if ( count( $tagFilter ) ) {
623 $extraOpts['tagfilter'] = $tagFilter;
624 }
625
626 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
627 return $extraOpts;
628 }
629
630 /**
631 * Send the text to be displayed above the options
632 *
633 * @param $opts FormOptions
634 */
635 function setTopText( FormOptions $opts ) {
636 global $wgContLang;
637
638 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
639 if ( !$message->isDisabled() ) {
640 $this->getOutput()->addWikiText(
641 Html::rawElement( 'p',
642 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
643 "\n" . $message->plain() . "\n"
644 ),
645 /* $lineStart */ false,
646 /* $interface */ false
647 );
648 }
649 }
650
651 /**
652 * Send the text to be displayed after the options, for use in
653 * Recentchangeslinked
654 *
655 * @param $opts FormOptions
656 */
657 function setBottomText( FormOptions $opts ) {}
658
659 /**
660 * Creates the choose namespace selection
661 *
662 * @todo Uses radio buttons (HASHAR)
663 * @param $opts FormOptions
664 * @return String
665 */
666 protected function namespaceFilterForm( FormOptions $opts ) {
667 $nsSelect = Html::namespaceSelector(
668 array( 'selected' => $opts['namespace'], 'all' => '' ),
669 array( 'name' => 'namespace', 'id' => 'namespace' )
670 );
671 $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
672 $invert = Xml::checkLabel(
673 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
674 $opts['invert'],
675 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
676 );
677 $associated = Xml::checkLabel(
678 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
679 $opts['associated'],
680 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
681 );
682 return array( $nsLabel, "$nsSelect $invert $associated" );
683 }
684
685 /**
686 * Create a input to filter changes by categories
687 *
688 * @param $opts FormOptions
689 * @return Array
690 */
691 protected function categoryFilterForm( FormOptions $opts ) {
692 list( $label, $input ) = Xml::inputLabelSep( $this->msg( 'rc_categories' )->text(),
693 'categories', 'mw-categories', false, $opts['categories'] );
694
695 $input .= ' ' . Xml::checkLabel( $this->msg( 'rc_categories_any' )->text(),
696 'categories_any', 'mw-categories_any', $opts['categories_any'] );
697
698 return array( $label, $input );
699 }
700
701 /**
702 * Filter $rows by categories set in $opts
703 *
704 * @param $rows Array of database rows
705 * @param $opts FormOptions
706 */
707 function filterByCategories( &$rows, FormOptions $opts ) {
708 $categories = array_map( 'trim', explode( '|' , $opts['categories'] ) );
709
710 if( !count( $categories ) ) {
711 return;
712 }
713
714 # Filter categories
715 $cats = array();
716 foreach( $categories as $cat ) {
717 $cat = trim( $cat );
718 if( $cat == '' ) {
719 continue;
720 }
721 $cats[] = $cat;
722 }
723
724 # Filter articles
725 $articles = array();
726 $a2r = array();
727 $rowsarr = array();
728 foreach( $rows as $k => $r ) {
729 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
730 $id = $nt->getArticleID();
731 if( $id == 0 ) {
732 continue; # Page might have been deleted...
733 }
734 if( !in_array( $id, $articles ) ) {
735 $articles[] = $id;
736 }
737 if( !isset( $a2r[$id] ) ) {
738 $a2r[$id] = array();
739 }
740 $a2r[$id][] = $k;
741 $rowsarr[$k] = $r;
742 }
743
744 # Shortcut?
745 if( !count( $articles ) || !count( $cats ) ) {
746 return;
747 }
748
749 # Look up
750 $c = new Categoryfinder;
751 $c->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
752 $match = $c->run();
753
754 # Filter
755 $newrows = array();
756 foreach( $match as $id ) {
757 foreach( $a2r[$id] as $rev ) {
758 $k = $rev;
759 $newrows[$k] = $rowsarr[$k];
760 }
761 }
762 $rows = $newrows;
763 }
764
765 /**
766 * Makes change an option link which carries all the other options
767 *
768 * @param $title Title
769 * @param $override Array: options to override
770 * @param $options Array: current options
771 * @param $active Boolean: whether to show the link in bold
772 * @return string
773 */
774 function makeOptionsLink( $title, $override, $options, $active = false ) {
775 $params = $override + $options;
776
777 // Bug 36524: false values have be converted to "0" otherwise
778 // wfArrayToCgi() will omit it them.
779 foreach ( $params as &$value ) {
780 if ( $value === false ) {
781 $value = '0';
782 }
783 }
784 unset( $value );
785
786 $text = htmlspecialchars( $title );
787 if ( $active ) {
788 $text = '<strong>' . $text . '</strong>';
789 }
790 return Linker::linkKnown( $this->getTitle(), $text, array(), $params );
791 }
792
793 /**
794 * Creates the options panel.
795 *
796 * @param $defaults Array
797 * @param $nondefaults Array
798 * @return string
799 */
800 function optionsPanel( $defaults, $nondefaults ) {
801 global $wgRCLinkLimits, $wgRCLinkDays;
802
803 $options = $nondefaults + $defaults;
804
805 $note = '';
806 $msg = $this->msg( 'rclegend' );
807 if( !$msg->isDisabled() ) {
808 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
809 }
810
811 $lang = $this->getLanguage();
812 $user = $this->getUser();
813 if( $options['from'] ) {
814 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
815 $lang->userTimeAndDate( $options['from'], $user ),
816 $lang->userDate( $options['from'], $user ),
817 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
818 }
819
820 # Sort data for display and make sure it's unique after we've added user data.
821 $wgRCLinkLimits[] = $options['limit'];
822 $wgRCLinkDays[] = $options['days'];
823 sort( $wgRCLinkLimits );
824 sort( $wgRCLinkDays );
825 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
826 $wgRCLinkDays = array_unique( $wgRCLinkDays );
827
828 // limit links
829 foreach( $wgRCLinkLimits as $value ) {
830 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
831 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
832 }
833 $cl = $lang->pipeList( $cl );
834
835 // day links, reset 'from' to none
836 foreach( $wgRCLinkDays as $value ) {
837 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
838 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
839 }
840 $dl = $lang->pipeList( $dl );
841
842
843 // show/hide links
844 $showhide = array( $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() );
845 $filters = array(
846 'hideminor' => 'rcshowhideminor',
847 'hidebots' => 'rcshowhidebots',
848 'hideanons' => 'rcshowhideanons',
849 'hideliu' => 'rcshowhideliu',
850 'hidepatrolled' => 'rcshowhidepatr',
851 'hidemyself' => 'rcshowhidemine'
852 );
853 foreach ( $this->getCustomFilters() as $key => $params ) {
854 $filters[$key] = $params['msg'];
855 }
856 // Disable some if needed
857 if ( !$user->useRCPatrol() ) {
858 unset( $filters['hidepatrolled'] );
859 }
860
861 $links = array();
862 foreach ( $filters as $key => $msg ) {
863 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
864 array( $key => 1-$options[$key] ), $nondefaults );
865 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
866 }
867
868 // show from this onward link
869 $timestamp = wfTimestampNow();
870 $now = $lang->userTimeAndDate( $timestamp, $user );
871 $tl = $this->makeOptionsLink(
872 $now, array( 'from' => $timestamp ), $nondefaults
873 );
874
875 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )->parse();
876 $rclistfrom = $this->msg( 'rclistfrom' )->rawParams( $tl )->parse();
877 return "{$note}$rclinks<br />$rclistfrom";
878 }
879
880 /**
881 * add javascript specific to the [[Special:RecentChanges]] page
882 */
883 function addRecentChangesJS() {
884 $this->getOutput()->addModules( array(
885 'mediawiki.special.recentchanges',
886 ) );
887 }
888 }