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