Fixing some of the "@return true" or "@return false", need to be "@return bool" and...
[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 wfMsgForContent( 'recentchanges' ),
194 wfMsgForContent( 'recentchanges-feed-description' ),
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 if ( !$this->including() ) {
400 // Tag stuff.
401 // Doesn't work when transcluding. See bug 23293
402 ChangeTags::modifyDisplayQuery(
403 $tables, $fields, $conds, $join_conds, $query_options,
404 $opts['tagfilter']
405 );
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 */
538 public function getFeedQuery() {
539 return false;
540 }
541
542 /**
543 * Return the text to be displayed above the changes
544 *
545 * @param $opts FormOptions
546 * @return String: XHTML
547 */
548 public function doHeader( $opts ) {
549 global $wgScript;
550
551 $this->setTopText( $opts );
552
553 $defaults = $opts->getAllValues();
554 $nondefaults = $opts->getChangedValues();
555 $opts->consumeValues( array(
556 'namespace', 'invert', 'associated', 'tagfilter',
557 'categories', 'categories_any'
558 ) );
559
560 $panel = array();
561 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
562 $panel[] = '<hr />';
563
564 $extraOpts = $this->getExtraOptions( $opts );
565 $extraOptsCount = count( $extraOpts );
566 $count = 0;
567 $submit = ' ' . Xml::submitbutton( wfMsg( 'allpagessubmit' ) );
568
569 $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
570 foreach( $extraOpts as $optionRow ) {
571 # Add submit button to the last row only
572 ++$count;
573 $addSubmit = $count === $extraOptsCount ? $submit : '';
574
575 $out .= Xml::openElement( 'tr' );
576 if( is_array( $optionRow ) ) {
577 $out .= Xml::tags( 'td', array( 'class' => 'mw-label' ), $optionRow[0] );
578 $out .= Xml::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
579 } else {
580 $out .= Xml::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
581 }
582 $out .= Xml::closeElement( 'tr' );
583 }
584 $out .= Xml::closeElement( 'table' );
585
586 $unconsumed = $opts->getUnconsumedValues();
587 foreach( $unconsumed as $key => $value ) {
588 $out .= Html::hidden( $key, $value );
589 }
590
591 $t = $this->getTitle();
592 $out .= Html::hidden( 'title', $t->getPrefixedText() );
593 $form = Xml::tags( 'form', array( 'action' => $wgScript ), $out );
594 $panel[] = $form;
595 $panelString = implode( "\n", $panel );
596
597 $this->getOutput()->addHTML(
598 Xml::fieldset( wfMsg( 'recentchanges-legend' ), $panelString, array( 'class' => 'rcoptions' ) )
599 );
600
601 $this->setBottomText( $opts );
602 }
603
604 /**
605 * Get options to be displayed in a form
606 *
607 * @param $opts FormOptions
608 * @return Array
609 */
610 function getExtraOptions( $opts ) {
611 $extraOpts = array();
612 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
613
614 global $wgAllowCategorizedRecentChanges;
615 if( $wgAllowCategorizedRecentChanges ) {
616 $extraOpts['category'] = $this->categoryFilterForm( $opts );
617 }
618
619 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
620 if ( count( $tagFilter ) ) {
621 $extraOpts['tagfilter'] = $tagFilter;
622 }
623
624 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
625 return $extraOpts;
626 }
627
628 /**
629 * Send the text to be displayed above the options
630 *
631 * @param $opts FormOptions
632 */
633 function setTopText( FormOptions $opts ) {
634 global $wgContLang;
635 $this->getOutput()->addWikiText(
636 Html::rawElement( 'p',
637 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
638 "\n" . wfMsgForContentNoTrans( 'recentchangestext' ) . "\n"
639 ), false );
640 }
641
642 /**
643 * Send the text to be displayed after the options, for use in
644 * Recentchangeslinked
645 *
646 * @param $opts FormOptions
647 */
648 function setBottomText( FormOptions $opts ) {}
649
650 /**
651 * Creates the choose namespace selection
652 *
653 * @todo Uses radio buttons (HASHAR)
654 * @param $opts FormOptions
655 * @return String
656 */
657 protected function namespaceFilterForm( FormOptions $opts ) {
658 $nsSelect = Html::namespaceSelector(
659 array( 'selected' => $opts['namespace'], 'all' => '' ),
660 array( 'name' => 'namespace', 'id' => 'namespace' )
661 );
662 $nsLabel = Xml::label( wfMsg( 'namespace' ), 'namespace' );
663 $invert = Xml::checkLabel(
664 wfMsg( 'invert' ), 'invert', 'nsinvert',
665 $opts['invert'],
666 array( 'title' => wfMsg( 'tooltip-invert' ) )
667 );
668 $associated = Xml::checkLabel(
669 wfMsg( 'namespace_association' ), 'associated', 'nsassociated',
670 $opts['associated'],
671 array( 'title' => wfMsg( 'tooltip-namespace_association' ) )
672 );
673 return array( $nsLabel, "$nsSelect $invert $associated" );
674 }
675
676 /**
677 * Create a input to filter changes by categories
678 *
679 * @param $opts FormOptions
680 * @return Array
681 */
682 protected function categoryFilterForm( FormOptions $opts ) {
683 list( $label, $input ) = Xml::inputLabelSep( wfMsg( 'rc_categories' ),
684 'categories', 'mw-categories', false, $opts['categories'] );
685
686 $input .= ' ' . Xml::checkLabel( wfMsg( 'rc_categories_any' ),
687 'categories_any', 'mw-categories_any', $opts['categories_any'] );
688
689 return array( $label, $input );
690 }
691
692 /**
693 * Filter $rows by categories set in $opts
694 *
695 * @param $rows Array of database rows
696 * @param $opts FormOptions
697 */
698 function filterByCategories( &$rows, FormOptions $opts ) {
699 $categories = array_map( 'trim', explode( '|' , $opts['categories'] ) );
700
701 if( !count( $categories ) ) {
702 return;
703 }
704
705 # Filter categories
706 $cats = array();
707 foreach( $categories as $cat ) {
708 $cat = trim( $cat );
709 if( $cat == '' ) {
710 continue;
711 }
712 $cats[] = $cat;
713 }
714
715 # Filter articles
716 $articles = array();
717 $a2r = array();
718 $rowsarr = array();
719 foreach( $rows as $k => $r ) {
720 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
721 $id = $nt->getArticleID();
722 if( $id == 0 ) {
723 continue; # Page might have been deleted...
724 }
725 if( !in_array( $id, $articles ) ) {
726 $articles[] = $id;
727 }
728 if( !isset( $a2r[$id] ) ) {
729 $a2r[$id] = array();
730 }
731 $a2r[$id][] = $k;
732 $rowsarr[$k] = $r;
733 }
734
735 # Shortcut?
736 if( !count( $articles ) || !count( $cats ) ) {
737 return;
738 }
739
740 # Look up
741 $c = new Categoryfinder;
742 $c->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
743 $match = $c->run();
744
745 # Filter
746 $newrows = array();
747 foreach( $match as $id ) {
748 foreach( $a2r[$id] as $rev ) {
749 $k = $rev;
750 $newrows[$k] = $rowsarr[$k];
751 }
752 }
753 $rows = $newrows;
754 }
755
756 /**
757 * Makes change an option link which carries all the other options
758 *
759 * @param $title Title
760 * @param $override Array: options to override
761 * @param $options Array: current options
762 * @param $active Boolean: whether to show the link in bold
763 */
764 function makeOptionsLink( $title, $override, $options, $active = false ) {
765 $params = $override + $options;
766 $text = htmlspecialchars( $title );
767 if ( $active ) {
768 $text = '<strong>' . $text . '</strong>';
769 }
770 return Linker::linkKnown( $this->getTitle(), $text, array(), $params );
771 }
772
773 /**
774 * Creates the options panel.
775 *
776 * @param $defaults Array
777 * @param $nondefaults Array
778 */
779 function optionsPanel( $defaults, $nondefaults ) {
780 global $wgRCLinkLimits, $wgRCLinkDays;
781
782 $options = $nondefaults + $defaults;
783
784 $note = '';
785 if( !wfEmptyMsg( 'rclegend' ) ) {
786 $note .= '<div class="mw-rclegend">' .
787 wfMsgExt( 'rclegend', array( 'parseinline' ) ) . "</div>\n";
788 }
789 if( $options['from'] ) {
790 $note .= wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
791 $this->getLanguage()->formatNum( $options['limit'] ),
792 $this->getLanguage()->timeanddate( $options['from'], true ),
793 $this->getLanguage()->date( $options['from'], true ),
794 $this->getLanguage()->time( $options['from'], true ) ) . '<br />';
795 }
796
797 # Sort data for display and make sure it's unique after we've added user data.
798 $wgRCLinkLimits[] = $options['limit'];
799 $wgRCLinkDays[] = $options['days'];
800 sort( $wgRCLinkLimits );
801 sort( $wgRCLinkDays );
802 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
803 $wgRCLinkDays = array_unique( $wgRCLinkDays );
804
805 // limit links
806 foreach( $wgRCLinkLimits as $value ) {
807 $cl[] = $this->makeOptionsLink( $this->getLanguage()->formatNum( $value ),
808 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
809 }
810 $cl = $this->getLanguage()->pipeList( $cl );
811
812 // day links, reset 'from' to none
813 foreach( $wgRCLinkDays as $value ) {
814 $dl[] = $this->makeOptionsLink( $this->getLanguage()->formatNum( $value ),
815 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
816 }
817 $dl = $this->getLanguage()->pipeList( $dl );
818
819
820 // show/hide links
821 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
822 $filters = array(
823 'hideminor' => 'rcshowhideminor',
824 'hidebots' => 'rcshowhidebots',
825 'hideanons' => 'rcshowhideanons',
826 'hideliu' => 'rcshowhideliu',
827 'hidepatrolled' => 'rcshowhidepatr',
828 'hidemyself' => 'rcshowhidemine'
829 );
830 foreach ( $this->getCustomFilters() as $key => $params ) {
831 $filters[$key] = $params['msg'];
832 }
833 // Disable some if needed
834 if ( !$this->getUser()->useRCPatrol() ) {
835 unset( $filters['hidepatrolled'] );
836 }
837
838 $links = array();
839 foreach ( $filters as $key => $msg ) {
840 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
841 array( $key => 1-$options[$key] ), $nondefaults );
842 $links[] = wfMsgHtml( $msg, $link );
843 }
844
845 // show from this onward link
846 $timestamp = wfTimestampNow();
847 $now = $this->getLanguage()->timeanddate( $timestamp, true );
848 $tl = $this->makeOptionsLink(
849 $now, array( 'from' => $timestamp ), $nondefaults
850 );
851
852 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
853 $cl, $dl, $this->getLanguage()->pipeList( $links ) );
854 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
855 return "{$note}$rclinks<br />$rclistfrom";
856 }
857
858 /**
859 * add javascript specific to the [[Special:RecentChanges]] page
860 */
861 function addRecentChangesJS() {
862 $this->getOutput()->addModules( array(
863 'mediawiki.special.recentchanges',
864 ) );
865 }
866 }