Merge "(bug 43137) Don't return the sha1 of revisions through the API if the content...
[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 $feedQuery = $this->getFeedQuery();
484 if ( $feedQuery !== '' ) {
485 $this->getOutput()->setFeedAppendQuery( $feedQuery );
486 } else {
487 $this->getOutput()->setFeedAppendQuery( false );
488 }
489
490 if( $wgAllowCategorizedRecentChanges ) {
491 $this->filterByCategories( $rows, $opts );
492 }
493
494 $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
495 $watcherCache = array();
496
497 $dbr = wfGetDB( DB_SLAVE );
498
499 $counter = 1;
500 $list = ChangesList::newFromContext( $this->getContext() );
501
502 $s = $list->beginRecentChangesList();
503 foreach( $rows as $obj ) {
504 if( $limit == 0 ) {
505 break;
506 }
507 $rc = RecentChange::newFromRow( $obj );
508 $rc->counter = $counter++;
509 # Check if the page has been updated since the last visit
510 if( $wgShowUpdatedMarker && !empty( $obj->wl_notificationtimestamp ) ) {
511 $rc->notificationtimestamp = ( $obj->rc_timestamp >= $obj->wl_notificationtimestamp );
512 } else {
513 $rc->notificationtimestamp = false; // Default
514 }
515 # Check the number of users watching the page
516 $rc->numberofWatchingusers = 0; // Default
517 if( $showWatcherCount && $obj->rc_namespace >= 0 ) {
518 if( !isset( $watcherCache[$obj->rc_namespace][$obj->rc_title] ) ) {
519 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
520 $dbr->selectField(
521 'watchlist',
522 'COUNT(*)',
523 array(
524 'wl_namespace' => $obj->rc_namespace,
525 'wl_title' => $obj->rc_title,
526 ),
527 __METHOD__ . '-watchers'
528 );
529 }
530 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
531 }
532 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
533 --$limit;
534 }
535 $s .= $list->endRecentChangesList();
536 $this->getOutput()->addHTML( $s );
537 }
538
539 /**
540 * Get the query string to append to feed link URLs.
541 *
542 * @return string
543 */
544 public function getFeedQuery() {
545 global $wgFeedLimit;
546
547 $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
548 $options = $this->getOptions()->getChangedValues();
549
550 // wfArrayToCgi() omits options set to null or false
551 foreach ( $options as &$value ) {
552 if ( $value === false ) {
553 $value = '0';
554 }
555 }
556 unset( $value );
557
558 return wfArrayToCgi( $options );
559 }
560
561 /**
562 * Return the text to be displayed above the changes
563 *
564 * @param $opts FormOptions
565 * @return String: XHTML
566 */
567 public function doHeader( $opts ) {
568 global $wgScript;
569
570 $this->setTopText( $opts );
571
572 $defaults = $opts->getAllValues();
573 $nondefaults = $opts->getChangedValues();
574 $opts->consumeValues( array(
575 'namespace', 'invert', 'associated', 'tagfilter',
576 'categories', 'categories_any'
577 ) );
578
579 $panel = array();
580 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
581 $panel[] = '<hr />';
582
583 $extraOpts = $this->getExtraOptions( $opts );
584 $extraOptsCount = count( $extraOpts );
585 $count = 0;
586 $submit = ' ' . Xml::submitbutton( $this->msg( 'allpagessubmit' )->text() );
587
588 $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
589 foreach( $extraOpts as $name => $optionRow ) {
590 # Add submit button to the last row only
591 ++$count;
592 $addSubmit = ( $count === $extraOptsCount ) ? $submit : '';
593
594 $out .= Xml::openElement( 'tr' );
595 if( is_array( $optionRow ) ) {
596 $out .= Xml::tags( 'td', array( 'class' => 'mw-label mw-' . $name . '-label' ), $optionRow[0] );
597 $out .= Xml::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
598 } else {
599 $out .= Xml::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
600 }
601 $out .= Xml::closeElement( 'tr' );
602 }
603 $out .= Xml::closeElement( 'table' );
604
605 $unconsumed = $opts->getUnconsumedValues();
606 foreach( $unconsumed as $key => $value ) {
607 $out .= Html::hidden( $key, $value );
608 }
609
610 $t = $this->getTitle();
611 $out .= Html::hidden( 'title', $t->getPrefixedText() );
612 $form = Xml::tags( 'form', array( 'action' => $wgScript ), $out );
613 $panel[] = $form;
614 $panelString = implode( "\n", $panel );
615
616 $this->getOutput()->addHTML(
617 Xml::fieldset( $this->msg( 'recentchanges-legend' )->text(), $panelString, array( 'class' => 'rcoptions' ) )
618 );
619
620 $this->setBottomText( $opts );
621 }
622
623 /**
624 * Get options to be displayed in a form
625 *
626 * @param $opts FormOptions
627 * @return Array
628 */
629 function getExtraOptions( $opts ) {
630 $extraOpts = array();
631 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
632
633 global $wgAllowCategorizedRecentChanges;
634 if( $wgAllowCategorizedRecentChanges ) {
635 $extraOpts['category'] = $this->categoryFilterForm( $opts );
636 }
637
638 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
639 if ( count( $tagFilter ) ) {
640 $extraOpts['tagfilter'] = $tagFilter;
641 }
642
643 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
644 return $extraOpts;
645 }
646
647 /**
648 * Send the text to be displayed above the options
649 *
650 * @param $opts FormOptions
651 */
652 function setTopText( FormOptions $opts ) {
653 global $wgContLang;
654
655 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
656 if ( !$message->isDisabled() ) {
657 $this->getOutput()->addWikiText(
658 Html::rawElement( 'p',
659 array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
660 "\n" . $message->plain() . "\n"
661 ),
662 /* $lineStart */ false,
663 /* $interface */ false
664 );
665 }
666 }
667
668 /**
669 * Send the text to be displayed after the options, for use in
670 * Recentchangeslinked
671 *
672 * @param $opts FormOptions
673 */
674 function setBottomText( FormOptions $opts ) {}
675
676 /**
677 * Creates the choose namespace selection
678 *
679 * @todo Uses radio buttons (HASHAR)
680 * @param $opts FormOptions
681 * @return String
682 */
683 protected function namespaceFilterForm( FormOptions $opts ) {
684 $nsSelect = Html::namespaceSelector(
685 array( 'selected' => $opts['namespace'], 'all' => '' ),
686 array( 'name' => 'namespace', 'id' => 'namespace' )
687 );
688 $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
689 $invert = Xml::checkLabel(
690 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
691 $opts['invert'],
692 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
693 );
694 $associated = Xml::checkLabel(
695 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
696 $opts['associated'],
697 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
698 );
699 return array( $nsLabel, "$nsSelect $invert $associated" );
700 }
701
702 /**
703 * Create a input to filter changes by categories
704 *
705 * @param $opts FormOptions
706 * @return Array
707 */
708 protected function categoryFilterForm( FormOptions $opts ) {
709 list( $label, $input ) = Xml::inputLabelSep( $this->msg( 'rc_categories' )->text(),
710 'categories', 'mw-categories', false, $opts['categories'] );
711
712 $input .= ' ' . Xml::checkLabel( $this->msg( 'rc_categories_any' )->text(),
713 'categories_any', 'mw-categories_any', $opts['categories_any'] );
714
715 return array( $label, $input );
716 }
717
718 /**
719 * Filter $rows by categories set in $opts
720 *
721 * @param $rows Array of database rows
722 * @param $opts FormOptions
723 */
724 function filterByCategories( &$rows, FormOptions $opts ) {
725 $categories = array_map( 'trim', explode( '|' , $opts['categories'] ) );
726
727 if( !count( $categories ) ) {
728 return;
729 }
730
731 # Filter categories
732 $cats = array();
733 foreach( $categories as $cat ) {
734 $cat = trim( $cat );
735 if( $cat == '' ) {
736 continue;
737 }
738 $cats[] = $cat;
739 }
740
741 # Filter articles
742 $articles = array();
743 $a2r = array();
744 $rowsarr = array();
745 foreach( $rows as $k => $r ) {
746 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
747 $id = $nt->getArticleID();
748 if( $id == 0 ) {
749 continue; # Page might have been deleted...
750 }
751 if( !in_array( $id, $articles ) ) {
752 $articles[] = $id;
753 }
754 if( !isset( $a2r[$id] ) ) {
755 $a2r[$id] = array();
756 }
757 $a2r[$id][] = $k;
758 $rowsarr[$k] = $r;
759 }
760
761 # Shortcut?
762 if( !count( $articles ) || !count( $cats ) ) {
763 return;
764 }
765
766 # Look up
767 $c = new Categoryfinder;
768 $c->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
769 $match = $c->run();
770
771 # Filter
772 $newrows = array();
773 foreach( $match as $id ) {
774 foreach( $a2r[$id] as $rev ) {
775 $k = $rev;
776 $newrows[$k] = $rowsarr[$k];
777 }
778 }
779 $rows = $newrows;
780 }
781
782 /**
783 * Makes change an option link which carries all the other options
784 *
785 * @param $title Title
786 * @param $override Array: options to override
787 * @param $options Array: current options
788 * @param $active Boolean: whether to show the link in bold
789 * @return string
790 */
791 function makeOptionsLink( $title, $override, $options, $active = false ) {
792 $params = $override + $options;
793
794 // Bug 36524: false values have be converted to "0" otherwise
795 // wfArrayToCgi() will omit it them.
796 foreach ( $params as &$value ) {
797 if ( $value === false ) {
798 $value = '0';
799 }
800 }
801 unset( $value );
802
803 $text = htmlspecialchars( $title );
804 if ( $active ) {
805 $text = '<strong>' . $text . '</strong>';
806 }
807 return Linker::linkKnown( $this->getTitle(), $text, array(), $params );
808 }
809
810 /**
811 * Creates the options panel.
812 *
813 * @param $defaults Array
814 * @param $nondefaults Array
815 * @return string
816 */
817 function optionsPanel( $defaults, $nondefaults ) {
818 global $wgRCLinkLimits, $wgRCLinkDays;
819
820 $options = $nondefaults + $defaults;
821
822 $note = '';
823 $msg = $this->msg( 'rclegend' );
824 if( !$msg->isDisabled() ) {
825 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
826 }
827
828 $lang = $this->getLanguage();
829 $user = $this->getUser();
830 if( $options['from'] ) {
831 $note .= $this->msg( 'rcnotefrom' )->numParams( $options['limit'] )->params(
832 $lang->userTimeAndDate( $options['from'], $user ),
833 $lang->userDate( $options['from'], $user ),
834 $lang->userTime( $options['from'], $user ) )->parse() . '<br />';
835 }
836
837 # Sort data for display and make sure it's unique after we've added user data.
838 $wgRCLinkLimits[] = $options['limit'];
839 $wgRCLinkDays[] = $options['days'];
840 sort( $wgRCLinkLimits );
841 sort( $wgRCLinkDays );
842 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
843 $wgRCLinkDays = array_unique( $wgRCLinkDays );
844
845 // limit links
846 foreach( $wgRCLinkLimits as $value ) {
847 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
848 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] );
849 }
850 $cl = $lang->pipeList( $cl );
851
852 // day links, reset 'from' to none
853 foreach( $wgRCLinkDays as $value ) {
854 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
855 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] );
856 }
857 $dl = $lang->pipeList( $dl );
858
859
860 // show/hide links
861 $showhide = array( $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() );
862 $filters = array(
863 'hideminor' => 'rcshowhideminor',
864 'hidebots' => 'rcshowhidebots',
865 'hideanons' => 'rcshowhideanons',
866 'hideliu' => 'rcshowhideliu',
867 'hidepatrolled' => 'rcshowhidepatr',
868 'hidemyself' => 'rcshowhidemine'
869 );
870 foreach ( $this->getCustomFilters() as $key => $params ) {
871 $filters[$key] = $params['msg'];
872 }
873 // Disable some if needed
874 if ( !$user->useRCPatrol() ) {
875 unset( $filters['hidepatrolled'] );
876 }
877
878 $links = array();
879 foreach ( $filters as $key => $msg ) {
880 $link = $this->makeOptionsLink( $showhide[1 - $options[$key]],
881 array( $key => 1-$options[$key] ), $nondefaults );
882 $links[] = $this->msg( $msg )->rawParams( $link )->escaped();
883 }
884
885 // show from this onward link
886 $timestamp = wfTimestampNow();
887 $now = $lang->userTimeAndDate( $timestamp, $user );
888 $tl = $this->makeOptionsLink(
889 $now, array( 'from' => $timestamp ), $nondefaults
890 );
891
892 $rclinks = $this->msg( 'rclinks' )->rawParams( $cl, $dl, $lang->pipeList( $links ) )->parse();
893 $rclistfrom = $this->msg( 'rclistfrom' )->rawParams( $tl )->parse();
894 return "{$note}$rclinks<br />$rclistfrom";
895 }
896
897 /**
898 * add javascript specific to the [[Special:RecentChanges]] page
899 */
900 function addRecentChangesJS() {
901 $this->getOutput()->addModules( array(
902 'mediawiki.special.recentchanges',
903 ) );
904 }
905 }