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