3 * Special page which uses a ChangesList to show query results.
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.
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.
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
21 * @ingroup SpecialPage
23 use MediaWiki\Logger\LoggerFactory
;
24 use Wikimedia\Rdbms\ResultWrapper
;
25 use Wikimedia\Rdbms\FakeResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * Special page which uses a ChangesList to show query results.
30 * @todo Way too many public functions, most of them should be protected
32 * @ingroup SpecialPage
34 abstract class ChangesListSpecialPage
extends SpecialPage
{
38 /** @var FormOptions */
42 protected $customFilters;
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.
49 * Definition information for the filters and their groups
51 * The value is $groupDefinition, a parameter to the ChangesListFilterGroup constructor.
52 * However, priority is dynamically added for the core groups, to ease maintenance.
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.
59 * @var array $filterGroupDefinitions
61 private $filterGroupDefinitions;
63 // Same format as filterGroupDefinitions, but for a single group (reviewStatus)
64 // that is registered conditionally.
65 private $reviewStatusFilterGroupDefinition;
67 // Single filter registered conditionally
68 private $hideCategorizationFilterDefinition;
71 * Filter groups, and their contained filters
72 * This is an associative array (with group name as key) of ChangesListFilterGroup objects.
74 * @var array $filterGroups
76 protected $filterGroups = [];
78 public function __construct( $name, $restriction ) {
79 parent
::__construct( $name, $restriction );
81 $this->filterGroupDefinitions
= [
83 'name' => 'registration',
84 'title' => 'rcfilters-filtergroup-registration',
85 'class' => ChangesListBooleanFilterGroup
::class,
89 // rcshowhideliu-show, rcshowhideliu-hide,
91 'showHideSuffix' => 'showhideliu',
93 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
94 &$query_options, &$join_conds
96 $conds[] = 'rc_user = 0';
98 'isReplacedInStructuredUi' => true,
102 'name' => 'hideanons',
103 // rcshowhideanons-show, rcshowhideanons-hide,
105 'showHideSuffix' => 'showhideanons',
107 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
108 &$query_options, &$join_conds
110 $conds[] = 'rc_user != 0';
112 'isReplacedInStructuredUi' => true,
118 'name' => 'userExpLevel',
119 'title' => 'rcfilters-filtergroup-userExpLevel',
120 'class' => ChangesListStringOptionsFilterGroup
::class,
121 'isFullCoverage' => true,
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' );
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' );
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';
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';
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';
175 'default' => ChangesListStringOptionsFilterGroup
::NONE
,
176 'queryCallable' => [ $this, 'filterOnUserExperienceLevel' ],
180 'name' => 'authorship',
181 'title' => 'rcfilters-filtergroup-authorship',
182 'class' => ChangesListBooleanFilterGroup
::class,
185 'name' => 'hidemyself',
186 'label' => 'rcfilters-filter-editsbyself-label',
187 'description' => 'rcfilters-filter-editsbyself-description',
188 // rcshowhidemine-show, rcshowhidemine-hide,
190 'showHideSuffix' => 'showhidemine',
192 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
193 &$query_options, &$join_conds
195 $user = $ctx->getUser();
196 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
198 'cssClassSuffix' => 'self',
199 'isRowApplicableCallable' => function ( $ctx, $rc ) {
200 return $ctx->getUser()->equals( $rc->getPerformer() );
204 'name' => 'hidebyothers',
205 'label' => 'rcfilters-filter-editsbyother-label',
206 'description' => 'rcfilters-filter-editsbyother-description',
208 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
209 &$query_options, &$join_conds
211 $user = $ctx->getUser();
212 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
214 'cssClassSuffix' => 'others',
215 'isRowApplicableCallable' => function ( $ctx, $rc ) {
216 return !$ctx->getUser()->equals( $rc->getPerformer() );
223 'name' => 'automated',
224 'title' => 'rcfilters-filtergroup-automated',
225 'class' => ChangesListBooleanFilterGroup
::class,
228 'name' => 'hidebots',
229 'label' => 'rcfilters-filter-bots-label',
230 'description' => 'rcfilters-filter-bots-description',
231 // rcshowhidebots-show, rcshowhidebots-hide,
233 'showHideSuffix' => 'showhidebots',
235 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
236 &$query_options, &$join_conds
238 $conds[] = 'rc_bot = 0';
240 'cssClassSuffix' => 'bot',
241 'isRowApplicableCallable' => function ( $ctx, $rc ) {
242 return $rc->getAttribute( 'rc_bot' );
246 'name' => 'hidehumans',
247 'label' => 'rcfilters-filter-humans-label',
248 'description' => 'rcfilters-filter-humans-description',
250 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
251 &$query_options, &$join_conds
253 $conds[] = 'rc_bot = 1';
255 'cssClassSuffix' => 'human',
256 'isRowApplicableCallable' => function ( $ctx, $rc ) {
257 return !$rc->getAttribute( 'rc_bot' );
263 // reviewStatus (conditional)
266 'name' => 'significance',
267 'title' => 'rcfilters-filtergroup-significance',
268 'class' => ChangesListBooleanFilterGroup
::class,
272 'name' => 'hideminor',
273 'label' => 'rcfilters-filter-minor-label',
274 'description' => 'rcfilters-filter-minor-description',
275 // rcshowhideminor-show, rcshowhideminor-hide,
277 'showHideSuffix' => 'showhideminor',
279 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
280 &$query_options, &$join_conds
282 $conds[] = 'rc_minor = 0';
284 'cssClassSuffix' => 'minor',
285 'isRowApplicableCallable' => function ( $ctx, $rc ) {
286 return $rc->getAttribute( 'rc_minor' );
290 'name' => 'hidemajor',
291 'label' => 'rcfilters-filter-major-label',
292 'description' => 'rcfilters-filter-major-description',
294 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
295 &$query_options, &$join_conds
297 $conds[] = 'rc_minor = 1';
299 'cssClassSuffix' => 'major',
300 'isRowApplicableCallable' => function ( $ctx, $rc ) {
301 return !$rc->getAttribute( 'rc_minor' );
308 'name' => 'lastRevision',
309 'title' => 'rcfilters-filtergroup-lastRevision',
310 'class' => ChangesListBooleanFilterGroup
::class,
314 'name' => 'hidelastrevision',
315 'label' => 'rcfilters-filter-lastrevision-label',
316 'description' => 'rcfilters-filter-lastrevision-description',
318 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
319 &$query_options, &$join_conds ) {
320 $conds[] = 'rc_this_oldid <> page_latest';
322 'cssClassSuffix' => 'last',
323 'isRowApplicableCallable' => function ( $ctx, $rc ) {
324 return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
328 'name' => 'hidepreviousrevisions',
329 'label' => 'rcfilters-filter-previousrevision-label',
330 'description' => 'rcfilters-filter-previousrevision-description',
332 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
333 &$query_options, &$join_conds ) {
334 $conds[] = 'rc_this_oldid = page_latest';
336 'cssClassSuffix' => 'previous',
337 'isRowApplicableCallable' => function ( $ctx, $rc ) {
338 return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
344 // With extensions, there can be change types that will not be hidden by any of these.
346 'name' => 'changeType',
347 'title' => 'rcfilters-filtergroup-changetype',
348 'class' => ChangesListBooleanFilterGroup
::class,
352 'name' => 'hidepageedits',
353 'label' => 'rcfilters-filter-pageedits-label',
354 'description' => 'rcfilters-filter-pageedits-description',
357 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
358 &$query_options, &$join_conds
360 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT
);
362 'cssClassSuffix' => 'src-mw-edit',
363 'isRowApplicableCallable' => function ( $ctx, $rc ) {
364 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_EDIT
;
368 'name' => 'hidenewpages',
369 'label' => 'rcfilters-filter-newpages-label',
370 'description' => 'rcfilters-filter-newpages-description',
373 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
374 &$query_options, &$join_conds
376 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW
);
378 'cssClassSuffix' => 'src-mw-new',
379 'isRowApplicableCallable' => function ( $ctx, $rc ) {
380 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_NEW
;
384 // hidecategorization
388 'label' => 'rcfilters-filter-logactions-label',
389 'description' => 'rcfilters-filter-logactions-description',
392 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
393 &$query_options, &$join_conds
395 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG
);
397 'cssClassSuffix' => 'src-mw-log',
398 'isRowApplicableCallable' => function ( $ctx, $rc ) {
399 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_LOG
;
407 $this->reviewStatusFilterGroupDefinition
= [
409 'name' => 'reviewStatus',
410 'title' => 'rcfilters-filtergroup-reviewstatus',
411 'class' => ChangesListBooleanFilterGroup
::class,
415 'name' => 'hidepatrolled',
416 'label' => 'rcfilters-filter-patrolled-label',
417 'description' => 'rcfilters-filter-patrolled-description',
418 // rcshowhidepatr-show, rcshowhidepatr-hide
420 'showHideSuffix' => 'showhidepatr',
422 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
423 &$query_options, &$join_conds
425 $conds[] = 'rc_patrolled = 0';
427 'cssClassSuffix' => 'patrolled',
428 'isRowApplicableCallable' => function ( $ctx, $rc ) {
429 return $rc->getAttribute( 'rc_patrolled' );
433 'name' => 'hideunpatrolled',
434 'label' => 'rcfilters-filter-unpatrolled-label',
435 'description' => 'rcfilters-filter-unpatrolled-description',
437 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
438 &$query_options, &$join_conds
440 $conds[] = 'rc_patrolled = 1';
442 'cssClassSuffix' => 'unpatrolled',
443 'isRowApplicableCallable' => function ( $ctx, $rc ) {
444 return !$rc->getAttribute( 'rc_patrolled' );
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',
460 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
461 &$query_options, &$join_conds
463 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE
);
465 'cssClassSuffix' => 'src-mw-categorize',
466 'isRowApplicableCallable' => function ( $ctx, $rc ) {
467 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_CATEGORIZE
;
473 * Check if filters are in conflict and guaranteed to return no results.
477 protected function areFiltersInConflict() {
478 $opts = $this->getOptions();
479 /** @var ChangesListFilterGroup $group */
480 foreach ( $this->getFilterGroups() as $group ) {
481 if ( $group->getConflictingGroups() ) {
484 " specifies conflicts with other groups but these are not supported yet."
488 /** @var ChangesListFilter $conflictingFilter */
489 foreach ( $group->getConflictingFilters() as $conflictingFilter ) {
490 if ( $conflictingFilter->activelyInConflictWithGroup( $group, $opts ) ) {
495 /** @var ChangesListFilter $filter */
496 foreach ( $group->getFilters() as $filter ) {
497 /** @var ChangesListFilter $conflictingFilter */
498 foreach ( $filter->getConflictingFilters() as $conflictingFilter ) {
500 $conflictingFilter->activelyInConflictWithFilter( $filter, $opts ) &&
501 $filter->activelyInConflictWithFilter( $conflictingFilter, $opts )
515 * Main execution point
517 * @param string $subpage
519 public function execute( $subpage ) {
520 $this->rcSubpage
= $subpage;
522 $rows = $this->getRows();
523 $opts = $this->getOptions();
524 if ( $rows === false ) {
525 $rows = new FakeResultWrapper( [] );
528 // Used by Structured UI app to get results without MW chrome
529 if ( $this->getRequest()->getVal( 'action' ) === 'render' ) {
530 $this->getOutput()->setArticleBodyOnly( true );
533 // Used by "live update" and "view newest" to check
534 // if there's new changes with minimal data transfer
535 if ( $this->getRequest()->getBool( 'peek' ) ) {
536 $code = $rows->numRows() > 0 ?
200 : 304;
537 $this->getOutput()->setStatusCode( $code );
541 $batch = new LinkBatch
;
542 foreach ( $rows as $row ) {
543 $batch->add( NS_USER
, $row->rc_user_text
);
544 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
545 $batch->add( $row->rc_namespace
, $row->rc_title
);
546 if ( $row->rc_source
=== RecentChange
::SRC_LOG
) {
547 $formatter = LogFormatter
::newFromRow( $row );
548 foreach ( $formatter->getPreloadTitles() as $title ) {
549 $batch->addObj( $title );
556 $this->outputHeader();
558 $this->webOutput( $rows, $opts );
562 if ( $this->getConfig()->get( 'EnableWANCacheReaper' ) ) {
563 // Clean up any bad page entries for titles showing up in RC
564 DeferredUpdates
::addUpdate( new WANCacheReapUpdate(
566 LoggerFactory
::getInstance( 'objectcache' )
570 $this->includeRcFiltersApp();
574 * Include the modules and configuration for the RCFilters app.
575 * Conditional on the user having the feature enabled.
577 * If it is disabled, add a <body> class marking that
579 protected function includeRcFiltersApp() {
580 $out = $this->getOutput();
581 if ( $this->isStructuredFilterUiEnabled() ) {
582 $jsData = $this->getStructuredFilterJsData();
585 foreach ( $jsData['messageKeys'] as $key ) {
586 $messages[$key] = $this->msg( $key )->plain();
589 $out->addBodyClasses( 'mw-rcfilters-enabled' );
592 ResourceLoader
::makeInlineScript(
593 ResourceLoader
::makeMessageSetScript( $messages )
597 $experimentalStructuredChangeFilters =
598 $this->getConfig()->get( 'StructuredChangeFiltersEnableExperimentalViews' );
600 $out->addJsConfigVars( 'wgStructuredChangeFilters', $jsData['groups'] );
601 $out->addJsConfigVars(
602 'wgStructuredChangeFiltersEnableExperimentalViews',
603 $experimentalStructuredChangeFilters
605 $out->addJsConfigVars(
606 'wgStructuredChangeFiltersEnableLiveUpdate',
607 $this->getConfig()->get( 'StructuredChangeFiltersEnableLiveUpdate' )
609 $out->addJsConfigVars(
610 'wgRCFiltersChangeTags',
611 $this->buildChangeTagList()
613 $out->addJsConfigVars(
614 'StructuredChangeFiltersDisplayConfig',
616 'maxDays' => (int)$this->getConfig()->get( 'RCMaxAge' ) / ( 24 * 3600 ), // Translate to days
617 'limitArray' => $this->getConfig()->get( 'RCLinkLimits' ),
618 'limitDefault' => $this->getDefaultLimit(),
619 'daysArray' => $this->getConfig()->get( 'RCLinkDays' ),
620 'daysDefault' => $this->getDefaultDays(),
624 $out->addBodyClasses( 'mw-rcfilters-disabled' );
629 * Fetch the change tags list for the front end
631 * @return Array Tag data
633 protected function buildChangeTagList() {
634 $explicitlyDefinedTags = array_fill_keys( ChangeTags
::listExplicitlyDefinedTags(), 0 );
635 $softwareActivatedTags = array_fill_keys( ChangeTags
::listSoftwareActivatedTags(), 0 );
637 // Hit counts disabled for perf reasons, see T169997
639 $tagStats = ChangeTags::tagUsageStatistics();
640 $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags, $tagStats );
643 arsort( $tagHitCounts );
645 $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags );
647 // Build the list and data
649 foreach ( $tagHitCounts as $tagName => $hits ) {
651 // Only get active tags
652 isset( $explicitlyDefinedTags[ $tagName ] ) ||
653 isset( $softwareActivatedTags[ $tagName ] )
656 $desc = ChangeTags
::tagLongDescriptionMessage( $tagName, $this->getContext() );
660 'label' => Sanitizer
::stripAllTags(
661 ChangeTags
::tagDescription( $tagName, $this->getContext() )
663 'description' => $desc ? Sanitizer
::stripAllTags( $desc->parse() ) : '',
664 'cssClass' => Sanitizer
::escapeClass( 'mw-tag-' . $tagName ),
670 // Instead of sorting by hit count (disabled, see above), sort by display name
671 usort( $result, function ( $a, $b ) {
672 return strcasecmp( $a['label'], $b['label'] );
679 * Add the "no results" message to the output
681 protected function outputNoResults() {
682 $this->getOutput()->addHTML(
683 '<div class="mw-changeslist-empty">' .
684 $this->msg( 'recentchanges-noresult' )->parse() .
690 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
692 * @return bool|ResultWrapper Result or false
694 public function getRows() {
695 $opts = $this->getOptions();
702 $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
704 return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
708 * Get the current FormOptions for this request
710 * @return FormOptions
712 public function getOptions() {
713 if ( $this->rcOptions
=== null ) {
714 $this->rcOptions
= $this->setup( $this->rcSubpage
);
717 return $this->rcOptions
;
721 * Register all filters and their groups (including those from hooks), plus handle
722 * conflicts and defaults.
724 * You might want to customize these in the same method, in subclasses. You can
725 * call getFilterGroup to access a group, and (on the group) getFilter to access a
726 * filter, then make necessary modfications to the filter or group (e.g. with
729 protected function registerFilters() {
730 $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions
);
732 // Make sure this is not being transcluded (we don't want to show this
733 // information to all users just because the user that saves the edit can
734 // patrol or is logged in)
735 if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
736 $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition
);
739 $changeTypeGroup = $this->getFilterGroup( 'changeType' );
741 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
742 $transformedHideCategorizationDef = $this->transformFilterDefinition(
743 $this->hideCategorizationFilterDefinition
746 $transformedHideCategorizationDef['group'] = $changeTypeGroup;
748 $hideCategorization = new ChangesListBooleanFilter(
749 $transformedHideCategorizationDef
753 Hooks
::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
755 $unstructuredGroupDefinition =
756 $this->getFilterGroupDefinitionFromLegacyCustomFilters(
757 $this->getCustomFilters()
759 $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
761 $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
762 $registered = $userExperienceLevel->getFilter( 'registered' );
763 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'newcomer' ) );
764 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'learner' ) );
765 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'experienced' ) );
767 $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
768 $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
769 $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
771 $significanceTypeGroup = $this->getFilterGroup( 'significance' );
772 $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
774 // categoryFilter is conditional; see registerFilters
775 if ( $categoryFilter !== null ) {
776 $hideMinorFilter->conflictsWith(
778 'rcfilters-hideminor-conflicts-typeofchange-global',
779 'rcfilters-hideminor-conflicts-typeofchange',
780 'rcfilters-typeofchange-conflicts-hideminor'
783 $hideMinorFilter->conflictsWith(
785 'rcfilters-hideminor-conflicts-typeofchange-global',
786 'rcfilters-hideminor-conflicts-typeofchange',
787 'rcfilters-typeofchange-conflicts-hideminor'
789 $hideMinorFilter->conflictsWith(
791 'rcfilters-hideminor-conflicts-typeofchange-global',
792 'rcfilters-hideminor-conflicts-typeofchange',
793 'rcfilters-typeofchange-conflicts-hideminor'
798 * Transforms filter definition to prepare it for constructor.
800 * See overrides of this method as well.
802 * @param array $filterDefinition Original filter definition
804 * @return array Transformed definition
806 protected function transformFilterDefinition( array $filterDefinition ) {
807 return $filterDefinition;
811 * Register filters from a definition object
813 * Array specifying groups and their filters; see Filter and
814 * ChangesListFilterGroup constructors.
816 * There is light processing to simplify core maintenance.
817 * @param array $definition
819 protected function registerFiltersFromDefinitions( array $definition ) {
820 $autoFillPriority = -1;
821 foreach ( $definition as $groupDefinition ) {
822 if ( !isset( $groupDefinition['priority'] ) ) {
823 $groupDefinition['priority'] = $autoFillPriority;
825 // If it's explicitly specified, start over the auto-fill
826 $autoFillPriority = $groupDefinition['priority'];
831 $className = $groupDefinition['class'];
832 unset( $groupDefinition['class'] );
834 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
835 $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
838 $this->registerFilterGroup( new $className( $groupDefinition ) );
843 * Get filter group definition from legacy custom filters
845 * @param array $customFilters Custom filters from legacy hooks
846 * @return array Group definition
848 protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
849 // Special internal unstructured group
850 $unstructuredGroupDefinition = [
851 'name' => 'unstructured',
852 'class' => ChangesListBooleanFilterGroup
::class,
853 'priority' => -1, // Won't display in structured
857 foreach ( $customFilters as $name => $params ) {
858 $unstructuredGroupDefinition['filters'][] = [
860 'showHide' => $params['msg'],
861 'default' => $params['default'],
865 return $unstructuredGroupDefinition;
869 * Register all the filters, including legacy hook-driven ones.
870 * Then create a FormOptions object with options as specified by the user
872 * @param array $parameters
874 * @return FormOptions
876 public function setup( $parameters ) {
877 $this->registerFilters();
879 $opts = $this->getDefaultOptions();
881 $opts = $this->fetchOptionsFromRequest( $opts );
883 // Give precedence to subpage syntax
884 if ( $parameters !== null ) {
885 $this->parseParameters( $parameters, $opts );
888 $this->validateOptions( $opts );
894 * Get a FormOptions object containing the default options. By default, returns
895 * some basic options. The filters listed explicitly here are overriden in this
896 * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
897 * and more) are structured. Structured filters are overriden in registerFilters.
900 * @return FormOptions
902 public function getDefaultOptions() {
903 $opts = new FormOptions();
904 $structuredUI = $this->isStructuredFilterUiEnabled();
905 // If urlversion=2 is set, ignore the filter defaults and set them all to false/empty
906 $useDefaults = $this->getRequest()->getInt( 'urlversion' ) !== 2;
909 /** @var ChangesListFilterGroup $filterGroup */
910 foreach ( $this->filterGroups
as $filterGroup ) {
911 // URL parameters can be per-group, like 'userExpLevel',
912 // or per-filter, like 'hideminor'.
913 if ( $filterGroup->isPerGroupRequestParameter() ) {
914 $opts->add( $filterGroup->getName(), $useDefaults ?
$filterGroup->getDefault() : '' );
916 /** @var ChangesListBooleanFilter $filter */
917 foreach ( $filterGroup->getFilters() as $filter ) {
918 $opts->add( $filter->getName(), $useDefaults ?
$filter->getDefault( $structuredUI ) : false );
923 $opts->add( 'namespace', '', FormOptions
::STRING );
924 $opts->add( 'invert', false );
925 $opts->add( 'associated', false );
926 $opts->add( 'urlversion', 1 );
927 $opts->add( 'tagfilter', '' );
933 * Register a structured changes list filter group
935 * @param ChangesListFilterGroup $group
937 public function registerFilterGroup( ChangesListFilterGroup
$group ) {
938 $groupName = $group->getName();
940 $this->filterGroups
[$groupName] = $group;
944 * Gets the currently registered filters groups
946 * @return array Associative array of ChangesListFilterGroup objects, with group name as key
948 protected function getFilterGroups() {
949 return $this->filterGroups
;
953 * Gets a specified ChangesListFilterGroup by name
955 * @param string $groupName Name of group
957 * @return ChangesListFilterGroup|null Group, or null if not registered
959 public function getFilterGroup( $groupName ) {
960 return isset( $this->filterGroups
[$groupName] ) ?
961 $this->filterGroups
[$groupName] :
965 // Currently, this intentionally only includes filters that display
966 // in the structured UI. This can be changed easily, though, if we want
967 // to include data on filters that use the unstructured UI. messageKeys is a
968 // special top-level value, with the value being an array of the message keys to
969 // send to the client.
971 * Gets structured filter information needed by JS
973 * @return array Associative array
974 * * array $return['groups'] Group data
975 * * array $return['messageKeys'] Array of message keys
977 public function getStructuredFilterJsData() {
983 usort( $this->filterGroups
, function ( $a, $b ) {
984 return $b->getPriority() - $a->getPriority();
987 foreach ( $this->filterGroups
as $groupName => $group ) {
988 $groupOutput = $group->getJsData( $this );
989 if ( $groupOutput !== null ) {
990 $output['messageKeys'] = array_merge(
991 $output['messageKeys'],
992 $groupOutput['messageKeys']
995 unset( $groupOutput['messageKeys'] );
996 $output['groups'][] = $groupOutput;
1004 * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
1007 * @return array Map of filter URL param names to properties (msg/default)
1009 protected function getCustomFilters() {
1010 if ( $this->customFilters
=== null ) {
1011 $this->customFilters
= [];
1012 Hooks
::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters
], '1.29' );
1015 return $this->customFilters
;
1019 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
1021 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
1023 * @param FormOptions $opts
1024 * @return FormOptions
1026 protected function fetchOptionsFromRequest( $opts ) {
1027 $opts->fetchValuesFromRequest( $this->getRequest() );
1033 * Process $par and put options found in $opts. Used when including the page.
1035 * @param string $par
1036 * @param FormOptions $opts
1038 public function parseParameters( $par, FormOptions
$opts ) {
1039 $stringParameterNameSet = [];
1040 $hideParameterNameSet = [];
1042 // URL parameters can be per-group, like 'userExpLevel',
1043 // or per-filter, like 'hideminor'.
1045 foreach ( $this->filterGroups
as $filterGroup ) {
1046 if ( $filterGroup->isPerGroupRequestParameter() ) {
1047 $stringParameterNameSet[$filterGroup->getName()] = true;
1048 } elseif ( $filterGroup->getType() === ChangesListBooleanFilterGroup
::TYPE
) {
1049 foreach ( $filterGroup->getFilters() as $filter ) {
1050 $hideParameterNameSet[$filter->getName()] = true;
1055 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
1056 foreach ( $bits as $bit ) {
1058 if ( isset( $hideParameterNameSet[$bit] ) ) {
1059 // hidefoo => hidefoo=true
1061 } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
1062 // foo => hidefoo=false
1063 $opts["hide$bit"] = false;
1064 } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
1065 if ( isset( $stringParameterNameSet[$m[1]] ) ) {
1066 $opts[$m[1]] = $m[2];
1073 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
1075 * @param FormOptions $opts
1077 public function validateOptions( FormOptions
$opts ) {
1078 if ( $this->fixContradictoryOptions( $opts ) ) {
1079 $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
1080 $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
1085 * Fix invalid options by resetting pairs that should never appear together.
1087 * @param FormOptions $opts
1088 * @return bool True if any option was reset
1090 private function fixContradictoryOptions( FormOptions
$opts ) {
1091 $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
1093 foreach ( $this->filterGroups
as $filterGroup ) {
1094 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
1095 $filters = $filterGroup->getFilters();
1097 if ( count( $filters ) === 1 ) {
1098 // legacy boolean filters should not be considered
1102 $allInGroupEnabled = array_reduce(
1104 function ( $carry, $filter ) use ( $opts ) {
1105 return $carry && $opts[ $filter->getName() ];
1107 /* initialValue */ count( $filters ) > 0
1110 if ( $allInGroupEnabled ) {
1111 foreach ( $filters as $filter ) {
1112 $opts[ $filter->getName() ] = false;
1124 * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
1127 * This is deprecated and may be removed.
1129 * @param FormOptions $opts
1130 * @return bool True if this change was mode
1132 private function fixBackwardsCompatibilityOptions( FormOptions
$opts ) {
1133 if ( $opts['hideanons'] && $opts['hideliu'] ) {
1134 $opts->reset( 'hideanons' );
1135 if ( !$opts['hidebots'] ) {
1136 $opts->reset( 'hideliu' );
1137 $opts['hidehumans'] = 1;
1147 * Convert parameters values from true/false to 1/0
1148 * so they are not omitted by wfArrayToCgi()
1151 * @param array $params
1154 protected function convertParamsForLink( $params ) {
1155 foreach ( $params as &$value ) {
1156 if ( $value === false ) {
1165 * Sets appropriate tables, fields, conditions, etc. depending on which filters
1166 * the user requested.
1168 * @param array &$tables Array of tables; see IDatabase::select $table
1169 * @param array &$fields Array of fields; see IDatabase::select $vars
1170 * @param array &$conds Array of conditions; see IDatabase::select $conds
1171 * @param array &$query_options Array of query options; see IDatabase::select $options
1172 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1173 * @param FormOptions $opts
1175 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
1176 &$join_conds, FormOptions
$opts
1178 $dbr = $this->getDB();
1179 $isStructuredUI = $this->isStructuredFilterUiEnabled();
1181 foreach ( $this->filterGroups
as $filterGroup ) {
1182 // URL parameters can be per-group, like 'userExpLevel',
1183 // or per-filter, like 'hideminor'.
1184 if ( $filterGroup->isPerGroupRequestParameter() ) {
1185 $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1186 $query_options, $join_conds, $opts[$filterGroup->getName()] );
1188 foreach ( $filterGroup->getFilters() as $filter ) {
1189 if ( $filter->isActive( $opts, $isStructuredUI ) ) {
1190 $filter->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1191 $query_options, $join_conds );
1197 // Namespace filtering
1198 if ( $opts[ 'namespace' ] !== '' ) {
1199 $namespaces = explode( ';', $opts[ 'namespace' ] );
1201 if ( $opts[ 'associated' ] ) {
1202 $associatedNamespaces = array_map(
1204 return MWNamespace
::getAssociated( $ns );
1208 $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
1211 if ( count( $namespaces ) === 1 ) {
1212 $operator = $opts[ 'invert' ] ?
'!=' : '=';
1213 $value = $dbr->addQuotes( reset( $namespaces ) );
1215 $operator = $opts[ 'invert' ] ?
'NOT IN' : 'IN';
1216 sort( $namespaces );
1217 $value = '(' . $dbr->makeList( $namespaces ) . ')';
1219 $conds[] = "rc_namespace $operator $value";
1226 * @param array $tables Array of tables; see IDatabase::select $table
1227 * @param array $fields Array of fields; see IDatabase::select $vars
1228 * @param array $conds Array of conditions; see IDatabase::select $conds
1229 * @param array $query_options Array of query options; see IDatabase::select $options
1230 * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
1231 * @param FormOptions $opts
1232 * @return bool|ResultWrapper Result or false
1234 protected function doMainQuery( $tables, $fields, $conds,
1235 $query_options, $join_conds, FormOptions
$opts
1237 $tables[] = 'recentchanges';
1238 $fields = array_merge( RecentChange
::selectFields(), $fields );
1240 ChangeTags
::modifyDisplayQuery(
1249 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
1255 $dbr = $this->getDB();
1257 return $dbr->select(
1267 protected function runMainQueryHook( &$tables, &$fields, &$conds,
1268 &$query_options, &$join_conds, $opts
1271 'ChangesListSpecialPageQuery',
1272 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
1277 * Return a IDatabase object for reading
1281 protected function getDB() {
1282 return wfGetDB( DB_REPLICA
);
1286 * Send output to the OutputPage object, only called if not used feeds
1288 * @param ResultWrapper $rows Database rows
1289 * @param FormOptions $opts
1291 public function webOutput( $rows, $opts ) {
1292 if ( !$this->including() ) {
1293 $this->outputFeedLinks();
1294 $this->doHeader( $opts, $rows->numRows() );
1297 $this->outputChangesList( $rows, $opts );
1301 * Output feed links.
1303 public function outputFeedLinks() {
1304 // nothing by default
1308 * Build and output the actual changes list.
1310 * @param ResultWrapper $rows Database rows
1311 * @param FormOptions $opts
1313 abstract public function outputChangesList( $rows, $opts );
1316 * Set the text to be displayed above the changes
1318 * @param FormOptions $opts
1319 * @param int $numRows Number of rows in the result to show after this header
1321 public function doHeader( $opts, $numRows ) {
1322 $this->setTopText( $opts );
1324 // @todo Lots of stuff should be done here.
1326 $this->setBottomText( $opts );
1330 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
1331 * or similar methods to print the text.
1333 * @param FormOptions $opts
1335 public function setTopText( FormOptions
$opts ) {
1336 // nothing by default
1340 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
1341 * or similar methods to print the text.
1343 * @param FormOptions $opts
1345 public function setBottomText( FormOptions
$opts ) {
1346 // nothing by default
1350 * Get options to be displayed in a form
1351 * @todo This should handle options returned by getDefaultOptions().
1352 * @todo Not called by anything in this class (but is in subclasses), should be
1353 * called by something… doHeader() maybe?
1355 * @param FormOptions $opts
1358 public function getExtraOptions( $opts ) {
1363 * Return the legend displayed within the fieldset
1367 public function makeLegend() {
1368 $context = $this->getContext();
1369 $user = $context->getUser();
1370 # The legend showing what the letters and stuff mean
1371 $legend = Html
::openElement( 'dl' ) . "\n";
1372 # Iterates through them and gets the messages for both letter and tooltip
1373 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
1374 if ( !( $user->useRCPatrol() ||
$user->useNPPatrol() ) ) {
1375 unset( $legendItems['unpatrolled'] );
1377 foreach ( $legendItems as $key => $item ) { # generate items of the legend
1378 $label = isset( $item['legend'] ) ?
$item['legend'] : $item['title'];
1379 $letter = $item['letter'];
1380 $cssClass = isset( $item['class'] ) ?
$item['class'] : $key;
1382 $legend .= Html
::element( 'dt',
1383 [ 'class' => $cssClass ], $context->msg( $letter )->text()
1385 Html
::rawElement( 'dd',
1386 [ 'class' => Sanitizer
::escapeClass( 'mw-changeslist-legend-' . $key ) ],
1387 $context->msg( $label )->parse()
1391 $legend .= Html
::rawElement( 'dt',
1392 [ 'class' => 'mw-plusminus-pos' ],
1393 $context->msg( 'recentchanges-legend-plusminus' )->parse()
1395 $legend .= Html
::element(
1397 [ 'class' => 'mw-changeslist-legend-plusminus' ],
1398 $context->msg( 'recentchanges-label-plusminus' )->text()
1400 $legend .= Html
::closeElement( 'dl' ) . "\n";
1402 $legendHeading = $this->isStructuredFilterUiEnabled() ?
1403 $context->msg( 'rcfilters-legend-heading' )->parse() :
1404 $context->msg( 'recentchanges-legend-heading' )->parse();
1408 '<div class="mw-changeslist-legend">' .
1410 '<div class="mw-collapsible-content">' . $legend . '</div>' .
1417 * Add page-specific modules.
1419 protected function addModules() {
1420 $out = $this->getOutput();
1421 // Styles and behavior for the legend box (see makeLegend())
1422 $out->addModuleStyles( [
1423 'mediawiki.special.changeslist.legend',
1424 'mediawiki.special.changeslist',
1426 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
1428 if ( $this->isStructuredFilterUiEnabled() ) {
1429 $out->addModules( 'mediawiki.rcfilters.filters.ui' );
1430 $out->addModuleStyles( 'mediawiki.rcfilters.filters.base.styles' );
1434 protected function getGroupName() {
1439 * Filter on users' experience levels; this will not be called if nothing is
1442 * @param string $specialPageClassName Class name of current special page
1443 * @param IContextSource $context Context, for e.g. user
1444 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
1445 * @param array &$tables Array of tables; see IDatabase::select $table
1446 * @param array &$fields Array of fields; see IDatabase::select $vars
1447 * @param array &$conds Array of conditions; see IDatabase::select $conds
1448 * @param array &$query_options Array of query options; see IDatabase::select $options
1449 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1450 * @param array $selectedExpLevels The allowed active values, sorted
1451 * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
1454 public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
1455 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0
1457 global $wgLearnerEdits,
1458 $wgExperiencedUserEdits,
1459 $wgLearnerMemberSince,
1460 $wgExperiencedUserMemberSince;
1464 // If all levels are selected, don't filter
1465 if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
1469 // both 'registered' and 'unregistered', experience levels, if any, are included in 'registered'
1471 in_array( 'registered', $selectedExpLevels ) &&
1472 in_array( 'unregistered', $selectedExpLevels )
1477 // 'registered' but not 'unregistered', experience levels, if any, are included in 'registered'
1479 in_array( 'registered', $selectedExpLevels ) &&
1480 !in_array( 'unregistered', $selectedExpLevels )
1482 $conds[] = 'rc_user != 0';
1486 if ( $selectedExpLevels === [ 'unregistered' ] ) {
1487 $conds[] = 'rc_user = 0';
1492 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
1497 $secondsPerDay = 86400;
1498 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
1499 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
1501 $aboveNewcomer = $dbr->makeList(
1503 'user_editcount >= ' . intval( $wgLearnerEdits ),
1504 'user_registration <= ' . $dbr->addQuotes( $dbr->timestamp( $learnerCutoff ) ),
1509 $aboveLearner = $dbr->makeList(
1511 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
1512 'user_registration <= ' .
1513 $dbr->addQuotes( $dbr->timestamp( $experiencedUserCutoff ) ),
1520 if ( in_array( 'unregistered', $selectedExpLevels ) ) {
1521 $selectedExpLevels = array_diff( $selectedExpLevels, [ 'unregistered' ] );
1522 $conditions[] = 'rc_user = 0';
1525 if ( $selectedExpLevels === [ 'newcomer' ] ) {
1526 $conditions[] = "NOT ( $aboveNewcomer )";
1527 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
1528 $conditions[] = $dbr->makeList(
1529 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
1532 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
1533 $conditions[] = $aboveLearner;
1534 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
1535 $conditions[] = "NOT ( $aboveLearner )";
1536 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
1537 $conditions[] = $dbr->makeList(
1538 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
1541 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
1542 $conditions[] = $aboveNewcomer;
1543 } elseif ( $selectedExpLevels === [ 'experienced', 'learner', 'newcomer' ] ) {
1544 $conditions[] = 'rc_user != 0';
1547 if ( count( $conditions ) > 1 ) {
1548 $conds[] = $dbr->makeList( $conditions, IDatabase
::LIST_OR
);
1549 } elseif ( count( $conditions ) === 1 ) {
1550 $conds[] = reset( $conditions );
1555 * Check whether the structured filter UI is enabled
1559 public function isStructuredFilterUiEnabled() {
1560 if ( $this->getRequest()->getBool( 'rcfilters' ) ) {
1564 if ( $this->getConfig()->get( 'StructuredChangeFiltersShowPreference' ) ) {
1565 return !$this->getUser()->getOption( 'rcenhancedfilters-disable' );
1567 return $this->getUser()->getOption( 'rcenhancedfilters' );
1572 * Check whether the structured filter UI is enabled by default (regardless of
1573 * this particular user's setting)
1577 public function isStructuredFilterUiEnabledByDefault() {
1578 if ( $this->getConfig()->get( 'StructuredChangeFiltersShowPreference' ) ) {
1579 return !$this->getUser()->getDefaultOption( 'rcenhancedfilters-disable' );
1581 return $this->getUser()->getDefaultOption( 'rcenhancedfilters' );
1585 abstract function getDefaultLimit();
1587 abstract function getDefaultDays();