Merge "RC filters: custom help link for beta users"
[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 // Insert a placeholder for RCFilters
460 if ( $this->getUser()->getOption( 'rcenhancedfilters' ) ) {
461 $this->getOutput()->addHTML(
462 Html::element(
463 'div',
464 [ 'class' => 'rcfilters-container' ]
465 )
466 );
467 }
468
469 $this->getOutput()->addHTML(
470 Xml::fieldset(
471 $this->msg( 'recentchanges-legend' )->text(),
472 $panelString,
473 [ 'class' => 'rcoptions' ]
474 )
475 );
476
477 $this->setBottomText( $opts );
478 }
479
480 /**
481 * Send the text to be displayed above the options
482 *
483 * @param FormOptions $opts Unused
484 */
485 function setTopText( FormOptions $opts ) {
486 global $wgContLang;
487
488 $message = $this->msg( 'recentchangestext' )->inContentLanguage();
489 if ( !$message->isDisabled() ) {
490 $this->getOutput()->addWikiText(
491 Html::rawElement( 'div',
492 [ 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ],
493 "\n" . $message->plain() . "\n"
494 ),
495 /* $lineStart */ true,
496 /* $interface */ false
497 );
498 }
499 }
500
501 /**
502 * Get options to be displayed in a form
503 *
504 * @param FormOptions $opts
505 * @return array
506 */
507 function getExtraOptions( $opts ) {
508 $opts->consumeValues( [
509 'namespace', 'invert', 'associated', 'tagfilter', 'categories', 'categories_any'
510 ] );
511
512 $extraOpts = [];
513 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
514
515 if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) {
516 $extraOpts['category'] = $this->categoryFilterForm( $opts );
517 }
518
519 $tagFilter = ChangeTags::buildTagFilterSelector(
520 $opts['tagfilter'], false, $this->getContext() );
521 if ( count( $tagFilter ) ) {
522 $extraOpts['tagfilter'] = $tagFilter;
523 }
524
525 // Don't fire the hook for subclasses. (Or should we?)
526 if ( $this->getName() === 'Recentchanges' ) {
527 Hooks::run( 'SpecialRecentChangesPanel', [ &$extraOpts, $opts ] );
528 }
529
530 return $extraOpts;
531 }
532
533 /**
534 * Add page-specific modules.
535 */
536 protected function addModules() {
537 parent::addModules();
538 $out = $this->getOutput();
539 $out->addModules( 'mediawiki.special.recentchanges' );
540 if ( $this->getUser()->getOption( 'rcenhancedfilters' ) ) {
541 $out->addModules( 'mediawiki.rcfilters.filters.ui' );
542 $out->addModuleStyles( 'mediawiki.rcfilters.filters.base.styles' );
543 }
544 }
545
546 /**
547 * Get last modified date, for client caching
548 * Don't use this if we are using the patrol feature, patrol changes don't
549 * update the timestamp
550 *
551 * @return string|bool
552 */
553 public function checkLastModified() {
554 $dbr = $this->getDB();
555 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
556
557 return $lastmod;
558 }
559
560 /**
561 * Creates the choose namespace selection
562 *
563 * @param FormOptions $opts
564 * @return string
565 */
566 protected function namespaceFilterForm( FormOptions $opts ) {
567 $nsSelect = Html::namespaceSelector(
568 [ 'selected' => $opts['namespace'], 'all' => '' ],
569 [ 'name' => 'namespace', 'id' => 'namespace' ]
570 );
571 $nsLabel = Xml::label( $this->msg( 'namespace' )->text(), 'namespace' );
572 $invert = Xml::checkLabel(
573 $this->msg( 'invert' )->text(), 'invert', 'nsinvert',
574 $opts['invert'],
575 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
576 );
577 $associated = Xml::checkLabel(
578 $this->msg( 'namespace_association' )->text(), 'associated', 'nsassociated',
579 $opts['associated'],
580 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
581 );
582
583 return [ $nsLabel, "$nsSelect $invert $associated" ];
584 }
585
586 /**
587 * Create an input to filter changes by categories
588 *
589 * @param FormOptions $opts
590 * @return array
591 */
592 protected function categoryFilterForm( FormOptions $opts ) {
593 list( $label, $input ) = Xml::inputLabelSep( $this->msg( 'rc_categories' )->text(),
594 'categories', 'mw-categories', false, $opts['categories'] );
595
596 $input .= ' ' . Xml::checkLabel( $this->msg( 'rc_categories_any' )->text(),
597 'categories_any', 'mw-categories_any', $opts['categories_any'] );
598
599 return [ $label, $input ];
600 }
601
602 /**
603 * Filter $rows by categories set in $opts
604 *
605 * @param ResultWrapper $rows Database rows
606 * @param FormOptions $opts
607 */
608 function filterByCategories( &$rows, FormOptions $opts ) {
609 $categories = array_map( 'trim', explode( '|', $opts['categories'] ) );
610
611 if ( !count( $categories ) ) {
612 return;
613 }
614
615 # Filter categories
616 $cats = [];
617 foreach ( $categories as $cat ) {
618 $cat = trim( $cat );
619 if ( $cat == '' ) {
620 continue;
621 }
622 $cats[] = $cat;
623 }
624
625 # Filter articles
626 $articles = [];
627 $a2r = [];
628 $rowsarr = [];
629 foreach ( $rows as $k => $r ) {
630 $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
631 $id = $nt->getArticleID();
632 if ( $id == 0 ) {
633 continue; # Page might have been deleted...
634 }
635 if ( !in_array( $id, $articles ) ) {
636 $articles[] = $id;
637 }
638 if ( !isset( $a2r[$id] ) ) {
639 $a2r[$id] = [];
640 }
641 $a2r[$id][] = $k;
642 $rowsarr[$k] = $r;
643 }
644
645 # Shortcut?
646 if ( !count( $articles ) || !count( $cats ) ) {
647 return;
648 }
649
650 # Look up
651 $catFind = new CategoryFinder;
652 $catFind->seed( $articles, $cats, $opts['categories_any'] ? 'OR' : 'AND' );
653 $match = $catFind->run();
654
655 # Filter
656 $newrows = [];
657 foreach ( $match as $id ) {
658 foreach ( $a2r[$id] as $rev ) {
659 $k = $rev;
660 $newrows[$k] = $rowsarr[$k];
661 }
662 }
663 $rows = $newrows;
664 }
665
666 /**
667 * Makes change an option link which carries all the other options
668 *
669 * @param string $title Title
670 * @param array $override Options to override
671 * @param array $options Current options
672 * @param bool $active Whether to show the link in bold
673 * @return string
674 */
675 function makeOptionsLink( $title, $override, $options, $active = false ) {
676 $params = $override + $options;
677
678 // T38524: false values have be converted to "0" otherwise
679 // wfArrayToCgi() will omit it them.
680 foreach ( $params as &$value ) {
681 if ( $value === false ) {
682 $value = '0';
683 }
684 }
685 unset( $value );
686
687 if ( $active ) {
688 $title = new HtmlArmor( '<strong>' . htmlspecialchars( $title ) . '</strong>' );
689 }
690
691 return $this->getLinkRenderer()->makeKnownLink( $this->getPageTitle(), $title, [
692 'data-params' => json_encode( $override ),
693 'data-keys' => implode( ',', array_keys( $override ) ),
694 ], $params );
695 }
696
697 /**
698 * Creates the options panel.
699 *
700 * @param array $defaults
701 * @param array $nondefaults
702 * @param int $numRows Number of rows in the result to show after this header
703 * @return string
704 */
705 function optionsPanel( $defaults, $nondefaults, $numRows ) {
706 $options = $nondefaults + $defaults;
707
708 $note = '';
709 $msg = $this->msg( 'rclegend' );
710 if ( !$msg->isDisabled() ) {
711 $note .= '<div class="mw-rclegend">' . $msg->parse() . "</div>\n";
712 }
713
714 $lang = $this->getLanguage();
715 $user = $this->getUser();
716 $config = $this->getConfig();
717 if ( $options['from'] ) {
718 $note .= $this->msg( 'rcnotefrom' )
719 ->numParams( $options['limit'] )
720 ->params(
721 $lang->userTimeAndDate( $options['from'], $user ),
722 $lang->userDate( $options['from'], $user ),
723 $lang->userTime( $options['from'], $user )
724 )
725 ->numParams( $numRows )
726 ->parse() . '<br />';
727 }
728
729 # Sort data for display and make sure it's unique after we've added user data.
730 $linkLimits = $config->get( 'RCLinkLimits' );
731 $linkLimits[] = $options['limit'];
732 sort( $linkLimits );
733 $linkLimits = array_unique( $linkLimits );
734
735 $linkDays = $config->get( 'RCLinkDays' );
736 $linkDays[] = $options['days'];
737 sort( $linkDays );
738 $linkDays = array_unique( $linkDays );
739
740 // limit links
741 $cl = [];
742 foreach ( $linkLimits as $value ) {
743 $cl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
744 [ 'limit' => $value ], $nondefaults, $value == $options['limit'] );
745 }
746 $cl = $lang->pipeList( $cl );
747
748 // day links, reset 'from' to none
749 $dl = [];
750 foreach ( $linkDays as $value ) {
751 $dl[] = $this->makeOptionsLink( $lang->formatNum( $value ),
752 [ 'days' => $value, 'from' => '' ], $nondefaults, $value == $options['days'] );
753 }
754 $dl = $lang->pipeList( $dl );
755
756 // show/hide links
757 $filters = [
758 'hideminor' => 'rcshowhideminor',
759 'hidebots' => 'rcshowhidebots',
760 'hideanons' => 'rcshowhideanons',
761 'hideliu' => 'rcshowhideliu',
762 'hidepatrolled' => 'rcshowhidepatr',
763 'hidemyself' => 'rcshowhidemine'
764 ];
765
766 if ( $config->get( 'RCWatchCategoryMembership' ) ) {
767 $filters['hidecategorization'] = 'rcshowhidecategorization';
768 }
769
770 $showhide = [ 'show', 'hide' ];
771
772 foreach ( $this->getRenderableCustomFilters( $this->getCustomFilters() ) as $key => $params ) {
773 $filters[$key] = $params['msg'];
774 }
775
776 // Disable some if needed
777 if ( !$user->useRCPatrol() ) {
778 unset( $filters['hidepatrolled'] );
779 }
780
781 $links = [];
782 foreach ( $filters as $key => $msg ) {
783 // The following messages are used here:
784 // rcshowhideminor-show, rcshowhideminor-hide, rcshowhidebots-show, rcshowhidebots-hide,
785 // rcshowhideanons-show, rcshowhideanons-hide, rcshowhideliu-show, rcshowhideliu-hide,
786 // rcshowhidepatr-show, rcshowhidepatr-hide, rcshowhidemine-show, rcshowhidemine-hide,
787 // rcshowhidecategorization-show, rcshowhidecategorization-hide.
788 $linkMessage = $this->msg( $msg . '-' . $showhide[1 - $options[$key]] );
789 // Extensions can define additional filters, but don't need to define the corresponding
790 // messages. If they don't exist, just fall back to 'show' and 'hide'.
791 if ( !$linkMessage->exists() ) {
792 $linkMessage = $this->msg( $showhide[1 - $options[$key]] );
793 }
794
795 $link = $this->makeOptionsLink( $linkMessage->text(),
796 [ $key => 1 - $options[$key] ], $nondefaults );
797 $links[] = "<span class=\"$msg rcshowhideoption\">"
798 . $this->msg( $msg )->rawParams( $link )->escaped() . '</span>';
799 }
800
801 // show from this onward link
802 $timestamp = wfTimestampNow();
803 $now = $lang->userTimeAndDate( $timestamp, $user );
804 $timenow = $lang->userTime( $timestamp, $user );
805 $datenow = $lang->userDate( $timestamp, $user );
806 $pipedLinks = '<span class="rcshowhide">' . $lang->pipeList( $links ) . '</span>';
807
808 $rclinks = '<span class="rclinks">' . $this->msg( 'rclinks' )->rawParams( $cl, $dl, $pipedLinks )
809 ->parse() . '</span>';
810
811 $rclistfrom = '<span class="rclistfrom">' . $this->makeOptionsLink(
812 $this->msg( 'rclistfrom' )->rawParams( $now, $timenow, $datenow )->parse(),
813 [ 'from' => $timestamp ],
814 $nondefaults
815 ) . '</span>';
816
817 return "{$note}$rclinks<br />$rclistfrom";
818 }
819
820 public function isIncludable() {
821 return true;
822 }
823
824 protected function getCacheTTL() {
825 return 60 * 5;
826 }
827
828 function filterOnUserExperienceLevel( &$tables, &$conds, &$join_conds, $opts ) {
829 global $wgLearnerEdits,
830 $wgExperiencedUserEdits,
831 $wgLearnerMemberSince,
832 $wgExperiencedUserMemberSince;
833
834 $selectedExpLevels = explode( ',', strtolower( $opts['userExpLevel'] ) );
835 // remove values that are not recognized
836 $selectedExpLevels = array_intersect(
837 $selectedExpLevels,
838 [ 'newcomer', 'learner', 'experienced' ]
839 );
840 sort( $selectedExpLevels );
841
842 if ( $selectedExpLevels ) {
843 $tables[] = 'user';
844 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
845
846 $now = time();
847 $secondsPerDay = 86400;
848 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
849 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
850
851 $aboveNewcomer = $this->getDB()->makeList(
852 [
853 'user_editcount >= ' . intval( $wgLearnerEdits ),
854 'user_registration <= ' . $this->getDB()->timestamp( $learnerCutoff ),
855 ],
856 IDatabase::LIST_AND
857 );
858
859 $aboveLearner = $this->getDB()->makeList(
860 [
861 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
862 'user_registration <= ' . $this->getDB()->timestamp( $experiencedUserCutoff ),
863 ],
864 IDatabase::LIST_AND
865 );
866
867 if ( $selectedExpLevels === [ 'newcomer' ] ) {
868 $conds[] = "NOT ( $aboveNewcomer )";
869 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
870 $conds[] = $this->getDB()->makeList(
871 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
872 IDatabase::LIST_AND
873 );
874 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
875 $conds[] = $aboveLearner;
876 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
877 $conds[] = "NOT ( $aboveLearner )";
878 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
879 $conds[] = $this->getDB()->makeList(
880 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
881 IDatabase::LIST_OR
882 );
883 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
884 $conds[] = $aboveNewcomer;
885 }
886 }
887 }
888
889 }