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