Merge "RC Filters: always join with 'page'"
[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 // JOIN on page, used for 'last revision' filter highlight
251 $tables[] = 'page';
252 $fields[] = 'page_latest';
253 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
254
255 ChangeTags::modifyDisplayQuery(
256 $tables,
257 $fields,
258 $conds,
259 $join_conds,
260 $query_options,
261 $opts['tagfilter']
262 );
263
264 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
265 $opts )
266 ) {
267 return false;
268 }
269
270 if ( $this->areFiltersInConflict() ) {
271 return false;
272 }
273
274 // array_merge() is used intentionally here so that hooks can, should
275 // they so desire, override the ORDER BY / LIMIT condition(s); prior to
276 // MediaWiki 1.26 this used to use the plus operator instead, which meant
277 // that extensions weren't able to change these conditions
278 $query_options = array_merge( [
279 'ORDER BY' => 'rc_timestamp DESC',
280 'LIMIT' => $opts['limit'] ], $query_options );
281 $rows = $dbr->select(
282 $tables,
283 $fields,
284 // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
285 // knowledge to use an index merge if it wants (it may use some other index though).
286 $conds + [ 'rc_new' => [ 0, 1 ] ],
287 __METHOD__,
288 $query_options,
289 $join_conds
290 );
291
292 // Build the final data
293 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
294 $this->filterByCategories( $rows, $opts );
295 }
296
297 return $rows;
298 }
299
300 protected function runMainQueryHook( &$tables, &$fields, &$conds,
301 &$query_options, &$join_conds, $opts
302 ) {
303 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
304 && Hooks::run(
305 'SpecialRecentChangesQuery',
306 [ &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ],
307 '1.23'
308 );
309 }
310
311 protected function getDB() {
312 return wfGetDB( DB_REPLICA, 'recentchanges' );
313 }
314
315 public function outputFeedLinks() {
316 $this->addFeedLinks( $this->getFeedQuery() );
317 }
318
319 /**
320 * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view.
321 *
322 * @return array
323 */
324 protected function getFeedQuery() {
325 $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) {
326 // API handles empty parameters in a different way
327 return $value !== '';
328 } );
329 $query['action'] = 'feedrecentchanges';
330 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
331 if ( $query['limit'] > $feedLimit ) {
332 $query['limit'] = $feedLimit;
333 }
334
335 return $query;
336 }
337
338 /**
339 * Build and output the actual changes list.
340 *
341 * @param ResultWrapper $rows Database rows
342 * @param FormOptions $opts
343 */
344 public function outputChangesList( $rows, $opts ) {
345 $limit = $opts['limit'];
346
347 $showWatcherCount = $this->getConfig()->get( 'RCShowWatchingUsers' )
348 && $this->getUser()->getOption( 'shownumberswatching' );
349 $watcherCache = [];
350
351 $dbr = $this->getDB();
352
353 $counter = 1;
354 $list = ChangesList::newFromContext( $this->getContext(), $this->filterGroups );
355 $list->initChangesListRows( $rows );
356
357 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
358 $rclistOutput = $list->beginRecentChangesList();
359 foreach ( $rows as $obj ) {
360 if ( $limit == 0 ) {
361 break;
362 }
363 $rc = RecentChange::newFromRow( $obj );
364
365 # Skip CatWatch entries for hidden cats based on user preference
366 if (
367 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE &&
368 !$userShowHiddenCats &&
369 $rc->getParam( 'hidden-cat' )
370 ) {
371 continue;
372 }
373
374 $rc->counter = $counter++;
375 # Check if the page has been updated since the last visit
376 if ( $this->getConfig()->get( 'ShowUpdatedMarker' )
377 && !empty( $obj->wl_notificationtimestamp )
378 ) {
379 $rc->notificationtimestamp = ( $obj->rc_timestamp >= $obj->wl_notificationtimestamp );
380 } else {
381 $rc->notificationtimestamp = false; // Default
382 }
383 # Check the number of users watching the page
384 $rc->numberofWatchingusers = 0; // Default
385 if ( $showWatcherCount && $obj->rc_namespace >= 0 ) {
386 if ( !isset( $watcherCache[$obj->rc_namespace][$obj->rc_title] ) ) {
387 $watcherCache[$obj->rc_namespace][$obj->rc_title] =
388 MediaWikiServices::getInstance()->getWatchedItemStore()->countWatchers(
389 new TitleValue( (int)$obj->rc_namespace, $obj->rc_title )
390 );
391 }
392 $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
393 }
394
395 $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
396 if ( $changeLine !== false ) {
397 $rclistOutput .= $changeLine;
398 --$limit;
399 }
400 }
401 $rclistOutput .= $list->endRecentChangesList();
402
403 if ( $rows->numRows() === 0 ) {
404 $this->outputNoResults();
405 if ( !$this->including() ) {
406 $this->getOutput()->setStatusCode( 404 );
407 }
408 } else {
409 $this->getOutput()->addHTML( $rclistOutput );
410 }
411 }
412
413 /**
414 * Set the text to be displayed above the changes
415 *
416 * @param FormOptions $opts
417 * @param int $numRows Number of rows in the result to show after this header
418 */
419 public function doHeader( $opts, $numRows ) {
420 $this->setTopText( $opts );
421
422 $defaults = $opts->getAllValues();
423 $nondefaults = $opts->getChangedValues();
424
425 $panel = [];
426 $panel[] = $this->makeLegend();
427 $panel[] = $this->optionsPanel( $defaults, $nondefaults, $numRows );
428 $panel[] = '<hr />';
429
430 $extraOpts = $this->getExtraOptions( $opts );
431 $extraOptsCount = count( $extraOpts );
432 $count = 0;
433 $submit = ' ' . Xml::submitButton( $this->msg( 'recentchanges-submit' )->text() );
434
435 $out = Xml::openElement( 'table', [ 'class' => 'mw-recentchanges-table' ] );
436 foreach ( $extraOpts as $name => $optionRow ) {
437 # Add submit button to the last row only
438 ++$count;
439 $addSubmit = ( $count === $extraOptsCount ) ? $submit : '';
440
441 $out .= Xml::openElement( 'tr' );
442 if ( is_array( $optionRow ) ) {
443 $out .= Xml::tags(
444 'td',
445 [ 'class' => 'mw-label mw-' . $name . '-label' ],
446 $optionRow[0]
447 );
448 $out .= Xml::tags(
449 'td',
450 [ 'class' => 'mw-input' ],
451 $optionRow[1] . $addSubmit
452 );
453 } else {
454 $out .= Xml::tags(
455 'td',
456 [ 'class' => 'mw-input', 'colspan' => 2 ],
457 $optionRow . $addSubmit
458 );
459 }
460 $out .= Xml::closeElement( 'tr' );
461 }
462 $out .= Xml::closeElement( 'table' );
463
464 $unconsumed = $opts->getUnconsumedValues();
465 foreach ( $unconsumed as $key => $value ) {
466 $out .= Html::hidden( $key, $value );
467 }
468
469 $t = $this->getPageTitle();
470 $out .= Html::hidden( 'title', $t->getPrefixedText() );
471 $form = Xml::tags( 'form', [ 'action' => wfScript() ], $out );
472 $panel[] = $form;
473 $panelString = implode( "\n", $panel );
474
475 $rcoptions = Xml::fieldset(
476 $this->msg( 'recentchanges-legend' )->text(),
477 $panelString,
478 [ 'class' => 'rcoptions' ]
479 );
480
481 // Insert a placeholder for RCFilters
482 if ( $this->getUser()->getOption( 'rcenhancedfilters' ) ) {
483 $rcfilterContainer = Html::element(
484 'div',
485 [ 'class' => 'rcfilters-container' ]
486 );
487
488 // Wrap both with rcfilters-head
489 $this->getOutput()->addHTML(
490 Html::rawElement(
491 'div',
492 [ 'class' => 'rcfilters-head' ],
493 $rcfilterContainer . $rcoptions
494 )
495 );
496 } else {
497 $this->getOutput()->addHTML( $rcoptions );
498 }
499
500 $this->setBottomText( $opts );
501 }
502
503 /**
504 * Send the text to be displayed above the options
505 *
506 * @param FormOptions $opts Unused
507 */
508 function setTopText( FormOptions $opts ) {
509 global $wgContLang;
510
511 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
512 if ( !$message->isDisabled() ) {
513 $this->getOutput()->addWikiText(
514 Html::rawElement( 'div',
515 [ 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ],
516 "\n" . $message->plain() . "\n"
517 ),
518 /* $lineStart */ true,
519 /* $interface */ false
520 );
521 }
522 }
523
524 /**
525 * Get options to be displayed in a form
526 *
527 * @param FormOptions $opts
528 * @return array
529 */
530 function getExtraOptions( $opts ) {
531 $opts->consumeValues( [
532 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
533 ] );
534
535 $extraOpts = [];
536 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
537
538 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
539 $extraOpts['category'] = $this->categoryFilterForm( $opts );
540 }
541
542 $tagFilter = ChangeTags::buildTagFilterSelector(
543 $opts['tagfilter'], false, $this->getContext() );
544 if ( count( $tagFilter ) ) {
545 $extraOpts['tagfilter'] = $tagFilter;
546 }
547
548 // Don't fire the hook for subclasses. (Or should we?)
549 if ( $this->getName() === 'Recentchanges' ) {
550 Hooks::run( 'SpecialRecentChangesPanel', [ &$extraOpts, $opts ] );
551 }
552
553 return $extraOpts;
554 }
555
556 /**
557 * Check whether the structured filter UI is enabled
558 *
559 * @return bool
560 */
561 protected function isStructuredFilterUiEnabled() {
562 return $this->getUser()->getOption(
563 'rcenhancedfilters'
564 );
565 }
566
567 /**
568 * Add page-specific modules.
569 */
570 protected function addModules() {
571 parent::addModules();
572 $out = $this->getOutput();
573 $out->addModules( 'mediawiki.special.recentchanges' );
574 if ( $this->isStructuredFilterUiEnabled() ) {
575 $out->addModules( 'mediawiki.rcfilters.filters.ui' );
576 $out->addModuleStyles( 'mediawiki.rcfilters.filters.base.styles' );
577 }
578 }
579
580 /**
581 * Get last modified date, for client caching
582 * Don't use this if we are using the patrol feature, patrol changes don't
583 * update the timestamp
584 *
585 * @return string|bool
586 */
587 public function checkLastModified() {
588 $dbr = $this->getDB();
589 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
590
591 return $lastmod;
592 }
593
594 /**
595 * Creates the choose namespace selection
596 *
597 * @param FormOptions $opts
598 * @return string
599 */
600 protected function namespaceFilterForm( FormOptions $opts ) {
601 $nsSelect = Html::namespaceSelector(
602 [ 'selected' => $opts['namespace'], 'all' => '' ],
603 [ 'name' => 'namespace', 'id' => 'namespace' ]
604 );
605 $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
606 $invert = Xml::checkLabel(
607 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
608 $opts['invert'],
609 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
610 );
611 $associated = Xml::checkLabel(
612 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
613 $opts['associated'],
614 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
615 );
616
617 return [ $nsLabel, "$nsSelect $invert $associated" ];
618 }
619
620 /**
621 * Create an input to filter changes by categories
622 *
623 * @param FormOptions $opts
624 * @return array
625 */
626 protected function categoryFilterForm( FormOptions $opts ) {
627 list( $label, $input ) = Xml::inputLabelSep( $this->msg( 'rc_categories' )->text(),
628 'categories', 'mw-categories', false, $opts['categories'] );
629
630 $input .= ' ' . Xml::checkLabel( $this->msg( 'rc_categories_any' )->text(),
631 'categories_any', 'mw-categories_any', $opts['categories_any'] );
632
633 return [ $label, $input ];
634 }
635
636 /**
637 * Filter $rows by categories set in $opts
638 *
639 * @param ResultWrapper $rows Database rows
640 * @param FormOptions $opts
641 */
642 function filterByCategories( &$rows, FormOptions $opts ) {
643 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
644
645 if ( !count( $categories ) ) {
646 return;
647 }
648
649 # Filter categories
650 $cats = [];
651 foreach ( $categories as $cat ) {
652 $cat = trim( $cat );
653 if ( $cat == '' ) {
654 continue;
655 }
656 $cats[] = $cat;
657 }
658
659 # Filter articles
660 $articles = [];
661 $a2r = [];
662 $rowsarr = [];
663 foreach ( $rows as $k => $r ) {
664 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
665 $id = $nt->getArticleID();
666 if ( $id == 0 ) {
667 continue; # Page might have been deleted...
668 }
669 if ( !in_array( $id, $articles ) ) {
670 $articles[] = $id;
671 }
672 if ( !isset( $a2r[$id] ) ) {
673 $a2r[$id] = [];
674 }
675 $a2r[$id][] = $k;
676 $rowsarr[$k] = $r;
677 }
678
679 # Shortcut?
680 if ( !count( $articles ) || !count( $cats ) ) {
681 return;
682 }
683
684 # Look up
685 $catFind = new CategoryFinder;
686 $catFind->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
687 $match = $catFind->run();
688
689 # Filter
690 $newrows = [];
691 foreach ( $match as $id ) {
692 foreach ( $a2r[$id] as $rev ) {
693 $k = $rev;
694 $newrows[$k] = $rowsarr[$k];
695 }
696 }
697 $rows = new FakeResultWrapper( array_values( $newrows ) );
698 }
699
700 /**
701 * Makes change an option link which carries all the other options
702 *
703 * @param string $title Title
704 * @param array $override Options to override
705 * @param array $options Current options
706 * @param bool $active Whether to show the link in bold
707 * @return string
708 */
709 function makeOptionsLink( $title, $override, $options, $active = false ) {
710 $params = $override + $options;
711
712 // T38524: false values have be converted to "0" otherwise
713 // wfArrayToCgi() will omit it them.
714 foreach ( $params as &$value ) {
715 if ( $value === false ) {
716 $value = '0';
717 }
718 }
719 unset( $value );
720
721 if ( $active ) {
722 $title = new HtmlArmor( '<strong>' . htmlspecialchars( $title ) . '</strong>' );
723 }
724
725 return $this->getLinkRenderer()->makeKnownLink( $this->getPageTitle(), $title, [
726 'data-params' => json_encode( $override ),
727 'data-keys' => implode( ',', array_keys( $override ) ),
728 ], $params );
729 }
730
731 /**
732 * Creates the options panel.
733 *
734 * @param array $defaults
735 * @param array $nondefaults
736 * @param int $numRows Number of rows in the result to show after this header
737 * @return string
738 */
739 function optionsPanel( $defaults, $nondefaults, $numRows ) {
740 $options = $nondefaults + $defaults;
741
742 $note = '';
743 $msg = $this->msg( 'rclegend' );
744 if ( !$msg->isDisabled() ) {
745 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
746 }
747
748 $lang = $this->getLanguage();
749 $user = $this->getUser();
750 $config = $this->getConfig();
751 if ( $options['from'] ) {
752 $resetLink = $this->makeOptionsLink( $this->msg( 'rclistfromreset' ),
753 [ 'from' => '' ], $nondefaults );
754
755 $note .= $this->msg( 'rcnotefrom' )
756 ->numParams( $options['limit'] )
757 ->params(
758 $lang->userTimeAndDate( $options['from'], $user ),
759 $lang->userDate( $options['from'], $user ),
760 $lang->userTime( $options['from'], $user )
761 )
762 ->numParams( $numRows )
763 ->parse() . ' ' .
764 Html::rawElement(
765 'span',
766 [ 'class' => 'rcoptions-listfromreset' ],
767 $this->msg( 'parentheses' )->rawParams( $resetLink )->parse()
768 ) .
769 '<br />';
770 }
771
772 # Sort data for display and make sure it's unique after we've added user data.
773 $linkLimits = $config->get( 'RCLinkLimits' );
774 $linkLimits[] = $options['limit'];
775 sort( $linkLimits );
776 $linkLimits = array_unique( $linkLimits );
777
778 $linkDays = $config->get( 'RCLinkDays' );
779 $linkDays[] = $options['days'];
780 sort( $linkDays );
781 $linkDays = array_unique( $linkDays );
782
783 // limit links
784 $cl = [];
785 foreach ( $linkLimits as $value ) {
786 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
787 [ 'limit' => $value ], $nondefaults, $value == $options['limit'] );
788 }
789 $cl = $lang->pipeList( $cl );
790
791 // day links, reset 'from' to none
792 $dl = [];
793 foreach ( $linkDays as $value ) {
794 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
795 [ 'days' => $value, 'from' => '' ], $nondefaults, $value == $options['days'] );
796 }
797 $dl = $lang->pipeList( $dl );
798
799 $showhide = [ 'show', 'hide' ];
800
801 $links = [];
802
803 $filterGroups = $this->getFilterGroups();
804
805 $context = $this->getContext();
806 foreach ( $filterGroups as $groupName => $group ) {
807 if ( !$group->isPerGroupRequestParameter() ) {
808 foreach ( $group->getFilters() as $key => $filter ) {
809 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
810 $msg = $filter->getShowHide();
811 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
812 // Extensions can define additional filters, but don't need to define the corresponding
813 // messages. If they don't exist, just fall back to 'show' and 'hide'.
814 if ( !$linkMessage->exists() ) {
815 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
816 }
817
818 $link = $this->makeOptionsLink( $linkMessage->text(),
819 [ $key => 1 - $options[$key] ], $nondefaults );
820
821 $attribs = [
822 'class' => "$msg rcshowhideoption",
823 'data-filter-name' => $filter->getName(),
824 ];
825
826 if ( $filter->isFeatureAvailableOnStructuredUi( $this ) ) {
827 $attribs['data-feature-in-structured-ui'] = true;
828 }
829
830 $links[] = Html::rawElement(
831 'span',
832 $attribs,
833 $this->msg( $msg )->rawParams( $link )->escaped()
834 );
835 }
836 }
837 }
838 }
839
840 // show from this onward link
841 $timestamp = wfTimestampNow();
842 $now = $lang->userTimeAndDate( $timestamp, $user );
843 $timenow = $lang->userTime( $timestamp, $user );
844 $datenow = $lang->userDate( $timestamp, $user );
845 $pipedLinks = '<span class="rcshowhide">' . $lang->pipeList( $links ) . '</span>';
846
847 $rclinks = '<span class="rclinks">' . $this->msg( 'rclinks' )->rawParams( $cl, $dl, '' )
848 ->parse() . '</span>';
849
850 $rclistfrom = '<span class="rclistfrom">' . $this->makeOptionsLink(
851 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
852 [ 'from' => $timestamp ],
853 $nondefaults
854 ) . '</span>';
855
856 return "{$note}$rclinks<br />$pipedLinks<br />$rclistfrom";
857 }
858
859 public function isIncludable() {
860 return true;
861 }
862
863 protected function getCacheTTL() {
864 return 60 * 5;
865 }
866 }