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