3f6d86516fe0f20cf8f16b6ed9732c3574e02164
[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 'priority' => -8,
342 'filters' => [
343 [
344 'name' => 'hidepageedits',
345 'label' => 'rcfilters-filter-pageedits-label',
346 'description' => 'rcfilters-filter-pageedits-description',
347 'default' => false,
348 'priority' => -2,
349 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
350 &$query_options, &$join_conds ) {
351
352 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT );
353 },
354 'cssClassSuffix' => 'src-mw-edit',
355 'isRowApplicableCallable' => function ( $ctx, $rc ) {
356 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_EDIT;
357 },
358 ],
359 [
360 'name' => 'hidenewpages',
361 'label' => 'rcfilters-filter-newpages-label',
362 'description' => 'rcfilters-filter-newpages-description',
363 'default' => false,
364 'priority' => -3,
365 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
366 &$query_options, &$join_conds ) {
367
368 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW );
369 },
370 'cssClassSuffix' => 'src-mw-new',
371 'isRowApplicableCallable' => function ( $ctx, $rc ) {
372 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_NEW;
373 },
374 ],
375
376 // hidecategorization
377
378 [
379 'name' => 'hidelog',
380 'label' => 'rcfilters-filter-logactions-label',
381 'description' => 'rcfilters-filter-logactions-description',
382 'default' => false,
383 'priority' => -5,
384 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
385 &$query_options, &$join_conds ) {
386
387 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG );
388 },
389 'cssClassSuffix' => 'src-mw-log',
390 'isRowApplicableCallable' => function ( $ctx, $rc ) {
391 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_LOG;
392 }
393 ],
394 ],
395 ],
396
397 ];
398
399 $this->reviewStatusFilterGroupDefinition = [
400 [
401 'name' => 'reviewStatus',
402 'title' => 'rcfilters-filtergroup-reviewstatus',
403 'class' => ChangesListBooleanFilterGroup::class,
404 'priority' => -5,
405 'filters' => [
406 [
407 'name' => 'hidepatrolled',
408 'label' => 'rcfilters-filter-patrolled-label',
409 'description' => 'rcfilters-filter-patrolled-description',
410 // rcshowhidepatr-show, rcshowhidepatr-hide
411 // wlshowhidepatr
412 'showHideSuffix' => 'showhidepatr',
413 'default' => false,
414 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
415 &$query_options, &$join_conds ) {
416
417 $conds[] = 'rc_patrolled = 0';
418 },
419 'cssClassSuffix' => 'patrolled',
420 'isRowApplicableCallable' => function ( $ctx, $rc ) {
421 return $rc->getAttribute( 'rc_patrolled' );
422 },
423 ],
424 [
425 'name' => 'hideunpatrolled',
426 'label' => 'rcfilters-filter-unpatrolled-label',
427 'description' => 'rcfilters-filter-unpatrolled-description',
428 'default' => false,
429 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
430 &$query_options, &$join_conds ) {
431
432 $conds[] = 'rc_patrolled = 1';
433 },
434 'cssClassSuffix' => 'unpatrolled',
435 'isRowApplicableCallable' => function ( $ctx, $rc ) {
436 return !$rc->getAttribute( 'rc_patrolled' );
437 },
438 ],
439 ],
440 ]
441 ];
442
443 $this->hideCategorizationFilterDefinition = [
444 'name' => 'hidecategorization',
445 'label' => 'rcfilters-filter-categorization-label',
446 'description' => 'rcfilters-filter-categorization-description',
447 // rcshowhidecategorization-show, rcshowhidecategorization-hide.
448 // wlshowhidecategorization
449 'showHideSuffix' => 'showhidecategorization',
450 'default' => false,
451 'priority' => -4,
452 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
453 &$query_options, &$join_conds ) {
454
455 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE );
456 },
457 'cssClassSuffix' => 'src-mw-categorize',
458 'isRowApplicableCallable' => function ( $ctx, $rc ) {
459 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_CATEGORIZE;
460 },
461 ];
462 }
463
464 /**
465 * Check if filters are in conflict and guaranteed to return no results.
466 *
467 * @return bool
468 */
469 protected function areFiltersInConflict() {
470 $opts = $this->getOptions();
471 /** @var ChangesListFilterGroup $group */
472 foreach ( $this->getFilterGroups() as $group ) {
473
474 if ( $group->getConflictingGroups() ) {
475 wfLogWarning(
476 $group->getName() .
477 " specifies conflicts with other groups but these are not supported yet."
478 );
479 }
480
481 /** @var ChangesListFilter $conflictingFilter */
482 foreach ( $group->getConflictingFilters() as $conflictingFilter ) {
483 if ( $conflictingFilter->activelyInConflictWithGroup( $group, $opts ) ) {
484 return true;
485 }
486 }
487
488 /** @var ChangesListFilter $filter */
489 foreach ( $group->getFilters() as $filter ) {
490
491 /** @var ChangesListFilter $conflictingFilter */
492 foreach ( $filter->getConflictingFilters() as $conflictingFilter ) {
493 if (
494 $conflictingFilter->activelyInConflictWithFilter( $filter, $opts ) &&
495 $filter->activelyInConflictWithFilter( $conflictingFilter, $opts )
496 ) {
497 return true;
498 }
499 }
500
501 }
502
503 }
504
505 return false;
506 }
507
508 /**
509 * Main execution point
510 *
511 * @param string $subpage
512 */
513 public function execute( $subpage ) {
514 $this->rcSubpage = $subpage;
515
516 $this->setHeaders();
517 $this->outputHeader();
518 $this->addModules();
519
520 $rows = $this->getRows();
521 $opts = $this->getOptions();
522 if ( $rows === false ) {
523 if ( !$this->including() ) {
524 $this->doHeader( $opts, 0 );
525 $this->outputNoResults();
526 $this->getOutput()->setStatusCode( 404 );
527 }
528
529 return;
530 }
531
532 $batch = new LinkBatch;
533 foreach ( $rows as $row ) {
534 $batch->add( NS_USER, $row->rc_user_text );
535 $batch->add( NS_USER_TALK, $row->rc_user_text );
536 $batch->add( $row->rc_namespace, $row->rc_title );
537 if ( $row->rc_source === RecentChange::SRC_LOG ) {
538 $formatter = LogFormatter::newFromRow( $row );
539 foreach ( $formatter->getPreloadTitles() as $title ) {
540 $batch->addObj( $title );
541 }
542 }
543 }
544 $batch->execute();
545 $this->webOutput( $rows, $opts );
546
547 $rows->free();
548
549 if ( $this->getConfig()->get( 'EnableWANCacheReaper' ) ) {
550 // Clean up any bad page entries for titles showing up in RC
551 DeferredUpdates::addUpdate( new WANCacheReapUpdate(
552 $this->getDB(),
553 LoggerFactory::getInstance( 'objectcache' )
554 ) );
555 }
556 }
557
558 /**
559 * Add the "no results" message to the output
560 */
561 protected function outputNoResults() {
562 $this->getOutput()->addHTML(
563 '<div class="mw-changeslist-empty">' .
564 $this->msg( 'recentchanges-noresult' )->parse() .
565 '</div>'
566 );
567 }
568
569 /**
570 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
571 *
572 * @return bool|ResultWrapper Result or false
573 */
574 public function getRows() {
575 $opts = $this->getOptions();
576
577 $tables = [];
578 $fields = [];
579 $conds = [];
580 $query_options = [];
581 $join_conds = [];
582 $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
583
584 return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
585 }
586
587 /**
588 * Get the current FormOptions for this request
589 *
590 * @return FormOptions
591 */
592 public function getOptions() {
593 if ( $this->rcOptions === null ) {
594 $this->rcOptions = $this->setup( $this->rcSubpage );
595 }
596
597 return $this->rcOptions;
598 }
599
600 /**
601 * Register all filters and their groups (including those from hooks), plus handle
602 * conflicts and defaults.
603 *
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
607 * setDefault).
608 */
609 protected function registerFilters() {
610 $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions );
611
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 );
617 }
618
619 $changeTypeGroup = $this->getFilterGroup( 'changeType' );
620
621 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
622 $transformedHideCategorizationDef = $this->transformFilterDefinition(
623 $this->hideCategorizationFilterDefinition
624 );
625
626 $transformedHideCategorizationDef['group'] = $changeTypeGroup;
627
628 $hideCategorization = new ChangesListBooleanFilter(
629 $transformedHideCategorizationDef
630 );
631 }
632
633 Hooks::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
634
635 $unstructuredGroupDefinition =
636 $this->getFilterGroupDefinitionFromLegacyCustomFilters(
637 $this->getCustomFilters()
638 );
639 $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
640
641 $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
642
643 $registration = $this->getFilterGroup( 'registration' );
644 $anons = $registration->getFilter( 'hideanons' );
645
646 // This means there is a conflict between any item in user experience level
647 // being checked and only anons being *shown* (hideliu=1&hideanons=0 in the
648 // URL, or equivalent).
649 $userExperienceLevel->conflictsWith(
650 $anons,
651 'rcfilters-filtergroup-user-experience-level-conflicts-unregistered-global',
652 'rcfilters-filtergroup-user-experience-level-conflicts-unregistered',
653 'rcfilters-filter-unregistered-conflicts-user-experience-level'
654 );
655
656 $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
657 $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
658 $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
659
660 $significanceTypeGroup = $this->getFilterGroup( 'significance' );
661 $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
662
663 // categoryFilter is conditional; see registerFilters
664 if ( $categoryFilter !== null ) {
665 $hideMinorFilter->conflictsWith(
666 $categoryFilter,
667 'rcfilters-hideminor-conflicts-typeofchange-global',
668 'rcfilters-hideminor-conflicts-typeofchange',
669 'rcfilters-typeofchange-conflicts-hideminor'
670 );
671 }
672 $hideMinorFilter->conflictsWith(
673 $logactionsFilter,
674 'rcfilters-hideminor-conflicts-typeofchange-global',
675 'rcfilters-hideminor-conflicts-typeofchange',
676 'rcfilters-typeofchange-conflicts-hideminor'
677 );
678 $hideMinorFilter->conflictsWith(
679 $pagecreationFilter,
680 'rcfilters-hideminor-conflicts-typeofchange-global',
681 'rcfilters-hideminor-conflicts-typeofchange',
682 'rcfilters-typeofchange-conflicts-hideminor'
683 );
684 }
685
686 /**
687 * Transforms filter definition to prepare it for constructor.
688 *
689 * See overrides of this method as well.
690 *
691 * @param array $filterDefinition Original filter definition
692 *
693 * @return array Transformed definition
694 */
695 protected function transformFilterDefinition( array $filterDefinition ) {
696 return $filterDefinition;
697 }
698
699 /**
700 * Register filters from a definition object
701 *
702 * Array specifying groups and their filters; see Filter and
703 * ChangesListFilterGroup constructors.
704 *
705 * There is light processing to simplify core maintenance.
706 */
707 protected function registerFiltersFromDefinitions( array $definition ) {
708 $autoFillPriority = -1;
709 foreach ( $definition as $groupDefinition ) {
710 if ( !isset( $groupDefinition['priority'] ) ) {
711 $groupDefinition['priority'] = $autoFillPriority;
712 } else {
713 // If it's explicitly specified, start over the auto-fill
714 $autoFillPriority = $groupDefinition['priority'];
715 }
716
717 $autoFillPriority--;
718
719 $className = $groupDefinition['class'];
720 unset( $groupDefinition['class'] );
721
722 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
723 $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
724 }
725
726 $this->registerFilterGroup( new $className( $groupDefinition ) );
727 }
728 }
729
730 /**
731 * Get filter group definition from legacy custom filters
732 *
733 * @param array Custom filters from legacy hooks
734 * @return array Group definition
735 */
736 protected function getFilterGroupDefinitionFromLegacyCustomFilters( $customFilters ) {
737 // Special internal unstructured group
738 $unstructuredGroupDefinition = [
739 'name' => 'unstructured',
740 'class' => ChangesListBooleanFilterGroup::class,
741 'priority' => -1, // Won't display in structured
742 'filters' => [],
743 ];
744
745 foreach ( $customFilters as $name => $params ) {
746 $unstructuredGroupDefinition['filters'][] = [
747 'name' => $name,
748 'showHide' => $params['msg'],
749 'default' => $params['default'],
750 ];
751 }
752
753 return $unstructuredGroupDefinition;
754 }
755
756 /**
757 * Register all the filters, including legacy hook-driven ones.
758 * Then create a FormOptions object with options as specified by the user
759 *
760 * @param array $parameters
761 *
762 * @return FormOptions
763 */
764 public function setup( $parameters ) {
765 $this->registerFilters();
766
767 $opts = $this->getDefaultOptions();
768
769 $opts = $this->fetchOptionsFromRequest( $opts );
770
771 // Give precedence to subpage syntax
772 if ( $parameters !== null ) {
773 $this->parseParameters( $parameters, $opts );
774 }
775
776 $this->validateOptions( $opts );
777
778 return $opts;
779 }
780
781 /**
782 * Get a FormOptions object containing the default options. By default, returns
783 * some basic options. The filters listed explicitly here are overriden in this
784 * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
785 * and more) are structured. Structured filters are overriden in registerFilters.
786 * not here.
787 *
788 * @return FormOptions
789 */
790 public function getDefaultOptions() {
791 $config = $this->getConfig();
792 $opts = new FormOptions();
793 $structuredUI = $this->getUser()->getOption( 'rcenhancedfilters' );
794
795 // Add all filters
796 foreach ( $this->filterGroups as $filterGroup ) {
797 // URL parameters can be per-group, like 'userExpLevel',
798 // or per-filter, like 'hideminor'.
799 if ( $filterGroup->isPerGroupRequestParameter() ) {
800 $opts->add( $filterGroup->getName(), $filterGroup->getDefault() );
801 } else {
802 foreach ( $filterGroup->getFilters() as $filter ) {
803 $opts->add( $filter->getName(), $filter->getDefault( $structuredUI ) );
804 }
805 }
806 }
807
808 $opts->add( 'namespace', '', FormOptions::STRING );
809 $opts->add( 'invert', false );
810 $opts->add( 'associated', false );
811
812 return $opts;
813 }
814
815 /**
816 * Register a structured changes list filter group
817 *
818 * @param ChangesListFilterGroup $group
819 */
820 public function registerFilterGroup( ChangesListFilterGroup $group ) {
821 $groupName = $group->getName();
822
823 $this->filterGroups[$groupName] = $group;
824 }
825
826 /**
827 * Gets the currently registered filters groups
828 *
829 * @return array Associative array of ChangesListFilterGroup objects, with group name as key
830 */
831 protected function getFilterGroups() {
832 return $this->filterGroups;
833 }
834
835 /**
836 * Gets a specified ChangesListFilterGroup by name
837 *
838 * @param string $groupName Name of group
839 *
840 * @return ChangesListFilterGroup|null Group, or null if not registered
841 */
842 public function getFilterGroup( $groupName ) {
843 return isset( $this->filterGroups[$groupName] ) ?
844 $this->filterGroups[$groupName] :
845 null;
846 }
847
848 // Currently, this intentionally only includes filters that display
849 // in the structured UI. This can be changed easily, though, if we want
850 // to include data on filters that use the unstructured UI. messageKeys is a
851 // special top-level value, with the value being an array of the message keys to
852 // send to the client.
853 /**
854 * Gets structured filter information needed by JS
855 *
856 * @return array Associative array
857 * * array $return['groups'] Group data
858 * * array $return['messageKeys'] Array of message keys
859 */
860 public function getStructuredFilterJsData() {
861 $output = [
862 'groups' => [],
863 'messageKeys' => [],
864 ];
865
866 $context = $this->getContext();
867
868 usort( $this->filterGroups, function ( $a, $b ) {
869 return $b->getPriority() - $a->getPriority();
870 } );
871
872 foreach ( $this->filterGroups as $groupName => $group ) {
873 $groupOutput = $group->getJsData( $this );
874 if ( $groupOutput !== null ) {
875 $output['messageKeys'] = array_merge(
876 $output['messageKeys'],
877 $groupOutput['messageKeys']
878 );
879
880 unset( $groupOutput['messageKeys'] );
881 $output['groups'][] = $groupOutput;
882 }
883 }
884
885 return $output;
886 }
887
888 /**
889 * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
890 * hook.
891 *
892 * @return array Map of filter URL param names to properties (msg/default)
893 */
894 protected function getCustomFilters() {
895 if ( $this->customFilters === null ) {
896 $this->customFilters = [];
897 Hooks::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters ], '1.29' );
898 }
899
900 return $this->customFilters;
901 }
902
903 /**
904 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
905 *
906 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
907 *
908 * @param FormOptions $opts
909 * @return FormOptions
910 */
911 protected function fetchOptionsFromRequest( $opts ) {
912 $opts->fetchValuesFromRequest( $this->getRequest() );
913
914 return $opts;
915 }
916
917 /**
918 * Process $par and put options found in $opts. Used when including the page.
919 *
920 * @param string $par
921 * @param FormOptions $opts
922 */
923 public function parseParameters( $par, FormOptions $opts ) {
924 $stringParameterNameSet = [];
925 $hideParameterNameSet = [];
926
927 // URL parameters can be per-group, like 'userExpLevel',
928 // or per-filter, like 'hideminor'.
929
930 foreach ( $this->filterGroups as $filterGroup ) {
931 if ( $filterGroup->isPerGroupRequestParameter() ) {
932 $stringParameterNameSet[$filterGroup->getName()] = true;
933 } elseif ( $filterGroup->getType() === ChangesListBooleanFilterGroup::TYPE ) {
934 foreach ( $filterGroup->getFilters() as $filter ) {
935 $hideParameterNameSet[$filter->getName()] = true;
936 }
937 }
938 }
939
940 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
941 foreach ( $bits as $bit ) {
942 $m = [];
943 if ( isset( $hideParameterNameSet[$bit] ) ) {
944 // hidefoo => hidefoo=true
945 $opts[$bit] = true;
946 } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
947 // foo => hidefoo=false
948 $opts["hide$bit"] = false;
949 } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
950 if ( isset( $stringParameterNameSet[$m[1]] ) ) {
951 $opts[$m[1]] = $m[2];
952 }
953 }
954 }
955 }
956
957 /**
958 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
959 *
960 * @param FormOptions $opts
961 */
962 public function validateOptions( FormOptions $opts ) {
963 // nothing by default
964 }
965
966 /**
967 * Sets appropriate tables, fields, conditions, etc. depending on which filters
968 * the user requested.
969 *
970 * @param array &$tables Array of tables; see IDatabase::select $table
971 * @param array &$fields Array of fields; see IDatabase::select $vars
972 * @param array &$conds Array of conditions; see IDatabase::select $conds
973 * @param array &$query_options Array of query options; see IDatabase::select $options
974 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
975 * @param FormOptions $opts
976 */
977 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
978 &$join_conds, FormOptions $opts ) {
979
980 $dbr = $this->getDB();
981 $user = $this->getUser();
982
983 $context = $this->getContext();
984 foreach ( $this->filterGroups as $filterGroup ) {
985 // URL parameters can be per-group, like 'userExpLevel',
986 // or per-filter, like 'hideminor'.
987 if ( $filterGroup->isPerGroupRequestParameter() ) {
988 $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
989 $query_options, $join_conds, $opts[$filterGroup->getName()] );
990 } else {
991 foreach ( $filterGroup->getFilters() as $filter ) {
992 if ( $opts[$filter->getName()] ) {
993 $filter->modifyQuery( $dbr, $this, $tables, $fields, $conds,
994 $query_options, $join_conds );
995 }
996 }
997 }
998 }
999
1000 // Namespace filtering
1001 if ( $opts[ 'namespace' ] !== '' ) {
1002 $namespaces = explode( ',', $opts[ 'namespace' ] );
1003
1004 if ( $opts[ 'associated' ] ) {
1005 $associatedNamespaces = array_map(
1006 function ( $ns ) {
1007 return MWNamespace::getAssociated( $ns );
1008 },
1009 $namespaces
1010 );
1011 $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
1012 }
1013
1014 if ( count( $namespaces ) === 1 ) {
1015 $operator = $opts[ 'invert' ] ? '!=' : '=';
1016 $value = $dbr->addQuotes( reset( $namespaces ) );
1017 } else {
1018 $operator = $opts[ 'invert' ] ? 'NOT IN' : 'IN';
1019 sort( $namespaces );
1020 $value = '(' . $dbr->makeList( $namespaces ) . ')';
1021 }
1022 $conds[] = "rc_namespace $operator $value";
1023 }
1024 }
1025
1026 /**
1027 * Process the query
1028 *
1029 * @param array $tables Array of tables; see IDatabase::select $table
1030 * @param array $fields Array of fields; see IDatabase::select $vars
1031 * @param array $conds Array of conditions; see IDatabase::select $conds
1032 * @param array $query_options Array of query options; see IDatabase::select $options
1033 * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
1034 * @param FormOptions $opts
1035 * @return bool|ResultWrapper Result or false
1036 */
1037 protected function doMainQuery( $tables, $fields, $conds,
1038 $query_options, $join_conds, FormOptions $opts ) {
1039
1040 $tables[] = 'recentchanges';
1041 $fields = array_merge( RecentChange::selectFields(), $fields );
1042
1043 ChangeTags::modifyDisplayQuery(
1044 $tables,
1045 $fields,
1046 $conds,
1047 $join_conds,
1048 $query_options,
1049 ''
1050 );
1051
1052 // It makes no sense to hide both anons and logged-in users. When this occurs, try a guess on
1053 // what the user meant and either show only bots or force anons to be shown.
1054
1055 // -------
1056
1057 // XXX: We're no longer doing this handling. To preserve back-compat, we need to complete
1058 // T151873 (particularly the hideanons/hideliu/hidebots/hidehumans part) in conjunction
1059 // with merging this.
1060
1061 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
1062 $opts )
1063 ) {
1064 return false;
1065 }
1066
1067 $dbr = $this->getDB();
1068
1069 return $dbr->select(
1070 $tables,
1071 $fields,
1072 $conds,
1073 __METHOD__,
1074 $query_options,
1075 $join_conds
1076 );
1077 }
1078
1079 protected function runMainQueryHook( &$tables, &$fields, &$conds,
1080 &$query_options, &$join_conds, $opts
1081 ) {
1082 return Hooks::run(
1083 'ChangesListSpecialPageQuery',
1084 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
1085 );
1086 }
1087
1088 /**
1089 * Return a IDatabase object for reading
1090 *
1091 * @return IDatabase
1092 */
1093 protected function getDB() {
1094 return wfGetDB( DB_REPLICA );
1095 }
1096
1097 /**
1098 * Send output to the OutputPage object, only called if not used feeds
1099 *
1100 * @param ResultWrapper $rows Database rows
1101 * @param FormOptions $opts
1102 */
1103 public function webOutput( $rows, $opts ) {
1104 if ( !$this->including() ) {
1105 $this->outputFeedLinks();
1106 $this->doHeader( $opts, $rows->numRows() );
1107 }
1108
1109 $this->outputChangesList( $rows, $opts );
1110 }
1111
1112 /**
1113 * Output feed links.
1114 */
1115 public function outputFeedLinks() {
1116 // nothing by default
1117 }
1118
1119 /**
1120 * Build and output the actual changes list.
1121 *
1122 * @param ResultWrapper $rows Database rows
1123 * @param FormOptions $opts
1124 */
1125 abstract public function outputChangesList( $rows, $opts );
1126
1127 /**
1128 * Set the text to be displayed above the changes
1129 *
1130 * @param FormOptions $opts
1131 * @param int $numRows Number of rows in the result to show after this header
1132 */
1133 public function doHeader( $opts, $numRows ) {
1134 $this->setTopText( $opts );
1135
1136 // @todo Lots of stuff should be done here.
1137
1138 $this->setBottomText( $opts );
1139 }
1140
1141 /**
1142 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
1143 * or similar methods to print the text.
1144 *
1145 * @param FormOptions $opts
1146 */
1147 public function setTopText( FormOptions $opts ) {
1148 // nothing by default
1149 }
1150
1151 /**
1152 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
1153 * or similar methods to print the text.
1154 *
1155 * @param FormOptions $opts
1156 */
1157 public function setBottomText( FormOptions $opts ) {
1158 // nothing by default
1159 }
1160
1161 /**
1162 * Get options to be displayed in a form
1163 * @todo This should handle options returned by getDefaultOptions().
1164 * @todo Not called by anything in this class (but is in subclasses), should be
1165 * called by something… doHeader() maybe?
1166 *
1167 * @param FormOptions $opts
1168 * @return array
1169 */
1170 public function getExtraOptions( $opts ) {
1171 return [];
1172 }
1173
1174 /**
1175 * Return the legend displayed within the fieldset
1176 *
1177 * @return string
1178 */
1179 public function makeLegend() {
1180 $context = $this->getContext();
1181 $user = $context->getUser();
1182 # The legend showing what the letters and stuff mean
1183 $legend = Html::openElement( 'dl' ) . "\n";
1184 # Iterates through them and gets the messages for both letter and tooltip
1185 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
1186 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
1187 unset( $legendItems['unpatrolled'] );
1188 }
1189 foreach ( $legendItems as $key => $item ) { # generate items of the legend
1190 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
1191 $letter = $item['letter'];
1192 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
1193
1194 $legend .= Html::element( 'dt',
1195 [ 'class' => $cssClass ], $context->msg( $letter )->text()
1196 ) . "\n" .
1197 Html::rawElement( 'dd',
1198 [ 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ],
1199 $context->msg( $label )->parse()
1200 ) . "\n";
1201 }
1202 # (+-123)
1203 $legend .= Html::rawElement( 'dt',
1204 [ 'class' => 'mw-plusminus-pos' ],
1205 $context->msg( 'recentchanges-legend-plusminus' )->parse()
1206 ) . "\n";
1207 $legend .= Html::element(
1208 'dd',
1209 [ 'class' => 'mw-changeslist-legend-plusminus' ],
1210 $context->msg( 'recentchanges-label-plusminus' )->text()
1211 ) . "\n";
1212 $legend .= Html::closeElement( 'dl' ) . "\n";
1213
1214 # Collapsibility
1215 $legend =
1216 '<div class="mw-changeslist-legend">' .
1217 $context->msg( 'recentchanges-legend-heading' )->parse() .
1218 '<div class="mw-collapsible-content">' . $legend . '</div>' .
1219 '</div>';
1220
1221 return $legend;
1222 }
1223
1224 /**
1225 * Add page-specific modules.
1226 */
1227 protected function addModules() {
1228 $out = $this->getOutput();
1229 // Styles and behavior for the legend box (see makeLegend())
1230 $out->addModuleStyles( [
1231 'mediawiki.special.changeslist.legend',
1232 'mediawiki.special.changeslist',
1233 ] );
1234 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
1235 }
1236
1237 protected function getGroupName() {
1238 return 'changes';
1239 }
1240
1241 /**
1242 * Filter on users' experience levels; this will not be called if nothing is
1243 * selected.
1244 *
1245 * @param string $specialPageClassName Class name of current special page
1246 * @param IContextSource $context Context, for e.g. user
1247 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
1248 * @param array &$tables Array of tables; see IDatabase::select $table
1249 * @param array &$fields Array of fields; see IDatabase::select $vars
1250 * @param array &$conds Array of conditions; see IDatabase::select $conds
1251 * @param array &$query_options Array of query options; see IDatabase::select $options
1252 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1253 * @param array $selectedExpLevels The allowed active values, sorted
1254 */
1255 public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
1256 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0 ) {
1257
1258 global $wgLearnerEdits,
1259 $wgExperiencedUserEdits,
1260 $wgLearnerMemberSince,
1261 $wgExperiencedUserMemberSince;
1262
1263 $LEVEL_COUNT = 3;
1264
1265 // If all levels are selected, all logged-in users are included (but no
1266 // anons), so we can short-circuit.
1267 if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
1268 $conds[] = 'rc_user != 0';
1269 return;
1270 }
1271
1272 $tables[] = 'user';
1273 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
1274
1275 if ( $now === 0 ) {
1276 $now = time();
1277 }
1278 $secondsPerDay = 86400;
1279 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
1280 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
1281
1282 $aboveNewcomer = $dbr->makeList(
1283 [
1284 'user_editcount >= ' . intval( $wgLearnerEdits ),
1285 'user_registration <= ' . $dbr->timestamp( $learnerCutoff ),
1286 ],
1287 IDatabase::LIST_AND
1288 );
1289
1290 $aboveLearner = $dbr->makeList(
1291 [
1292 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
1293 'user_registration <= ' . $dbr->timestamp( $experiencedUserCutoff ),
1294 ],
1295 IDatabase::LIST_AND
1296 );
1297
1298 if ( $selectedExpLevels === [ 'newcomer' ] ) {
1299 $conds[] = "NOT ( $aboveNewcomer )";
1300 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
1301 $conds[] = $dbr->makeList(
1302 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
1303 IDatabase::LIST_AND
1304 );
1305 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
1306 $conds[] = $aboveLearner;
1307 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
1308 $conds[] = "NOT ( $aboveLearner )";
1309 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
1310 $conds[] = $dbr->makeList(
1311 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
1312 IDatabase::LIST_OR
1313 );
1314 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
1315 $conds[] = $aboveNewcomer;
1316 }
1317 }
1318 }