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