phpunit: Avoid use of deprecated getMock for PHPUnit 5 compat
[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 use MediaWiki\MediaWikiServices;
25 use Wikimedia\Rdbms\ResultWrapper;
26 use Wikimedia\Rdbms\FakeResultWrapper;
27
28 /**
29 * A special page that lists last changes made to the wiki
30 *
31 * @ingroup SpecialPage
32 */
33 class SpecialRecentChanges extends ChangesListSpecialPage {
34 // @codingStandardsIgnoreStart Needed "useless" override to change parameters.
35 public function __construct( $name = 'Recentchanges', $restriction = '' ) {
36 parent::__construct( $name, $restriction );
37 }
38 // @codingStandardsIgnoreEnd
39
40 /**
41 * Main execution point
42 *
43 * @param string $subpage
44 */
45 public function execute( $subpage ) {
46 // Backwards-compatibility: redirect to new feed URLs
47 $feedFormat = $this->getRequest()->getVal( 'feed' );
48 if ( !$this->including() && $feedFormat ) {
49 $query = $this->getFeedQuery();
50 $query['feedformat'] = $feedFormat === 'atom' ? 'atom' : 'rss';
51 $this->getOutput()->redirect( wfAppendQuery( wfScript( 'api' ), $query ) );
52
53 return;
54 }
55
56 // 10 seconds server-side caching max
57 $out = $this->getOutput();
58 $out->setCdnMaxage( 10 );
59 // Check if the client has a cached version
60 $lastmod = $this->checkLastModified();
61 if ( $lastmod === false ) {
62 return;
63 }
64
65 $this->addHelpLink(
66 '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Recent_changes',
67 true
68 );
69 parent::execute( $subpage );
70
71 if ( $this->isStructuredFilterUiEnabled() ) {
72 $jsData = $this->getStructuredFilterJsData();
73
74 $messages = [];
75 foreach ( $jsData['messageKeys'] as $key ){
76 $messages[$key] = $this->msg( $key )->plain();
77 }
78
79 $out->addHTML(
80 ResourceLoader::makeInlineScript(
81 ResourceLoader::makeMessageSetScript( $messages )
82 )
83 );
84
85 $out->addJsConfigVars( 'wgStructuredChangeFilters', $jsData['groups'] );
86 }
87 }
88
89 /**
90 * @inheritdoc
91 */
92 protected function transformFilterDefinition( array $filterDefinition ) {
93 if ( isset( $filterDefinition['showHideSuffix'] ) ) {
94 $filterDefinition['showHide'] = 'rc' . $filterDefinition['showHideSuffix'];
95 }
96
97 return $filterDefinition;
98 }
99
100 /**
101 * @inheritdoc
102 */
103 protected function registerFilters() {
104 parent::registerFilters();
105
106 $user = $this->getUser();
107
108 $significance = $this->getFilterGroup( 'significance' );
109 $hideMinor = $significance->getFilter( 'hideminor' );
110 $hideMinor->setDefault( $user->getBoolOption( 'hideminor' ) );
111
112 $automated = $this->getFilterGroup( 'automated' );
113 $hideBots = $automated->getFilter( 'hidebots' );
114 $hideBots->setDefault( true );
115
116 $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
117 if ( $reviewStatus !== null ) {
118 // Conditional on feature being available and rights
119 $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
120 $hidePatrolled->setDefault( $user->getBoolOption( 'hidepatrolled' ) );
121 }
122
123 $changeType = $this->getFilterGroup( 'changeType' );
124 $hideCategorization = $changeType->getFilter( 'hidecategorization' );
125 if ( $hideCategorization !== null ) {
126 // Conditional on feature being available
127 $hideCategorization->setDefault( $user->getBoolOption( 'hidecategorization' ) );
128 }
129 }
130
131 /**
132 * Get a FormOptions object containing the default options
133 *
134 * @return FormOptions
135 */
136 public function getDefaultOptions() {
137 $opts = parent::getDefaultOptions();
138 $user = $this->getUser();
139
140 $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
141 $opts->add( 'limit', $user->getIntOption( 'rclimit' ) );
142 $opts->add( 'from', '' );
143
144 $opts->add( 'categories', '' );
145 $opts->add( 'categories_any', false );
146 $opts->add( 'tagfilter', '' );
147
148 return $opts;
149 }
150
151 /**
152 * Get all custom filters
153 *
154 * @return array Map of filter URL param names to properties (msg/default)
155 */
156 protected function getCustomFilters() {
157 if ( $this->customFilters === null ) {
158 $this->customFilters = parent::getCustomFilters();
159 Hooks::run( 'SpecialRecentChangesFilters', [ $this, &$this->customFilters ], '1.23' );
160 }
161
162 return $this->customFilters;
163 }
164
165 /**
166 * Process $par and put options found in $opts. Used when including the page.
167 *
168 * @param string $par
169 * @param FormOptions $opts
170 */
171 public function parseParameters( $par, FormOptions $opts ) {
172 parent::parseParameters( $par, $opts );
173
174 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
175 foreach ( $bits as $bit ) {
176 if ( is_numeric( $bit ) ) {
177 $opts['limit'] = $bit;
178 }
179
180 $m = [];
181 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
182 $opts['limit'] = $m[1];
183 }
184 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
185 $opts['days'] = $m[1];
186 }
187 if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
188 $opts['namespace'] = $m[1];
189 }
190 if ( preg_match( '/^tagfilter=(.*)$/', $bit, $m ) ) {
191 $opts['tagfilter'] = $m[1];
192 }
193 }
194 }
195
196 public function validateOptions( FormOptions $opts ) {
197 $opts->validateIntBounds( 'limit', 0, 5000 );
198 parent::validateOptions( $opts );
199 }
200
201 /**
202 * @inheritdoc
203 */
204 protected function buildQuery( &$tables, &$fields, &$conds,
205 &$query_options, &$join_conds, FormOptions $opts ) {
206
207 $dbr = $this->getDB();
208 parent::buildQuery( $tables, $fields, $conds,
209 $query_options, $join_conds, $opts );
210
211 // Calculate cutoff
212 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
213 $cutoff_unixtime = $cutoff_unixtime - ( $cutoff_unixtime % 86400 );
214 $cutoff = $dbr->timestamp( $cutoff_unixtime );
215
216 $fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
217 if ( $fromValid && $opts['from'] > wfTimestamp( TS_MW, $cutoff ) ) {
218 $cutoff = $dbr->timestamp( $opts['from'] );
219 } else {
220 $opts->reset( 'from' );
221 }
222
223 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
224 }
225
226 /**
227 * @inheritdoc
228 */
229 protected function doMainQuery( $tables, $fields, $conds, $query_options,
230 $join_conds, FormOptions $opts ) {
231
232 $dbr = $this->getDB();
233 $user = $this->getUser();
234
235 $tables[] = 'recentchanges';
236 $fields = array_merge( RecentChange::selectFields(), $fields );
237
238 // JOIN on watchlist for users
239 if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
240 $tables[] = 'watchlist';
241 $fields[] = 'wl_user';
242 $fields[] = 'wl_notificationtimestamp';
243 $join_conds['watchlist'] = [ 'LEFT JOIN', [
244 'wl_user' => $user->getId(),
245 'wl_title=rc_title',
246 'wl_namespace=rc_namespace'
247 ] ];
248 }
249
250 if ( $user->isAllowed( 'rollback' ) ) {
251 $tables[] = 'page';
252 $fields[] = 'page_latest';
253 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
254 }
255
256 ChangeTags::modifyDisplayQuery(
257 $tables,
258 $fields,
259 $conds,
260 $join_conds,
261 $query_options,
262 $opts['tagfilter']
263 );
264
265 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
266 $opts )
267 ) {
268 return false;
269 }
270
271 // array_merge() is used intentionally here so that hooks can, should
272 // they so desire, override the ORDER BY / LIMIT condition(s); prior to
273 // MediaWiki 1.26 this used to use the plus operator instead, which meant
274 // that extensions weren't able to change these conditions
275 $query_options = array_merge( [
276 'ORDER BY' => 'rc_timestamp DESC',
277 'LIMIT' => $opts['limit'] ], $query_options );
278 $rows = $dbr->select(
279 $tables,
280 $fields,
281 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
282 // knowledge to use an index merge if it wants (it may use some other index though).
283 $conds + [ 'rc_new' => [ 0, 1 ] ],
284 __METHOD__,
285 $query_options,
286 $join_conds
287 );
288
289 // Build the final data
290 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
291 $this->filterByCategories( $rows, $opts );
292 }
293
294 return $rows;
295 }
296
297 protected function runMainQueryHook( &$tables, &$fields, &$conds,
298 &$query_options, &$join_conds, $opts
299 ) {
300 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
301 && Hooks::run(
302 'SpecialRecentChangesQuery',
303 [ &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ],
304 '1.23'
305 );
306 }
307
308 protected function getDB() {
309 return wfGetDB( DB_REPLICA, 'recentchanges' );
310 }
311
312 public function outputFeedLinks() {
313 $this->addFeedLinks( $this->getFeedQuery() );
314 }
315
316 /**
317 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
318 *
319 * @return array
320 */
321 protected function getFeedQuery() {
322 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
323 // API handles empty parameters in a different way
324 return $value !== '';
325 } );
326 $query['action'] = 'feedrecentchanges';
327 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
328 if ( $query['limit'] > $feedLimit ) {
329 $query['limit'] = $feedLimit;
330 }
331
332 return $query;
333 }
334
335 /**
336 * Build and output the actual changes list.
337 *
338 * @param ResultWrapper $rows Database rows
339 * @param FormOptions $opts
340 */
341 public function outputChangesList( $rows, $opts ) {
342 $limit = $opts['limit'];
343
344 $showWatcherCount = $this->getConfig()->get( 'RCShowWatchingUsers' )
345 && $this->getUser()->getOption( 'shownumberswatching' );
346 $watcherCache = [];
347
348 $dbr = $this->getDB();
349
350 $counter = 1;
351 $list = ChangesList::newFromContext( $this->getContext(), $this->filterGroups );
352 $list->initChangesListRows( $rows );
353
354 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
355 $rclistOutput = $list->beginRecentChangesList();
356 foreach ( $rows as $obj ) {
357 if ( $limit == 0 ) {
358 break;
359 }
360 $rc = RecentChange::newFromRow( $obj );
361
362 # Skip CatWatch entries for hidden cats based on user preference
363 if (
364 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE &&
365 !$userShowHiddenCats &&
366 $rc->getParam( 'hidden-cat' )
367 ) {
368 continue;
369 }
370
371 $rc->counter = $counter++;
372 # Check if the page has been updated since the last visit
373 if ( $this->getConfig()->get( 'ShowUpdatedMarker' )
374 && !empty( $obj->wl_notificationtimestamp )
375 ) {
376 $rc->notificationtimestamp = ( $obj->rc_timestamp >= $obj->wl_notificationtimestamp );
377 } else {
378 $rc->notificationtimestamp = false; // Default
379 }
380 # Check the number of users watching the page
381 $rc->numberofWatchingusers = 0; // Default
382 if ( $showWatcherCount && $obj->rc_namespace >= 0 ) {
383 if ( !isset( $watcherCache[$obj->rc_namespace][$obj->rc_title] ) ) {
384 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
385 MediaWikiServices::getInstance()->getWatchedItemStore()->countWatchers(
386 new TitleValue( (int)$obj->rc_namespace, $obj->rc_title )
387 );
388 }
389 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
390 }
391
392 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
393 if ( $changeLine !== false ) {
394 $rclistOutput .= $changeLine;
395 --$limit;
396 }
397 }
398 $rclistOutput .= $list->endRecentChangesList();
399
400 if ( $rows->numRows() === 0 ) {
401 $this->outputNoResults();
402 if ( !$this->including() ) {
403 $this->getOutput()->setStatusCode( 404 );
404 }
405 } else {
406 $this->getOutput()->addHTML( $rclistOutput );
407 }
408 }
409
410 /**
411 * Set the text to be displayed above the changes
412 *
413 * @param FormOptions $opts
414 * @param int $numRows Number of rows in the result to show after this header
415 */
416 public function doHeader( $opts, $numRows ) {
417 $this->setTopText( $opts );
418
419 $defaults = $opts->getAllValues();
420 $nondefaults = $opts->getChangedValues();
421
422 $panel = [];
423 $panel[] = $this->makeLegend();
424 $panel[] = $this->optionsPanel( $defaults, $nondefaults, $numRows );
425 $panel[] = '<hr />';
426
427 $extraOpts = $this->getExtraOptions( $opts );
428 $extraOptsCount = count( $extraOpts );
429 $count = 0;
430 $submit = ' ' . Xml::submitButton( $this->msg( 'recentchanges-submit' )->text() );
431
432 $out = Xml::openElement( 'table', [ 'class' => 'mw-recentchanges-table' ] );
433 foreach ( $extraOpts as $name => $optionRow ) {
434 # Add submit button to the last row only
435 ++$count;
436 $addSubmit = ( $count === $extraOptsCount ) ? $submit : '';
437
438 $out .= Xml::openElement( 'tr' );
439 if ( is_array( $optionRow ) ) {
440 $out .= Xml::tags(
441 'td',
442 [ 'class' => 'mw-label mw-' . $name . '-label' ],
443 $optionRow[0]
444 );
445 $out .= Xml::tags(
446 'td',
447 [ 'class' => 'mw-input' ],
448 $optionRow[1] . $addSubmit
449 );
450 } else {
451 $out .= Xml::tags(
452 'td',
453 [ 'class' => 'mw-input', 'colspan' => 2 ],
454 $optionRow . $addSubmit
455 );
456 }
457 $out .= Xml::closeElement( 'tr' );
458 }
459 $out .= Xml::closeElement( 'table' );
460
461 $unconsumed = $opts->getUnconsumedValues();
462 foreach ( $unconsumed as $key => $value ) {
463 $out .= Html::hidden( $key, $value );
464 }
465
466 $t = $this->getPageTitle();
467 $out .= Html::hidden( 'title', $t->getPrefixedText() );
468 $form = Xml::tags( 'form', [ 'action' => wfScript() ], $out );
469 $panel[] = $form;
470 $panelString = implode( "\n", $panel );
471
472 $rcoptions = Xml::fieldset(
473 $this->msg( 'recentchanges-legend' )->text(),
474 $panelString,
475 [ 'class' => 'rcoptions' ]
476 );
477
478 // Insert a placeholder for RCFilters
479 if ( $this->getUser()->getOption( 'rcenhancedfilters' ) ) {
480 $rcfilterContainer = Html::element(
481 'div',
482 [ 'class' => 'rcfilters-container' ]
483 );
484
485 // Wrap both with rcfilters-head
486 $this->getOutput()->addHTML(
487 Html::rawElement(
488 'div',
489 [ 'class' => 'rcfilters-head' ],
490 $rcfilterContainer . $rcoptions
491 )
492 );
493 } else {
494 $this->getOutput()->addHTML( $rcoptions );
495 }
496
497 $this->setBottomText( $opts );
498 }
499
500 /**
501 * Send the text to be displayed above the options
502 *
503 * @param FormOptions $opts Unused
504 */
505 function setTopText( FormOptions $opts ) {
506 global $wgContLang;
507
508 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
509 if ( !$message->isDisabled() ) {
510 $this->getOutput()->addWikiText(
511 Html::rawElement( 'div',
512 [ 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ],
513 "\n" . $message->plain() . "\n"
514 ),
515 /* $lineStart */ true,
516 /* $interface */ false
517 );
518 }
519 }
520
521 /**
522 * Get options to be displayed in a form
523 *
524 * @param FormOptions $opts
525 * @return array
526 */
527 function getExtraOptions( $opts ) {
528 $opts->consumeValues( [
529 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
530 ] );
531
532 $extraOpts = [];
533 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
534
535 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
536 $extraOpts['category'] = $this->categoryFilterForm( $opts );
537 }
538
539 $tagFilter = ChangeTags::buildTagFilterSelector(
540 $opts['tagfilter'], false, $this->getContext() );
541 if ( count( $tagFilter ) ) {
542 $extraOpts['tagfilter'] = $tagFilter;
543 }
544
545 // Don't fire the hook for subclasses. (Or should we?)
546 if ( $this->getName() === 'Recentchanges' ) {
547 Hooks::run( 'SpecialRecentChangesPanel', [ &$extraOpts, $opts ] );
548 }
549
550 return $extraOpts;
551 }
552
553 /**
554 * Check whether the structured filter UI is enabled
555 *
556 * @return bool
557 */
558 protected function isStructuredFilterUiEnabled() {
559 return $this->getUser()->getOption(
560 'rcenhancedfilters'
561 );
562 }
563
564 /**
565 * Add page-specific modules.
566 */
567 protected function addModules() {
568 parent::addModules();
569 $out = $this->getOutput();
570 $out->addModules( 'mediawiki.special.recentchanges' );
571 if ( $this->isStructuredFilterUiEnabled() ) {
572 $out->addModules( 'mediawiki.rcfilters.filters.ui' );
573 $out->addModuleStyles( 'mediawiki.rcfilters.filters.base.styles' );
574 }
575 }
576
577 /**
578 * Get last modified date, for client caching
579 * Don't use this if we are using the patrol feature, patrol changes don't
580 * update the timestamp
581 *
582 * @return string|bool
583 */
584 public function checkLastModified() {
585 $dbr = $this->getDB();
586 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
587
588 return $lastmod;
589 }
590
591 /**
592 * Creates the choose namespace selection
593 *
594 * @param FormOptions $opts
595 * @return string
596 */
597 protected function namespaceFilterForm( FormOptions $opts ) {
598 $nsSelect = Html::namespaceSelector(
599 [ 'selected' => $opts['namespace'], 'all' => '' ],
600 [ 'name' => 'namespace', 'id' => 'namespace' ]
601 );
602 $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
603 $invert = Xml::checkLabel(
604 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
605 $opts['invert'],
606 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
607 );
608 $associated = Xml::checkLabel(
609 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
610 $opts['associated'],
611 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
612 );
613
614 return [ $nsLabel, "$nsSelect $invert $associated" ];
615 }
616
617 /**
618 * Create an input to filter changes by categories
619 *
620 * @param FormOptions $opts
621 * @return array
622 */
623 protected function categoryFilterForm( FormOptions $opts ) {
624 list( $label, $input ) = Xml::inputLabelSep( $this->msg( 'rc_categories' )->text(),
625 'categories', 'mw-categories', false, $opts['categories'] );
626
627 $input .= ' ' . Xml::checkLabel( $this->msg( 'rc_categories_any' )->text(),
628 'categories_any', 'mw-categories_any', $opts['categories_any'] );
629
630 return [ $label, $input ];
631 }
632
633 /**
634 * Filter $rows by categories set in $opts
635 *
636 * @param ResultWrapper $rows Database rows
637 * @param FormOptions $opts
638 */
639 function filterByCategories( &$rows, FormOptions $opts ) {
640 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
641
642 if ( !count( $categories ) ) {
643 return;
644 }
645
646 # Filter categories
647 $cats = [];
648 foreach ( $categories as $cat ) {
649 $cat = trim( $cat );
650 if ( $cat == '' ) {
651 continue;
652 }
653 $cats[] = $cat;
654 }
655
656 # Filter articles
657 $articles = [];
658 $a2r = [];
659 $rowsarr = [];
660 foreach ( $rows as $k => $r ) {
661 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
662 $id = $nt->getArticleID();
663 if ( $id == 0 ) {
664 continue; # Page might have been deleted...
665 }
666 if ( !in_array( $id, $articles ) ) {
667 $articles[] = $id;
668 }
669 if ( !isset( $a2r[$id] ) ) {
670 $a2r[$id] = [];
671 }
672 $a2r[$id][] = $k;
673 $rowsarr[$k] = $r;
674 }
675
676 # Shortcut?
677 if ( !count( $articles ) || !count( $cats ) ) {
678 return;
679 }
680
681 # Look up
682 $catFind = new CategoryFinder;
683 $catFind->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
684 $match = $catFind->run();
685
686 # Filter
687 $newrows = [];
688 foreach ( $match as $id ) {
689 foreach ( $a2r[$id] as $rev ) {
690 $k = $rev;
691 $newrows[$k] = $rowsarr[$k];
692 }
693 }
694 $rows = new FakeResultWrapper( array_values( $newrows ) );
695 }
696
697 /**
698 * Makes change an option link which carries all the other options
699 *
700 * @param string $title Title
701 * @param array $override Options to override
702 * @param array $options Current options
703 * @param bool $active Whether to show the link in bold
704 * @return string
705 */
706 function makeOptionsLink( $title, $override, $options, $active = false ) {
707 $params = $override + $options;
708
709 // T38524: false values have be converted to "0" otherwise
710 // wfArrayToCgi() will omit it them.
711 foreach ( $params as &$value ) {
712 if ( $value === false ) {
713 $value = '0';
714 }
715 }
716 unset( $value );
717
718 if ( $active ) {
719 $title = new HtmlArmor( '<strong>' . htmlspecialchars( $title ) . '</strong>' );
720 }
721
722 return $this->getLinkRenderer()->makeKnownLink( $this->getPageTitle(), $title, [
723 'data-params' => json_encode( $override ),
724 'data-keys' => implode( ',', array_keys( $override ) ),
725 ], $params );
726 }
727
728 /**
729 * Creates the options panel.
730 *
731 * @param array $defaults
732 * @param array $nondefaults
733 * @param int $numRows Number of rows in the result to show after this header
734 * @return string
735 */
736 function optionsPanel( $defaults, $nondefaults, $numRows ) {
737 $options = $nondefaults + $defaults;
738
739 $note = '';
740 $msg = $this->msg( 'rclegend' );
741 if ( !$msg->isDisabled() ) {
742 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
743 }
744
745 $lang = $this->getLanguage();
746 $user = $this->getUser();
747 $config = $this->getConfig();
748 if ( $options['from'] ) {
749 $note .= $this->msg( 'rcnotefrom' )
750 ->numParams( $options['limit'] )
751 ->params(
752 $lang->userTimeAndDate( $options['from'], $user ),
753 $lang->userDate( $options['from'], $user ),
754 $lang->userTime( $options['from'], $user )
755 )
756 ->numParams( $numRows )
757 ->parse() . '<br />';
758 }
759
760 # Sort data for display and make sure it's unique after we've added user data.
761 $linkLimits = $config->get( 'RCLinkLimits' );
762 $linkLimits[] = $options['limit'];
763 sort( $linkLimits );
764 $linkLimits = array_unique( $linkLimits );
765
766 $linkDays = $config->get( 'RCLinkDays' );
767 $linkDays[] = $options['days'];
768 sort( $linkDays );
769 $linkDays = array_unique( $linkDays );
770
771 // limit links
772 $cl = [];
773 foreach ( $linkLimits as $value ) {
774 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
775 [ 'limit' => $value ], $nondefaults, $value == $options['limit'] );
776 }
777 $cl = $lang->pipeList( $cl );
778
779 // day links, reset 'from' to none
780 $dl = [];
781 foreach ( $linkDays as $value ) {
782 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
783 [ 'days' => $value, 'from' => '' ], $nondefaults, $value == $options['days'] );
784 }
785 $dl = $lang->pipeList( $dl );
786
787 $showhide = [ 'show', 'hide' ];
788
789 $links = [];
790
791 $filterGroups = $this->getFilterGroups();
792
793 $context = $this->getContext();
794 foreach ( $filterGroups as $groupName => $group ) {
795 if ( !$group->isPerGroupRequestParameter() ) {
796 foreach ( $group->getFilters() as $key => $filter ) {
797 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
798 $msg = $filter->getShowHide();
799 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
800 // Extensions can define additional filters, but don't need to define the corresponding
801 // messages. If they don't exist, just fall back to 'show' and 'hide'.
802 if ( !$linkMessage->exists() ) {
803 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
804 }
805
806 $link = $this->makeOptionsLink( $linkMessage->text(),
807 [ $key => 1 - $options[$key] ], $nondefaults );
808
809 $attribs = [
810 'class' => "$msg rcshowhideoption",
811 'data-filter-name' => $filter->getName(),
812 ];
813
814 if ( $filter->isFeatureAvailableOnStructuredUi( $this ) ) {
815 $attribs['data-feature-in-structured-ui'] = true;
816 }
817
818 $links[] = Html::rawElement(
819 'span',
820 $attribs,
821 $this->msg( $msg )->rawParams( $link )->escaped()
822 );
823 }
824 }
825 }
826 }
827
828 // show from this onward link
829 $timestamp = wfTimestampNow();
830 $now = $lang->userTimeAndDate( $timestamp, $user );
831 $timenow = $lang->userTime( $timestamp, $user );
832 $datenow = $lang->userDate( $timestamp, $user );
833 $pipedLinks = '<span class="rcshowhide">' . $lang->pipeList( $links ) . '</span>';
834
835 $rclinks = '<span class="rclinks">' . $this->msg( 'rclinks' )->rawParams( $cl, $dl, $pipedLinks )
836 ->parse() . '</span>';
837
838 $rclistfrom = '<span class="rclistfrom">' . $this->makeOptionsLink(
839 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
840 [ 'from' => $timestamp ],
841 $nondefaults
842 ) . '</span>';
843
844 return "{$note}$rclinks<br />$rclistfrom";
845 }
846
847 public function isIncludable() {
848 return true;
849 }
850
851 protected function getCacheTTL() {
852 return 60 * 5;
853 }
854 }