RCFilters: remove wgStructuredChangeFiltersEnableExperimentalViews
[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\FakeResultWrapper;
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29 * Special page which uses a ChangesList to show query results.
30 * @todo Way too many public functions, most of them should be protected
31 *
32 * @ingroup SpecialPage
33 */
34 abstract class ChangesListSpecialPage extends SpecialPage {
35 /**
36 * Preference name for saved queries. Subclasses that use saved queries should override this.
37 * @var string
38 */
39 protected static $savedQueriesPreferenceName;
40
41 /** @var string */
42 protected $rcSubpage;
43
44 /** @var FormOptions */
45 protected $rcOptions;
46
47 /** @var array */
48 protected $customFilters;
49
50 // Order of both groups and filters is significant; first is top-most priority,
51 // descending from there.
52 // 'showHideSuffix' is a shortcut to and avoid spelling out
53 // details specific to subclasses here.
54 /**
55 * Definition information for the filters and their groups
56 *
57 * The value is $groupDefinition, a parameter to the ChangesListFilterGroup constructor.
58 * However, priority is dynamically added for the core groups, to ease maintenance.
59 *
60 * Groups are displayed to the user in the structured UI. However, if necessary,
61 * all of the filters in a group can be configured to only display on the
62 * unstuctured UI, in which case you don't need a group title. This is done in
63 * getFilterGroupDefinitionFromLegacyCustomFilters, for example.
64 *
65 * @var array $filterGroupDefinitions
66 */
67 private $filterGroupDefinitions;
68
69 // Same format as filterGroupDefinitions, but for a single group (reviewStatus)
70 // that is registered conditionally.
71 private $reviewStatusFilterGroupDefinition;
72
73 // Single filter registered conditionally
74 private $hideCategorizationFilterDefinition;
75
76 /**
77 * Filter groups, and their contained filters
78 * This is an associative array (with group name as key) of ChangesListFilterGroup objects.
79 *
80 * @var array $filterGroups
81 */
82 protected $filterGroups = [];
83
84 public function __construct( $name, $restriction ) {
85 parent::__construct( $name, $restriction );
86
87 $this->filterGroupDefinitions = [
88 [
89 'name' => 'registration',
90 'title' => 'rcfilters-filtergroup-registration',
91 'class' => ChangesListBooleanFilterGroup::class,
92 'filters' => [
93 [
94 'name' => 'hideliu',
95 // rcshowhideliu-show, rcshowhideliu-hide,
96 // wlshowhideliu
97 'showHideSuffix' => 'showhideliu',
98 'default' => false,
99 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
100 &$query_options, &$join_conds
101 ) {
102 $conds[] = 'rc_user = 0';
103 },
104 'isReplacedInStructuredUi' => true,
105
106 ],
107 [
108 'name' => 'hideanons',
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 'isReplacedInStructuredUi' => true,
119 ]
120 ],
121 ],
122
123 [
124 'name' => 'userExpLevel',
125 'title' => 'rcfilters-filtergroup-userExpLevel',
126 'class' => ChangesListStringOptionsFilterGroup::class,
127 'isFullCoverage' => true,
128 'filters' => [
129 [
130 'name' => 'unregistered',
131 'label' => 'rcfilters-filter-user-experience-level-unregistered-label',
132 'description' => 'rcfilters-filter-user-experience-level-unregistered-description',
133 'cssClassSuffix' => 'user-unregistered',
134 'isRowApplicableCallable' => function ( $ctx, $rc ) {
135 return !$rc->getAttribute( 'rc_user' );
136 }
137 ],
138 [
139 'name' => 'registered',
140 'label' => 'rcfilters-filter-user-experience-level-registered-label',
141 'description' => 'rcfilters-filter-user-experience-level-registered-description',
142 'cssClassSuffix' => 'user-registered',
143 'isRowApplicableCallable' => function ( $ctx, $rc ) {
144 return $rc->getAttribute( 'rc_user' );
145 }
146 ],
147 [
148 'name' => 'newcomer',
149 'label' => 'rcfilters-filter-user-experience-level-newcomer-label',
150 'description' => 'rcfilters-filter-user-experience-level-newcomer-description',
151 'cssClassSuffix' => 'user-newcomer',
152 'isRowApplicableCallable' => function ( $ctx, $rc ) {
153 $performer = $rc->getPerformer();
154 return $performer && $performer->isLoggedIn() &&
155 $performer->getExperienceLevel() === 'newcomer';
156 }
157 ],
158 [
159 'name' => 'learner',
160 'label' => 'rcfilters-filter-user-experience-level-learner-label',
161 'description' => 'rcfilters-filter-user-experience-level-learner-description',
162 'cssClassSuffix' => 'user-learner',
163 'isRowApplicableCallable' => function ( $ctx, $rc ) {
164 $performer = $rc->getPerformer();
165 return $performer && $performer->isLoggedIn() &&
166 $performer->getExperienceLevel() === 'learner';
167 },
168 ],
169 [
170 'name' => 'experienced',
171 'label' => 'rcfilters-filter-user-experience-level-experienced-label',
172 'description' => 'rcfilters-filter-user-experience-level-experienced-description',
173 'cssClassSuffix' => 'user-experienced',
174 'isRowApplicableCallable' => function ( $ctx, $rc ) {
175 $performer = $rc->getPerformer();
176 return $performer && $performer->isLoggedIn() &&
177 $performer->getExperienceLevel() === 'experienced';
178 },
179 ]
180 ],
181 'default' => ChangesListStringOptionsFilterGroup::NONE,
182 'queryCallable' => [ $this, 'filterOnUserExperienceLevel' ],
183 ],
184
185 [
186 'name' => 'authorship',
187 'title' => 'rcfilters-filtergroup-authorship',
188 'class' => ChangesListBooleanFilterGroup::class,
189 'filters' => [
190 [
191 'name' => 'hidemyself',
192 'label' => 'rcfilters-filter-editsbyself-label',
193 'description' => 'rcfilters-filter-editsbyself-description',
194 // rcshowhidemine-show, rcshowhidemine-hide,
195 // wlshowhidemine
196 'showHideSuffix' => 'showhidemine',
197 'default' => false,
198 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
199 &$query_options, &$join_conds
200 ) {
201 $user = $ctx->getUser();
202 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
203 },
204 'cssClassSuffix' => 'self',
205 'isRowApplicableCallable' => function ( $ctx, $rc ) {
206 return $ctx->getUser()->equals( $rc->getPerformer() );
207 },
208 ],
209 [
210 'name' => 'hidebyothers',
211 'label' => 'rcfilters-filter-editsbyother-label',
212 'description' => 'rcfilters-filter-editsbyother-description',
213 'default' => false,
214 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
215 &$query_options, &$join_conds
216 ) {
217 $user = $ctx->getUser();
218 $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
219 },
220 'cssClassSuffix' => 'others',
221 'isRowApplicableCallable' => function ( $ctx, $rc ) {
222 return !$ctx->getUser()->equals( $rc->getPerformer() );
223 },
224 ]
225 ]
226 ],
227
228 [
229 'name' => 'automated',
230 'title' => 'rcfilters-filtergroup-automated',
231 'class' => ChangesListBooleanFilterGroup::class,
232 'filters' => [
233 [
234 'name' => 'hidebots',
235 'label' => 'rcfilters-filter-bots-label',
236 'description' => 'rcfilters-filter-bots-description',
237 // rcshowhidebots-show, rcshowhidebots-hide,
238 // wlshowhidebots
239 'showHideSuffix' => 'showhidebots',
240 'default' => false,
241 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
242 &$query_options, &$join_conds
243 ) {
244 $conds[] = 'rc_bot = 0';
245 },
246 'cssClassSuffix' => 'bot',
247 'isRowApplicableCallable' => function ( $ctx, $rc ) {
248 return $rc->getAttribute( 'rc_bot' );
249 },
250 ],
251 [
252 'name' => 'hidehumans',
253 'label' => 'rcfilters-filter-humans-label',
254 'description' => 'rcfilters-filter-humans-description',
255 'default' => false,
256 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
257 &$query_options, &$join_conds
258 ) {
259 $conds[] = 'rc_bot = 1';
260 },
261 'cssClassSuffix' => 'human',
262 'isRowApplicableCallable' => function ( $ctx, $rc ) {
263 return !$rc->getAttribute( 'rc_bot' );
264 },
265 ]
266 ]
267 ],
268
269 // reviewStatus (conditional)
270
271 [
272 'name' => 'significance',
273 'title' => 'rcfilters-filtergroup-significance',
274 'class' => ChangesListBooleanFilterGroup::class,
275 'priority' => -6,
276 'filters' => [
277 [
278 'name' => 'hideminor',
279 'label' => 'rcfilters-filter-minor-label',
280 'description' => 'rcfilters-filter-minor-description',
281 // rcshowhideminor-show, rcshowhideminor-hide,
282 // wlshowhideminor
283 'showHideSuffix' => 'showhideminor',
284 'default' => false,
285 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
286 &$query_options, &$join_conds
287 ) {
288 $conds[] = 'rc_minor = 0';
289 },
290 'cssClassSuffix' => 'minor',
291 'isRowApplicableCallable' => function ( $ctx, $rc ) {
292 return $rc->getAttribute( 'rc_minor' );
293 }
294 ],
295 [
296 'name' => 'hidemajor',
297 'label' => 'rcfilters-filter-major-label',
298 'description' => 'rcfilters-filter-major-description',
299 'default' => false,
300 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
301 &$query_options, &$join_conds
302 ) {
303 $conds[] = 'rc_minor = 1';
304 },
305 'cssClassSuffix' => 'major',
306 'isRowApplicableCallable' => function ( $ctx, $rc ) {
307 return !$rc->getAttribute( 'rc_minor' );
308 }
309 ]
310 ]
311 ],
312
313 [
314 'name' => 'lastRevision',
315 'title' => 'rcfilters-filtergroup-lastRevision',
316 'class' => ChangesListBooleanFilterGroup::class,
317 'priority' => -7,
318 'filters' => [
319 [
320 'name' => 'hidelastrevision',
321 'label' => 'rcfilters-filter-lastrevision-label',
322 'description' => 'rcfilters-filter-lastrevision-description',
323 'default' => false,
324 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
325 &$query_options, &$join_conds ) {
326 $conds[] = 'rc_this_oldid <> page_latest';
327 },
328 'cssClassSuffix' => 'last',
329 'isRowApplicableCallable' => function ( $ctx, $rc ) {
330 return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
331 }
332 ],
333 [
334 'name' => 'hidepreviousrevisions',
335 'label' => 'rcfilters-filter-previousrevision-label',
336 'description' => 'rcfilters-filter-previousrevision-description',
337 'default' => false,
338 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
339 &$query_options, &$join_conds ) {
340 $conds[] = 'rc_this_oldid = page_latest';
341 },
342 'cssClassSuffix' => 'previous',
343 'isRowApplicableCallable' => function ( $ctx, $rc ) {
344 return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
345 }
346 ]
347 ]
348 ],
349
350 // With extensions, there can be change types that will not be hidden by any of these.
351 [
352 'name' => 'changeType',
353 'title' => 'rcfilters-filtergroup-changetype',
354 'class' => ChangesListBooleanFilterGroup::class,
355 'priority' => -8,
356 'filters' => [
357 [
358 'name' => 'hidepageedits',
359 'label' => 'rcfilters-filter-pageedits-label',
360 'description' => 'rcfilters-filter-pageedits-description',
361 'default' => false,
362 'priority' => -2,
363 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
364 &$query_options, &$join_conds
365 ) {
366 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT );
367 },
368 'cssClassSuffix' => 'src-mw-edit',
369 'isRowApplicableCallable' => function ( $ctx, $rc ) {
370 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_EDIT;
371 },
372 ],
373 [
374 'name' => 'hidenewpages',
375 'label' => 'rcfilters-filter-newpages-label',
376 'description' => 'rcfilters-filter-newpages-description',
377 'default' => false,
378 'priority' => -3,
379 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
380 &$query_options, &$join_conds
381 ) {
382 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW );
383 },
384 'cssClassSuffix' => 'src-mw-new',
385 'isRowApplicableCallable' => function ( $ctx, $rc ) {
386 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_NEW;
387 },
388 ],
389
390 // hidecategorization
391
392 [
393 'name' => 'hidelog',
394 'label' => 'rcfilters-filter-logactions-label',
395 'description' => 'rcfilters-filter-logactions-description',
396 'default' => false,
397 'priority' => -5,
398 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
399 &$query_options, &$join_conds
400 ) {
401 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG );
402 },
403 'cssClassSuffix' => 'src-mw-log',
404 'isRowApplicableCallable' => function ( $ctx, $rc ) {
405 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_LOG;
406 }
407 ],
408 ],
409 ],
410
411 ];
412
413 $this->reviewStatusFilterGroupDefinition = [
414 [
415 'name' => 'reviewStatus',
416 'title' => 'rcfilters-filtergroup-reviewstatus',
417 'class' => ChangesListBooleanFilterGroup::class,
418 'priority' => -5,
419 'filters' => [
420 [
421 'name' => 'hidepatrolled',
422 'label' => 'rcfilters-filter-patrolled-label',
423 'description' => 'rcfilters-filter-patrolled-description',
424 // rcshowhidepatr-show, rcshowhidepatr-hide
425 // wlshowhidepatr
426 'showHideSuffix' => 'showhidepatr',
427 'default' => false,
428 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
429 &$query_options, &$join_conds
430 ) {
431 $conds[] = 'rc_patrolled = 0';
432 },
433 'cssClassSuffix' => 'patrolled',
434 'isRowApplicableCallable' => function ( $ctx, $rc ) {
435 return $rc->getAttribute( 'rc_patrolled' );
436 },
437 ],
438 [
439 'name' => 'hideunpatrolled',
440 'label' => 'rcfilters-filter-unpatrolled-label',
441 'description' => 'rcfilters-filter-unpatrolled-description',
442 'default' => false,
443 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
444 &$query_options, &$join_conds
445 ) {
446 $conds[] = 'rc_patrolled = 1';
447 },
448 'cssClassSuffix' => 'unpatrolled',
449 'isRowApplicableCallable' => function ( $ctx, $rc ) {
450 return !$rc->getAttribute( 'rc_patrolled' );
451 },
452 ],
453 ],
454 ]
455 ];
456
457 $this->hideCategorizationFilterDefinition = [
458 'name' => 'hidecategorization',
459 'label' => 'rcfilters-filter-categorization-label',
460 'description' => 'rcfilters-filter-categorization-description',
461 // rcshowhidecategorization-show, rcshowhidecategorization-hide.
462 // wlshowhidecategorization
463 'showHideSuffix' => 'showhidecategorization',
464 'default' => false,
465 'priority' => -4,
466 'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
467 &$query_options, &$join_conds
468 ) {
469 $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE );
470 },
471 'cssClassSuffix' => 'src-mw-categorize',
472 'isRowApplicableCallable' => function ( $ctx, $rc ) {
473 return $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_CATEGORIZE;
474 },
475 ];
476 }
477
478 /**
479 * Check if filters are in conflict and guaranteed to return no results.
480 *
481 * @return bool
482 */
483 protected function areFiltersInConflict() {
484 $opts = $this->getOptions();
485 /** @var ChangesListFilterGroup $group */
486 foreach ( $this->getFilterGroups() as $group ) {
487 if ( $group->getConflictingGroups() ) {
488 wfLogWarning(
489 $group->getName() .
490 " specifies conflicts with other groups but these are not supported yet."
491 );
492 }
493
494 /** @var ChangesListFilter $conflictingFilter */
495 foreach ( $group->getConflictingFilters() as $conflictingFilter ) {
496 if ( $conflictingFilter->activelyInConflictWithGroup( $group, $opts ) ) {
497 return true;
498 }
499 }
500
501 /** @var ChangesListFilter $filter */
502 foreach ( $group->getFilters() as $filter ) {
503 /** @var ChangesListFilter $conflictingFilter */
504 foreach ( $filter->getConflictingFilters() as $conflictingFilter ) {
505 if (
506 $conflictingFilter->activelyInConflictWithFilter( $filter, $opts ) &&
507 $filter->activelyInConflictWithFilter( $conflictingFilter, $opts )
508 ) {
509 return true;
510 }
511 }
512
513 }
514
515 }
516
517 return false;
518 }
519
520 /**
521 * Main execution point
522 *
523 * @param string $subpage
524 */
525 public function execute( $subpage ) {
526 $this->rcSubpage = $subpage;
527
528 $rows = $this->getRows();
529 $opts = $this->getOptions();
530 if ( $rows === false ) {
531 $rows = new FakeResultWrapper( [] );
532 }
533
534 // Used by Structured UI app to get results without MW chrome
535 if ( $this->getRequest()->getVal( 'action' ) === 'render' ) {
536 $this->getOutput()->setArticleBodyOnly( true );
537 }
538
539 // Used by "live update" and "view newest" to check
540 // if there's new changes with minimal data transfer
541 if ( $this->getRequest()->getBool( 'peek' ) ) {
542 $code = $rows->numRows() > 0 ? 200 : 204;
543 $this->getOutput()->setStatusCode( $code );
544 return;
545 }
546
547 $batch = new LinkBatch;
548 foreach ( $rows as $row ) {
549 $batch->add( NS_USER, $row->rc_user_text );
550 $batch->add( NS_USER_TALK, $row->rc_user_text );
551 $batch->add( $row->rc_namespace, $row->rc_title );
552 if ( $row->rc_source === RecentChange::SRC_LOG ) {
553 $formatter = LogFormatter::newFromRow( $row );
554 foreach ( $formatter->getPreloadTitles() as $title ) {
555 $batch->addObj( $title );
556 }
557 }
558 }
559 $batch->execute();
560
561 $this->setHeaders();
562 $this->outputHeader();
563 $this->addModules();
564 $this->webOutput( $rows, $opts );
565
566 $rows->free();
567
568 if ( $this->getConfig()->get( 'EnableWANCacheReaper' ) ) {
569 // Clean up any bad page entries for titles showing up in RC
570 DeferredUpdates::addUpdate( new WANCacheReapUpdate(
571 $this->getDB(),
572 LoggerFactory::getInstance( 'objectcache' )
573 ) );
574 }
575
576 $this->includeRcFiltersApp();
577 }
578
579 /**
580 * Include the modules and configuration for the RCFilters app.
581 * Conditional on the user having the feature enabled.
582 *
583 * If it is disabled, add a <body> class marking that
584 */
585 protected function includeRcFiltersApp() {
586 $out = $this->getOutput();
587 if ( $this->isStructuredFilterUiEnabled() ) {
588 $jsData = $this->getStructuredFilterJsData();
589
590 $messages = [];
591 foreach ( $jsData['messageKeys'] as $key ) {
592 $messages[$key] = $this->msg( $key )->plain();
593 }
594
595 $out->addBodyClasses( 'mw-rcfilters-enabled' );
596
597 $out->addHTML(
598 ResourceLoader::makeInlineScript(
599 ResourceLoader::makeMessageSetScript( $messages )
600 )
601 );
602
603 $out->addJsConfigVars( 'wgStructuredChangeFilters', $jsData['groups'] );
604
605 $out->addJsConfigVars(
606 'wgRCFiltersChangeTags',
607 $this->buildChangeTagList()
608 );
609 $out->addJsConfigVars(
610 'StructuredChangeFiltersDisplayConfig',
611 [
612 'maxDays' => (int)$this->getConfig()->get( 'RCMaxAge' ) / ( 24 * 3600 ), // Translate to days
613 'limitArray' => $this->getConfig()->get( 'RCLinkLimits' ),
614 'limitDefault' => $this->getDefaultLimit(),
615 'daysArray' => $this->getConfig()->get( 'RCLinkDays' ),
616 'daysDefault' => $this->getDefaultDays(),
617 ]
618 );
619
620 if ( static::$savedQueriesPreferenceName ) {
621 $savedQueries = FormatJson::decode(
622 $this->getUser()->getOption( static::$savedQueriesPreferenceName )
623 );
624 if ( $savedQueries && isset( $savedQueries->default ) ) {
625 // If there is a default saved query, show a loading spinner,
626 // since the frontend is going to reload the results
627 $out->addBodyClasses( 'mw-rcfilters-ui-loading' );
628 }
629 $out->addJsConfigVars(
630 'wgStructuredChangeFiltersSavedQueriesPreferenceName',
631 static::$savedQueriesPreferenceName
632 );
633 }
634 } else {
635 $out->addBodyClasses( 'mw-rcfilters-disabled' );
636 }
637 }
638
639 /**
640 * Fetch the change tags list for the front end
641 *
642 * @return Array Tag data
643 */
644 protected function buildChangeTagList() {
645 $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 );
646 $softwareActivatedTags = array_fill_keys( ChangeTags::listSoftwareActivatedTags(), 0 );
647
648 // Hit counts disabled for perf reasons, see T169997
649 /*
650 $tagStats = ChangeTags::tagUsageStatistics();
651 $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags, $tagStats );
652
653 // Sort by hits
654 arsort( $tagHitCounts );
655 */
656 $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags );
657
658 // Build the list and data
659 $result = [];
660 foreach ( $tagHitCounts as $tagName => $hits ) {
661 if (
662 // Only get active tags
663 isset( $explicitlyDefinedTags[ $tagName ] ) ||
664 isset( $softwareActivatedTags[ $tagName ] )
665 ) {
666 // Parse description
667 $desc = ChangeTags::tagLongDescriptionMessage( $tagName, $this->getContext() );
668
669 $result[] = [
670 'name' => $tagName,
671 'label' => Sanitizer::stripAllTags(
672 ChangeTags::tagDescription( $tagName, $this->getContext() )
673 ),
674 'description' => $desc ? Sanitizer::stripAllTags( $desc->parse() ) : '',
675 'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ),
676 'hits' => $hits,
677 ];
678 }
679 }
680
681 // Instead of sorting by hit count (disabled, see above), sort by display name
682 usort( $result, function ( $a, $b ) {
683 return strcasecmp( $a['label'], $b['label'] );
684 } );
685
686 return $result;
687 }
688
689 /**
690 * Add the "no results" message to the output
691 */
692 protected function outputNoResults() {
693 $this->getOutput()->addHTML(
694 '<div class="mw-changeslist-empty">' .
695 $this->msg( 'recentchanges-noresult' )->parse() .
696 '</div>'
697 );
698 }
699
700 /**
701 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
702 *
703 * @return bool|ResultWrapper Result or false
704 */
705 public function getRows() {
706 $opts = $this->getOptions();
707
708 $tables = [];
709 $fields = [];
710 $conds = [];
711 $query_options = [];
712 $join_conds = [];
713 $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
714
715 return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
716 }
717
718 /**
719 * Get the current FormOptions for this request
720 *
721 * @return FormOptions
722 */
723 public function getOptions() {
724 if ( $this->rcOptions === null ) {
725 $this->rcOptions = $this->setup( $this->rcSubpage );
726 }
727
728 return $this->rcOptions;
729 }
730
731 /**
732 * Register all filters and their groups (including those from hooks), plus handle
733 * conflicts and defaults.
734 *
735 * You might want to customize these in the same method, in subclasses. You can
736 * call getFilterGroup to access a group, and (on the group) getFilter to access a
737 * filter, then make necessary modfications to the filter or group (e.g. with
738 * setDefault).
739 */
740 protected function registerFilters() {
741 $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions );
742
743 // Make sure this is not being transcluded (we don't want to show this
744 // information to all users just because the user that saves the edit can
745 // patrol or is logged in)
746 if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
747 $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition );
748 }
749
750 $changeTypeGroup = $this->getFilterGroup( 'changeType' );
751
752 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
753 $transformedHideCategorizationDef = $this->transformFilterDefinition(
754 $this->hideCategorizationFilterDefinition
755 );
756
757 $transformedHideCategorizationDef['group'] = $changeTypeGroup;
758
759 $hideCategorization = new ChangesListBooleanFilter(
760 $transformedHideCategorizationDef
761 );
762 }
763
764 Hooks::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
765
766 $unstructuredGroupDefinition =
767 $this->getFilterGroupDefinitionFromLegacyCustomFilters(
768 $this->getCustomFilters()
769 );
770 $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
771
772 $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
773 $registered = $userExperienceLevel->getFilter( 'registered' );
774 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'newcomer' ) );
775 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'learner' ) );
776 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'experienced' ) );
777
778 $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
779 $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
780 $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
781
782 $significanceTypeGroup = $this->getFilterGroup( 'significance' );
783 $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
784
785 // categoryFilter is conditional; see registerFilters
786 if ( $categoryFilter !== null ) {
787 $hideMinorFilter->conflictsWith(
788 $categoryFilter,
789 'rcfilters-hideminor-conflicts-typeofchange-global',
790 'rcfilters-hideminor-conflicts-typeofchange',
791 'rcfilters-typeofchange-conflicts-hideminor'
792 );
793 }
794 $hideMinorFilter->conflictsWith(
795 $logactionsFilter,
796 'rcfilters-hideminor-conflicts-typeofchange-global',
797 'rcfilters-hideminor-conflicts-typeofchange',
798 'rcfilters-typeofchange-conflicts-hideminor'
799 );
800 $hideMinorFilter->conflictsWith(
801 $pagecreationFilter,
802 'rcfilters-hideminor-conflicts-typeofchange-global',
803 'rcfilters-hideminor-conflicts-typeofchange',
804 'rcfilters-typeofchange-conflicts-hideminor'
805 );
806 }
807
808 /**
809 * Transforms filter definition to prepare it for constructor.
810 *
811 * See overrides of this method as well.
812 *
813 * @param array $filterDefinition Original filter definition
814 *
815 * @return array Transformed definition
816 */
817 protected function transformFilterDefinition( array $filterDefinition ) {
818 return $filterDefinition;
819 }
820
821 /**
822 * Register filters from a definition object
823 *
824 * Array specifying groups and their filters; see Filter and
825 * ChangesListFilterGroup constructors.
826 *
827 * There is light processing to simplify core maintenance.
828 * @param array $definition
829 */
830 protected function registerFiltersFromDefinitions( array $definition ) {
831 $autoFillPriority = -1;
832 foreach ( $definition as $groupDefinition ) {
833 if ( !isset( $groupDefinition['priority'] ) ) {
834 $groupDefinition['priority'] = $autoFillPriority;
835 } else {
836 // If it's explicitly specified, start over the auto-fill
837 $autoFillPriority = $groupDefinition['priority'];
838 }
839
840 $autoFillPriority--;
841
842 $className = $groupDefinition['class'];
843 unset( $groupDefinition['class'] );
844
845 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
846 $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
847 }
848
849 $this->registerFilterGroup( new $className( $groupDefinition ) );
850 }
851 }
852
853 /**
854 * Get filter group definition from legacy custom filters
855 *
856 * @param array $customFilters Custom filters from legacy hooks
857 * @return array Group definition
858 */
859 protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
860 // Special internal unstructured group
861 $unstructuredGroupDefinition = [
862 'name' => 'unstructured',
863 'class' => ChangesListBooleanFilterGroup::class,
864 'priority' => -1, // Won't display in structured
865 'filters' => [],
866 ];
867
868 foreach ( $customFilters as $name => $params ) {
869 $unstructuredGroupDefinition['filters'][] = [
870 'name' => $name,
871 'showHide' => $params['msg'],
872 'default' => $params['default'],
873 ];
874 }
875
876 return $unstructuredGroupDefinition;
877 }
878
879 /**
880 * Register all the filters, including legacy hook-driven ones.
881 * Then create a FormOptions object with options as specified by the user
882 *
883 * @param array $parameters
884 *
885 * @return FormOptions
886 */
887 public function setup( $parameters ) {
888 $this->registerFilters();
889
890 $opts = $this->getDefaultOptions();
891
892 $opts = $this->fetchOptionsFromRequest( $opts );
893
894 // Give precedence to subpage syntax
895 if ( $parameters !== null ) {
896 $this->parseParameters( $parameters, $opts );
897 }
898
899 $this->validateOptions( $opts );
900
901 return $opts;
902 }
903
904 /**
905 * Get a FormOptions object containing the default options. By default, returns
906 * some basic options. The filters listed explicitly here are overriden in this
907 * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
908 * and more) are structured. Structured filters are overriden in registerFilters.
909 * not here.
910 *
911 * @return FormOptions
912 */
913 public function getDefaultOptions() {
914 $opts = new FormOptions();
915 $structuredUI = $this->isStructuredFilterUiEnabled();
916 // If urlversion=2 is set, ignore the filter defaults and set them all to false/empty
917 $useDefaults = $this->getRequest()->getInt( 'urlversion' ) !== 2;
918
919 // Add all filters
920 /** @var ChangesListFilterGroup $filterGroup */
921 foreach ( $this->filterGroups as $filterGroup ) {
922 // URL parameters can be per-group, like 'userExpLevel',
923 // or per-filter, like 'hideminor'.
924 if ( $filterGroup->isPerGroupRequestParameter() ) {
925 $opts->add( $filterGroup->getName(), $useDefaults ? $filterGroup->getDefault() : '' );
926 } else {
927 /** @var ChangesListBooleanFilter $filter */
928 foreach ( $filterGroup->getFilters() as $filter ) {
929 $opts->add( $filter->getName(), $useDefaults ? $filter->getDefault( $structuredUI ) : false );
930 }
931 }
932 }
933
934 $opts->add( 'namespace', '', FormOptions::STRING );
935 $opts->add( 'invert', false );
936 $opts->add( 'associated', false );
937 $opts->add( 'urlversion', 1 );
938 $opts->add( 'tagfilter', '' );
939
940 return $opts;
941 }
942
943 /**
944 * Register a structured changes list filter group
945 *
946 * @param ChangesListFilterGroup $group
947 */
948 public function registerFilterGroup( ChangesListFilterGroup $group ) {
949 $groupName = $group->getName();
950
951 $this->filterGroups[$groupName] = $group;
952 }
953
954 /**
955 * Gets the currently registered filters groups
956 *
957 * @return array Associative array of ChangesListFilterGroup objects, with group name as key
958 */
959 protected function getFilterGroups() {
960 return $this->filterGroups;
961 }
962
963 /**
964 * Gets a specified ChangesListFilterGroup by name
965 *
966 * @param string $groupName Name of group
967 *
968 * @return ChangesListFilterGroup|null Group, or null if not registered
969 */
970 public function getFilterGroup( $groupName ) {
971 return isset( $this->filterGroups[$groupName] ) ?
972 $this->filterGroups[$groupName] :
973 null;
974 }
975
976 // Currently, this intentionally only includes filters that display
977 // in the structured UI. This can be changed easily, though, if we want
978 // to include data on filters that use the unstructured UI. messageKeys is a
979 // special top-level value, with the value being an array of the message keys to
980 // send to the client.
981 /**
982 * Gets structured filter information needed by JS
983 *
984 * @return array Associative array
985 * * array $return['groups'] Group data
986 * * array $return['messageKeys'] Array of message keys
987 */
988 public function getStructuredFilterJsData() {
989 $output = [
990 'groups' => [],
991 'messageKeys' => [],
992 ];
993
994 usort( $this->filterGroups, function ( $a, $b ) {
995 return $b->getPriority() - $a->getPriority();
996 } );
997
998 foreach ( $this->filterGroups as $groupName => $group ) {
999 $groupOutput = $group->getJsData( $this );
1000 if ( $groupOutput !== null ) {
1001 $output['messageKeys'] = array_merge(
1002 $output['messageKeys'],
1003 $groupOutput['messageKeys']
1004 );
1005
1006 unset( $groupOutput['messageKeys'] );
1007 $output['groups'][] = $groupOutput;
1008 }
1009 }
1010
1011 return $output;
1012 }
1013
1014 /**
1015 * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
1016 * hook.
1017 *
1018 * @return array Map of filter URL param names to properties (msg/default)
1019 */
1020 protected function getCustomFilters() {
1021 if ( $this->customFilters === null ) {
1022 $this->customFilters = [];
1023 Hooks::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters ], '1.29' );
1024 }
1025
1026 return $this->customFilters;
1027 }
1028
1029 /**
1030 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
1031 *
1032 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
1033 *
1034 * @param FormOptions $opts
1035 * @return FormOptions
1036 */
1037 protected function fetchOptionsFromRequest( $opts ) {
1038 $opts->fetchValuesFromRequest( $this->getRequest() );
1039
1040 return $opts;
1041 }
1042
1043 /**
1044 * Process $par and put options found in $opts. Used when including the page.
1045 *
1046 * @param string $par
1047 * @param FormOptions $opts
1048 */
1049 public function parseParameters( $par, FormOptions $opts ) {
1050 $stringParameterNameSet = [];
1051 $hideParameterNameSet = [];
1052
1053 // URL parameters can be per-group, like 'userExpLevel',
1054 // or per-filter, like 'hideminor'.
1055
1056 foreach ( $this->filterGroups as $filterGroup ) {
1057 if ( $filterGroup->isPerGroupRequestParameter() ) {
1058 $stringParameterNameSet[$filterGroup->getName()] = true;
1059 } elseif ( $filterGroup->getType() === ChangesListBooleanFilterGroup::TYPE ) {
1060 foreach ( $filterGroup->getFilters() as $filter ) {
1061 $hideParameterNameSet[$filter->getName()] = true;
1062 }
1063 }
1064 }
1065
1066 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
1067 foreach ( $bits as $bit ) {
1068 $m = [];
1069 if ( isset( $hideParameterNameSet[$bit] ) ) {
1070 // hidefoo => hidefoo=true
1071 $opts[$bit] = true;
1072 } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
1073 // foo => hidefoo=false
1074 $opts["hide$bit"] = false;
1075 } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
1076 if ( isset( $stringParameterNameSet[$m[1]] ) ) {
1077 $opts[$m[1]] = $m[2];
1078 }
1079 }
1080 }
1081 }
1082
1083 /**
1084 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
1085 *
1086 * @param FormOptions $opts
1087 */
1088 public function validateOptions( FormOptions $opts ) {
1089 if ( $this->fixContradictoryOptions( $opts ) ) {
1090 $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
1091 $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
1092 }
1093 }
1094
1095 /**
1096 * Fix invalid options by resetting pairs that should never appear together.
1097 *
1098 * @param FormOptions $opts
1099 * @return bool True if any option was reset
1100 */
1101 private function fixContradictoryOptions( FormOptions $opts ) {
1102 $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
1103
1104 foreach ( $this->filterGroups as $filterGroup ) {
1105 if ( $filterGroup instanceof ChangesListBooleanFilterGroup ) {
1106 $filters = $filterGroup->getFilters();
1107
1108 if ( count( $filters ) === 1 ) {
1109 // legacy boolean filters should not be considered
1110 continue;
1111 }
1112
1113 $allInGroupEnabled = array_reduce(
1114 $filters,
1115 function ( $carry, $filter ) use ( $opts ) {
1116 return $carry && $opts[ $filter->getName() ];
1117 },
1118 /* initialValue */ count( $filters ) > 0
1119 );
1120
1121 if ( $allInGroupEnabled ) {
1122 foreach ( $filters as $filter ) {
1123 $opts[ $filter->getName() ] = false;
1124 }
1125
1126 $fixed = true;
1127 }
1128 }
1129 }
1130
1131 return $fixed;
1132 }
1133
1134 /**
1135 * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
1136 * compatibility.
1137 *
1138 * This is deprecated and may be removed.
1139 *
1140 * @param FormOptions $opts
1141 * @return bool True if this change was mode
1142 */
1143 private function fixBackwardsCompatibilityOptions( FormOptions $opts ) {
1144 if ( $opts['hideanons'] && $opts['hideliu'] ) {
1145 $opts->reset( 'hideanons' );
1146 if ( !$opts['hidebots'] ) {
1147 $opts->reset( 'hideliu' );
1148 $opts['hidehumans'] = 1;
1149 }
1150
1151 return true;
1152 }
1153
1154 return false;
1155 }
1156
1157 /**
1158 * Convert parameters values from true/false to 1/0
1159 * so they are not omitted by wfArrayToCgi()
1160 * Bug 36524
1161 *
1162 * @param array $params
1163 * @return array
1164 */
1165 protected function convertParamsForLink( $params ) {
1166 foreach ( $params as &$value ) {
1167 if ( $value === false ) {
1168 $value = '0';
1169 }
1170 }
1171 unset( $value );
1172 return $params;
1173 }
1174
1175 /**
1176 * Sets appropriate tables, fields, conditions, etc. depending on which filters
1177 * the user requested.
1178 *
1179 * @param array &$tables Array of tables; see IDatabase::select $table
1180 * @param array &$fields Array of fields; see IDatabase::select $vars
1181 * @param array &$conds Array of conditions; see IDatabase::select $conds
1182 * @param array &$query_options Array of query options; see IDatabase::select $options
1183 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1184 * @param FormOptions $opts
1185 */
1186 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
1187 &$join_conds, FormOptions $opts
1188 ) {
1189 $dbr = $this->getDB();
1190 $isStructuredUI = $this->isStructuredFilterUiEnabled();
1191
1192 foreach ( $this->filterGroups as $filterGroup ) {
1193 // URL parameters can be per-group, like 'userExpLevel',
1194 // or per-filter, like 'hideminor'.
1195 if ( $filterGroup->isPerGroupRequestParameter() ) {
1196 $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1197 $query_options, $join_conds, $opts[$filterGroup->getName()] );
1198 } else {
1199 foreach ( $filterGroup->getFilters() as $filter ) {
1200 if ( $filter->isActive( $opts, $isStructuredUI ) ) {
1201 $filter->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1202 $query_options, $join_conds );
1203 }
1204 }
1205 }
1206 }
1207
1208 // Namespace filtering
1209 if ( $opts[ 'namespace' ] !== '' ) {
1210 $namespaces = explode( ';', $opts[ 'namespace' ] );
1211
1212 if ( $opts[ 'associated' ] ) {
1213 $associatedNamespaces = array_map(
1214 function ( $ns ) {
1215 return MWNamespace::getAssociated( $ns );
1216 },
1217 $namespaces
1218 );
1219 $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
1220 }
1221
1222 if ( count( $namespaces ) === 1 ) {
1223 $operator = $opts[ 'invert' ] ? '!=' : '=';
1224 $value = $dbr->addQuotes( reset( $namespaces ) );
1225 } else {
1226 $operator = $opts[ 'invert' ] ? 'NOT IN' : 'IN';
1227 sort( $namespaces );
1228 $value = '(' . $dbr->makeList( $namespaces ) . ')';
1229 }
1230 $conds[] = "rc_namespace $operator $value";
1231 }
1232 }
1233
1234 /**
1235 * Process the query
1236 *
1237 * @param array $tables Array of tables; see IDatabase::select $table
1238 * @param array $fields Array of fields; see IDatabase::select $vars
1239 * @param array $conds Array of conditions; see IDatabase::select $conds
1240 * @param array $query_options Array of query options; see IDatabase::select $options
1241 * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
1242 * @param FormOptions $opts
1243 * @return bool|ResultWrapper Result or false
1244 */
1245 protected function doMainQuery( $tables, $fields, $conds,
1246 $query_options, $join_conds, FormOptions $opts
1247 ) {
1248 $tables[] = 'recentchanges';
1249 $fields = array_merge( RecentChange::selectFields(), $fields );
1250
1251 ChangeTags::modifyDisplayQuery(
1252 $tables,
1253 $fields,
1254 $conds,
1255 $join_conds,
1256 $query_options,
1257 ''
1258 );
1259
1260 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
1261 $opts )
1262 ) {
1263 return false;
1264 }
1265
1266 $dbr = $this->getDB();
1267
1268 return $dbr->select(
1269 $tables,
1270 $fields,
1271 $conds,
1272 __METHOD__,
1273 $query_options,
1274 $join_conds
1275 );
1276 }
1277
1278 protected function runMainQueryHook( &$tables, &$fields, &$conds,
1279 &$query_options, &$join_conds, $opts
1280 ) {
1281 return Hooks::run(
1282 'ChangesListSpecialPageQuery',
1283 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
1284 );
1285 }
1286
1287 /**
1288 * Return a IDatabase object for reading
1289 *
1290 * @return IDatabase
1291 */
1292 protected function getDB() {
1293 return wfGetDB( DB_REPLICA );
1294 }
1295
1296 /**
1297 * Send output to the OutputPage object, only called if not used feeds
1298 *
1299 * @param ResultWrapper $rows Database rows
1300 * @param FormOptions $opts
1301 */
1302 public function webOutput( $rows, $opts ) {
1303 if ( !$this->including() ) {
1304 $this->outputFeedLinks();
1305 $this->doHeader( $opts, $rows->numRows() );
1306 }
1307
1308 $this->outputChangesList( $rows, $opts );
1309 }
1310
1311 /**
1312 * Output feed links.
1313 */
1314 public function outputFeedLinks() {
1315 // nothing by default
1316 }
1317
1318 /**
1319 * Build and output the actual changes list.
1320 *
1321 * @param ResultWrapper $rows Database rows
1322 * @param FormOptions $opts
1323 */
1324 abstract public function outputChangesList( $rows, $opts );
1325
1326 /**
1327 * Set the text to be displayed above the changes
1328 *
1329 * @param FormOptions $opts
1330 * @param int $numRows Number of rows in the result to show after this header
1331 */
1332 public function doHeader( $opts, $numRows ) {
1333 $this->setTopText( $opts );
1334
1335 // @todo Lots of stuff should be done here.
1336
1337 $this->setBottomText( $opts );
1338 }
1339
1340 /**
1341 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
1342 * or similar methods to print the text.
1343 *
1344 * @param FormOptions $opts
1345 */
1346 public function setTopText( FormOptions $opts ) {
1347 // nothing by default
1348 }
1349
1350 /**
1351 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
1352 * or similar methods to print the text.
1353 *
1354 * @param FormOptions $opts
1355 */
1356 public function setBottomText( FormOptions $opts ) {
1357 // nothing by default
1358 }
1359
1360 /**
1361 * Get options to be displayed in a form
1362 * @todo This should handle options returned by getDefaultOptions().
1363 * @todo Not called by anything in this class (but is in subclasses), should be
1364 * called by something… doHeader() maybe?
1365 *
1366 * @param FormOptions $opts
1367 * @return array
1368 */
1369 public function getExtraOptions( $opts ) {
1370 return [];
1371 }
1372
1373 /**
1374 * Return the legend displayed within the fieldset
1375 *
1376 * @return string
1377 */
1378 public function makeLegend() {
1379 $context = $this->getContext();
1380 $user = $context->getUser();
1381 # The legend showing what the letters and stuff mean
1382 $legend = Html::openElement( 'dl' ) . "\n";
1383 # Iterates through them and gets the messages for both letter and tooltip
1384 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
1385 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
1386 unset( $legendItems['unpatrolled'] );
1387 }
1388 foreach ( $legendItems as $key => $item ) { # generate items of the legend
1389 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
1390 $letter = $item['letter'];
1391 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
1392
1393 $legend .= Html::element( 'dt',
1394 [ 'class' => $cssClass ], $context->msg( $letter )->text()
1395 ) . "\n" .
1396 Html::rawElement( 'dd',
1397 [ 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ],
1398 $context->msg( $label )->parse()
1399 ) . "\n";
1400 }
1401 # (+-123)
1402 $legend .= Html::rawElement( 'dt',
1403 [ 'class' => 'mw-plusminus-pos' ],
1404 $context->msg( 'recentchanges-legend-plusminus' )->parse()
1405 ) . "\n";
1406 $legend .= Html::element(
1407 'dd',
1408 [ 'class' => 'mw-changeslist-legend-plusminus' ],
1409 $context->msg( 'recentchanges-label-plusminus' )->text()
1410 ) . "\n";
1411 $legend .= Html::closeElement( 'dl' ) . "\n";
1412
1413 $legendHeading = $this->isStructuredFilterUiEnabled() ?
1414 $context->msg( 'rcfilters-legend-heading' )->parse() :
1415 $context->msg( 'recentchanges-legend-heading' )->parse();
1416
1417 # Collapsible
1418 $legend =
1419 '<div class="mw-changeslist-legend">' .
1420 $legendHeading .
1421 '<div class="mw-collapsible-content">' . $legend . '</div>' .
1422 '</div>';
1423
1424 return $legend;
1425 }
1426
1427 /**
1428 * Add page-specific modules.
1429 */
1430 protected function addModules() {
1431 $out = $this->getOutput();
1432 // Styles and behavior for the legend box (see makeLegend())
1433 $out->addModuleStyles( [
1434 'mediawiki.special.changeslist.legend',
1435 'mediawiki.special.changeslist',
1436 ] );
1437 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
1438
1439 if ( $this->isStructuredFilterUiEnabled() ) {
1440 $out->addModules( 'mediawiki.rcfilters.filters.ui' );
1441 $out->addModuleStyles( 'mediawiki.rcfilters.filters.base.styles' );
1442 }
1443 }
1444
1445 protected function getGroupName() {
1446 return 'changes';
1447 }
1448
1449 /**
1450 * Filter on users' experience levels; this will not be called if nothing is
1451 * selected.
1452 *
1453 * @param string $specialPageClassName Class name of current special page
1454 * @param IContextSource $context Context, for e.g. user
1455 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
1456 * @param array &$tables Array of tables; see IDatabase::select $table
1457 * @param array &$fields Array of fields; see IDatabase::select $vars
1458 * @param array &$conds Array of conditions; see IDatabase::select $conds
1459 * @param array &$query_options Array of query options; see IDatabase::select $options
1460 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1461 * @param array $selectedExpLevels The allowed active values, sorted
1462 * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
1463 * (optional)
1464 */
1465 public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
1466 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0
1467 ) {
1468 global $wgLearnerEdits,
1469 $wgExperiencedUserEdits,
1470 $wgLearnerMemberSince,
1471 $wgExperiencedUserMemberSince;
1472
1473 $LEVEL_COUNT = 5;
1474
1475 // If all levels are selected, don't filter
1476 if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
1477 return;
1478 }
1479
1480 // both 'registered' and 'unregistered', experience levels, if any, are included in 'registered'
1481 if (
1482 in_array( 'registered', $selectedExpLevels ) &&
1483 in_array( 'unregistered', $selectedExpLevels )
1484 ) {
1485 return;
1486 }
1487
1488 // 'registered' but not 'unregistered', experience levels, if any, are included in 'registered'
1489 if (
1490 in_array( 'registered', $selectedExpLevels ) &&
1491 !in_array( 'unregistered', $selectedExpLevels )
1492 ) {
1493 $conds[] = 'rc_user != 0';
1494 return;
1495 }
1496
1497 if ( $selectedExpLevels === [ 'unregistered' ] ) {
1498 $conds[] = 'rc_user = 0';
1499 return;
1500 }
1501
1502 $tables[] = 'user';
1503 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
1504
1505 if ( $now === 0 ) {
1506 $now = time();
1507 }
1508 $secondsPerDay = 86400;
1509 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
1510 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
1511
1512 $aboveNewcomer = $dbr->makeList(
1513 [
1514 'user_editcount >= ' . intval( $wgLearnerEdits ),
1515 'user_registration <= ' . $dbr->addQuotes( $dbr->timestamp( $learnerCutoff ) ),
1516 ],
1517 IDatabase::LIST_AND
1518 );
1519
1520 $aboveLearner = $dbr->makeList(
1521 [
1522 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
1523 'user_registration <= ' .
1524 $dbr->addQuotes( $dbr->timestamp( $experiencedUserCutoff ) ),
1525 ],
1526 IDatabase::LIST_AND
1527 );
1528
1529 $conditions = [];
1530
1531 if ( in_array( 'unregistered', $selectedExpLevels ) ) {
1532 $selectedExpLevels = array_diff( $selectedExpLevels, [ 'unregistered' ] );
1533 $conditions[] = 'rc_user = 0';
1534 }
1535
1536 if ( $selectedExpLevels === [ 'newcomer' ] ) {
1537 $conditions[] = "NOT ( $aboveNewcomer )";
1538 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
1539 $conditions[] = $dbr->makeList(
1540 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
1541 IDatabase::LIST_AND
1542 );
1543 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
1544 $conditions[] = $aboveLearner;
1545 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
1546 $conditions[] = "NOT ( $aboveLearner )";
1547 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
1548 $conditions[] = $dbr->makeList(
1549 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
1550 IDatabase::LIST_OR
1551 );
1552 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
1553 $conditions[] = $aboveNewcomer;
1554 } elseif ( $selectedExpLevels === [ 'experienced', 'learner', 'newcomer' ] ) {
1555 $conditions[] = 'rc_user != 0';
1556 }
1557
1558 if ( count( $conditions ) > 1 ) {
1559 $conds[] = $dbr->makeList( $conditions, IDatabase::LIST_OR );
1560 } elseif ( count( $conditions ) === 1 ) {
1561 $conds[] = reset( $conditions );
1562 }
1563 }
1564
1565 /**
1566 * Check whether the structured filter UI is enabled
1567 *
1568 * @return bool
1569 */
1570 public function isStructuredFilterUiEnabled() {
1571 if ( $this->getRequest()->getBool( 'rcfilters' ) ) {
1572 return true;
1573 }
1574
1575 if ( $this->getConfig()->get( 'StructuredChangeFiltersShowPreference' ) ) {
1576 return !$this->getUser()->getOption( 'rcenhancedfilters-disable' );
1577 } else {
1578 return $this->getUser()->getOption( 'rcenhancedfilters' );
1579 }
1580 }
1581
1582 /**
1583 * Check whether the structured filter UI is enabled by default (regardless of
1584 * this particular user's setting)
1585 *
1586 * @return bool
1587 */
1588 public function isStructuredFilterUiEnabledByDefault() {
1589 if ( $this->getConfig()->get( 'StructuredChangeFiltersShowPreference' ) ) {
1590 return !$this->getUser()->getDefaultOption( 'rcenhancedfilters-disable' );
1591 } else {
1592 return $this->getUser()->getDefaultOption( 'rcenhancedfilters' );
1593 }
1594 }
1595
1596 abstract function getDefaultLimit();
1597
1598 /**
1599 * Get the default value of the number of days to display when loading
1600 * the result set.
1601 * Supports fractional values, and should be cast to a float.
1602 *
1603 * @return float
1604 */
1605 abstract function getDefaultDays();
1606 }