patch from MrBlueSky, resloves bug 25909. Add dropdown list for tags in recent change...
[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 $opts->add( 'tagfilterdropdown', '' );
64 return $opts;
65 }
66
67 /**
68 * Create a FormOptions object with options as specified by the user
69 *
70 * @param $parameters array
71 *
72 * @return FormOptions
73 */
74 public function setup( $parameters ) {
75 $opts = $this->getDefaultOptions();
76
77 foreach( $this->getCustomFilters() as $key => $params ) {
78 $opts->add( $key, $params['default'] );
79 }
80
81 $opts->fetchValuesFromRequest( $this->getRequest() );
82
83 // Give precedence to subpage syntax
84 if( $parameters !== null ) {
85 $this->parseParameters( $parameters, $opts );
86 }
87
88 $opts->validateIntBounds( 'limit', 0, 5000 );
89 return $opts;
90 }
91
92 /**
93 * Get custom show/hide filters
94 *
95 * @return Array Map of filter URL param names to properties (msg/default)
96 */
97 protected function getCustomFilters() {
98 if ( $this->customFilters === null ) {
99 $this->customFilters = array();
100 wfRunHooks( 'SpecialRecentChangesFilters', array( $this, &$this->customFilters ) );
101 }
102 return $this->customFilters;
103 }
104
105 /**
106 * Create a FormOptions object specific for feed requests and return it
107 *
108 * @return FormOptions
109 */
110 public function feedSetup() {
111 global $wgFeedLimit;
112 $opts = $this->getDefaultOptions();
113 # Feed is cached on limit,hideminor,namespace; other params would randomly not work
114 $opts->fetchValuesFromRequest( $this->getRequest(), array( 'limit', 'hideminor', 'namespace' ) );
115 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
116 return $opts;
117 }
118
119 /**
120 * Get the current FormOptions for this request
121 */
122 public function getOptions() {
123 if ( $this->rcOptions === null ) {
124 if ( $this->including() ) {
125 $isFeed = false;
126 } else {
127 $isFeed = (bool)$this->getRequest()->getVal( 'feed' );
128 }
129 $this->rcOptions = $isFeed ? $this->feedSetup() : $this->setup( $this->rcSubpage );
130 }
131 return $this->rcOptions;
132 }
133
134
135 /**
136 * Main execution point
137 *
138 * @param $subpage String
139 */
140 public function execute( $subpage ) {
141 $this->rcSubpage = $subpage;
142 $feedFormat = $this->including() ? null : $this->getRequest()->getVal( 'feed' );
143
144 # 10 seconds server-side caching max
145 $this->getOutput()->setSquidMaxage( 10 );
146 # Check if the client has a cached version
147 $lastmod = $this->checkLastModified( $feedFormat );
148 if( $lastmod === false ) {
149 return;
150 }
151
152 $opts = $this->getOptions();
153 $this->setHeaders();
154 $this->outputHeader();
155 $this->addRecentChangesJS();
156
157 // Fetch results, prepare a batch link existence check query
158 $conds = $this->buildMainQueryConds( $opts );
159 $rows = $this->doMainQuery( $conds, $opts );
160 if( $rows === false ){
161 if( !$this->including() ) {
162 $this->doHeader( $opts );
163 }
164 return;
165 }
166
167 if( !$feedFormat ) {
168 $batch = new LinkBatch;
169 foreach( $rows as $row ) {
170 $batch->add( NS_USER, $row->rc_user_text );
171 $batch->add( NS_USER_TALK, $row->rc_user_text );
172 $batch->add( $row->rc_namespace, $row->rc_title );
173 }
174 $batch->execute();
175 }
176 if( $feedFormat ) {
177 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
178 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
179 } else {
180 $this->webOutput( $rows, $opts );
181 }
182
183 $rows->free();
184 }
185
186 /**
187 * Return an array with a ChangesFeed object and ChannelFeed object
188 *
189 * @return Array
190 */
191 public function getFeedObject( $feedFormat ){
192 $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
193 $formatter = $changesFeed->getFeedObject(
194 wfMsgForContent( 'recentchanges' ),
195 wfMsgForContent( 'recentchanges-feed-description' ),
196 $this->getTitle()->getFullURL()
197 );
198 return array( $changesFeed, $formatter );
199 }
200
201 /**
202 * Process $par and put options found if $opts
203 * Mainly used when including the page
204 *
205 * @param $par String
206 * @param $opts FormOptions
207 */
208 public function parseParameters( $par, FormOptions $opts ) {
209 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
210 foreach( $bits as $bit ) {
211 if( 'hidebots' === $bit ) {
212 $opts['hidebots'] = true;
213 }
214 if( 'bots' === $bit ) {
215 $opts['hidebots'] = false;
216 }
217 if( 'hideminor' === $bit ) {
218 $opts['hideminor'] = true;
219 }
220 if( 'minor' === $bit ) {
221 $opts['hideminor'] = false;
222 }
223 if( 'hideliu' === $bit ) {
224 $opts['hideliu'] = true;
225 }
226 if( 'hidepatrolled' === $bit ) {
227 $opts['hidepatrolled'] = true;
228 }
229 if( 'hideanons' === $bit ) {
230 $opts['hideanons'] = true;
231 }
232 if( 'hidemyself' === $bit ) {
233 $opts['hidemyself'] = true;
234 }
235
236 if( is_numeric( $bit ) ) {
237 $opts['limit'] = $bit;
238 }
239
240 $m = array();
241 if( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
242 $opts['limit'] = $m[1];
243 }
244 if( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
245 $opts['days'] = $m[1];
246 }
247 if( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
248 $opts['namespace'] = $m[1];
249 }
250 }
251 }
252
253 /**
254 * Get last modified date, for client caching
255 * Don't use this if we are using the patrol feature, patrol changes don't
256 * update the timestamp
257 *
258 * @param $feedFormat String
259 * @return String or false
260 */
261 public function checkLastModified( $feedFormat ) {
262 $dbr = wfGetDB( DB_SLAVE );
263 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
264 if( $feedFormat || !$this->getUser()->useRCPatrol() ) {
265 if( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
266 # Client cache fresh and headers sent, nothing more to do.
267 return false;
268 }
269 }
270 return $lastmod;
271 }
272
273 /**
274 * Return an array of conditions depending of options set in $opts
275 *
276 * @param $opts FormOptions
277 * @return array
278 */
279 public function buildMainQueryConds( FormOptions $opts ) {
280 $dbr = wfGetDB( DB_SLAVE );
281 $conds = array();
282
283 # It makes no sense to hide both anons and logged-in users
284 # Where this occurs, force anons to be shown
285 $forcebot = false;
286 if( $opts['hideanons'] && $opts['hideliu'] ){
287 # Check if the user wants to show bots only
288 if( $opts['hidebots'] ){
289 $opts['hideanons'] = false;
290 } else {
291 $forcebot = true;
292 $opts['hidebots'] = false;
293 }
294 }
295
296 // Calculate cutoff
297 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
298 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
299 $cutoff = $dbr->timestamp( $cutoff_unixtime );
300
301 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
302 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW,$cutoff) ) {
303 $cutoff = $dbr->timestamp($opts['from']);
304 } else {
305 $opts->reset( 'from' );
306 }
307
308 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
309
310 $hidePatrol = $this->getUser()->useRCPatrol() && $opts['hidepatrolled'];
311 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
312 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
313
314 if( $opts['hideminor'] ) {
315 $conds['rc_minor'] = 0;
316 }
317 if( $opts['hidebots'] ) {
318 $conds['rc_bot'] = 0;
319 }
320 if( $hidePatrol ) {
321 $conds['rc_patrolled'] = 0;
322 }
323 if( $forcebot ) {
324 $conds['rc_bot'] = 1;
325 }
326 if( $hideLoggedInUsers ) {
327 $conds[] = 'rc_user = 0';
328 }
329 if( $hideAnonymousUsers ) {
330 $conds[] = 'rc_user != 0';
331 }
332
333 if( $opts['hidemyself'] ) {
334 if( $this->getUser()->getId() ) {
335 $conds[] = 'rc_user != ' . $dbr->addQuotes( $this->getUser()->getId() );
336 } else {
337 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $this->getUser()->getName() );
338 }
339 }
340
341 # Namespace filtering
342 if( $opts['namespace'] !== '' ) {
343 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
344 $operator = $opts['invert'] ? '!=' : '=';
345 $boolean = $opts['invert'] ? 'AND' : 'OR';
346
347 # namespace association (bug 2429)
348 if( !$opts['associated'] ) {
349 $condition = "rc_namespace $operator $selectedNS";
350 } else {
351 # Also add the associated namespace
352 $associatedNS = $dbr->addQuotes(
353 MWNamespace::getAssociated( $opts['namespace'] )
354 );
355 $condition = "(rc_namespace $operator $selectedNS "
356 . $boolean
357 . " rc_namespace $operator $associatedNS)";
358 }
359
360 $conds[] = $condition;
361 }
362 return $conds;
363 }
364
365 /**
366 * Process the query
367 *
368 * @param $conds Array
369 * @param $opts FormOptions
370 * @return database result or false (for Recentchangeslinked only)
371 */
372 public function doMainQuery( $conds, $opts ) {
373 $tables = array( 'recentchanges' );
374 $join_conds = array();
375 $query_options = array(
376 'USE INDEX' => array( 'recentchanges' => 'rc_timestamp' )
377 );
378
379 $uid = $this->getUser()->getId();
380 $dbr = wfGetDB( DB_SLAVE );
381 $limit = $opts['limit'];
382 $namespace = $opts['namespace'];
383 $invert = $opts['invert'];
384 $associated = $opts['associated'];
385
386 $fields = array( $dbr->tableName( 'recentchanges' ) . '.*' ); // all rc columns
387 // JOIN on watchlist for users
388 if ( $uid ) {
389 $tables[] = 'watchlist';
390 $fields[] = 'wl_user';
391 $fields[] = 'wl_notificationtimestamp';
392 $join_conds['watchlist'] = array('LEFT JOIN',
393 "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace");
394 }
395 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
396 $tables[] = 'page';
397 $fields[] = 'page_latest';
398 $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
399 }
400 if ( !$this->including() ) {
401 // Tag stuff.
402 // Doesn't work when transcluding. See bug 23293
403 ChangeTags::modifyDisplayQuery(
404 $tables, $fields, $conds, $join_conds, $query_options
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', 'tagfilterdropdown'
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::buildTagFilterWithDropdown( 'tag-filter-dropdown-list',
620 $opts['tagfilter'], $opts['tagfilterdropdown'] );
621 if ( count( $tagFilter ) ) {
622 $extraOpts['tagfilter'] = implode( '&#160;', $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 $this->getOutput()->addWikiText(
637 Html::rawElement( 'p',
638 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
639 "\n" . wfMsgForContentNoTrans( 'recentchangestext' ) . "\n"
640 ), false );
641 }
642
643 /**
644 * Send the text to be displayed after the options, for use in
645 * Recentchangeslinked
646 *
647 * @param $opts FormOptions
648 */
649 function setBottomText( FormOptions $opts ) {}
650
651 /**
652 * Creates the choose namespace selection
653 *
654 * @todo Uses radio buttons (HASHAR)
655 * @param $opts FormOptions
656 * @return String
657 */
658 protected function namespaceFilterForm( FormOptions $opts ) {
659 $nsSelect = Xml::namespaceSelector( $opts['namespace'], '' );
660 $nsLabel = Xml::label( wfMsg( 'namespace' ), 'namespace' );
661 $invert = Xml::checkLabel(
662 wfMsg( 'invert' ), 'invert', 'nsinvert',
663 $opts['invert'],
664 array( 'title' => wfMsg( 'tooltip-invert' ) )
665 );
666 $associated = Xml::checkLabel(
667 wfMsg( 'namespace_association' ), 'associated', 'nsassociated',
668 $opts['associated'],
669 array( 'title' => wfMsg( 'tooltip-namespace_association' ) )
670 );
671 return array( $nsLabel, "$nsSelect $invert $associated" );
672 }
673
674 /**
675 * Create a input to filter changes by categories
676 *
677 * @param $opts FormOptions
678 * @return Array
679 */
680 protected function categoryFilterForm( FormOptions $opts ) {
681 list( $label, $input ) = Xml::inputLabelSep( wfMsg( 'rc_categories' ),
682 'categories', 'mw-categories', false, $opts['categories'] );
683
684 $input .= ' ' . Xml::checkLabel( wfMsg( 'rc_categories_any' ),
685 'categories_any', 'mw-categories_any', $opts['categories_any'] );
686
687 return array( $label, $input );
688 }
689
690 /**
691 * Filter $rows by categories set in $opts
692 *
693 * @param $rows Array of database rows
694 * @param $opts FormOptions
695 */
696 function filterByCategories( &$rows, FormOptions $opts ) {
697 $categories = array_map( 'trim', explode( '|' , $opts['categories'] ) );
698
699 if( !count( $categories ) ) {
700 return;
701 }
702
703 # Filter categories
704 $cats = array();
705 foreach( $categories as $cat ) {
706 $cat = trim( $cat );
707 if( $cat == '' ) {
708 continue;
709 }
710 $cats[] = $cat;
711 }
712
713 # Filter articles
714 $articles = array();
715 $a2r = array();
716 $rowsarr = array();
717 foreach( $rows as $k => $r ) {
718 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
719 $id = $nt->getArticleID();
720 if( $id == 0 ) {
721 continue; # Page might have been deleted...
722 }
723 if( !in_array( $id, $articles ) ) {
724 $articles[] = $id;
725 }
726 if( !isset( $a2r[$id] ) ) {
727 $a2r[$id] = array();
728 }
729 $a2r[$id][] = $k;
730 $rowsarr[$k] = $r;
731 }
732
733 # Shortcut?
734 if( !count( $articles ) || !count( $cats ) ) {
735 return;
736 }
737
738 # Look up
739 $c = new Categoryfinder;
740 $c->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
741 $match = $c->run();
742
743 # Filter
744 $newrows = array();
745 foreach( $match as $id ) {
746 foreach( $a2r[$id] as $rev ) {
747 $k = $rev;
748 $newrows[$k] = $rowsarr[$k];
749 }
750 }
751 $rows = $newrows;
752 }
753
754 /**
755 * Makes change an option link which carries all the other options
756 *
757 * @param $title Title
758 * @param $override Array: options to override
759 * @param $options Array: current options
760 * @param $active Boolean: whether to show the link in bold
761 */
762 function makeOptionsLink( $title, $override, $options, $active = false ) {
763 $params = $override + $options;
764 $text = htmlspecialchars( $title );
765 if ( $active ) {
766 $text = '<strong>' . $text . '</strong>';
767 }
768 return Linker::linkKnown( $this->getTitle(), $text, array(), $params );
769 }
770
771 /**
772 * Creates the options panel.
773 *
774 * @param $defaults Array
775 * @param $nondefaults Array
776 */
777 function optionsPanel( $defaults, $nondefaults ) {
778 global $wgRCLinkLimits, $wgRCLinkDays;
779
780 $options = $nondefaults + $defaults;
781
782 $note = '';
783 if( !wfEmptyMsg( 'rclegend' ) ) {
784 $note .= '<div class="mw-rclegend">' .
785 wfMsgExt( 'rclegend', array( 'parseinline' ) ) . "</div>\n";
786 }
787 if( $options['from'] ) {
788 $note .= wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
789 $this->getLanguage()->formatNum( $options['limit'] ),
790 $this->getLanguage()->timeanddate( $options['from'], true ),
791 $this->getLanguage()->date( $options['from'], true ),
792 $this->getLanguage()->time( $options['from'], true ) ) . '<br />';
793 }
794
795 # Sort data for display and make sure it's unique after we've added user data.
796 $wgRCLinkLimits[] = $options['limit'];
797 $wgRCLinkDays[] = $options['days'];
798 sort( $wgRCLinkLimits );
799 sort( $wgRCLinkDays );
800 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
801 $wgRCLinkDays = array_unique( $wgRCLinkDays );
802
803 // limit links
804 foreach( $wgRCLinkLimits as $value ) {
805 $cl[] = $this->makeOptionsLink( $this->getLanguage()->formatNum( $value ),
806 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
807 }
808 $cl = $this->getLanguage()->pipeList( $cl );
809
810 // day links, reset 'from' to none
811 foreach( $wgRCLinkDays as $value ) {
812 $dl[] = $this->makeOptionsLink( $this->getLanguage()->formatNum( $value ),
813 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
814 }
815 $dl = $this->getLanguage()->pipeList( $dl );
816
817
818 // show/hide links
819 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
820 $filters = array(
821 'hideminor' => 'rcshowhideminor',
822 'hidebots' => 'rcshowhidebots',
823 'hideanons' => 'rcshowhideanons',
824 'hideliu' => 'rcshowhideliu',
825 'hidepatrolled' => 'rcshowhidepatr',
826 'hidemyself' => 'rcshowhidemine'
827 );
828 foreach ( $this->getCustomFilters() as $key => $params ) {
829 $filters[$key] = $params['msg'];
830 }
831 // Disable some if needed
832 if ( !$this->getUser()->useRCPatrol() ) {
833 unset( $filters['hidepatrolled'] );
834 }
835
836 $links = array();
837 foreach ( $filters as $key => $msg ) {
838 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
839 array( $key => 1-$options[$key] ), $nondefaults );
840 $links[] = wfMsgHtml( $msg, $link );
841 }
842
843 // show from this onward link
844 $timestamp = wfTimestampNow();
845 $now = $this->getLanguage()->timeanddate( $timestamp, true );
846 $tl = $this->makeOptionsLink(
847 $now, array( 'from' => $timestamp ), $nondefaults
848 );
849
850 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
851 $cl, $dl, $this->getLanguage()->pipeList( $links ) );
852 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
853 return "{$note}$rclinks<br />$rclistfrom";
854 }
855
856 /**
857 * add javascript specific to the [[Special:RecentChanges]] page
858 */
859 function addRecentChangesJS() {
860 $this->getOutput()->addModules( array(
861 'mediawiki.special.recentchanges',
862 ) );
863 }
864 }