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