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