Deprecate IDatabase::nextSequenceValue()
[lhc/web/wiklou.git] / includes / specialpage / ChangesListSpecialPage.php
1 <?php
2 /**
3 * Special page which uses a ChangesList to show query results.
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 use MediaWiki\Logger\LoggerFactory;
24 use Wikimedia\Rdbms\ResultWrapper;
25 use Wikimedia\Rdbms\FakeResultWrapper;
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29 * Special page which uses a ChangesList to show query results.
30 * @todo Way too many public functions, most of them should be protected
31 *
32 * @ingroup SpecialPage
33 */
34 abstract class ChangesListSpecialPage extends SpecialPage {
35 /** @var string */
36 protected $rcSubpage;
37
38 /** @var FormOptions */
39 protected $rcOptions;
40
41 /** @var array */
42 protected $customFilters;
43
44 // Order of both groups and filters is significant; first is top-most priority,
45 // descending from there.
46 // 'showHideSuffix' is a shortcut to and avoid spelling out
47 // details specific to subclasses here.
48 /**
49 * Definition information for the filters and their groups
50 *
51 * The value is $groupDefinition, a parameter to the ChangesListFilterGroup constructor.
52 * However, priority is dynamically added for the core groups, to ease maintenance.
53 *
54 * Groups are displayed to the user in the structured UI. However, if necessary,
55 * all of the filters in a group can be configured to only display on the
56 * unstuctured UI, in which case you don't need a group title. This is done in
57 * getFilterGroupDefinitionFromLegacyCustomFilters, for example.
58 *
59 * @var array $filterGroupDefinitions
60 */
61 private $filterGroupDefinitions;
62
63 // Same format as filterGroupDefinitions, but for a single group (reviewStatus)
64 // that is registered conditionally.
65 private $reviewStatusFilterGroupDefinition;
66
67 // Single filter registered conditionally
68 private $hideCategorizationFilterDefinition;
69
70 /**
71 * Filter groups, and their contained filters
72 * This is an associative array (with group name as key) of ChangesListFilterGroup objects.
73 *
74 * @var array $filterGroups
75 */
76 protected $filterGroups = [];
77
78 public function __construct( $name, $restriction ) {
79 parent::__construct( $name, $restriction );
80
81 $this->filterGroupDefinitions = [
82 [
83 'name' => 'registration',
84 'title' => 'rcfilters-filtergroup-registration',
85 'class' => ChangesListBooleanFilterGroup::class,
86 'filters' => [
87 [
88 'name' => 'hideliu',
89 // rcshowhideliu-show, rcshowhideliu-hide,
90 // wlshowhideliu
91 'showHideSuffix' => 'showhideliu',
92 'default' => false,
93 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
94 &$query_options, &$join_conds
95 ) {
96 $conds[] = 'rc_user = 0';
97 },
98 'isReplacedInStructuredUi' => true,
99
100 ],
101 [
102 'name' => 'hideanons',
103 // rcshowhideanons-show, rcshowhideanons-hide,
104 // wlshowhideanons
105 'showHideSuffix' => 'showhideanons',
106 'default' => false,
107 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
108 &$query_options, &$join_conds
109 ) {
110 $conds[] = 'rc_user != 0';
111 },
112 'isReplacedInStructuredUi' => true,
113 ]
114 ],
115 ],
116
117 [
118 'name' => 'userExpLevel',
119 'title' => 'rcfilters-filtergroup-userExpLevel',
120 'class' => ChangesListStringOptionsFilterGroup::class,
121 'isFullCoverage' => true,
122 'filters' => [
123 [
124 'name' => 'unregistered',
125 'label' => 'rcfilters-filter-user-experience-level-unregistered-label',
126 'description' => 'rcfilters-filter-user-experience-level-unregistered-description',
127 'cssClassSuffix' => 'user-unregistered',
128 'isRowApplicableCallable' => function ( $ctx, $rc ) {
129 return !$rc->getAttribute( 'rc_user' );
130 }
131 ],
132 [
133 'name' => 'registered',
134 'label' => 'rcfilters-filter-user-experience-level-registered-label',
135 'description' => 'rcfilters-filter-user-experience-level-registered-description',
136 'cssClassSuffix' => 'user-registered',
137 'isRowApplicableCallable' => function ( $ctx, $rc ) {
138 return $rc->getAttribute( 'rc_user' );
139 }
140 ],
141 [
142 'name' => 'newcomer',
143 'label' => 'rcfilters-filter-user-experience-level-newcomer-label',
144 'description' => 'rcfilters-filter-user-experience-level-newcomer-description',
145 'cssClassSuffix' => 'user-newcomer',
146 'isRowApplicableCallable' => function ( $ctx, $rc ) {
147 $performer = $rc->getPerformer();
148 return $performer && $performer->isLoggedIn() &&
149 $performer->getExperienceLevel() === 'newcomer';
150 }
151 ],
152 [
153 'name' => 'learner',
154 'label' => 'rcfilters-filter-user-experience-level-learner-label',
155 'description' => 'rcfilters-filter-user-experience-level-learner-description',
156 'cssClassSuffix' => 'user-learner',
157 'isRowApplicableCallable' => function ( $ctx, $rc ) {
158 $performer = $rc->getPerformer();
159 return $performer && $performer->isLoggedIn() &&
160 $performer->getExperienceLevel() === 'learner';
161 },
162 ],
163 [
164 'name' => 'experienced',
165 'label' => 'rcfilters-filter-user-experience-level-experienced-label',
166 'description' => 'rcfilters-filter-user-experience-level-experienced-description',
167 'cssClassSuffix' => 'user-experienced',
168 'isRowApplicableCallable' => function ( $ctx, $rc ) {
169 $performer = $rc->getPerformer();
170 return $performer && $performer->isLoggedIn() &&
171 $performer->getExperienceLevel() === 'experienced';
172 },
173 ]
174 ],
175 'default' => ChangesListStringOptionsFilterGroup::NONE,
176 'queryCallable' => [ $this, 'filterOnUserExperienceLevel' ],
177 ],
178
179 [
180 'name' => 'authorship',
181 'title' => 'rcfilters-filtergroup-authorship',
182 'class' => ChangesListBooleanFilterGroup::class,
183 'filters' => [
184 [
185 'name' => 'hidemyself',
186 'label' => 'rcfilters-filter-editsbyself-label',
187 'description' => 'rcfilters-filter-editsbyself-description',
188 // rcshowhidemine-show, rcshowhidemine-hide,
189 // wlshowhidemine
190 'showHideSuffix' => 'showhidemine',
191 'default' => false,
192 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
193 &$query_options, &$join_conds
194 ) {
195 $user = $ctx->getUser();
196 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
197 },
198 'cssClassSuffix' => 'self',
199 'isRowApplicableCallable' => function ( $ctx, $rc ) {
200 return $ctx->getUser()->equals( $rc->getPerformer() );
201 },
202 ],
203 [
204 'name' => 'hidebyothers',
205 'label' => 'rcfilters-filter-editsbyother-label',
206 'description' => 'rcfilters-filter-editsbyother-description',
207 'default' => false,
208 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
209 &$query_options, &$join_conds
210 ) {
211 $user = $ctx->getUser();
212 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
213 },
214 'cssClassSuffix' => 'others',
215 'isRowApplicableCallable' => function ( $ctx, $rc ) {
216 return !$ctx->getUser()->equals( $rc->getPerformer() );
217 },
218 ]
219 ]
220 ],
221
222 [
223 'name' => 'automated',
224 'title' => 'rcfilters-filtergroup-automated',
225 'class' => ChangesListBooleanFilterGroup::class,
226 'filters' => [
227 [
228 'name' => 'hidebots',
229 'label' => 'rcfilters-filter-bots-label',
230 'description' => 'rcfilters-filter-bots-description',
231 // rcshowhidebots-show, rcshowhidebots-hide,
232 // wlshowhidebots
233 'showHideSuffix' => 'showhidebots',
234 'default' => false,
235 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
236 &$query_options, &$join_conds
237 ) {
238 $conds[] = 'rc_bot = 0';
239 },
240 'cssClassSuffix' => 'bot',
241 'isRowApplicableCallable' => function ( $ctx, $rc ) {
242 return $rc->getAttribute( 'rc_bot' );
243 },
244 ],
245 [
246 'name' => 'hidehumans',
247 'label' => 'rcfilters-filter-humans-label',
248 'description' => 'rcfilters-filter-humans-description',
249 'default' => false,
250 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
251 &$query_options, &$join_conds
252 ) {
253 $conds[] = 'rc_bot = 1';
254 },
255 'cssClassSuffix' => 'human',
256 'isRowApplicableCallable' => function ( $ctx, $rc ) {
257 return !$rc->getAttribute( 'rc_bot' );
258 },
259 ]
260 ]
261 ],
262
263 // reviewStatus (conditional)
264
265 [
266 'name' => 'significance',
267 'title' => 'rcfilters-filtergroup-significance',
268 'class' => ChangesListBooleanFilterGroup::class,
269 'priority' => -6,
270 'filters' => [
271 [
272 'name' => 'hideminor',
273 'label' => 'rcfilters-filter-minor-label',
274 'description' => 'rcfilters-filter-minor-description',
275 // rcshowhideminor-show, rcshowhideminor-hide,
276 // wlshowhideminor
277 'showHideSuffix' => 'showhideminor',
278 'default' => false,
279 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
280 &$query_options, &$join_conds
281 ) {
282 $conds[] = 'rc_minor = 0';
283 },
284 'cssClassSuffix' => 'minor',
285 'isRowApplicableCallable' => function ( $ctx, $rc ) {
286 return $rc->getAttribute( 'rc_minor' );
287 }
288 ],
289 [
290 'name' => 'hidemajor',
291 'label' => 'rcfilters-filter-major-label',
292 'description' => 'rcfilters-filter-major-description',
293 'default' => false,
294 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
295 &$query_options, &$join_conds
296 ) {
297 $conds[] = 'rc_minor = 1';
298 },
299 'cssClassSuffix' => 'major',
300 'isRowApplicableCallable' => function ( $ctx, $rc ) {
301 return !$rc->getAttribute( 'rc_minor' );
302 }
303 ]
304 ]
305 ],
306
307 [
308 'name' => 'lastRevision',
309 'title' => 'rcfilters-filtergroup-lastRevision',
310 'class' => ChangesListBooleanFilterGroup::class,
311 'priority' => -7,
312 'filters' => [
313 [
314 'name' => 'hidelastrevision',
315 'label' => 'rcfilters-filter-lastrevision-label',
316 'description' => 'rcfilters-filter-lastrevision-description',
317 'default' => false,
318 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
319 &$query_options, &$join_conds ) {
320 $conds[] = 'rc_this_oldid <> page_latest';
321 },
322 'cssClassSuffix' => 'last',
323 'isRowApplicableCallable' => function ( $ctx, $rc ) {
324 return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
325 }
326 ],
327 [
328 'name' => 'hidepreviousrevisions',
329 'label' => 'rcfilters-filter-previousrevision-label',
330 'description' => 'rcfilters-filter-previousrevision-description',
331 'default' => false,
332 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
333 &$query_options, &$join_conds ) {
334 $conds[] = 'rc_this_oldid = page_latest';
335 },
336 'cssClassSuffix' => 'previous',
337 'isRowApplicableCallable' => function ( $ctx, $rc ) {
338 return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
339 }
340 ]
341 ]
342 ],
343
344 // With extensions, there can be change types that will not be hidden by any of these.
345 [
346 'name' => 'changeType',
347 'title' => 'rcfilters-filtergroup-changetype',
348 'class' => ChangesListBooleanFilterGroup::class,
349 'priority' => -8,
350 'filters' => [
351 [
352 'name' => 'hidepageedits',
353 'label' => 'rcfilters-filter-pageedits-label',
354 'description' => 'rcfilters-filter-pageedits-description',
355 'default' => false,
356 'priority' => -2,
357 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
358 &$query_options, &$join_conds
359 ) {
360 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT );
361 },
362 'cssClassSuffix' => 'src-mw-edit',
363 'isRowApplicableCallable' => function ( $ctx, $rc ) {
364 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_EDIT;
365 },
366 ],
367 [
368 'name' => 'hidenewpages',
369 'label' => 'rcfilters-filter-newpages-label',
370 'description' => 'rcfilters-filter-newpages-description',
371 'default' => false,
372 'priority' => -3,
373 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
374 &$query_options, &$join_conds
375 ) {
376 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW );
377 },
378 'cssClassSuffix' => 'src-mw-new',
379 'isRowApplicableCallable' => function ( $ctx, $rc ) {
380 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_NEW;
381 },
382 ],
383
384 // hidecategorization
385
386 [
387 'name' => 'hidelog',
388 'label' => 'rcfilters-filter-logactions-label',
389 'description' => 'rcfilters-filter-logactions-description',
390 'default' => false,
391 'priority' => -5,
392 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
393 &$query_options, &$join_conds
394 ) {
395 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG );
396 },
397 'cssClassSuffix' => 'src-mw-log',
398 'isRowApplicableCallable' => function ( $ctx, $rc ) {
399 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_LOG;
400 }
401 ],
402 ],
403 ],
404
405 ];
406
407 $this->reviewStatusFilterGroupDefinition = [
408 [
409 'name' => 'reviewStatus',
410 'title' => 'rcfilters-filtergroup-reviewstatus',
411 'class' => ChangesListBooleanFilterGroup::class,
412 'priority' => -5,
413 'filters' => [
414 [
415 'name' => 'hidepatrolled',
416 'label' => 'rcfilters-filter-patrolled-label',
417 'description' => 'rcfilters-filter-patrolled-description',
418 // rcshowhidepatr-show, rcshowhidepatr-hide
419 // wlshowhidepatr
420 'showHideSuffix' => 'showhidepatr',
421 'default' => false,
422 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
423 &$query_options, &$join_conds
424 ) {
425 $conds[] = 'rc_patrolled = 0';
426 },
427 'cssClassSuffix' => 'patrolled',
428 'isRowApplicableCallable' => function ( $ctx, $rc ) {
429 return $rc->getAttribute( 'rc_patrolled' );
430 },
431 ],
432 [
433 'name' => 'hideunpatrolled',
434 'label' => 'rcfilters-filter-unpatrolled-label',
435 'description' => 'rcfilters-filter-unpatrolled-description',
436 'default' => false,
437 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
438 &$query_options, &$join_conds
439 ) {
440 $conds[] = 'rc_patrolled = 1';
441 },
442 'cssClassSuffix' => 'unpatrolled',
443 'isRowApplicableCallable' => function ( $ctx, $rc ) {
444 return !$rc->getAttribute( 'rc_patrolled' );
445 },
446 ],
447 ],
448 ]
449 ];
450
451 $this->hideCategorizationFilterDefinition = [
452 'name' => 'hidecategorization',
453 'label' => 'rcfilters-filter-categorization-label',
454 'description' => 'rcfilters-filter-categorization-description',
455 // rcshowhidecategorization-show, rcshowhidecategorization-hide.
456 // wlshowhidecategorization
457 'showHideSuffix' => 'showhidecategorization',
458 'default' => false,
459 'priority' => -4,
460 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
461 &$query_options, &$join_conds
462 ) {
463 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE );
464 },
465 'cssClassSuffix' => 'src-mw-categorize',
466 'isRowApplicableCallable' => function ( $ctx, $rc ) {
467 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_CATEGORIZE;
468 },
469 ];
470 }
471
472 /**
473 * Check if filters are in conflict and guaranteed to return no results.
474 *
475 * @return bool
476 */
477 protected function areFiltersInConflict() {
478 $opts = $this->getOptions();
479 /** @var ChangesListFilterGroup $group */
480 foreach ( $this->getFilterGroups() as $group ) {
481 if ( $group->getConflictingGroups() ) {
482 wfLogWarning(
483 $group->getName() .
484 " specifies conflicts with other groups but these are not supported yet."
485 );
486 }
487
488 /** @var ChangesListFilter $conflictingFilter */
489 foreach ( $group->getConflictingFilters() as $conflictingFilter ) {
490 if ( $conflictingFilter->activelyInConflictWithGroup( $group, $opts ) ) {
491 return true;
492 }
493 }
494
495 /** @var ChangesListFilter $filter */
496 foreach ( $group->getFilters() as $filter ) {
497 /** @var ChangesListFilter $conflictingFilter */
498 foreach ( $filter->getConflictingFilters() as $conflictingFilter ) {
499 if (
500 $conflictingFilter->activelyInConflictWithFilter( $filter, $opts ) &&
501 $filter->activelyInConflictWithFilter( $conflictingFilter, $opts )
502 ) {
503 return true;
504 }
505 }
506
507 }
508
509 }
510
511 return false;
512 }
513
514 /**
515 * Main execution point
516 *
517 * @param string $subpage
518 */
519 public function execute( $subpage ) {
520 $this->rcSubpage = $subpage;
521
522 $this->setHeaders();
523 $this->outputHeader();
524 $this->addModules();
525
526 $rows = $this->getRows();
527 $opts = $this->getOptions();
528 if ( $rows === false ) {
529 $rows = new FakeResultWrapper( [] );
530 }
531
532 $batch = new LinkBatch;
533 foreach ( $rows as $row ) {
534 $batch->add( NS_USER, $row->rc_user_text );
535 $batch->add( NS_USER_TALK, $row->rc_user_text );
536 $batch->add( $row->rc_namespace, $row->rc_title );
537 if ( $row->rc_source === RecentChange::SRC_LOG ) {
538 $formatter = LogFormatter::newFromRow( $row );
539 foreach ( $formatter->getPreloadTitles() as $title ) {
540 $batch->addObj( $title );
541 }
542 }
543 }
544 $batch->execute();
545 $this->webOutput( $rows, $opts );
546
547 $rows->free();
548
549 if ( $this->getConfig()->get( 'EnableWANCacheReaper' ) ) {
550 // Clean up any bad page entries for titles showing up in RC
551 DeferredUpdates::addUpdate( new WANCacheReapUpdate(
552 $this->getDB(),
553 LoggerFactory::getInstance( 'objectcache' )
554 ) );
555 }
556
557 $this->includeRcFiltersApp();
558 }
559
560 /**
561 * Include the modules and configuration for the RCFilters app.
562 * Conditional on the user having the feature enabled.
563 */
564 protected function includeRcFiltersApp() {
565 if ( $this->isStructuredFilterUiEnabled() ) {
566 $out = $this->getOutput();
567 $jsData = $this->getStructuredFilterJsData();
568
569 $messages = [];
570 foreach ( $jsData['messageKeys'] as $key ) {
571 $messages[$key] = $this->msg( $key )->plain();
572 }
573
574 $out->addHTML(
575 ResourceLoader::makeInlineScript(
576 ResourceLoader::makeMessageSetScript( $messages )
577 )
578 );
579
580 $experimentalStructuredChangeFilters =
581 $this->getConfig()->get( 'StructuredChangeFiltersEnableExperimentalViews' );
582
583 $out->addJsConfigVars( 'wgStructuredChangeFilters', $jsData['groups'] );
584 $out->addJsConfigVars(
585 'wgStructuredChangeFiltersEnableExperimentalViews',
586 $experimentalStructuredChangeFilters
587 );
588 $out->addJsConfigVars(
589 'wgStructuredChangeFiltersEnableLiveUpdate',
590 $this->getConfig()->get( 'StructuredChangeFiltersEnableLiveUpdate' )
591 );
592 $out->addJsConfigVars(
593 'wgRCFiltersChangeTags',
594 $this->buildChangeTagList()
595 );
596 $out->addJsConfigVars(
597 'StructuredChangeFiltersDisplayConfig',
598 [
599 'maxDays' => (int)$this->getConfig()->get( 'RCMaxAge' ) / ( 24 * 3600 ), // Translate to days
600 'limitArray' => $this->getConfig()->get( 'RCLinkLimits' ),
601 'limitDefault' => $this->getDefaultLimit(),
602 'daysArray' => $this->getConfig()->get( 'RCLinkDays' ),
603 'daysDefault' => $this->getDefaultDays(),
604 ]
605 );
606 }
607 }
608
609 /**
610 * Fetch the change tags list for the front end
611 *
612 * @return Array Tag data
613 */
614 protected function buildChangeTagList() {
615 $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 );
616 $softwareActivatedTags = array_fill_keys( ChangeTags::listSoftwareActivatedTags(), 0 );
617
618 // Hit counts disabled for perf reasons, see T169997
619 /*
620 $tagStats = ChangeTags::tagUsageStatistics();
621 $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags, $tagStats );
622
623 // Sort by hits
624 arsort( $tagHitCounts );
625 */
626 $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags );
627
628 // Build the list and data
629 $result = [];
630 foreach ( $tagHitCounts as $tagName => $hits ) {
631 if (
632 // Only get active tags
633 isset( $explicitlyDefinedTags[ $tagName ] ) ||
634 isset( $softwareActivatedTags[ $tagName ] )
635 ) {
636 // Parse description
637 $desc = ChangeTags::tagLongDescriptionMessage( $tagName, $this->getContext() );
638
639 $result[] = [
640 'name' => $tagName,
641 'label' => Sanitizer::stripAllTags(
642 ChangeTags::tagDescription( $tagName, $this->getContext() )
643 ),
644 'description' => $desc ? Sanitizer::stripAllTags( $desc->parse() ) : '',
645 'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ),
646 'hits' => $hits,
647 ];
648 }
649 }
650
651 // Instead of sorting by hit count (disabled, see above), sort by display name
652 usort( $result, function ( $a, $b ) {
653 return strcasecmp( $a['label'], $b['label'] );
654 } );
655
656 return $result;
657 }
658
659 /**
660 * Add the "no results" message to the output
661 */
662 protected function outputNoResults() {
663 $this->getOutput()->addHTML(
664 '<div class="mw-changeslist-empty">' .
665 $this->msg( 'recentchanges-noresult' )->parse() .
666 '</div>'
667 );
668 }
669
670 /**
671 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
672 *
673 * @return bool|ResultWrapper Result or false
674 */
675 public function getRows() {
676 $opts = $this->getOptions();
677
678 $tables = [];
679 $fields = [];
680 $conds = [];
681 $query_options = [];
682 $join_conds = [];
683 $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
684
685 return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
686 }
687
688 /**
689 * Get the current FormOptions for this request
690 *
691 * @return FormOptions
692 */
693 public function getOptions() {
694 if ( $this->rcOptions === null ) {
695 $this->rcOptions = $this->setup( $this->rcSubpage );
696 }
697
698 return $this->rcOptions;
699 }
700
701 /**
702 * Register all filters and their groups (including those from hooks), plus handle
703 * conflicts and defaults.
704 *
705 * You might want to customize these in the same method, in subclasses. You can
706 * call getFilterGroup to access a group, and (on the group) getFilter to access a
707 * filter, then make necessary modfications to the filter or group (e.g. with
708 * setDefault).
709 */
710 protected function registerFilters() {
711 $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions );
712
713 // Make sure this is not being transcluded (we don't want to show this
714 // information to all users just because the user that saves the edit can
715 // patrol or is logged in)
716 if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
717 $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition );
718 }
719
720 $changeTypeGroup = $this->getFilterGroup( 'changeType' );
721
722 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
723 $transformedHideCategorizationDef = $this->transformFilterDefinition(
724 $this->hideCategorizationFilterDefinition
725 );
726
727 $transformedHideCategorizationDef['group'] = $changeTypeGroup;
728
729 $hideCategorization = new ChangesListBooleanFilter(
730 $transformedHideCategorizationDef
731 );
732 }
733
734 Hooks::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
735
736 $unstructuredGroupDefinition =
737 $this->getFilterGroupDefinitionFromLegacyCustomFilters(
738 $this->getCustomFilters()
739 );
740 $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
741
742 $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
743 $registered = $userExperienceLevel->getFilter( 'registered' );
744 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'newcomer' ) );
745 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'learner' ) );
746 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'experienced' ) );
747
748 $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
749 $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
750 $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
751
752 $significanceTypeGroup = $this->getFilterGroup( 'significance' );
753 $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
754
755 // categoryFilter is conditional; see registerFilters
756 if ( $categoryFilter !== null ) {
757 $hideMinorFilter->conflictsWith(
758 $categoryFilter,
759 'rcfilters-hideminor-conflicts-typeofchange-global',
760 'rcfilters-hideminor-conflicts-typeofchange',
761 'rcfilters-typeofchange-conflicts-hideminor'
762 );
763 }
764 $hideMinorFilter->conflictsWith(
765 $logactionsFilter,
766 'rcfilters-hideminor-conflicts-typeofchange-global',
767 'rcfilters-hideminor-conflicts-typeofchange',
768 'rcfilters-typeofchange-conflicts-hideminor'
769 );
770 $hideMinorFilter->conflictsWith(
771 $pagecreationFilter,
772 'rcfilters-hideminor-conflicts-typeofchange-global',
773 'rcfilters-hideminor-conflicts-typeofchange',
774 'rcfilters-typeofchange-conflicts-hideminor'
775 );
776 }
777
778 /**
779 * Transforms filter definition to prepare it for constructor.
780 *
781 * See overrides of this method as well.
782 *
783 * @param array $filterDefinition Original filter definition
784 *
785 * @return array Transformed definition
786 */
787 protected function transformFilterDefinition( array $filterDefinition ) {
788 return $filterDefinition;
789 }
790
791 /**
792 * Register filters from a definition object
793 *
794 * Array specifying groups and their filters; see Filter and
795 * ChangesListFilterGroup constructors.
796 *
797 * There is light processing to simplify core maintenance.
798 */
799 protected function registerFiltersFromDefinitions( array $definition ) {
800 $autoFillPriority = -1;
801 foreach ( $definition as $groupDefinition ) {
802 if ( !isset( $groupDefinition['priority'] ) ) {
803 $groupDefinition['priority'] = $autoFillPriority;
804 } else {
805 // If it's explicitly specified, start over the auto-fill
806 $autoFillPriority = $groupDefinition['priority'];
807 }
808
809 $autoFillPriority--;
810
811 $className = $groupDefinition['class'];
812 unset( $groupDefinition['class'] );
813
814 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
815 $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
816 }
817
818 $this->registerFilterGroup( new $className( $groupDefinition ) );
819 }
820 }
821
822 /**
823 * Get filter group definition from legacy custom filters
824 *
825 * @param array $customFilters Custom filters from legacy hooks
826 * @return array Group definition
827 */
828 protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
829 // Special internal unstructured group
830 $unstructuredGroupDefinition = [
831 'name' => 'unstructured',
832 'class' => ChangesListBooleanFilterGroup::class,
833 'priority' => -1, // Won't display in structured
834 'filters' => [],
835 ];
836
837 foreach ( $customFilters as $name => $params ) {
838 $unstructuredGroupDefinition['filters'][] = [
839 'name' => $name,
840 'showHide' => $params['msg'],
841 'default' => $params['default'],
842 ];
843 }
844
845 return $unstructuredGroupDefinition;
846 }
847
848 /**
849 * Register all the filters, including legacy hook-driven ones.
850 * Then create a FormOptions object with options as specified by the user
851 *
852 * @param array $parameters
853 *
854 * @return FormOptions
855 */
856 public function setup( $parameters ) {
857 $this->registerFilters();
858
859 $opts = $this->getDefaultOptions();
860
861 $opts = $this->fetchOptionsFromRequest( $opts );
862
863 // Give precedence to subpage syntax
864 if ( $parameters !== null ) {
865 $this->parseParameters( $parameters, $opts );
866 }
867
868 $this->validateOptions( $opts );
869
870 return $opts;
871 }
872
873 /**
874 * Get a FormOptions object containing the default options. By default, returns
875 * some basic options. The filters listed explicitly here are overriden in this
876 * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
877 * and more) are structured. Structured filters are overriden in registerFilters.
878 * not here.
879 *
880 * @return FormOptions
881 */
882 public function getDefaultOptions() {
883 $opts = new FormOptions();
884 $structuredUI = $this->isStructuredFilterUiEnabled();
885 // If urlversion=2 is set, ignore the filter defaults and set them all to false/empty
886 $useDefaults = $this->getRequest()->getInt( 'urlversion' ) !== 2;
887
888 // Add all filters
889 /** @var ChangesListFilterGroup $filterGroup */
890 foreach ( $this->filterGroups as $filterGroup ) {
891 // URL parameters can be per-group, like 'userExpLevel',
892 // or per-filter, like 'hideminor'.
893 if ( $filterGroup->isPerGroupRequestParameter() ) {
894 $opts->add( $filterGroup->getName(), $useDefaults ? $filterGroup->getDefault() : '' );
895 } else {
896 /** @var ChangesListBooleanFilter $filter */
897 foreach ( $filterGroup->getFilters() as $filter ) {
898 $opts->add( $filter->getName(), $useDefaults ? $filter->getDefault( $structuredUI ) : false );
899 }
900 }
901 }
902
903 $opts->add( 'namespace', '', FormOptions::STRING );
904 $opts->add( 'invert', false );
905 $opts->add( 'associated', false );
906 $opts->add( 'urlversion', 1 );
907
908 return $opts;
909 }
910
911 /**
912 * Register a structured changes list filter group
913 *
914 * @param ChangesListFilterGroup $group
915 */
916 public function registerFilterGroup( ChangesListFilterGroup $group ) {
917 $groupName = $group->getName();
918
919 $this->filterGroups[$groupName] = $group;
920 }
921
922 /**
923 * Gets the currently registered filters groups
924 *
925 * @return array Associative array of ChangesListFilterGroup objects, with group name as key
926 */
927 protected function getFilterGroups() {
928 return $this->filterGroups;
929 }
930
931 /**
932 * Gets a specified ChangesListFilterGroup by name
933 *
934 * @param string $groupName Name of group
935 *
936 * @return ChangesListFilterGroup|null Group, or null if not registered
937 */
938 public function getFilterGroup( $groupName ) {
939 return isset( $this->filterGroups[$groupName] ) ?
940 $this->filterGroups[$groupName] :
941 null;
942 }
943
944 // Currently, this intentionally only includes filters that display
945 // in the structured UI. This can be changed easily, though, if we want
946 // to include data on filters that use the unstructured UI. messageKeys is a
947 // special top-level value, with the value being an array of the message keys to
948 // send to the client.
949 /**
950 * Gets structured filter information needed by JS
951 *
952 * @return array Associative array
953 * * array $return['groups'] Group data
954 * * array $return['messageKeys'] Array of message keys
955 */
956 public function getStructuredFilterJsData() {
957 $output = [
958 'groups' => [],
959 'messageKeys' => [],
960 ];
961
962 usort( $this->filterGroups, function ( $a, $b ) {
963 return $b->getPriority() - $a->getPriority();
964 } );
965
966 foreach ( $this->filterGroups as $groupName => $group ) {
967 $groupOutput = $group->getJsData( $this );
968 if ( $groupOutput !== null ) {
969 $output['messageKeys'] = array_merge(
970 $output['messageKeys'],
971 $groupOutput['messageKeys']
972 );
973
974 unset( $groupOutput['messageKeys'] );
975 $output['groups'][] = $groupOutput;
976 }
977 }
978
979 return $output;
980 }
981
982 /**
983 * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
984 * hook.
985 *
986 * @return array Map of filter URL param names to properties (msg/default)
987 */
988 protected function getCustomFilters() {
989 if ( $this->customFilters === null ) {
990 $this->customFilters = [];
991 Hooks::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters ], '1.29' );
992 }
993
994 return $this->customFilters;
995 }
996
997 /**
998 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
999 *
1000 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
1001 *
1002 * @param FormOptions $opts
1003 * @return FormOptions
1004 */
1005 protected function fetchOptionsFromRequest( $opts ) {
1006 $opts->fetchValuesFromRequest( $this->getRequest() );
1007
1008 return $opts;
1009 }
1010
1011 /**
1012 * Process $par and put options found in $opts. Used when including the page.
1013 *
1014 * @param string $par
1015 * @param FormOptions $opts
1016 */
1017 public function parseParameters( $par, FormOptions $opts ) {
1018 $stringParameterNameSet = [];
1019 $hideParameterNameSet = [];
1020
1021 // URL parameters can be per-group, like 'userExpLevel',
1022 // or per-filter, like 'hideminor'.
1023
1024 foreach ( $this->filterGroups as $filterGroup ) {
1025 if ( $filterGroup->isPerGroupRequestParameter() ) {
1026 $stringParameterNameSet[$filterGroup->getName()] = true;
1027 } elseif ( $filterGroup->getType() === ChangesListBooleanFilterGroup::TYPE ) {
1028 foreach ( $filterGroup->getFilters() as $filter ) {
1029 $hideParameterNameSet[$filter->getName()] = true;
1030 }
1031 }
1032 }
1033
1034 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
1035 foreach ( $bits as $bit ) {
1036 $m = [];
1037 if ( isset( $hideParameterNameSet[$bit] ) ) {
1038 // hidefoo => hidefoo=true
1039 $opts[$bit] = true;
1040 } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
1041 // foo => hidefoo=false
1042 $opts["hide$bit"] = false;
1043 } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
1044 if ( isset( $stringParameterNameSet[$m[1]] ) ) {
1045 $opts[$m[1]] = $m[2];
1046 }
1047 }
1048 }
1049 }
1050
1051 /**
1052 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
1053 *
1054 * @param FormOptions $opts
1055 */
1056 public function validateOptions( FormOptions $opts ) {
1057 if ( $this->fixContradictoryOptions( $opts ) ) {
1058 $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
1059 $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
1060 }
1061 }
1062
1063 /**
1064 * Fix invalid options by resetting pairs that should never appear together.
1065 *
1066 * @param FormOptions $opts
1067 * @return bool True if any option was reset
1068 */
1069 private function fixContradictoryOptions( FormOptions $opts ) {
1070 $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
1071
1072 foreach ( $this->filterGroups as $filterGroup ) {
1073 if ( $filterGroup instanceof ChangesListBooleanFilterGroup ) {
1074 $filters = $filterGroup->getFilters();
1075
1076 if ( count( $filters ) === 1 ) {
1077 // legacy boolean filters should not be considered
1078 continue;
1079 }
1080
1081 $allInGroupEnabled = array_reduce(
1082 $filters,
1083 function ( $carry, $filter ) use ( $opts ) {
1084 return $carry && $opts[ $filter->getName() ];
1085 },
1086 /* initialValue */ count( $filters ) > 0
1087 );
1088
1089 if ( $allInGroupEnabled ) {
1090 foreach ( $filters as $filter ) {
1091 $opts[ $filter->getName() ] = false;
1092 }
1093
1094 $fixed = true;
1095 }
1096 }
1097 }
1098
1099 return $fixed;
1100 }
1101
1102 /**
1103 * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
1104 * compatibility.
1105 *
1106 * This is deprecated and may be removed.
1107 *
1108 * @param FormOptions $opts
1109 * @return bool True if this change was mode
1110 */
1111 private function fixBackwardsCompatibilityOptions( FormOptions $opts ) {
1112 if ( $opts['hideanons'] && $opts['hideliu'] ) {
1113 $opts->reset( 'hideanons' );
1114 if ( !$opts['hidebots'] ) {
1115 $opts->reset( 'hideliu' );
1116 $opts['hidehumans'] = 1;
1117 }
1118
1119 return true;
1120 }
1121
1122 return false;
1123 }
1124
1125 /**
1126 * Convert parameters values from true/false to 1/0
1127 * so they are not omitted by wfArrayToCgi()
1128 * Bug 36524
1129 *
1130 * @param array $params
1131 * @return array
1132 */
1133 protected function convertParamsForLink( $params ) {
1134 foreach ( $params as &$value ) {
1135 if ( $value === false ) {
1136 $value = '0';
1137 }
1138 }
1139 unset( $value );
1140 return $params;
1141 }
1142
1143 /**
1144 * Sets appropriate tables, fields, conditions, etc. depending on which filters
1145 * the user requested.
1146 *
1147 * @param array &$tables Array of tables; see IDatabase::select $table
1148 * @param array &$fields Array of fields; see IDatabase::select $vars
1149 * @param array &$conds Array of conditions; see IDatabase::select $conds
1150 * @param array &$query_options Array of query options; see IDatabase::select $options
1151 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1152 * @param FormOptions $opts
1153 */
1154 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
1155 &$join_conds, FormOptions $opts
1156 ) {
1157 $dbr = $this->getDB();
1158 $isStructuredUI = $this->isStructuredFilterUiEnabled();
1159
1160 foreach ( $this->filterGroups as $filterGroup ) {
1161 // URL parameters can be per-group, like 'userExpLevel',
1162 // or per-filter, like 'hideminor'.
1163 if ( $filterGroup->isPerGroupRequestParameter() ) {
1164 $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1165 $query_options, $join_conds, $opts[$filterGroup->getName()] );
1166 } else {
1167 foreach ( $filterGroup->getFilters() as $filter ) {
1168 if ( $filter->isActive( $opts, $isStructuredUI ) ) {
1169 $filter->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1170 $query_options, $join_conds );
1171 }
1172 }
1173 }
1174 }
1175
1176 // Namespace filtering
1177 if ( $opts[ 'namespace' ] !== '' ) {
1178 $namespaces = explode( ';', $opts[ 'namespace' ] );
1179
1180 if ( $opts[ 'associated' ] ) {
1181 $associatedNamespaces = array_map(
1182 function ( $ns ) {
1183 return MWNamespace::getAssociated( $ns );
1184 },
1185 $namespaces
1186 );
1187 $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
1188 }
1189
1190 if ( count( $namespaces ) === 1 ) {
1191 $operator = $opts[ 'invert' ] ? '!=' : '=';
1192 $value = $dbr->addQuotes( reset( $namespaces ) );
1193 } else {
1194 $operator = $opts[ 'invert' ] ? 'NOT IN' : 'IN';
1195 sort( $namespaces );
1196 $value = '(' . $dbr->makeList( $namespaces ) . ')';
1197 }
1198 $conds[] = "rc_namespace $operator $value";
1199 }
1200 }
1201
1202 /**
1203 * Process the query
1204 *
1205 * @param array $tables Array of tables; see IDatabase::select $table
1206 * @param array $fields Array of fields; see IDatabase::select $vars
1207 * @param array $conds Array of conditions; see IDatabase::select $conds
1208 * @param array $query_options Array of query options; see IDatabase::select $options
1209 * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
1210 * @param FormOptions $opts
1211 * @return bool|ResultWrapper Result or false
1212 */
1213 protected function doMainQuery( $tables, $fields, $conds,
1214 $query_options, $join_conds, FormOptions $opts
1215 ) {
1216 $tables[] = 'recentchanges';
1217 $fields = array_merge( RecentChange::selectFields(), $fields );
1218
1219 ChangeTags::modifyDisplayQuery(
1220 $tables,
1221 $fields,
1222 $conds,
1223 $join_conds,
1224 $query_options,
1225 ''
1226 );
1227
1228 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
1229 $opts )
1230 ) {
1231 return false;
1232 }
1233
1234 $dbr = $this->getDB();
1235
1236 return $dbr->select(
1237 $tables,
1238 $fields,
1239 $conds,
1240 __METHOD__,
1241 $query_options,
1242 $join_conds
1243 );
1244 }
1245
1246 protected function runMainQueryHook( &$tables, &$fields, &$conds,
1247 &$query_options, &$join_conds, $opts
1248 ) {
1249 return Hooks::run(
1250 'ChangesListSpecialPageQuery',
1251 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
1252 );
1253 }
1254
1255 /**
1256 * Return a IDatabase object for reading
1257 *
1258 * @return IDatabase
1259 */
1260 protected function getDB() {
1261 return wfGetDB( DB_REPLICA );
1262 }
1263
1264 /**
1265 * Send output to the OutputPage object, only called if not used feeds
1266 *
1267 * @param ResultWrapper $rows Database rows
1268 * @param FormOptions $opts
1269 */
1270 public function webOutput( $rows, $opts ) {
1271 if ( !$this->including() ) {
1272 $this->outputFeedLinks();
1273 $this->doHeader( $opts, $rows->numRows() );
1274 }
1275
1276 $this->outputChangesList( $rows, $opts );
1277 }
1278
1279 /**
1280 * Output feed links.
1281 */
1282 public function outputFeedLinks() {
1283 // nothing by default
1284 }
1285
1286 /**
1287 * Build and output the actual changes list.
1288 *
1289 * @param ResultWrapper $rows Database rows
1290 * @param FormOptions $opts
1291 */
1292 abstract public function outputChangesList( $rows, $opts );
1293
1294 /**
1295 * Set the text to be displayed above the changes
1296 *
1297 * @param FormOptions $opts
1298 * @param int $numRows Number of rows in the result to show after this header
1299 */
1300 public function doHeader( $opts, $numRows ) {
1301 $this->setTopText( $opts );
1302
1303 // @todo Lots of stuff should be done here.
1304
1305 $this->setBottomText( $opts );
1306 }
1307
1308 /**
1309 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
1310 * or similar methods to print the text.
1311 *
1312 * @param FormOptions $opts
1313 */
1314 public function setTopText( FormOptions $opts ) {
1315 // nothing by default
1316 }
1317
1318 /**
1319 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
1320 * or similar methods to print the text.
1321 *
1322 * @param FormOptions $opts
1323 */
1324 public function setBottomText( FormOptions $opts ) {
1325 // nothing by default
1326 }
1327
1328 /**
1329 * Get options to be displayed in a form
1330 * @todo This should handle options returned by getDefaultOptions().
1331 * @todo Not called by anything in this class (but is in subclasses), should be
1332 * called by something… doHeader() maybe?
1333 *
1334 * @param FormOptions $opts
1335 * @return array
1336 */
1337 public function getExtraOptions( $opts ) {
1338 return [];
1339 }
1340
1341 /**
1342 * Return the legend displayed within the fieldset
1343 *
1344 * @return string
1345 */
1346 public function makeLegend() {
1347 $context = $this->getContext();
1348 $user = $context->getUser();
1349 # The legend showing what the letters and stuff mean
1350 $legend = Html::openElement( 'dl' ) . "\n";
1351 # Iterates through them and gets the messages for both letter and tooltip
1352 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
1353 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
1354 unset( $legendItems['unpatrolled'] );
1355 }
1356 foreach ( $legendItems as $key => $item ) { # generate items of the legend
1357 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
1358 $letter = $item['letter'];
1359 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
1360
1361 $legend .= Html::element( 'dt',
1362 [ 'class' => $cssClass ], $context->msg( $letter )->text()
1363 ) . "\n" .
1364 Html::rawElement( 'dd',
1365 [ 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ],
1366 $context->msg( $label )->parse()
1367 ) . "\n";
1368 }
1369 # (+-123)
1370 $legend .= Html::rawElement( 'dt',
1371 [ 'class' => 'mw-plusminus-pos' ],
1372 $context->msg( 'recentchanges-legend-plusminus' )->parse()
1373 ) . "\n";
1374 $legend .= Html::element(
1375 'dd',
1376 [ 'class' => 'mw-changeslist-legend-plusminus' ],
1377 $context->msg( 'recentchanges-label-plusminus' )->text()
1378 ) . "\n";
1379 $legend .= Html::closeElement( 'dl' ) . "\n";
1380
1381 $legendHeading = $this->isStructuredFilterUiEnabled() ?
1382 $context->msg( 'rcfilters-legend-heading' )->parse() :
1383 $context->msg( 'recentchanges-legend-heading' )->parse();
1384
1385 # Collapsible
1386 $legend =
1387 '<div class="mw-changeslist-legend">' .
1388 $legendHeading .
1389 '<div class="mw-collapsible-content">' . $legend . '</div>' .
1390 '</div>';
1391
1392 return $legend;
1393 }
1394
1395 /**
1396 * Add page-specific modules.
1397 */
1398 protected function addModules() {
1399 $out = $this->getOutput();
1400 // Styles and behavior for the legend box (see makeLegend())
1401 $out->addModuleStyles( [
1402 'mediawiki.special.changeslist.legend',
1403 'mediawiki.special.changeslist',
1404 ] );
1405 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
1406
1407 if ( $this->isStructuredFilterUiEnabled() ) {
1408 $out->addModules( 'mediawiki.rcfilters.filters.ui' );
1409 $out->addModuleStyles( 'mediawiki.rcfilters.filters.base.styles' );
1410 }
1411 }
1412
1413 protected function getGroupName() {
1414 return 'changes';
1415 }
1416
1417 /**
1418 * Filter on users' experience levels; this will not be called if nothing is
1419 * selected.
1420 *
1421 * @param string $specialPageClassName Class name of current special page
1422 * @param IContextSource $context Context, for e.g. user
1423 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
1424 * @param array &$tables Array of tables; see IDatabase::select $table
1425 * @param array &$fields Array of fields; see IDatabase::select $vars
1426 * @param array &$conds Array of conditions; see IDatabase::select $conds
1427 * @param array &$query_options Array of query options; see IDatabase::select $options
1428 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1429 * @param array $selectedExpLevels The allowed active values, sorted
1430 * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
1431 * (optional)
1432 */
1433 public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
1434 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0
1435 ) {
1436 global $wgLearnerEdits,
1437 $wgExperiencedUserEdits,
1438 $wgLearnerMemberSince,
1439 $wgExperiencedUserMemberSince;
1440
1441 $LEVEL_COUNT = 5;
1442
1443 // If all levels are selected, don't filter
1444 if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
1445 return;
1446 }
1447
1448 // both 'registered' and 'unregistered', experience levels, if any, are included in 'registered'
1449 if (
1450 in_array( 'registered', $selectedExpLevels ) &&
1451 in_array( 'unregistered', $selectedExpLevels )
1452 ) {
1453 return;
1454 }
1455
1456 // 'registered' but not 'unregistered', experience levels, if any, are included in 'registered'
1457 if (
1458 in_array( 'registered', $selectedExpLevels ) &&
1459 !in_array( 'unregistered', $selectedExpLevels )
1460 ) {
1461 $conds[] = 'rc_user != 0';
1462 return;
1463 }
1464
1465 if ( $selectedExpLevels === [ 'unregistered' ] ) {
1466 $conds[] = 'rc_user = 0';
1467 return;
1468 }
1469
1470 $tables[] = 'user';
1471 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
1472
1473 if ( $now === 0 ) {
1474 $now = time();
1475 }
1476 $secondsPerDay = 86400;
1477 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
1478 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
1479
1480 $aboveNewcomer = $dbr->makeList(
1481 [
1482 'user_editcount >= ' . intval( $wgLearnerEdits ),
1483 'user_registration <= ' . $dbr->addQuotes( $dbr->timestamp( $learnerCutoff ) ),
1484 ],
1485 IDatabase::LIST_AND
1486 );
1487
1488 $aboveLearner = $dbr->makeList(
1489 [
1490 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
1491 'user_registration <= ' .
1492 $dbr->addQuotes( $dbr->timestamp( $experiencedUserCutoff ) ),
1493 ],
1494 IDatabase::LIST_AND
1495 );
1496
1497 $conditions = [];
1498
1499 if ( in_array( 'unregistered', $selectedExpLevels ) ) {
1500 $selectedExpLevels = array_diff( $selectedExpLevels, [ 'unregistered' ] );
1501 $conditions[] = 'rc_user = 0';
1502 }
1503
1504 if ( $selectedExpLevels === [ 'newcomer' ] ) {
1505 $conditions[] = "NOT ( $aboveNewcomer )";
1506 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
1507 $conditions[] = $dbr->makeList(
1508 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
1509 IDatabase::LIST_AND
1510 );
1511 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
1512 $conditions[] = $aboveLearner;
1513 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
1514 $conditions[] = "NOT ( $aboveLearner )";
1515 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
1516 $conditions[] = $dbr->makeList(
1517 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
1518 IDatabase::LIST_OR
1519 );
1520 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
1521 $conditions[] = $aboveNewcomer;
1522 } elseif ( $selectedExpLevels === [ 'experienced', 'learner', 'newcomer' ] ) {
1523 $conditions[] = 'rc_user != 0';
1524 }
1525
1526 if ( count( $conditions ) > 1 ) {
1527 $conds[] = $dbr->makeList( $conditions, IDatabase::LIST_OR );
1528 } elseif ( count( $conditions ) === 1 ) {
1529 $conds[] = reset( $conditions );
1530 }
1531 }
1532
1533 /**
1534 * Check whether the structured filter UI is enabled
1535 *
1536 * @return bool
1537 */
1538 protected function isStructuredFilterUiEnabled() {
1539 return $this->getUser()->getOption( 'rcenhancedfilters' );
1540 }
1541
1542 abstract function getDefaultLimit();
1543
1544 abstract function getDefaultDays();
1545 }