RCFilters: Make live update polling configurable
[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 $experimentalStructuredChangeFilters =
604 $this->getConfig()->get( 'StructuredChangeFiltersEnableExperimentalViews' );
605
606 $out->addJsConfigVars( 'wgStructuredChangeFilters', $jsData['groups'] );
607 $out->addJsConfigVars(
608 'wgStructuredChangeFiltersEnableExperimentalViews',
609 $experimentalStructuredChangeFilters
610 );
611
612 $out->addJsConfigVars(
613 'wgRCFiltersChangeTags',
614 $this->buildChangeTagList()
615 );
616 $out->addJsConfigVars(
617 'StructuredChangeFiltersDisplayConfig',
618 [
619 'maxDays' => (int)$this->getConfig()->get( 'RCMaxAge' ) / ( 24 * 3600 ), // Translate to days
620 'limitArray' => $this->getConfig()->get( 'RCLinkLimits' ),
621 'limitDefault' => $this->getDefaultLimit(),
622 'daysArray' => $this->getConfig()->get( 'RCLinkDays' ),
623 'daysDefault' => $this->getDefaultDays(),
624 ]
625 );
626
627 $out->addJsConfigVars(
628 'StructuredChangeFiltersLiveUpdatePollingRate',
629 $this->getConfig()->get( 'StructuredChangeFiltersLiveUpdatePollingRate' )
630 );
631
632 if ( static::$savedQueriesPreferenceName ) {
633 $savedQueries = FormatJson::decode(
634 $this->getUser()->getOption( static::$savedQueriesPreferenceName )
635 );
636 if ( $savedQueries && isset( $savedQueries->default ) ) {
637 // If there is a default saved query, show a loading spinner,
638 // since the frontend is going to reload the results
639 $out->addBodyClasses( 'mw-rcfilters-ui-loading' );
640 }
641 $out->addJsConfigVars(
642 'wgStructuredChangeFiltersSavedQueriesPreferenceName',
643 static::$savedQueriesPreferenceName
644 );
645 }
646 } else {
647 $out->addBodyClasses( 'mw-rcfilters-disabled' );
648 }
649 }
650
651 /**
652 * Fetch the change tags list for the front end
653 *
654 * @return Array Tag data
655 */
656 protected function buildChangeTagList() {
657 $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 );
658 $softwareActivatedTags = array_fill_keys( ChangeTags::listSoftwareActivatedTags(), 0 );
659
660 // Hit counts disabled for perf reasons, see T169997
661 /*
662 $tagStats = ChangeTags::tagUsageStatistics();
663 $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags, $tagStats );
664
665 // Sort by hits
666 arsort( $tagHitCounts );
667 */
668 $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags );
669
670 // Build the list and data
671 $result = [];
672 foreach ( $tagHitCounts as $tagName => $hits ) {
673 if (
674 // Only get active tags
675 isset( $explicitlyDefinedTags[ $tagName ] ) ||
676 isset( $softwareActivatedTags[ $tagName ] )
677 ) {
678 // Parse description
679 $desc = ChangeTags::tagLongDescriptionMessage( $tagName, $this->getContext() );
680
681 $result[] = [
682 'name' => $tagName,
683 'label' => Sanitizer::stripAllTags(
684 ChangeTags::tagDescription( $tagName, $this->getContext() )
685 ),
686 'description' => $desc ? Sanitizer::stripAllTags( $desc->parse() ) : '',
687 'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ),
688 'hits' => $hits,
689 ];
690 }
691 }
692
693 // Instead of sorting by hit count (disabled, see above), sort by display name
694 usort( $result, function ( $a, $b ) {
695 return strcasecmp( $a['label'], $b['label'] );
696 } );
697
698 return $result;
699 }
700
701 /**
702 * Add the "no results" message to the output
703 */
704 protected function outputNoResults() {
705 $this->getOutput()->addHTML(
706 '<div class="mw-changeslist-empty">' .
707 $this->msg( 'recentchanges-noresult' )->parse() .
708 '</div>'
709 );
710 }
711
712 /**
713 * Get the database result for this special page instance. Used by ApiFeedRecentChanges.
714 *
715 * @return bool|ResultWrapper Result or false
716 */
717 public function getRows() {
718 $opts = $this->getOptions();
719
720 $tables = [];
721 $fields = [];
722 $conds = [];
723 $query_options = [];
724 $join_conds = [];
725 $this->buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
726
727 return $this->doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts );
728 }
729
730 /**
731 * Get the current FormOptions for this request
732 *
733 * @return FormOptions
734 */
735 public function getOptions() {
736 if ( $this->rcOptions === null ) {
737 $this->rcOptions = $this->setup( $this->rcSubpage );
738 }
739
740 return $this->rcOptions;
741 }
742
743 /**
744 * Register all filters and their groups (including those from hooks), plus handle
745 * conflicts and defaults.
746 *
747 * You might want to customize these in the same method, in subclasses. You can
748 * call getFilterGroup to access a group, and (on the group) getFilter to access a
749 * filter, then make necessary modfications to the filter or group (e.g. with
750 * setDefault).
751 */
752 protected function registerFilters() {
753 $this->registerFiltersFromDefinitions( $this->filterGroupDefinitions );
754
755 // Make sure this is not being transcluded (we don't want to show this
756 // information to all users just because the user that saves the edit can
757 // patrol or is logged in)
758 if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
759 $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition );
760 }
761
762 $changeTypeGroup = $this->getFilterGroup( 'changeType' );
763
764 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
765 $transformedHideCategorizationDef = $this->transformFilterDefinition(
766 $this->hideCategorizationFilterDefinition
767 );
768
769 $transformedHideCategorizationDef['group'] = $changeTypeGroup;
770
771 $hideCategorization = new ChangesListBooleanFilter(
772 $transformedHideCategorizationDef
773 );
774 }
775
776 Hooks::run( 'ChangesListSpecialPageStructuredFilters', [ $this ] );
777
778 $unstructuredGroupDefinition =
779 $this->getFilterGroupDefinitionFromLegacyCustomFilters(
780 $this->getCustomFilters()
781 );
782 $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
783
784 $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
785 $registered = $userExperienceLevel->getFilter( 'registered' );
786 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'newcomer' ) );
787 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'learner' ) );
788 $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'experienced' ) );
789
790 $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
791 $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
792 $pagecreationFilter = $changeTypeGroup->getFilter( 'hidenewpages' );
793
794 $significanceTypeGroup = $this->getFilterGroup( 'significance' );
795 $hideMinorFilter = $significanceTypeGroup->getFilter( 'hideminor' );
796
797 // categoryFilter is conditional; see registerFilters
798 if ( $categoryFilter !== null ) {
799 $hideMinorFilter->conflictsWith(
800 $categoryFilter,
801 'rcfilters-hideminor-conflicts-typeofchange-global',
802 'rcfilters-hideminor-conflicts-typeofchange',
803 'rcfilters-typeofchange-conflicts-hideminor'
804 );
805 }
806 $hideMinorFilter->conflictsWith(
807 $logactionsFilter,
808 'rcfilters-hideminor-conflicts-typeofchange-global',
809 'rcfilters-hideminor-conflicts-typeofchange',
810 'rcfilters-typeofchange-conflicts-hideminor'
811 );
812 $hideMinorFilter->conflictsWith(
813 $pagecreationFilter,
814 'rcfilters-hideminor-conflicts-typeofchange-global',
815 'rcfilters-hideminor-conflicts-typeofchange',
816 'rcfilters-typeofchange-conflicts-hideminor'
817 );
818 }
819
820 /**
821 * Transforms filter definition to prepare it for constructor.
822 *
823 * See overrides of this method as well.
824 *
825 * @param array $filterDefinition Original filter definition
826 *
827 * @return array Transformed definition
828 */
829 protected function transformFilterDefinition( array $filterDefinition ) {
830 return $filterDefinition;
831 }
832
833 /**
834 * Register filters from a definition object
835 *
836 * Array specifying groups and their filters; see Filter and
837 * ChangesListFilterGroup constructors.
838 *
839 * There is light processing to simplify core maintenance.
840 * @param array $definition
841 */
842 protected function registerFiltersFromDefinitions( array $definition ) {
843 $autoFillPriority = -1;
844 foreach ( $definition as $groupDefinition ) {
845 if ( !isset( $groupDefinition['priority'] ) ) {
846 $groupDefinition['priority'] = $autoFillPriority;
847 } else {
848 // If it's explicitly specified, start over the auto-fill
849 $autoFillPriority = $groupDefinition['priority'];
850 }
851
852 $autoFillPriority--;
853
854 $className = $groupDefinition['class'];
855 unset( $groupDefinition['class'] );
856
857 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
858 $filterDefinition = $this->transformFilterDefinition( $filterDefinition );
859 }
860
861 $this->registerFilterGroup( new $className( $groupDefinition ) );
862 }
863 }
864
865 /**
866 * Get filter group definition from legacy custom filters
867 *
868 * @param array $customFilters Custom filters from legacy hooks
869 * @return array Group definition
870 */
871 protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
872 // Special internal unstructured group
873 $unstructuredGroupDefinition = [
874 'name' => 'unstructured',
875 'class' => ChangesListBooleanFilterGroup::class,
876 'priority' => -1, // Won't display in structured
877 'filters' => [],
878 ];
879
880 foreach ( $customFilters as $name => $params ) {
881 $unstructuredGroupDefinition['filters'][] = [
882 'name' => $name,
883 'showHide' => $params['msg'],
884 'default' => $params['default'],
885 ];
886 }
887
888 return $unstructuredGroupDefinition;
889 }
890
891 /**
892 * Register all the filters, including legacy hook-driven ones.
893 * Then create a FormOptions object with options as specified by the user
894 *
895 * @param array $parameters
896 *
897 * @return FormOptions
898 */
899 public function setup( $parameters ) {
900 $this->registerFilters();
901
902 $opts = $this->getDefaultOptions();
903
904 $opts = $this->fetchOptionsFromRequest( $opts );
905
906 // Give precedence to subpage syntax
907 if ( $parameters !== null ) {
908 $this->parseParameters( $parameters, $opts );
909 }
910
911 $this->validateOptions( $opts );
912
913 return $opts;
914 }
915
916 /**
917 * Get a FormOptions object containing the default options. By default, returns
918 * some basic options. The filters listed explicitly here are overriden in this
919 * method, in subclasses, but most filters (e.g. hideminor, userExpLevel filters,
920 * and more) are structured. Structured filters are overriden in registerFilters.
921 * not here.
922 *
923 * @return FormOptions
924 */
925 public function getDefaultOptions() {
926 $opts = new FormOptions();
927 $structuredUI = $this->isStructuredFilterUiEnabled();
928 // If urlversion=2 is set, ignore the filter defaults and set them all to false/empty
929 $useDefaults = $this->getRequest()->getInt( 'urlversion' ) !== 2;
930
931 // Add all filters
932 /** @var ChangesListFilterGroup $filterGroup */
933 foreach ( $this->filterGroups as $filterGroup ) {
934 // URL parameters can be per-group, like 'userExpLevel',
935 // or per-filter, like 'hideminor'.
936 if ( $filterGroup->isPerGroupRequestParameter() ) {
937 $opts->add( $filterGroup->getName(), $useDefaults ? $filterGroup->getDefault() : '' );
938 } else {
939 /** @var ChangesListBooleanFilter $filter */
940 foreach ( $filterGroup->getFilters() as $filter ) {
941 $opts->add( $filter->getName(), $useDefaults ? $filter->getDefault( $structuredUI ) : false );
942 }
943 }
944 }
945
946 $opts->add( 'namespace', '', FormOptions::STRING );
947 $opts->add( 'invert', false );
948 $opts->add( 'associated', false );
949 $opts->add( 'urlversion', 1 );
950 $opts->add( 'tagfilter', '' );
951
952 return $opts;
953 }
954
955 /**
956 * Register a structured changes list filter group
957 *
958 * @param ChangesListFilterGroup $group
959 */
960 public function registerFilterGroup( ChangesListFilterGroup $group ) {
961 $groupName = $group->getName();
962
963 $this->filterGroups[$groupName] = $group;
964 }
965
966 /**
967 * Gets the currently registered filters groups
968 *
969 * @return array Associative array of ChangesListFilterGroup objects, with group name as key
970 */
971 protected function getFilterGroups() {
972 return $this->filterGroups;
973 }
974
975 /**
976 * Gets a specified ChangesListFilterGroup by name
977 *
978 * @param string $groupName Name of group
979 *
980 * @return ChangesListFilterGroup|null Group, or null if not registered
981 */
982 public function getFilterGroup( $groupName ) {
983 return isset( $this->filterGroups[$groupName] ) ?
984 $this->filterGroups[$groupName] :
985 null;
986 }
987
988 // Currently, this intentionally only includes filters that display
989 // in the structured UI. This can be changed easily, though, if we want
990 // to include data on filters that use the unstructured UI. messageKeys is a
991 // special top-level value, with the value being an array of the message keys to
992 // send to the client.
993 /**
994 * Gets structured filter information needed by JS
995 *
996 * @return array Associative array
997 * * array $return['groups'] Group data
998 * * array $return['messageKeys'] Array of message keys
999 */
1000 public function getStructuredFilterJsData() {
1001 $output = [
1002 'groups' => [],
1003 'messageKeys' => [],
1004 ];
1005
1006 usort( $this->filterGroups, function ( $a, $b ) {
1007 return $b->getPriority() - $a->getPriority();
1008 } );
1009
1010 foreach ( $this->filterGroups as $groupName => $group ) {
1011 $groupOutput = $group->getJsData( $this );
1012 if ( $groupOutput !== null ) {
1013 $output['messageKeys'] = array_merge(
1014 $output['messageKeys'],
1015 $groupOutput['messageKeys']
1016 );
1017
1018 unset( $groupOutput['messageKeys'] );
1019 $output['groups'][] = $groupOutput;
1020 }
1021 }
1022
1023 return $output;
1024 }
1025
1026 /**
1027 * Get custom show/hide filters using deprecated ChangesListSpecialPageFilters
1028 * hook.
1029 *
1030 * @return array Map of filter URL param names to properties (msg/default)
1031 */
1032 protected function getCustomFilters() {
1033 if ( $this->customFilters === null ) {
1034 $this->customFilters = [];
1035 Hooks::run( 'ChangesListSpecialPageFilters', [ $this, &$this->customFilters ], '1.29' );
1036 }
1037
1038 return $this->customFilters;
1039 }
1040
1041 /**
1042 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
1043 *
1044 * Intended for subclassing, e.g. to add a backwards-compatibility layer.
1045 *
1046 * @param FormOptions $opts
1047 * @return FormOptions
1048 */
1049 protected function fetchOptionsFromRequest( $opts ) {
1050 $opts->fetchValuesFromRequest( $this->getRequest() );
1051
1052 return $opts;
1053 }
1054
1055 /**
1056 * Process $par and put options found in $opts. Used when including the page.
1057 *
1058 * @param string $par
1059 * @param FormOptions $opts
1060 */
1061 public function parseParameters( $par, FormOptions $opts ) {
1062 $stringParameterNameSet = [];
1063 $hideParameterNameSet = [];
1064
1065 // URL parameters can be per-group, like 'userExpLevel',
1066 // or per-filter, like 'hideminor'.
1067
1068 foreach ( $this->filterGroups as $filterGroup ) {
1069 if ( $filterGroup->isPerGroupRequestParameter() ) {
1070 $stringParameterNameSet[$filterGroup->getName()] = true;
1071 } elseif ( $filterGroup->getType() === ChangesListBooleanFilterGroup::TYPE ) {
1072 foreach ( $filterGroup->getFilters() as $filter ) {
1073 $hideParameterNameSet[$filter->getName()] = true;
1074 }
1075 }
1076 }
1077
1078 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
1079 foreach ( $bits as $bit ) {
1080 $m = [];
1081 if ( isset( $hideParameterNameSet[$bit] ) ) {
1082 // hidefoo => hidefoo=true
1083 $opts[$bit] = true;
1084 } elseif ( isset( $hideParameterNameSet["hide$bit"] ) ) {
1085 // foo => hidefoo=false
1086 $opts["hide$bit"] = false;
1087 } elseif ( preg_match( '/^(.*)=(.*)$/', $bit, $m ) ) {
1088 if ( isset( $stringParameterNameSet[$m[1]] ) ) {
1089 $opts[$m[1]] = $m[2];
1090 }
1091 }
1092 }
1093 }
1094
1095 /**
1096 * Validate a FormOptions object generated by getDefaultOptions() with values already populated.
1097 *
1098 * @param FormOptions $opts
1099 */
1100 public function validateOptions( FormOptions $opts ) {
1101 if ( $this->fixContradictoryOptions( $opts ) ) {
1102 $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
1103 $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
1104 }
1105 }
1106
1107 /**
1108 * Fix invalid options by resetting pairs that should never appear together.
1109 *
1110 * @param FormOptions $opts
1111 * @return bool True if any option was reset
1112 */
1113 private function fixContradictoryOptions( FormOptions $opts ) {
1114 $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
1115
1116 foreach ( $this->filterGroups as $filterGroup ) {
1117 if ( $filterGroup instanceof ChangesListBooleanFilterGroup ) {
1118 $filters = $filterGroup->getFilters();
1119
1120 if ( count( $filters ) === 1 ) {
1121 // legacy boolean filters should not be considered
1122 continue;
1123 }
1124
1125 $allInGroupEnabled = array_reduce(
1126 $filters,
1127 function ( $carry, $filter ) use ( $opts ) {
1128 return $carry && $opts[ $filter->getName() ];
1129 },
1130 /* initialValue */ count( $filters ) > 0
1131 );
1132
1133 if ( $allInGroupEnabled ) {
1134 foreach ( $filters as $filter ) {
1135 $opts[ $filter->getName() ] = false;
1136 }
1137
1138 $fixed = true;
1139 }
1140 }
1141 }
1142
1143 return $fixed;
1144 }
1145
1146 /**
1147 * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
1148 * compatibility.
1149 *
1150 * This is deprecated and may be removed.
1151 *
1152 * @param FormOptions $opts
1153 * @return bool True if this change was mode
1154 */
1155 private function fixBackwardsCompatibilityOptions( FormOptions $opts ) {
1156 if ( $opts['hideanons'] && $opts['hideliu'] ) {
1157 $opts->reset( 'hideanons' );
1158 if ( !$opts['hidebots'] ) {
1159 $opts->reset( 'hideliu' );
1160 $opts['hidehumans'] = 1;
1161 }
1162
1163 return true;
1164 }
1165
1166 return false;
1167 }
1168
1169 /**
1170 * Convert parameters values from true/false to 1/0
1171 * so they are not omitted by wfArrayToCgi()
1172 * Bug 36524
1173 *
1174 * @param array $params
1175 * @return array
1176 */
1177 protected function convertParamsForLink( $params ) {
1178 foreach ( $params as &$value ) {
1179 if ( $value === false ) {
1180 $value = '0';
1181 }
1182 }
1183 unset( $value );
1184 return $params;
1185 }
1186
1187 /**
1188 * Sets appropriate tables, fields, conditions, etc. depending on which filters
1189 * the user requested.
1190 *
1191 * @param array &$tables Array of tables; see IDatabase::select $table
1192 * @param array &$fields Array of fields; see IDatabase::select $vars
1193 * @param array &$conds Array of conditions; see IDatabase::select $conds
1194 * @param array &$query_options Array of query options; see IDatabase::select $options
1195 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1196 * @param FormOptions $opts
1197 */
1198 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
1199 &$join_conds, FormOptions $opts
1200 ) {
1201 $dbr = $this->getDB();
1202 $isStructuredUI = $this->isStructuredFilterUiEnabled();
1203
1204 foreach ( $this->filterGroups as $filterGroup ) {
1205 // URL parameters can be per-group, like 'userExpLevel',
1206 // or per-filter, like 'hideminor'.
1207 if ( $filterGroup->isPerGroupRequestParameter() ) {
1208 $filterGroup->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1209 $query_options, $join_conds, $opts[$filterGroup->getName()] );
1210 } else {
1211 foreach ( $filterGroup->getFilters() as $filter ) {
1212 if ( $filter->isActive( $opts, $isStructuredUI ) ) {
1213 $filter->modifyQuery( $dbr, $this, $tables, $fields, $conds,
1214 $query_options, $join_conds );
1215 }
1216 }
1217 }
1218 }
1219
1220 // Namespace filtering
1221 if ( $opts[ 'namespace' ] !== '' ) {
1222 $namespaces = explode( ';', $opts[ 'namespace' ] );
1223
1224 if ( $opts[ 'associated' ] ) {
1225 $associatedNamespaces = array_map(
1226 function ( $ns ) {
1227 return MWNamespace::getAssociated( $ns );
1228 },
1229 $namespaces
1230 );
1231 $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
1232 }
1233
1234 if ( count( $namespaces ) === 1 ) {
1235 $operator = $opts[ 'invert' ] ? '!=' : '=';
1236 $value = $dbr->addQuotes( reset( $namespaces ) );
1237 } else {
1238 $operator = $opts[ 'invert' ] ? 'NOT IN' : 'IN';
1239 sort( $namespaces );
1240 $value = '(' . $dbr->makeList( $namespaces ) . ')';
1241 }
1242 $conds[] = "rc_namespace $operator $value";
1243 }
1244 }
1245
1246 /**
1247 * Process the query
1248 *
1249 * @param array $tables Array of tables; see IDatabase::select $table
1250 * @param array $fields Array of fields; see IDatabase::select $vars
1251 * @param array $conds Array of conditions; see IDatabase::select $conds
1252 * @param array $query_options Array of query options; see IDatabase::select $options
1253 * @param array $join_conds Array of join conditions; see IDatabase::select $join_conds
1254 * @param FormOptions $opts
1255 * @return bool|ResultWrapper Result or false
1256 */
1257 protected function doMainQuery( $tables, $fields, $conds,
1258 $query_options, $join_conds, FormOptions $opts
1259 ) {
1260 $tables[] = 'recentchanges';
1261 $fields = array_merge( RecentChange::selectFields(), $fields );
1262
1263 ChangeTags::modifyDisplayQuery(
1264 $tables,
1265 $fields,
1266 $conds,
1267 $join_conds,
1268 $query_options,
1269 ''
1270 );
1271
1272 if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
1273 $opts )
1274 ) {
1275 return false;
1276 }
1277
1278 $dbr = $this->getDB();
1279
1280 return $dbr->select(
1281 $tables,
1282 $fields,
1283 $conds,
1284 __METHOD__,
1285 $query_options,
1286 $join_conds
1287 );
1288 }
1289
1290 protected function runMainQueryHook( &$tables, &$fields, &$conds,
1291 &$query_options, &$join_conds, $opts
1292 ) {
1293 return Hooks::run(
1294 'ChangesListSpecialPageQuery',
1295 [ $this->getName(), &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ]
1296 );
1297 }
1298
1299 /**
1300 * Return a IDatabase object for reading
1301 *
1302 * @return IDatabase
1303 */
1304 protected function getDB() {
1305 return wfGetDB( DB_REPLICA );
1306 }
1307
1308 /**
1309 * Send output to the OutputPage object, only called if not used feeds
1310 *
1311 * @param ResultWrapper $rows Database rows
1312 * @param FormOptions $opts
1313 */
1314 public function webOutput( $rows, $opts ) {
1315 if ( !$this->including() ) {
1316 $this->outputFeedLinks();
1317 $this->doHeader( $opts, $rows->numRows() );
1318 }
1319
1320 $this->outputChangesList( $rows, $opts );
1321 }
1322
1323 /**
1324 * Output feed links.
1325 */
1326 public function outputFeedLinks() {
1327 // nothing by default
1328 }
1329
1330 /**
1331 * Build and output the actual changes list.
1332 *
1333 * @param ResultWrapper $rows Database rows
1334 * @param FormOptions $opts
1335 */
1336 abstract public function outputChangesList( $rows, $opts );
1337
1338 /**
1339 * Set the text to be displayed above the changes
1340 *
1341 * @param FormOptions $opts
1342 * @param int $numRows Number of rows in the result to show after this header
1343 */
1344 public function doHeader( $opts, $numRows ) {
1345 $this->setTopText( $opts );
1346
1347 // @todo Lots of stuff should be done here.
1348
1349 $this->setBottomText( $opts );
1350 }
1351
1352 /**
1353 * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText()
1354 * or similar methods to print the text.
1355 *
1356 * @param FormOptions $opts
1357 */
1358 public function setTopText( FormOptions $opts ) {
1359 // nothing by default
1360 }
1361
1362 /**
1363 * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText()
1364 * or similar methods to print the text.
1365 *
1366 * @param FormOptions $opts
1367 */
1368 public function setBottomText( FormOptions $opts ) {
1369 // nothing by default
1370 }
1371
1372 /**
1373 * Get options to be displayed in a form
1374 * @todo This should handle options returned by getDefaultOptions().
1375 * @todo Not called by anything in this class (but is in subclasses), should be
1376 * called by something… doHeader() maybe?
1377 *
1378 * @param FormOptions $opts
1379 * @return array
1380 */
1381 public function getExtraOptions( $opts ) {
1382 return [];
1383 }
1384
1385 /**
1386 * Return the legend displayed within the fieldset
1387 *
1388 * @return string
1389 */
1390 public function makeLegend() {
1391 $context = $this->getContext();
1392 $user = $context->getUser();
1393 # The legend showing what the letters and stuff mean
1394 $legend = Html::openElement( 'dl' ) . "\n";
1395 # Iterates through them and gets the messages for both letter and tooltip
1396 $legendItems = $context->getConfig()->get( 'RecentChangesFlags' );
1397 if ( !( $user->useRCPatrol() || $user->useNPPatrol() ) ) {
1398 unset( $legendItems['unpatrolled'] );
1399 }
1400 foreach ( $legendItems as $key => $item ) { # generate items of the legend
1401 $label = isset( $item['legend'] ) ? $item['legend'] : $item['title'];
1402 $letter = $item['letter'];
1403 $cssClass = isset( $item['class'] ) ? $item['class'] : $key;
1404
1405 $legend .= Html::element( 'dt',
1406 [ 'class' => $cssClass ], $context->msg( $letter )->text()
1407 ) . "\n" .
1408 Html::rawElement( 'dd',
1409 [ 'class' => Sanitizer::escapeClass( 'mw-changeslist-legend-' . $key ) ],
1410 $context->msg( $label )->parse()
1411 ) . "\n";
1412 }
1413 # (+-123)
1414 $legend .= Html::rawElement( 'dt',
1415 [ 'class' => 'mw-plusminus-pos' ],
1416 $context->msg( 'recentchanges-legend-plusminus' )->parse()
1417 ) . "\n";
1418 $legend .= Html::element(
1419 'dd',
1420 [ 'class' => 'mw-changeslist-legend-plusminus' ],
1421 $context->msg( 'recentchanges-label-plusminus' )->text()
1422 ) . "\n";
1423 $legend .= Html::closeElement( 'dl' ) . "\n";
1424
1425 $legendHeading = $this->isStructuredFilterUiEnabled() ?
1426 $context->msg( 'rcfilters-legend-heading' )->parse() :
1427 $context->msg( 'recentchanges-legend-heading' )->parse();
1428
1429 # Collapsible
1430 $legend =
1431 '<div class="mw-changeslist-legend">' .
1432 $legendHeading .
1433 '<div class="mw-collapsible-content">' . $legend . '</div>' .
1434 '</div>';
1435
1436 return $legend;
1437 }
1438
1439 /**
1440 * Add page-specific modules.
1441 */
1442 protected function addModules() {
1443 $out = $this->getOutput();
1444 // Styles and behavior for the legend box (see makeLegend())
1445 $out->addModuleStyles( [
1446 'mediawiki.special.changeslist.legend',
1447 'mediawiki.special.changeslist',
1448 ] );
1449 $out->addModules( 'mediawiki.special.changeslist.legend.js' );
1450
1451 if ( $this->isStructuredFilterUiEnabled() ) {
1452 $out->addModules( 'mediawiki.rcfilters.filters.ui' );
1453 $out->addModuleStyles( 'mediawiki.rcfilters.filters.base.styles' );
1454 }
1455 }
1456
1457 protected function getGroupName() {
1458 return 'changes';
1459 }
1460
1461 /**
1462 * Filter on users' experience levels; this will not be called if nothing is
1463 * selected.
1464 *
1465 * @param string $specialPageClassName Class name of current special page
1466 * @param IContextSource $context Context, for e.g. user
1467 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
1468 * @param array &$tables Array of tables; see IDatabase::select $table
1469 * @param array &$fields Array of fields; see IDatabase::select $vars
1470 * @param array &$conds Array of conditions; see IDatabase::select $conds
1471 * @param array &$query_options Array of query options; see IDatabase::select $options
1472 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
1473 * @param array $selectedExpLevels The allowed active values, sorted
1474 * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
1475 * (optional)
1476 */
1477 public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
1478 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0
1479 ) {
1480 global $wgLearnerEdits,
1481 $wgExperiencedUserEdits,
1482 $wgLearnerMemberSince,
1483 $wgExperiencedUserMemberSince;
1484
1485 $LEVEL_COUNT = 5;
1486
1487 // If all levels are selected, don't filter
1488 if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
1489 return;
1490 }
1491
1492 // both 'registered' and 'unregistered', experience levels, if any, are included in 'registered'
1493 if (
1494 in_array( 'registered', $selectedExpLevels ) &&
1495 in_array( 'unregistered', $selectedExpLevels )
1496 ) {
1497 return;
1498 }
1499
1500 // 'registered' but not 'unregistered', experience levels, if any, are included in 'registered'
1501 if (
1502 in_array( 'registered', $selectedExpLevels ) &&
1503 !in_array( 'unregistered', $selectedExpLevels )
1504 ) {
1505 $conds[] = 'rc_user != 0';
1506 return;
1507 }
1508
1509 if ( $selectedExpLevels === [ 'unregistered' ] ) {
1510 $conds[] = 'rc_user = 0';
1511 return;
1512 }
1513
1514 $tables[] = 'user';
1515 $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
1516
1517 if ( $now === 0 ) {
1518 $now = time();
1519 }
1520 $secondsPerDay = 86400;
1521 $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
1522 $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
1523
1524 $aboveNewcomer = $dbr->makeList(
1525 [
1526 'user_editcount >= ' . intval( $wgLearnerEdits ),
1527 'user_registration <= ' . $dbr->addQuotes( $dbr->timestamp( $learnerCutoff ) ),
1528 ],
1529 IDatabase::LIST_AND
1530 );
1531
1532 $aboveLearner = $dbr->makeList(
1533 [
1534 'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
1535 'user_registration <= ' .
1536 $dbr->addQuotes( $dbr->timestamp( $experiencedUserCutoff ) ),
1537 ],
1538 IDatabase::LIST_AND
1539 );
1540
1541 $conditions = [];
1542
1543 if ( in_array( 'unregistered', $selectedExpLevels ) ) {
1544 $selectedExpLevels = array_diff( $selectedExpLevels, [ 'unregistered' ] );
1545 $conditions[] = 'rc_user = 0';
1546 }
1547
1548 if ( $selectedExpLevels === [ 'newcomer' ] ) {
1549 $conditions[] = "NOT ( $aboveNewcomer )";
1550 } elseif ( $selectedExpLevels === [ 'learner' ] ) {
1551 $conditions[] = $dbr->makeList(
1552 [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
1553 IDatabase::LIST_AND
1554 );
1555 } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
1556 $conditions[] = $aboveLearner;
1557 } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
1558 $conditions[] = "NOT ( $aboveLearner )";
1559 } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
1560 $conditions[] = $dbr->makeList(
1561 [ "NOT ( $aboveNewcomer )", $aboveLearner ],
1562 IDatabase::LIST_OR
1563 );
1564 } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
1565 $conditions[] = $aboveNewcomer;
1566 } elseif ( $selectedExpLevels === [ 'experienced', 'learner', 'newcomer' ] ) {
1567 $conditions[] = 'rc_user != 0';
1568 }
1569
1570 if ( count( $conditions ) > 1 ) {
1571 $conds[] = $dbr->makeList( $conditions, IDatabase::LIST_OR );
1572 } elseif ( count( $conditions ) === 1 ) {
1573 $conds[] = reset( $conditions );
1574 }
1575 }
1576
1577 /**
1578 * Check whether the structured filter UI is enabled
1579 *
1580 * @return bool
1581 */
1582 public function isStructuredFilterUiEnabled() {
1583 if ( $this->getRequest()->getBool( 'rcfilters' ) ) {
1584 return true;
1585 }
1586
1587 if ( $this->getConfig()->get( 'StructuredChangeFiltersShowPreference' ) ) {
1588 return !$this->getUser()->getOption( 'rcenhancedfilters-disable' );
1589 } else {
1590 return $this->getUser()->getOption( 'rcenhancedfilters' );
1591 }
1592 }
1593
1594 /**
1595 * Check whether the structured filter UI is enabled by default (regardless of
1596 * this particular user's setting)
1597 *
1598 * @return bool
1599 */
1600 public function isStructuredFilterUiEnabledByDefault() {
1601 if ( $this->getConfig()->get( 'StructuredChangeFiltersShowPreference' ) ) {
1602 return !$this->getUser()->getDefaultOption( 'rcenhancedfilters-disable' );
1603 } else {
1604 return $this->getUser()->getDefaultOption( 'rcenhancedfilters' );
1605 }
1606 }
1607
1608 abstract function getDefaultLimit();
1609
1610 /**
1611 * Get the default value of the number of days to display when loading
1612 * the result set.
1613 * Supports fractional values, and should be cast to a float.
1614 *
1615 * @return float
1616 */
1617 abstract function getDefaultDays();
1618 }