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