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 'label' => 'rcfilters-filter-registered-label',
90 'description' => 'rcfilters-filter-registered-description',
91 // rcshowhideliu-show, rcshowhideliu-hide,
93 'showHideSuffix' => 'showhideliu',
95 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
96 &$query_options, &$join_conds
98 $conds[] = 'rc_user = 0';
100 'cssClassSuffix' => 'liu',
101 'isRowApplicableCallable' => function ( $ctx, $rc ) {
102 return $rc->getAttribute( 'rc_user' );
107 'name' => 'hideanons',
108 'label' => 'rcfilters-filter-unregistered-label',
109 'description' => 'rcfilters-filter-unregistered-description',
110 // rcshowhideanons-show, rcshowhideanons-hide,
112 'showHideSuffix' => 'showhideanons',
114 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
115 &$query_options, &$join_conds
117 $conds[] = 'rc_user != 0';
119 'cssClassSuffix' => 'anon',
120 'isRowApplicableCallable' => function ( $ctx, $rc ) {
121 return !$rc->getAttribute( 'rc_user' );
128 'name' => 'userExpLevel',
129 'title' => 'rcfilters-filtergroup-userExpLevel',
130 'class' => ChangesListStringOptionsFilterGroup
::class,
131 // Excludes unregistered users
132 'isFullCoverage' => false,
135 'name' => 'newcomer',
136 'label' => 'rcfilters-filter-user-experience-level-newcomer-label',
137 'description' => 'rcfilters-filter-user-experience-level-newcomer-description',
138 'cssClassSuffix' => 'user-newcomer',
139 'isRowApplicableCallable' => function ( $ctx, $rc ) {
140 $performer = $rc->getPerformer();
141 return $performer && $performer->isLoggedIn() &&
142 $performer->getExperienceLevel() === 'newcomer';
147 'label' => 'rcfilters-filter-user-experience-level-learner-label',
148 'description' => 'rcfilters-filter-user-experience-level-learner-description',
149 'cssClassSuffix' => 'user-learner',
150 'isRowApplicableCallable' => function ( $ctx, $rc ) {
151 $performer = $rc->getPerformer();
152 return $performer && $performer->isLoggedIn() &&
153 $performer->getExperienceLevel() === 'learner';
157 'name' => 'experienced',
158 'label' => 'rcfilters-filter-user-experience-level-experienced-label',
159 'description' => 'rcfilters-filter-user-experience-level-experienced-description',
160 'cssClassSuffix' => 'user-experienced',
161 'isRowApplicableCallable' => function ( $ctx, $rc ) {
162 $performer = $rc->getPerformer();
163 return $performer && $performer->isLoggedIn() &&
164 $performer->getExperienceLevel() === 'experienced';
168 'default' => ChangesListStringOptionsFilterGroup
::NONE
,
169 'queryCallable' => [ $this, 'filterOnUserExperienceLevel' ],
173 'name' => 'authorship',
174 'title' => 'rcfilters-filtergroup-authorship',
175 'class' => ChangesListBooleanFilterGroup
::class,
178 'name' => 'hidemyself',
179 'label' => 'rcfilters-filter-editsbyself-label',
180 'description' => 'rcfilters-filter-editsbyself-description',
181 // rcshowhidemine-show, rcshowhidemine-hide,
183 'showHideSuffix' => 'showhidemine',
185 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
186 &$query_options, &$join_conds
188 $user = $ctx->getUser();
189 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
191 'cssClassSuffix' => 'self',
192 'isRowApplicableCallable' => function ( $ctx, $rc ) {
193 return $ctx->getUser()->equals( $rc->getPerformer() );
197 'name' => 'hidebyothers',
198 'label' => 'rcfilters-filter-editsbyother-label',
199 'description' => 'rcfilters-filter-editsbyother-description',
201 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
202 &$query_options, &$join_conds
204 $user = $ctx->getUser();
205 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
207 'cssClassSuffix' => 'others',
208 'isRowApplicableCallable' => function ( $ctx, $rc ) {
209 return !$ctx->getUser()->equals( $rc->getPerformer() );
216 'name' => 'automated',
217 'title' => 'rcfilters-filtergroup-automated',
218 'class' => ChangesListBooleanFilterGroup
::class,
221 'name' => 'hidebots',
222 'label' => 'rcfilters-filter-bots-label',
223 'description' => 'rcfilters-filter-bots-description',
224 // rcshowhidebots-show, rcshowhidebots-hide,
226 'showHideSuffix' => 'showhidebots',
228 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
229 &$query_options, &$join_conds
231 $conds[] = 'rc_bot = 0';
233 'cssClassSuffix' => 'bot',
234 'isRowApplicableCallable' => function ( $ctx, $rc ) {
235 return $rc->getAttribute( 'rc_bot' );
239 'name' => 'hidehumans',
240 'label' => 'rcfilters-filter-humans-label',
241 'description' => 'rcfilters-filter-humans-description',
243 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
244 &$query_options, &$join_conds
246 $conds[] = 'rc_bot = 1';
248 'cssClassSuffix' => 'human',
249 'isRowApplicableCallable' => function ( $ctx, $rc ) {
250 return !$rc->getAttribute( 'rc_bot' );
256 // reviewStatus (conditional)
259 'name' => 'significance',
260 'title' => 'rcfilters-filtergroup-significance',
261 'class' => ChangesListBooleanFilterGroup
::class,
265 'name' => 'hideminor',
266 'label' => 'rcfilters-filter-minor-label',
267 'description' => 'rcfilters-filter-minor-description',
268 // rcshowhideminor-show, rcshowhideminor-hide,
270 'showHideSuffix' => 'showhideminor',
272 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
273 &$query_options, &$join_conds
275 $conds[] = 'rc_minor = 0';
277 'cssClassSuffix' => 'minor',
278 'isRowApplicableCallable' => function ( $ctx, $rc ) {
279 return $rc->getAttribute( 'rc_minor' );
283 'name' => 'hidemajor',
284 'label' => 'rcfilters-filter-major-label',
285 'description' => 'rcfilters-filter-major-description',
287 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
288 &$query_options, &$join_conds
290 $conds[] = 'rc_minor = 1';
292 'cssClassSuffix' => 'major',
293 'isRowApplicableCallable' => function ( $ctx, $rc ) {
294 return !$rc->getAttribute( 'rc_minor' );
301 'name' => 'lastRevision',
302 'title' => 'rcfilters-filtergroup-lastRevision',
303 'class' => ChangesListBooleanFilterGroup
::class,
307 'name' => 'hidelastrevision',
308 'label' => 'rcfilters-filter-lastrevision-label',
309 'description' => 'rcfilters-filter-lastrevision-description',
311 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
312 &$query_options, &$join_conds ) {
313 $conds[] = 'rc_this_oldid <> page_latest';
315 'cssClassSuffix' => 'last',
316 'isRowApplicableCallable' => function ( $ctx, $rc ) {
317 return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
321 'name' => 'hidepreviousrevisions',
322 'label' => 'rcfilters-filter-previousrevision-label',
323 'description' => 'rcfilters-filter-previousrevision-description',
325 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
326 &$query_options, &$join_conds ) {
327 $conds[] = 'rc_this_oldid = page_latest';
329 'cssClassSuffix' => 'previous',
330 'isRowApplicableCallable' => function ( $ctx, $rc ) {
331 return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
337 // With extensions, there can be change types that will not be hidden by any of these.
339 'name' => 'changeType',
340 'title' => 'rcfilters-filtergroup-changetype',
341 'class' => ChangesListBooleanFilterGroup
::class,
345 'name' => 'hidepageedits',
346 'label' => 'rcfilters-filter-pageedits-label',
347 'description' => 'rcfilters-filter-pageedits-description',
350 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
351 &$query_options, &$join_conds
353 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT
);
355 'cssClassSuffix' => 'src-mw-edit',
356 'isRowApplicableCallable' => function ( $ctx, $rc ) {
357 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_EDIT
;
361 'name' => 'hidenewpages',
362 'label' => 'rcfilters-filter-newpages-label',
363 'description' => 'rcfilters-filter-newpages-description',
366 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
367 &$query_options, &$join_conds
369 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW
);
371 'cssClassSuffix' => 'src-mw-new',
372 'isRowApplicableCallable' => function ( $ctx, $rc ) {
373 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_NEW
;
377 // hidecategorization
381 'label' => 'rcfilters-filter-logactions-label',
382 'description' => 'rcfilters-filter-logactions-description',
385 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
386 &$query_options, &$join_conds
388 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG
);
390 'cssClassSuffix' => 'src-mw-log',
391 'isRowApplicableCallable' => function ( $ctx, $rc ) {
392 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_LOG
;
400 $this->reviewStatusFilterGroupDefinition
= [
402 'name' => 'reviewStatus',
403 'title' => 'rcfilters-filtergroup-reviewstatus',
404 'class' => ChangesListBooleanFilterGroup
::class,
408 'name' => 'hidepatrolled',
409 'label' => 'rcfilters-filter-patrolled-label',
410 'description' => 'rcfilters-filter-patrolled-description',
411 // rcshowhidepatr-show, rcshowhidepatr-hide
413 'showHideSuffix' => 'showhidepatr',
415 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
416 &$query_options, &$join_conds
418 $conds[] = 'rc_patrolled = 0';
420 'cssClassSuffix' => 'patrolled',
421 'isRowApplicableCallable' => function ( $ctx, $rc ) {
422 return $rc->getAttribute( 'rc_patrolled' );
426 'name' => 'hideunpatrolled',
427 'label' => 'rcfilters-filter-unpatrolled-label',
428 'description' => 'rcfilters-filter-unpatrolled-description',
430 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
431 &$query_options, &$join_conds
433 $conds[] = 'rc_patrolled = 1';
435 'cssClassSuffix' => 'unpatrolled',
436 'isRowApplicableCallable' => function ( $ctx, $rc ) {
437 return !$rc->getAttribute( 'rc_patrolled' );
444 $this->hideCategorizationFilterDefinition
= [
445 'name' => 'hidecategorization',
446 'label' => 'rcfilters-filter-categorization-label',
447 'description' => 'rcfilters-filter-categorization-description',
448 // rcshowhidecategorization-show, rcshowhidecategorization-hide.
449 // wlshowhidecategorization
450 'showHideSuffix' => 'showhidecategorization',
453 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
454 &$query_options, &$join_conds
456 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE
);
458 'cssClassSuffix' => 'src-mw-categorize',
459 'isRowApplicableCallable' => function ( $ctx, $rc ) {
460 return $rc->getAttribute( 'rc_source' ) === RecentChange
::SRC_CATEGORIZE
;
466 * Check if filters are in conflict and guaranteed to return no results.
470 protected function areFiltersInConflict() {
471 $opts = $this->getOptions();
472 /** @var ChangesListFilterGroup $group */
473 foreach ( $this->getFilterGroups() as $group ) {
474 if ( $group->getConflictingGroups() ) {
477 " specifies conflicts with other groups but these are not supported yet."
481 /** @var ChangesListFilter $conflictingFilter */
482 foreach ( $group->getConflictingFilters() as $conflictingFilter ) {
483 if ( $conflictingFilter->activelyInConflictWithGroup( $group, $opts ) ) {
488 /** @var ChangesListFilter $filter */
489 foreach ( $group->getFilters() as $filter ) {
490 /** @var ChangesListFilter $conflictingFilter */
491 foreach ( $filter->getConflictingFilters() as $conflictingFilter ) {
493 $conflictingFilter->activelyInConflictWithFilter( $filter, $opts ) &&
494 $filter->activelyInConflictWithFilter( $conflictingFilter, $opts )
508 * Main execution point
510 * @param string $subpage
512 public function execute( $subpage ) {
513 $this->rcSubpage
= $subpage;
516 $this->outputHeader();
519 $rows = $this->getRows();
520 $opts = $this->getOptions();
521 if ( $rows === false ) {
522 $rows = new FakeResultWrapper( [] );
525 $batch = new LinkBatch
;
526 foreach ( $rows as $row ) {
527 $batch->add( NS_USER
, $row->rc_user_text
);
528 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
529 $batch->add( $row->rc_namespace
, $row->rc_title
);
530 if ( $row->rc_source
=== RecentChange
::SRC_LOG
) {
531 $formatter = LogFormatter
::newFromRow( $row );
532 foreach ( $formatter->getPreloadTitles() as $title ) {
533 $batch->addObj( $title );
538 $this->webOutput( $rows, $opts );
542 if ( $this->getConfig()->get( 'EnableWANCacheReaper' ) ) {
543 // Clean up any bad page entries for titles showing up in RC
544 DeferredUpdates
::addUpdate( new WANCacheReapUpdate(
546 LoggerFactory
::getInstance( 'objectcache' )
552 * Add the "no results" message to the output
554 protected function outputNoResults() {
555 $this->getOutput()->addHTML(
556 '<div class="mw-changeslist-empty">' .
557 $this->msg( 'recentchanges-noresult' )->parse() .
563 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
565 * @return bool|ResultWrapper Result or false
567 public function getRows() {
568 $opts = $this->getOptions();
575 $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
577 return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
581 * Get the current FormOptions for this request
583 * @return FormOptions
585 public function getOptions() {
586 if ( $this->rcOptions
=== null ) {
587 $this->rcOptions
= $this->setup( $this->rcSubpage
);
590 return $this->rcOptions
;
594 * Register all filters and their groups (including those from hooks), plus handle
595 * conflicts and defaults.
597 * You might want to customize these in the same method, in subclasses. You can
598 * call getFilterGroup to access a group, and (on the group) getFilter to access a
599 * filter, then make necessary modfications to the filter or group (e.g. with
602 protected function registerFilters() {
603 $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions
);
605 // Make sure this is not being transcluded (we don't want to show this
606 // information to all users just because the user that saves the edit can
607 // patrol or is logged in)
608 if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
609 $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition
);
612 $changeTypeGroup = $this->getFilterGroup( 'changeType' );
614 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
615 $transformedHideCategorizationDef = $this->transformFilterDefinition(
616 $this->hideCategorizationFilterDefinition
619 $transformedHideCategorizationDef['group'] = $changeTypeGroup;
621 $hideCategorization = new ChangesListBooleanFilter(
622 $transformedHideCategorizationDef
626 Hooks
::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
628 $unstructuredGroupDefinition =
629 $this->getFilterGroupDefinitionFromLegacyCustomFilters(
630 $this->getCustomFilters()
632 $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
634 $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
636 $registration = $this->getFilterGroup( 'registration' );
637 $anons = $registration->getFilter( 'hideanons' );
639 // This means there is a conflict between any item in user experience level
640 // being checked and only anons being *shown* (hideliu=1&hideanons=0 in the
641 // URL, or equivalent).
642 $userExperienceLevel->conflictsWith(
644 'rcfilters-filtergroup-user-experience-level-conflicts-unregistered-global',
645 'rcfilters-filtergroup-user-experience-level-conflicts-unregistered',
646 'rcfilters-filter-unregistered-conflicts-user-experience-level'
649 $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
650 $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
651 $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
653 $significanceTypeGroup = $this->getFilterGroup( 'significance' );
654 $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
656 // categoryFilter is conditional; see registerFilters
657 if ( $categoryFilter !== null ) {
658 $hideMinorFilter->conflictsWith(
660 'rcfilters-hideminor-conflicts-typeofchange-global',
661 'rcfilters-hideminor-conflicts-typeofchange',
662 'rcfilters-typeofchange-conflicts-hideminor'
665 $hideMinorFilter->conflictsWith(
667 'rcfilters-hideminor-conflicts-typeofchange-global',
668 'rcfilters-hideminor-conflicts-typeofchange',
669 'rcfilters-typeofchange-conflicts-hideminor'
671 $hideMinorFilter->conflictsWith(
673 'rcfilters-hideminor-conflicts-typeofchange-global',
674 'rcfilters-hideminor-conflicts-typeofchange',
675 'rcfilters-typeofchange-conflicts-hideminor'
680 * Transforms filter definition to prepare it for constructor.
682 * See overrides of this method as well.
684 * @param array $filterDefinition Original filter definition
686 * @return array Transformed definition
688 protected function transformFilterDefinition( array $filterDefinition ) {
689 return $filterDefinition;
693 * Register filters from a definition object
695 * Array specifying groups and their filters; see Filter and
696 * ChangesListFilterGroup constructors.
698 * There is light processing to simplify core maintenance.
700 protected function registerFiltersFromDefinitions( array $definition ) {
701 $autoFillPriority = -1;
702 foreach ( $definition as $groupDefinition ) {
703 if ( !isset( $groupDefinition['priority'] ) ) {
704 $groupDefinition['priority'] = $autoFillPriority;
706 // If it's explicitly specified, start over the auto-fill
707 $autoFillPriority = $groupDefinition['priority'];
712 $className = $groupDefinition['class'];
713 unset( $groupDefinition['class'] );
715 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
716 $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
719 $this->registerFilterGroup( new $className( $groupDefinition ) );
724 * Get filter group definition from legacy custom filters
726 * @param array $customFilters Custom filters from legacy hooks
727 * @return array Group definition
729 protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
730 // Special internal unstructured group
731 $unstructuredGroupDefinition = [
732 'name' => 'unstructured',
733 'class' => ChangesListBooleanFilterGroup
::class,
734 'priority' => -1, // Won't display in structured
738 foreach ( $customFilters as $name => $params ) {
739 $unstructuredGroupDefinition['filters'][] = [
741 'showHide' => $params['msg'],
742 'default' => $params['default'],
746 return $unstructuredGroupDefinition;
750 * Register all the filters, including legacy hook-driven ones.
751 * Then create a FormOptions object with options as specified by the user
753 * @param array $parameters
755 * @return FormOptions
757 public function setup( $parameters ) {
758 $this->registerFilters();
760 $opts = $this->getDefaultOptions();
762 $opts = $this->fetchOptionsFromRequest( $opts );
764 // Give precedence to subpage syntax
765 if ( $parameters !== null ) {
766 $this->parseParameters( $parameters, $opts );
769 $this->validateOptions( $opts );
775 * Get a FormOptions object containing the default options. By default, returns
776 * some basic options. The filters listed explicitly here are overriden in this
777 * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
778 * and more) are structured. Structured filters are overriden in registerFilters.
781 * @return FormOptions
783 public function getDefaultOptions() {
784 $config = $this->getConfig();
785 $opts = new FormOptions();
786 $structuredUI = $this->getUser()->getOption( 'rcenhancedfilters' );
787 // If urlversion=2 is set, ignore the filter defaults and set them all to false/empty
788 $useDefaults = $this->getRequest()->getInt( 'urlversion' ) !== 2;
791 foreach ( $this->filterGroups
as $filterGroup ) {
792 // URL parameters can be per-group, like 'userExpLevel',
793 // or per-filter, like 'hideminor'.
794 if ( $filterGroup->isPerGroupRequestParameter() ) {
795 $opts->add( $filterGroup->getName(), $useDefaults ?
$filterGroup->getDefault() : '' );
797 foreach ( $filterGroup->getFilters() as $filter ) {
798 $opts->add( $filter->getName(), $useDefaults ?
$filter->getDefault( $structuredUI ) : false );
803 $opts->add( 'namespace', '', FormOptions
::STRING );
804 $opts->add( 'invert', false );
805 $opts->add( 'associated', false );
806 $opts->add( 'urlversion', 1 );
812 * Register a structured changes list filter group
814 * @param ChangesListFilterGroup $group
816 public function registerFilterGroup( ChangesListFilterGroup
$group ) {
817 $groupName = $group->getName();
819 $this->filterGroups
[$groupName] = $group;
823 * Gets the currently registered filters groups
825 * @return array Associative array of ChangesListFilterGroup objects, with group name as key
827 protected function getFilterGroups() {
828 return $this->filterGroups
;
832 * Gets a specified ChangesListFilterGroup by name
834 * @param string $groupName Name of group
836 * @return ChangesListFilterGroup|null Group, or null if not registered
838 public function getFilterGroup( $groupName ) {
839 return isset( $this->filterGroups
[$groupName] ) ?
840 $this->filterGroups
[$groupName] :
844 // Currently, this intentionally only includes filters that display
845 // in the structured UI. This can be changed easily, though, if we want
846 // to include data on filters that use the unstructured UI. messageKeys is a
847 // special top-level value, with the value being an array of the message keys to
848 // send to the client.
850 * Gets structured filter information needed by JS
852 * @return array Associative array
853 * * array $return['groups'] Group data
854 * * array $return['messageKeys'] Array of message keys
856 public function getStructuredFilterJsData() {
862 $context = $this->getContext();
864 usort( $this->filterGroups
, function ( $a, $b ) {
865 return $b->getPriority() - $a->getPriority();
868 foreach ( $this->filterGroups
as $groupName => $group ) {
869 $groupOutput = $group->getJsData( $this );
870 if ( $groupOutput !== null ) {
871 $output['messageKeys'] = array_merge(
872 $output['messageKeys'],
873 $groupOutput['messageKeys']
876 unset( $groupOutput['messageKeys'] );
877 $output['groups'][] = $groupOutput;
885 * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
888 * @return array Map of filter URL param names to properties (msg/default)
890 protected function getCustomFilters() {
891 if ( $this->customFilters
=== null ) {
892 $this->customFilters
= [];
893 Hooks
::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters
], '1.29' );
896 return $this->customFilters
;
900 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
902 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
904 * @param FormOptions $opts
905 * @return FormOptions
907 protected function fetchOptionsFromRequest( $opts ) {
908 $opts->fetchValuesFromRequest( $this->getRequest() );
914 * Process $par and put options found in $opts. Used when including the page.
917 * @param FormOptions $opts
919 public function parseParameters( $par, FormOptions
$opts ) {
920 $stringParameterNameSet = [];
921 $hideParameterNameSet = [];
923 // URL parameters can be per-group, like 'userExpLevel',
924 // or per-filter, like 'hideminor'.
926 foreach ( $this->filterGroups
as $filterGroup ) {
927 if ( $filterGroup->isPerGroupRequestParameter() ) {
928 $stringParameterNameSet[$filterGroup->getName()] = true;
929 } elseif ( $filterGroup->getType() === ChangesListBooleanFilterGroup
::TYPE
) {
930 foreach ( $filterGroup->getFilters() as $filter ) {
931 $hideParameterNameSet[$filter->getName()] = true;
936 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
937 foreach ( $bits as $bit ) {
939 if ( isset( $hideParameterNameSet[$bit] ) ) {
940 // hidefoo => hidefoo=true
942 } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
943 // foo => hidefoo=false
944 $opts["hide$bit"] = false;
945 } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
946 if ( isset( $stringParameterNameSet[$m[1]] ) ) {
947 $opts[$m[1]] = $m[2];
954 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
956 * @param FormOptions $opts
958 public function validateOptions( FormOptions
$opts ) {
959 if ( $this->fixContradictoryOptions( $opts ) ) {
960 $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
961 $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
966 * Fix invalid options by resetting pairs that should never appear together.
968 * @param FormOptions $opts
969 * @return bool True if any option was reset
971 private function fixContradictoryOptions( FormOptions
$opts ) {
972 $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
974 foreach ( $this->filterGroups
as $filterGroup ) {
975 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
976 $filters = $filterGroup->getFilters();
978 if ( count( $filters ) === 1 ) {
979 // legacy boolean filters should not be considered
983 $allInGroupEnabled = array_reduce(
985 function ( $carry, $filter ) use ( $opts ) {
986 return $carry && $opts[ $filter->getName() ];
988 /* initialValue */ count( $filters ) > 0
991 if ( $allInGroupEnabled ) {
992 foreach ( $filters as $filter ) {
993 $opts[ $filter->getName() ] = false;
1005 * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
1008 * This is deprecated and may be removed.
1010 * @param FormOptions $opts
1011 * @return bool True if this change was mode
1013 private function fixBackwardsCompatibilityOptions( FormOptions
$opts ) {
1014 if ( $opts['hideanons'] && $opts['hideliu'] ) {
1015 $opts->reset( 'hideanons' );
1016 if ( !$opts['hidebots'] ) {
1017 $opts->reset( 'hideliu' );
1018 $opts['hidehumans'] = 1;
1028 * Convert parameters values from true/false to 1/0
1029 * so they are not omitted by wfArrayToCgi()
1032 * @param array $params
1035 protected function convertParamsForLink( $params ) {
1036 foreach ( $params as &$value ) {
1037 if ( $value === false ) {
1046 * Sets appropriate tables, fields, conditions, etc. depending on which filters
1047 * the user requested.
1049 * @param array &$tables Array of tables; see IDatabase::select $table
1050 * @param array &$fields Array of fields; see IDatabase::select $vars
1051 * @param array &$conds Array of conditions; see IDatabase::select $conds
1052 * @param array &$query_options Array of query options; see IDatabase::select $options
1053 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1054 * @param FormOptions $opts
1056 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
1057 &$join_conds, FormOptions
$opts
1059 $dbr = $this->getDB();
1060 $user = $this->getUser();
1062 $context = $this->getContext();
1063 foreach ( $this->filterGroups
as $filterGroup ) {
1064 // URL parameters can be per-group, like 'userExpLevel',
1065 // or per-filter, like 'hideminor'.
1066 if ( $filterGroup->isPerGroupRequestParameter() ) {
1067 $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1068 $query_options, $join_conds, $opts[$filterGroup->getName()] );
1070 foreach ( $filterGroup->getFilters() as $filter ) {
1071 if ( $opts[$filter->getName()] ) {
1072 $filter->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1073 $query_options, $join_conds );
1079 // Namespace filtering
1080 if ( $opts[ 'namespace' ] !== '' ) {
1081 $namespaces = explode( ';', $opts[ 'namespace' ] );
1083 if ( $opts[ 'associated' ] ) {
1084 $associatedNamespaces = array_map(
1086 return MWNamespace
::getAssociated( $ns );
1090 $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
1093 if ( count( $namespaces ) === 1 ) {
1094 $operator = $opts[ 'invert' ] ?
'!=' : '=';
1095 $value = $dbr->addQuotes( reset( $namespaces ) );
1097 $operator = $opts[ 'invert' ] ?
'NOT IN' : 'IN';
1098 sort( $namespaces );
1099 $value = '(' . $dbr->makeList( $namespaces ) . ')';
1101 $conds[] = "rc_namespace $operator $value";
1108 * @param array $tables Array of tables; see IDatabase::select $table
1109 * @param array $fields Array of fields; see IDatabase::select $vars
1110 * @param array $conds Array of conditions; see IDatabase::select $conds
1111 * @param array $query_options Array of query options; see IDatabase::select $options
1112 * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
1113 * @param FormOptions $opts
1114 * @return bool|ResultWrapper Result or false
1116 protected function doMainQuery( $tables, $fields, $conds,
1117 $query_options, $join_conds, FormOptions
$opts
1119 $tables[] = 'recentchanges';
1120 $fields = array_merge( RecentChange
::selectFields(), $fields );
1122 ChangeTags
::modifyDisplayQuery(
1131 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
1137 $dbr = $this->getDB();
1139 return $dbr->select(
1149 protected function runMainQueryHook( &$tables, &$fields, &$conds,
1150 &$query_options, &$join_conds, $opts
1153 'ChangesListSpecialPageQuery',
1154 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
1159 * Return a IDatabase object for reading
1163 protected function getDB() {
1164 return wfGetDB( DB_REPLICA
);
1168 * Send output to the OutputPage object, only called if not used feeds
1170 * @param ResultWrapper $rows Database rows
1171 * @param FormOptions $opts
1173 public function webOutput( $rows, $opts ) {
1174 if ( !$this->including() ) {
1175 $this->outputFeedLinks();
1176 $this->doHeader( $opts, $rows->numRows() );
1179 $this->outputChangesList( $rows, $opts );
1183 * Output feed links.
1185 public function outputFeedLinks() {
1186 // nothing by default
1190 * Build and output the actual changes list.
1192 * @param ResultWrapper $rows Database rows
1193 * @param FormOptions $opts
1195 abstract public function outputChangesList( $rows, $opts );
1198 * Set the text to be displayed above the changes
1200 * @param FormOptions $opts
1201 * @param int $numRows Number of rows in the result to show after this header
1203 public function doHeader( $opts, $numRows ) {
1204 $this->setTopText( $opts );
1206 // @todo Lots of stuff should be done here.
1208 $this->setBottomText( $opts );
1212 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
1213 * or similar methods to print the text.
1215 * @param FormOptions $opts
1217 public function setTopText( FormOptions
$opts ) {
1218 // nothing by default
1222 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
1223 * or similar methods to print the text.
1225 * @param FormOptions $opts
1227 public function setBottomText( FormOptions
$opts ) {
1228 // nothing by default
1232 * Get options to be displayed in a form
1233 * @todo This should handle options returned by getDefaultOptions().
1234 * @todo Not called by anything in this class (but is in subclasses), should be
1235 * called by something… doHeader() maybe?
1237 * @param FormOptions $opts
1240 public function getExtraOptions( $opts ) {
1245 * Return the legend displayed within the fieldset
1249 public function makeLegend() {
1250 $context = $this->getContext();
1251 $user = $context->getUser();
1252 # The legend showing what the letters and stuff mean
1253 $legend = Html
::openElement( 'dl' ) . "\n";
1254 # Iterates through them and gets the messages for both letter and tooltip
1255 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
1256 if ( !( $user->useRCPatrol() ||
$user->useNPPatrol() ) ) {
1257 unset( $legendItems['unpatrolled'] );
1259 foreach ( $legendItems as $key => $item ) { # generate items of the legend
1260 $label = isset( $item['legend'] ) ?
$item['legend'] : $item['title'];
1261 $letter = $item['letter'];
1262 $cssClass = isset( $item['class'] ) ?
$item['class'] : $key;
1264 $legend .= Html
::element( 'dt',
1265 [ 'class' => $cssClass ], $context->msg( $letter )->text()
1267 Html
::rawElement( 'dd',
1268 [ 'class' => Sanitizer
::escapeClass( 'mw-changeslist-legend-' . $key ) ],
1269 $context->msg( $label )->parse()
1273 $legend .= Html
::rawElement( 'dt',
1274 [ 'class' => 'mw-plusminus-pos' ],
1275 $context->msg( 'recentchanges-legend-plusminus' )->parse()
1277 $legend .= Html
::element(
1279 [ 'class' => 'mw-changeslist-legend-plusminus' ],
1280 $context->msg( 'recentchanges-label-plusminus' )->text()
1282 $legend .= Html
::closeElement( 'dl' ) . "\n";
1285 $legendHeading = $this->getUser()->getOption(
1288 $context->msg( 'rcfilters-legend-heading' )->parse() :
1289 $context->msg( 'recentchanges-legend-heading' )->parse();
1291 '<div class="mw-changeslist-legend">' .
1293 '<div class="mw-collapsible-content">' . $legend . '</div>' .
1300 * Add page-specific modules.
1302 protected function addModules() {
1303 $out = $this->getOutput();
1304 // Styles and behavior for the legend box (see makeLegend())
1305 $out->addModuleStyles( [
1306 'mediawiki.special.changeslist.legend',
1307 'mediawiki.special.changeslist',
1309 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
1312 protected function getGroupName() {
1317 * Filter on users' experience levels; this will not be called if nothing is
1320 * @param string $specialPageClassName Class name of current special page
1321 * @param IContextSource $context Context, for e.g. user
1322 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
1323 * @param array &$tables Array of tables; see IDatabase::select $table
1324 * @param array &$fields Array of fields; see IDatabase::select $vars
1325 * @param array &$conds Array of conditions; see IDatabase::select $conds
1326 * @param array &$query_options Array of query options; see IDatabase::select $options
1327 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1328 * @param array $selectedExpLevels The allowed active values, sorted
1329 * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
1332 public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
1333 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0
1335 global $wgLearnerEdits,
1336 $wgExperiencedUserEdits,
1337 $wgLearnerMemberSince,
1338 $wgExperiencedUserMemberSince;
1342 // If all levels are selected, all logged-in users are included (but no
1343 // anons), so we can short-circuit.
1344 if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
1345 $conds[] = 'rc_user != 0';
1350 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
1355 $secondsPerDay = 86400;
1356 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
1357 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
1359 $aboveNewcomer = $dbr->makeList(
1361 'user_editcount >= ' . intval( $wgLearnerEdits ),
1362 'user_registration <= ' . $dbr->addQuotes( $dbr->timestamp( $learnerCutoff ) ),
1367 $aboveLearner = $dbr->makeList(
1369 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
1370 'user_registration <= ' .
1371 $dbr->addQuotes( $dbr->timestamp( $experiencedUserCutoff ) ),
1376 if ( $selectedExpLevels === [ 'newcomer' ] ) {
1377 $conds[] = "NOT ( $aboveNewcomer )";
1378 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
1379 $conds[] = $dbr->makeList(
1380 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
1383 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
1384 $conds[] = $aboveLearner;
1385 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
1386 $conds[] = "NOT ( $aboveLearner )";
1387 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
1388 $conds[] = $dbr->makeList(
1389 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
1392 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
1393 $conds[] = $aboveNewcomer;