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;
523 $this->outputHeader();
526 $rows = $this->getRows();
527 $opts = $this->getOptions();
528 if ( $rows === false ) {
529 $rows = new FakeResultWrapper( [] );
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 );
545 $this->webOutput( $rows, $opts );
549 if ( $this->getConfig()->get( 'EnableWANCacheReaper' ) ) {
550 // Clean up any bad page entries for titles showing up in RC
551 DeferredUpdates
::addUpdate( new WANCacheReapUpdate(
553 LoggerFactory
::getInstance( 'objectcache' )
559 * Add the "no results" message to the output
561 protected function outputNoResults() {
562 $this->getOutput()->addHTML(
563 '<div class="mw-changeslist-empty">' .
564 $this->msg( 'recentchanges-noresult' )->parse() .
570 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
572 * @return bool|ResultWrapper Result or false
574 public function getRows() {
575 $opts = $this->getOptions();
582 $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
584 return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
588 * Get the current FormOptions for this request
590 * @return FormOptions
592 public function getOptions() {
593 if ( $this->rcOptions
=== null ) {
594 $this->rcOptions
= $this->setup( $this->rcSubpage
);
597 return $this->rcOptions
;
601 * Register all filters and their groups (including those from hooks), plus handle
602 * conflicts and defaults.
604 * You might want to customize these in the same method, in subclasses. You can
605 * call getFilterGroup to access a group, and (on the group) getFilter to access a
606 * filter, then make necessary modfications to the filter or group (e.g. with
609 protected function registerFilters() {
610 $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions
);
612 // Make sure this is not being transcluded (we don't want to show this
613 // information to all users just because the user that saves the edit can
614 // patrol or is logged in)
615 if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
616 $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition
);
619 $changeTypeGroup = $this->getFilterGroup( 'changeType' );
621 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
622 $transformedHideCategorizationDef = $this->transformFilterDefinition(
623 $this->hideCategorizationFilterDefinition
626 $transformedHideCategorizationDef['group'] = $changeTypeGroup;
628 $hideCategorization = new ChangesListBooleanFilter(
629 $transformedHideCategorizationDef
633 Hooks
::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
635 $unstructuredGroupDefinition =
636 $this->getFilterGroupDefinitionFromLegacyCustomFilters(
637 $this->getCustomFilters()
639 $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
641 $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
642 $registered = $userExperienceLevel->getFilter( 'registered' );
643 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'newcomer' ) );
644 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'learner' ) );
645 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'experienced' ) );
647 $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
648 $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
649 $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
651 $significanceTypeGroup = $this->getFilterGroup( 'significance' );
652 $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
654 // categoryFilter is conditional; see registerFilters
655 if ( $categoryFilter !== null ) {
656 $hideMinorFilter->conflictsWith(
658 'rcfilters-hideminor-conflicts-typeofchange-global',
659 'rcfilters-hideminor-conflicts-typeofchange',
660 'rcfilters-typeofchange-conflicts-hideminor'
663 $hideMinorFilter->conflictsWith(
665 'rcfilters-hideminor-conflicts-typeofchange-global',
666 'rcfilters-hideminor-conflicts-typeofchange',
667 'rcfilters-typeofchange-conflicts-hideminor'
669 $hideMinorFilter->conflictsWith(
671 'rcfilters-hideminor-conflicts-typeofchange-global',
672 'rcfilters-hideminor-conflicts-typeofchange',
673 'rcfilters-typeofchange-conflicts-hideminor'
678 * Transforms filter definition to prepare it for constructor.
680 * See overrides of this method as well.
682 * @param array $filterDefinition Original filter definition
684 * @return array Transformed definition
686 protected function transformFilterDefinition( array $filterDefinition ) {
687 return $filterDefinition;
691 * Register filters from a definition object
693 * Array specifying groups and their filters; see Filter and
694 * ChangesListFilterGroup constructors.
696 * There is light processing to simplify core maintenance.
698 protected function registerFiltersFromDefinitions( array $definition ) {
699 $autoFillPriority = -1;
700 foreach ( $definition as $groupDefinition ) {
701 if ( !isset( $groupDefinition['priority'] ) ) {
702 $groupDefinition['priority'] = $autoFillPriority;
704 // If it's explicitly specified, start over the auto-fill
705 $autoFillPriority = $groupDefinition['priority'];
710 $className = $groupDefinition['class'];
711 unset( $groupDefinition['class'] );
713 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
714 $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
717 $this->registerFilterGroup( new $className( $groupDefinition ) );
722 * Get filter group definition from legacy custom filters
724 * @param array $customFilters Custom filters from legacy hooks
725 * @return array Group definition
727 protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
728 // Special internal unstructured group
729 $unstructuredGroupDefinition = [
730 'name' => 'unstructured',
731 'class' => ChangesListBooleanFilterGroup
::class,
732 'priority' => -1, // Won't display in structured
736 foreach ( $customFilters as $name => $params ) {
737 $unstructuredGroupDefinition['filters'][] = [
739 'showHide' => $params['msg'],
740 'default' => $params['default'],
744 return $unstructuredGroupDefinition;
748 * Register all the filters, including legacy hook-driven ones.
749 * Then create a FormOptions object with options as specified by the user
751 * @param array $parameters
753 * @return FormOptions
755 public function setup( $parameters ) {
756 $this->registerFilters();
758 $opts = $this->getDefaultOptions();
760 $opts = $this->fetchOptionsFromRequest( $opts );
762 // Give precedence to subpage syntax
763 if ( $parameters !== null ) {
764 $this->parseParameters( $parameters, $opts );
767 $this->validateOptions( $opts );
773 * Get a FormOptions object containing the default options. By default, returns
774 * some basic options. The filters listed explicitly here are overriden in this
775 * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
776 * and more) are structured. Structured filters are overriden in registerFilters.
779 * @return FormOptions
781 public function getDefaultOptions() {
782 $config = $this->getConfig();
783 $opts = new FormOptions();
784 $structuredUI = $this->getUser()->getOption( 'rcenhancedfilters' );
785 // If urlversion=2 is set, ignore the filter defaults and set them all to false/empty
786 $useDefaults = $this->getRequest()->getInt( 'urlversion' ) !== 2;
789 foreach ( $this->filterGroups
as $filterGroup ) {
790 // URL parameters can be per-group, like 'userExpLevel',
791 // or per-filter, like 'hideminor'.
792 if ( $filterGroup->isPerGroupRequestParameter() ) {
793 $opts->add( $filterGroup->getName(), $useDefaults ?
$filterGroup->getDefault() : '' );
795 foreach ( $filterGroup->getFilters() as $filter ) {
796 $opts->add( $filter->getName(), $useDefaults ?
$filter->getDefault( $structuredUI ) : false );
801 $opts->add( 'namespace', '', FormOptions
::STRING );
802 $opts->add( 'invert', false );
803 $opts->add( 'associated', false );
804 $opts->add( 'urlversion', 1 );
810 * Register a structured changes list filter group
812 * @param ChangesListFilterGroup $group
814 public function registerFilterGroup( ChangesListFilterGroup
$group ) {
815 $groupName = $group->getName();
817 $this->filterGroups
[$groupName] = $group;
821 * Gets the currently registered filters groups
823 * @return array Associative array of ChangesListFilterGroup objects, with group name as key
825 protected function getFilterGroups() {
826 return $this->filterGroups
;
830 * Gets a specified ChangesListFilterGroup by name
832 * @param string $groupName Name of group
834 * @return ChangesListFilterGroup|null Group, or null if not registered
836 public function getFilterGroup( $groupName ) {
837 return isset( $this->filterGroups
[$groupName] ) ?
838 $this->filterGroups
[$groupName] :
842 // Currently, this intentionally only includes filters that display
843 // in the structured UI. This can be changed easily, though, if we want
844 // to include data on filters that use the unstructured UI. messageKeys is a
845 // special top-level value, with the value being an array of the message keys to
846 // send to the client.
848 * Gets structured filter information needed by JS
850 * @return array Associative array
851 * * array $return['groups'] Group data
852 * * array $return['messageKeys'] Array of message keys
854 public function getStructuredFilterJsData() {
860 $context = $this->getContext();
862 usort( $this->filterGroups
, function ( $a, $b ) {
863 return $b->getPriority() - $a->getPriority();
866 foreach ( $this->filterGroups
as $groupName => $group ) {
867 $groupOutput = $group->getJsData( $this );
868 if ( $groupOutput !== null ) {
869 $output['messageKeys'] = array_merge(
870 $output['messageKeys'],
871 $groupOutput['messageKeys']
874 unset( $groupOutput['messageKeys'] );
875 $output['groups'][] = $groupOutput;
883 * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
886 * @return array Map of filter URL param names to properties (msg/default)
888 protected function getCustomFilters() {
889 if ( $this->customFilters
=== null ) {
890 $this->customFilters
= [];
891 Hooks
::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters
], '1.29' );
894 return $this->customFilters
;
898 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
900 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
902 * @param FormOptions $opts
903 * @return FormOptions
905 protected function fetchOptionsFromRequest( $opts ) {
906 $opts->fetchValuesFromRequest( $this->getRequest() );
912 * Process $par and put options found in $opts. Used when including the page.
915 * @param FormOptions $opts
917 public function parseParameters( $par, FormOptions
$opts ) {
918 $stringParameterNameSet = [];
919 $hideParameterNameSet = [];
921 // URL parameters can be per-group, like 'userExpLevel',
922 // or per-filter, like 'hideminor'.
924 foreach ( $this->filterGroups
as $filterGroup ) {
925 if ( $filterGroup->isPerGroupRequestParameter() ) {
926 $stringParameterNameSet[$filterGroup->getName()] = true;
927 } elseif ( $filterGroup->getType() === ChangesListBooleanFilterGroup
::TYPE
) {
928 foreach ( $filterGroup->getFilters() as $filter ) {
929 $hideParameterNameSet[$filter->getName()] = true;
934 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
935 foreach ( $bits as $bit ) {
937 if ( isset( $hideParameterNameSet[$bit] ) ) {
938 // hidefoo => hidefoo=true
940 } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
941 // foo => hidefoo=false
942 $opts["hide$bit"] = false;
943 } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
944 if ( isset( $stringParameterNameSet[$m[1]] ) ) {
945 $opts[$m[1]] = $m[2];
952 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
954 * @param FormOptions $opts
956 public function validateOptions( FormOptions
$opts ) {
957 if ( $this->fixContradictoryOptions( $opts ) ) {
958 $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
959 $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
964 * Fix invalid options by resetting pairs that should never appear together.
966 * @param FormOptions $opts
967 * @return bool True if any option was reset
969 private function fixContradictoryOptions( FormOptions
$opts ) {
970 $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
972 foreach ( $this->filterGroups
as $filterGroup ) {
973 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
974 $filters = $filterGroup->getFilters();
976 if ( count( $filters ) === 1 ) {
977 // legacy boolean filters should not be considered
981 $allInGroupEnabled = array_reduce(
983 function ( $carry, $filter ) use ( $opts ) {
984 return $carry && $opts[ $filter->getName() ];
986 /* initialValue */ count( $filters ) > 0
989 if ( $allInGroupEnabled ) {
990 foreach ( $filters as $filter ) {
991 $opts[ $filter->getName() ] = false;
1003 * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
1006 * This is deprecated and may be removed.
1008 * @param FormOptions $opts
1009 * @return bool True if this change was mode
1011 private function fixBackwardsCompatibilityOptions( FormOptions
$opts ) {
1012 if ( $opts['hideanons'] && $opts['hideliu'] ) {
1013 $opts->reset( 'hideanons' );
1014 if ( !$opts['hidebots'] ) {
1015 $opts->reset( 'hideliu' );
1016 $opts['hidehumans'] = 1;
1026 * Convert parameters values from true/false to 1/0
1027 * so they are not omitted by wfArrayToCgi()
1030 * @param array $params
1033 protected function convertParamsForLink( $params ) {
1034 foreach ( $params as &$value ) {
1035 if ( $value === false ) {
1044 * Sets appropriate tables, fields, conditions, etc. depending on which filters
1045 * the user requested.
1047 * @param array &$tables Array of tables; see IDatabase::select $table
1048 * @param array &$fields Array of fields; see IDatabase::select $vars
1049 * @param array &$conds Array of conditions; see IDatabase::select $conds
1050 * @param array &$query_options Array of query options; see IDatabase::select $options
1051 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1052 * @param FormOptions $opts
1054 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
1055 &$join_conds, FormOptions
$opts
1057 $dbr = $this->getDB();
1058 $user = $this->getUser();
1060 $context = $this->getContext();
1061 foreach ( $this->filterGroups
as $filterGroup ) {
1062 // URL parameters can be per-group, like 'userExpLevel',
1063 // or per-filter, like 'hideminor'.
1064 if ( $filterGroup->isPerGroupRequestParameter() ) {
1065 $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1066 $query_options, $join_conds, $opts[$filterGroup->getName()] );
1068 foreach ( $filterGroup->getFilters() as $filter ) {
1069 if ( $opts[$filter->getName()] ) {
1070 $filter->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1071 $query_options, $join_conds );
1077 // Namespace filtering
1078 if ( $opts[ 'namespace' ] !== '' ) {
1079 $namespaces = explode( ';', $opts[ 'namespace' ] );
1081 if ( $opts[ 'associated' ] ) {
1082 $associatedNamespaces = array_map(
1084 return MWNamespace
::getAssociated( $ns );
1088 $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
1091 if ( count( $namespaces ) === 1 ) {
1092 $operator = $opts[ 'invert' ] ?
'!=' : '=';
1093 $value = $dbr->addQuotes( reset( $namespaces ) );
1095 $operator = $opts[ 'invert' ] ?
'NOT IN' : 'IN';
1096 sort( $namespaces );
1097 $value = '(' . $dbr->makeList( $namespaces ) . ')';
1099 $conds[] = "rc_namespace $operator $value";
1106 * @param array $tables Array of tables; see IDatabase::select $table
1107 * @param array $fields Array of fields; see IDatabase::select $vars
1108 * @param array $conds Array of conditions; see IDatabase::select $conds
1109 * @param array $query_options Array of query options; see IDatabase::select $options
1110 * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
1111 * @param FormOptions $opts
1112 * @return bool|ResultWrapper Result or false
1114 protected function doMainQuery( $tables, $fields, $conds,
1115 $query_options, $join_conds, FormOptions
$opts
1117 $tables[] = 'recentchanges';
1118 $fields = array_merge( RecentChange
::selectFields(), $fields );
1120 ChangeTags
::modifyDisplayQuery(
1129 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
1135 $dbr = $this->getDB();
1137 return $dbr->select(
1147 protected function runMainQueryHook( &$tables, &$fields, &$conds,
1148 &$query_options, &$join_conds, $opts
1151 'ChangesListSpecialPageQuery',
1152 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
1157 * Return a IDatabase object for reading
1161 protected function getDB() {
1162 return wfGetDB( DB_REPLICA
);
1166 * Send output to the OutputPage object, only called if not used feeds
1168 * @param ResultWrapper $rows Database rows
1169 * @param FormOptions $opts
1171 public function webOutput( $rows, $opts ) {
1172 if ( !$this->including() ) {
1173 $this->outputFeedLinks();
1174 $this->doHeader( $opts, $rows->numRows() );
1177 $this->outputChangesList( $rows, $opts );
1181 * Output feed links.
1183 public function outputFeedLinks() {
1184 // nothing by default
1188 * Build and output the actual changes list.
1190 * @param ResultWrapper $rows Database rows
1191 * @param FormOptions $opts
1193 abstract public function outputChangesList( $rows, $opts );
1196 * Set the text to be displayed above the changes
1198 * @param FormOptions $opts
1199 * @param int $numRows Number of rows in the result to show after this header
1201 public function doHeader( $opts, $numRows ) {
1202 $this->setTopText( $opts );
1204 // @todo Lots of stuff should be done here.
1206 $this->setBottomText( $opts );
1210 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
1211 * or similar methods to print the text.
1213 * @param FormOptions $opts
1215 public function setTopText( FormOptions
$opts ) {
1216 // nothing by default
1220 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
1221 * or similar methods to print the text.
1223 * @param FormOptions $opts
1225 public function setBottomText( FormOptions
$opts ) {
1226 // nothing by default
1230 * Get options to be displayed in a form
1231 * @todo This should handle options returned by getDefaultOptions().
1232 * @todo Not called by anything in this class (but is in subclasses), should be
1233 * called by something… doHeader() maybe?
1235 * @param FormOptions $opts
1238 public function getExtraOptions( $opts ) {
1243 * Return the legend displayed within the fieldset
1247 public function makeLegend() {
1248 $context = $this->getContext();
1249 $user = $context->getUser();
1250 # The legend showing what the letters and stuff mean
1251 $legend = Html
::openElement( 'dl' ) . "\n";
1252 # Iterates through them and gets the messages for both letter and tooltip
1253 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
1254 if ( !( $user->useRCPatrol() ||
$user->useNPPatrol() ) ) {
1255 unset( $legendItems['unpatrolled'] );
1257 foreach ( $legendItems as $key => $item ) { # generate items of the legend
1258 $label = isset( $item['legend'] ) ?
$item['legend'] : $item['title'];
1259 $letter = $item['letter'];
1260 $cssClass = isset( $item['class'] ) ?
$item['class'] : $key;
1262 $legend .= Html
::element( 'dt',
1263 [ 'class' => $cssClass ], $context->msg( $letter )->text()
1265 Html
::rawElement( 'dd',
1266 [ 'class' => Sanitizer
::escapeClass( 'mw-changeslist-legend-' . $key ) ],
1267 $context->msg( $label )->parse()
1271 $legend .= Html
::rawElement( 'dt',
1272 [ 'class' => 'mw-plusminus-pos' ],
1273 $context->msg( 'recentchanges-legend-plusminus' )->parse()
1275 $legend .= Html
::element(
1277 [ 'class' => 'mw-changeslist-legend-plusminus' ],
1278 $context->msg( 'recentchanges-label-plusminus' )->text()
1280 $legend .= Html
::closeElement( 'dl' ) . "\n";
1283 $legendHeading = $this->getUser()->getOption(
1286 $context->msg( 'rcfilters-legend-heading' )->parse() :
1287 $context->msg( 'recentchanges-legend-heading' )->parse();
1289 '<div class="mw-changeslist-legend">' .
1291 '<div class="mw-collapsible-content">' . $legend . '</div>' .
1298 * Add page-specific modules.
1300 protected function addModules() {
1301 $out = $this->getOutput();
1302 // Styles and behavior for the legend box (see makeLegend())
1303 $out->addModuleStyles( [
1304 'mediawiki.special.changeslist.legend',
1305 'mediawiki.special.changeslist',
1307 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
1310 protected function getGroupName() {
1315 * Filter on users' experience levels; this will not be called if nothing is
1318 * @param string $specialPageClassName Class name of current special page
1319 * @param IContextSource $context Context, for e.g. user
1320 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
1321 * @param array &$tables Array of tables; see IDatabase::select $table
1322 * @param array &$fields Array of fields; see IDatabase::select $vars
1323 * @param array &$conds Array of conditions; see IDatabase::select $conds
1324 * @param array &$query_options Array of query options; see IDatabase::select $options
1325 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1326 * @param array $selectedExpLevels The allowed active values, sorted
1327 * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
1330 public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
1331 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0
1333 global $wgLearnerEdits,
1334 $wgExperiencedUserEdits,
1335 $wgLearnerMemberSince,
1336 $wgExperiencedUserMemberSince;
1340 // If all levels are selected, don't filter
1341 if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
1345 // both 'registered' and 'unregistered', experience levels, if any, are included in 'registered'
1347 in_array( 'registered', $selectedExpLevels ) &&
1348 in_array( 'unregistered', $selectedExpLevels )
1353 // 'registered' but not 'unregistered', experience levels, if any, are included in 'registered'
1355 in_array( 'registered', $selectedExpLevels ) &&
1356 !in_array( 'unregistered', $selectedExpLevels )
1358 $conds[] = 'rc_user != 0';
1362 if ( $selectedExpLevels === [ 'unregistered' ] ) {
1363 $conds[] = 'rc_user = 0';
1368 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
1373 $secondsPerDay = 86400;
1374 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
1375 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
1377 $aboveNewcomer = $dbr->makeList(
1379 'user_editcount >= ' . intval( $wgLearnerEdits ),
1380 'user_registration <= ' . $dbr->addQuotes( $dbr->timestamp( $learnerCutoff ) ),
1385 $aboveLearner = $dbr->makeList(
1387 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
1388 'user_registration <= ' .
1389 $dbr->addQuotes( $dbr->timestamp( $experiencedUserCutoff ) ),
1396 if ( in_array( 'unregistered', $selectedExpLevels ) ) {
1397 $selectedExpLevels = array_diff( $selectedExpLevels, [ 'unregistered' ] );
1398 $conditions[] = 'rc_user = 0';
1401 if ( $selectedExpLevels === [ 'newcomer' ] ) {
1402 $conditions[] = "NOT ( $aboveNewcomer )";
1403 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
1404 $conditions[] = $dbr->makeList(
1405 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
1408 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
1409 $conditions[] = $aboveLearner;
1410 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
1411 $conditions[] = "NOT ( $aboveLearner )";
1412 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
1413 $conditions[] = $dbr->makeList(
1414 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
1417 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
1418 $conditions[] = $aboveNewcomer;
1419 } elseif ( $selectedExpLevels === [ 'experienced', 'learner', 'newcomer' ] ) {
1420 $conditions[] = 'rc_user != 0';
1423 if ( count( $conditions ) > 1 ) {
1424 $conds[] = $dbr->makeList( $conditions, IDatabase
::LIST_OR
);
1425 } elseif ( count( $conditions ) === 1 ) {
1426 $conds[] = reset( $conditions );