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