Merge "WLFilters: avoid querying the DB when filters are in conflict"
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
1 <?php
2 /**
3 * Implements Special:Watchlist
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
24 use MediaWiki\MediaWikiServices;
25 use Wikimedia\Rdbms\ResultWrapper;
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29 * A special page that lists last changes made to the wiki,
30 * limited to user-defined list of titles.
31 *
32 * @ingroup SpecialPage
33 */
34 class SpecialWatchlist extends ChangesListSpecialPage {
35 private $maxDays;
36
37 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
38 parent::__construct( $page, $restriction );
39
40 $this->maxDays = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
41 }
42
43 public function doesWrites() {
44 return true;
45 }
46
47 /**
48 * Main execution point
49 *
50 * @param string $subpage
51 */
52 function execute( $subpage ) {
53 // Anons don't get a watchlist
54 $this->requireLogin( 'watchlistanontext' );
55
56 $output = $this->getOutput();
57 $request = $this->getRequest();
58 $this->addHelpLink( 'Help:Watching pages' );
59 $output->addModules( [
60 'mediawiki.special.changeslist.visitedstatus',
61 'mediawiki.special.watchlist',
62 ] );
63 $output->addModuleStyles( [ 'mediawiki.special.watchlist.styles' ] );
64
65 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
66 if ( $mode !== false ) {
67 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
68 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
69 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
70 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
71 } else {
72 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
73 }
74
75 $output->redirect( $title->getLocalURL() );
76
77 return;
78 }
79
80 $this->checkPermissions();
81
82 $user = $this->getUser();
83 $opts = $this->getOptions();
84
85 $config = $this->getConfig();
86 if ( ( $config->get( 'EnotifWatchlist' ) || $config->get( 'ShowUpdatedMarker' ) )
87 && $request->getVal( 'reset' )
88 && $request->wasPosted()
89 && $user->matchEditToken( $request->getVal( 'token' ) )
90 ) {
91 $user->clearAllNotifications();
92 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
93
94 return;
95 }
96
97 parent::execute( $subpage );
98
99 if ( $this->isStructuredFilterUiEnabled() ) {
100 $output->addJsConfigVars( 'wgStructuredChangeFiltersLiveUpdateSupported', false );
101 $output->addJsConfigVars(
102 'wgStructuredChangeFiltersSavedQueriesPreferenceName',
103 'rcfilters-wl-saved-queries'
104 );
105 }
106 }
107
108 protected function isStructuredFilterUiEnabled() {
109 return parent::isStructuredFilterUiEnabled()
110 && ( $this->getConfig()->get( 'StructuredChangeFiltersOnWatchlist' )
111 || $this->getRequest()->getBool( 'rcfilters' ) );
112 }
113
114 /**
115 * Return an array of subpages that this special page will accept.
116 *
117 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
118 * @return string[] subpages
119 */
120 public function getSubpagesForPrefixSearch() {
121 return [
122 'clear',
123 'edit',
124 'raw',
125 ];
126 }
127
128 /**
129 * @inheritDoc
130 */
131 protected function transformFilterDefinition( array $filterDefinition ) {
132 if ( isset( $filterDefinition['showHideSuffix'] ) ) {
133 $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
134 }
135
136 return $filterDefinition;
137 }
138
139 /**
140 * @inheritDoc
141 */
142 protected function registerFilters() {
143 parent::registerFilters();
144
145 $this->registerFilterGroup( new ChangesListStringOptionsFilterGroup( [
146 'name' => 'watchlistactivity',
147 'title' => 'rcfilters-filtergroup-watchlistactivity',
148 'class' => ChangesListStringOptionsFilterGroup::class,
149 'priority' => 3,
150 'isFullCoverage' => true,
151 'filters' => [
152 [
153 'name' => 'unseen',
154 'label' => 'rcfilters-filter-watchlistactivity-unseen-label',
155 'description' => 'rcfilters-filter-watchlistactivity-unseen-description',
156 'cssClassSuffix' => 'watchedunseen',
157 'isRowApplicableCallable' => function ( $ctx, $rc ) {
158 $changeTs = $rc->getAttribute( 'rc_timestamp' );
159 $lastVisitTs = $rc->getAttribute( 'wl_notificationtimestamp' );
160 return $changeTs >= $lastVisitTs;
161 },
162 ],
163 [
164 'name' => 'seen',
165 'label' => 'rcfilters-filter-watchlistactivity-seen-label',
166 'description' => 'rcfilters-filter-watchlistactivity-seen-description',
167 'cssClassSuffix' => 'watchedseen',
168 'isRowApplicableCallable' => function ( $ctx, $rc ) {
169 $changeTs = $rc->getAttribute( 'rc_timestamp' );
170 $lastVisitTs = $rc->getAttribute( 'wl_notificationtimestamp' );
171 return $changeTs < $lastVisitTs;
172 }
173 ],
174 ],
175 'default' => ChangesListStringOptionsFilterGroup::NONE,
176 'queryCallable' => function ( $specialPageClassName, $context, $dbr,
177 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedValues ) {
178 if ( $selectedValues === [ 'seen' ] ) {
179 $conds[] = 'rc_timestamp < wl_notificationtimestamp';
180 } elseif ( $selectedValues === [ 'unseen' ] ) {
181 $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
182 }
183 }
184 ] ) );
185
186 $user = $this->getUser();
187
188 $significance = $this->getFilterGroup( 'significance' );
189 $hideMinor = $significance->getFilter( 'hideminor' );
190 $hideMinor->setDefault( $user->getBoolOption( 'watchlisthideminor' ) );
191
192 $automated = $this->getFilterGroup( 'automated' );
193 $hideBots = $automated->getFilter( 'hidebots' );
194 $hideBots->setDefault( $user->getBoolOption( 'watchlisthidebots' ) );
195
196 $registration = $this->getFilterGroup( 'registration' );
197 $hideAnons = $registration->getFilter( 'hideanons' );
198 $hideAnons->setDefault( $user->getBoolOption( 'watchlisthideanons' ) );
199 $hideLiu = $registration->getFilter( 'hideliu' );
200 $hideLiu->setDefault( $user->getBoolOption( 'watchlisthideliu' ) );
201
202 $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
203 if ( $reviewStatus !== null ) {
204 // Conditional on feature being available and rights
205 $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
206 $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
207 }
208
209 $authorship = $this->getFilterGroup( 'authorship' );
210 $hideMyself = $authorship->getFilter( 'hidemyself' );
211 $hideMyself->setDefault( $user->getBoolOption( 'watchlisthideown' ) );
212
213 $changeType = $this->getFilterGroup( 'changeType' );
214 $hideCategorization = $changeType->getFilter( 'hidecategorization' );
215 if ( $hideCategorization !== null ) {
216 // Conditional on feature being available
217 $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
218 }
219 }
220
221 /**
222 * Get a FormOptions object containing the default options
223 *
224 * @return FormOptions
225 */
226 public function getDefaultOptions() {
227 $opts = parent::getDefaultOptions();
228 $user = $this->getUser();
229
230 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
231 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
232 $opts->add( 'limit', $user->getIntOption( 'wllimit' ), FormOptions::INT );
233
234 return $opts;
235 }
236
237 public function validateOptions( FormOptions $opts ) {
238 $opts->validateBounds( 'days', 0, $this->maxDays );
239 $opts->validateIntBounds( 'limit', 0, 5000 );
240 parent::validateOptions( $opts );
241 }
242
243 /**
244 * Get all custom filters
245 *
246 * @return array Map of filter URL param names to properties (msg/default)
247 */
248 protected function getCustomFilters() {
249 if ( $this->customFilters === null ) {
250 $this->customFilters = parent::getCustomFilters();
251 Hooks::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters ], '1.23' );
252 }
253
254 return $this->customFilters;
255 }
256
257 /**
258 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
259 *
260 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
261 * to the current ones.
262 *
263 * @param FormOptions $opts
264 * @return FormOptions
265 */
266 protected function fetchOptionsFromRequest( $opts ) {
267 static $compatibilityMap = [
268 'hideMinor' => 'hideminor',
269 'hideBots' => 'hidebots',
270 'hideAnons' => 'hideanons',
271 'hideLiu' => 'hideliu',
272 'hidePatrolled' => 'hidepatrolled',
273 'hideOwn' => 'hidemyself',
274 ];
275
276 $params = $this->getRequest()->getValues();
277 foreach ( $compatibilityMap as $from => $to ) {
278 if ( isset( $params[$from] ) ) {
279 $params[$to] = $params[$from];
280 unset( $params[$from] );
281 }
282 }
283
284 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
285 $allBooleansFalse = [];
286
287 // If the user submitted the form, start with a baseline of "all
288 // booleans are false", then change the ones they checked. This
289 // means we ignore the defaults.
290
291 // This is how we handle the fact that HTML forms don't submit
292 // unchecked boxes.
293 foreach ( $this->filterGroups as $filterGroup ) {
294 if ( $filterGroup instanceof ChangesListBooleanFilterGroup ) {
295 foreach ( $filterGroup->getFilters() as $filter ) {
296 $allBooleansFalse[$filter->getName()] = false;
297 }
298 }
299 }
300
301 $params = $params + $allBooleansFalse;
302 }
303
304 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
305 // methods defined on WebRequest and removing this dependency would cause some code duplication.
306 $request = new DerivativeRequest( $this->getRequest(), $params );
307 $opts->fetchValuesFromRequest( $request );
308
309 return $opts;
310 }
311
312 /**
313 * @inheritDoc
314 */
315 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
316 &$join_conds, FormOptions $opts
317 ) {
318 $dbr = $this->getDB();
319 parent::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
320 $opts );
321
322 // Calculate cutoff
323 if ( $opts['days'] > 0 ) {
324 $conds[] = 'rc_timestamp > ' .
325 $dbr->addQuotes( $dbr->timestamp( time() - $opts['days'] * 3600 * 24 ) );
326 }
327 }
328
329 /**
330 * @inheritDoc
331 */
332 protected function doMainQuery( $tables, $fields, $conds, $query_options,
333 $join_conds, FormOptions $opts
334 ) {
335 $dbr = $this->getDB();
336 $user = $this->getUser();
337
338 # Toggle watchlist content (all recent edits or just the latest)
339 if ( $opts['extended'] ) {
340 $usePage = false;
341 } else {
342 # Top log Ids for a page are not stored
343 $nonRevisionTypes = [ RC_LOG ];
344 Hooks::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
345 if ( $nonRevisionTypes ) {
346 $conds[] = $dbr->makeList(
347 [
348 'rc_this_oldid=page_latest',
349 'rc_type' => $nonRevisionTypes,
350 ],
351 LIST_OR
352 );
353 }
354 $usePage = true;
355 }
356
357 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
358 $fields = array_merge( RecentChange::selectFields(), $fields );
359
360 $query_options = array_merge( [
361 'ORDER BY' => 'rc_timestamp DESC',
362 'LIMIT' => $opts['limit']
363 ], $query_options );
364 $join_conds = array_merge(
365 [
366 'watchlist' => [
367 'INNER JOIN',
368 [
369 'wl_user' => $user->getId(),
370 'wl_namespace=rc_namespace',
371 'wl_title=rc_title'
372 ],
373 ],
374 ],
375 $join_conds
376 );
377
378 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
379 $fields[] = 'wl_notificationtimestamp';
380 }
381
382 $rollbacker = $user->isAllowed( 'rollback' );
383 if ( $usePage || $rollbacker ) {
384 $tables[] = 'page';
385 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
386 if ( $rollbacker ) {
387 $fields[] = 'page_latest';
388 }
389 }
390
391 // Log entries with DELETED_ACTION must not show up unless the user has
392 // the necessary rights.
393 if ( !$user->isAllowed( 'deletedhistory' ) ) {
394 $bitmask = LogPage::DELETED_ACTION;
395 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
396 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
397 } else {
398 $bitmask = 0;
399 }
400 if ( $bitmask ) {
401 $conds[] = $dbr->makeList( [
402 'rc_type != ' . RC_LOG,
403 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
404 ], LIST_OR );
405 }
406
407 ChangeTags::modifyDisplayQuery(
408 $tables,
409 $fields,
410 $conds,
411 $join_conds,
412 $query_options,
413 ''
414 );
415
416 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
417
418 if ( $this->areFiltersInConflict() ) {
419 return false;
420 }
421
422 return $dbr->select(
423 $tables,
424 $fields,
425 $conds,
426 __METHOD__,
427 $query_options,
428 $join_conds
429 );
430 }
431
432 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
433 &$join_conds, $opts
434 ) {
435 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
436 && Hooks::run(
437 'SpecialWatchlistQuery',
438 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
439 '1.23'
440 );
441 }
442
443 /**
444 * Return a IDatabase object for reading
445 *
446 * @return IDatabase
447 */
448 protected function getDB() {
449 return wfGetDB( DB_REPLICA, 'watchlist' );
450 }
451
452 /**
453 * Output feed links.
454 */
455 public function outputFeedLinks() {
456 $user = $this->getUser();
457 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
458 if ( $wlToken ) {
459 $this->addFeedLinks( [
460 'action' => 'feedwatchlist',
461 'allrev' => 1,
462 'wlowner' => $user->getName(),
463 'wltoken' => $wlToken,
464 ] );
465 }
466 }
467
468 /**
469 * Build and output the actual changes list.
470 *
471 * @param ResultWrapper $rows Database rows
472 * @param FormOptions $opts
473 */
474 public function outputChangesList( $rows, $opts ) {
475 $dbr = $this->getDB();
476 $user = $this->getUser();
477 $output = $this->getOutput();
478
479 # Show a message about replica DB lag, if applicable
480 $lag = wfGetLB()->safeGetLag( $dbr );
481 if ( $lag > 0 ) {
482 $output->showLagWarning( $lag );
483 }
484
485 # If no rows to display, show message before try to render the list
486 if ( $rows->numRows() == 0 ) {
487 $output->wrapWikiMsg(
488 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
489 );
490 return;
491 }
492
493 $dbr->dataSeek( $rows, 0 );
494
495 $list = ChangesList::newFromContext( $this->getContext(), $this->filterGroups );
496 $list->setWatchlistDivs();
497 $list->initChangesListRows( $rows );
498 if ( $user->getOption( 'watchlistunwatchlinks' ) ) {
499 $list->setChangeLinePrefixer( function ( RecentChange $rc, ChangesList $cl, $grouped ) {
500 // Don't show unwatch link if the line is a grouped log entry using EnhancedChangesList,
501 // since EnhancedChangesList groups log entries by performer rather than by target article
502 if ( $rc->mAttribs['rc_type'] == RC_LOG && $cl instanceof EnhancedChangesList &&
503 $grouped ) {
504 return '';
505 } else {
506 return $this->getLinkRenderer()
507 ->makeKnownLink( $rc->getTitle(),
508 $this->msg( 'watchlist-unwatch' )->text(), [
509 'class' => 'mw-unwatch-link',
510 'title' => $this->msg( 'tooltip-ca-unwatch' )->text()
511 ], [ 'action' => 'unwatch' ] ) . '&#160;';
512 }
513 } );
514 }
515 $dbr->dataSeek( $rows, 0 );
516
517 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
518 && $user->getOption( 'shownumberswatching' )
519 ) {
520 $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
521 }
522
523 $s = $list->beginRecentChangesList();
524
525 if ( $this->isStructuredFilterUiEnabled() ) {
526 $s .= $this->makeLegend();
527 }
528
529 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
530 $counter = 1;
531 foreach ( $rows as $obj ) {
532 # Make RC entry
533 $rc = RecentChange::newFromRow( $obj );
534
535 # Skip CatWatch entries for hidden cats based on user preference
536 if (
537 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE &&
538 !$userShowHiddenCats &&
539 $rc->getParam( 'hidden-cat' )
540 ) {
541 continue;
542 }
543
544 $rc->counter = $counter++;
545
546 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
547 $updated = $obj->wl_notificationtimestamp;
548 } else {
549 $updated = false;
550 }
551
552 if ( isset( $watchedItemStore ) ) {
553 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace, $obj->rc_title );
554 $rc->numberofWatchingusers = $watchedItemStore->countWatchers( $rcTitleValue );
555 } else {
556 $rc->numberofWatchingusers = 0;
557 }
558
559 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
560 if ( $changeLine !== false ) {
561 $s .= $changeLine;
562 }
563 }
564 $s .= $list->endRecentChangesList();
565
566 $output->addHTML( $s );
567 }
568
569 /**
570 * Set the text to be displayed above the changes
571 *
572 * @param FormOptions $opts
573 * @param int $numRows Number of rows in the result to show after this header
574 */
575 public function doHeader( $opts, $numRows ) {
576 $user = $this->getUser();
577 $out = $this->getOutput();
578
579 $out->addSubtitle(
580 $this->msg( 'watchlistfor2', $user->getName() )
581 ->rawParams( SpecialEditWatchlist::buildTools(
582 $this->getLanguage(),
583 $this->getLinkRenderer()
584 ) )
585 );
586
587 $this->setTopText( $opts );
588
589 $form = '';
590
591 $form .= Xml::openElement( 'form', [
592 'method' => 'get',
593 'action' => wfScript(),
594 'id' => 'mw-watchlist-form'
595 ] );
596 $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
597 $form .= Xml::fieldset(
598 $this->msg( 'watchlist-options' )->text(),
599 false,
600 [ 'id' => 'mw-watchlist-options', 'class' => 'cloptions' ]
601 );
602
603 if ( !$this->isStructuredFilterUiEnabled() ) {
604 $form .= $this->makeLegend();
605 }
606
607 $lang = $this->getLanguage();
608 if ( $opts['days'] > 0 ) {
609 $days = $opts['days'];
610 } else {
611 $days = $this->maxDays;
612 }
613 $timestamp = wfTimestampNow();
614 $wlInfo = Html::rawElement(
615 'span',
616 [ 'class' => 'wlinfo' ],
617 $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
618 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
619 )->parse()
620 ) . "<br />\n";
621
622 $nondefaults = $opts->getChangedValues();
623 $cutofflinks = Html::rawElement(
624 'span',
625 [ 'class' => 'cldays cloption' ],
626 $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts )
627 );
628
629 # Spit out some control panel links
630 $links = [];
631 $namesOfDisplayedFilters = [];
632 foreach ( $this->getFilterGroups() as $groupName => $group ) {
633 if ( !$group->isPerGroupRequestParameter() ) {
634 foreach ( $group->getFilters() as $filterName => $filter ) {
635 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
636 $namesOfDisplayedFilters[] = $filterName;
637 $links[] = $this->showHideCheck(
638 $nondefaults,
639 $filter->getShowHide(),
640 $filterName,
641 $opts[$filterName],
642 $filter->isFeatureAvailableOnStructuredUi( $this )
643 );
644 }
645 }
646 }
647 }
648
649 $hiddenFields = $nondefaults;
650 $hiddenFields['action'] = 'submit';
651 unset( $hiddenFields['namespace'] );
652 unset( $hiddenFields['invert'] );
653 unset( $hiddenFields['associated'] );
654 unset( $hiddenFields['days'] );
655 foreach ( $namesOfDisplayedFilters as $filterName ) {
656 unset( $hiddenFields[$filterName] );
657 }
658
659 # Namespace filter and put the whole form together.
660 $form .= $wlInfo;
661 $form .= $cutofflinks;
662 $form .= Html::rawElement(
663 'span',
664 [ 'class' => 'clshowhide' ],
665 $this->msg( 'watchlist-hide' ) .
666 $this->msg( 'colon-separator' )->escaped() .
667 implode( ' ', $links )
668 );
669 $form .= "\n<br />\n";
670
671 $namespaceForm = Html::namespaceSelector(
672 [
673 'selected' => $opts['namespace'],
674 'all' => '',
675 'label' => $this->msg( 'namespace' )->text()
676 ], [
677 'name' => 'namespace',
678 'id' => 'namespace',
679 'class' => 'namespaceselector',
680 ]
681 ) . "\n";
682 $namespaceForm .= '<span class="mw-input-with-label">' . Xml::checkLabel(
683 $this->msg( 'invert' )->text(),
684 'invert',
685 'nsinvert',
686 $opts['invert'],
687 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
688 ) . "</span>\n";
689 $namespaceForm .= '<span class="mw-input-with-label">' . Xml::checkLabel(
690 $this->msg( 'namespace_association' )->text(),
691 'associated',
692 'nsassociated',
693 $opts['associated'],
694 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
695 ) . "</span>\n";
696 $form .= Html::rawElement(
697 'span',
698 [ 'class' => 'namespaceForm cloption' ],
699 $namespaceForm
700 );
701
702 $form .= Xml::submitButton(
703 $this->msg( 'watchlist-submit' )->text(),
704 [ 'class' => 'cloption-submit' ]
705 ) . "\n";
706 foreach ( $hiddenFields as $key => $value ) {
707 $form .= Html::hidden( $key, $value ) . "\n";
708 }
709 $form .= Xml::closeElement( 'fieldset' ) . "\n";
710 $form .= Xml::closeElement( 'form' ) . "\n";
711
712 // Insert a placeholder for RCFilters
713 if ( $this->isStructuredFilterUiEnabled() ) {
714 $rcfilterContainer = Html::element(
715 'div',
716 [ 'class' => 'rcfilters-container' ]
717 );
718
719 $loadingContainer = Html::rawElement(
720 'div',
721 [ 'class' => 'rcfilters-spinner' ],
722 Html::element(
723 'div',
724 [ 'class' => 'rcfilters-spinner-bounce' ]
725 )
726 );
727
728 // Wrap both with rcfilters-head
729 $this->getOutput()->addHTML(
730 Html::rawElement(
731 'div',
732 [ 'class' => 'rcfilters-head' ],
733 $rcfilterContainer . $form
734 )
735 );
736
737 // Add spinner
738 $this->getOutput()->addHTML( $loadingContainer );
739 } else {
740 $this->getOutput()->addHTML( $form );
741 }
742
743 $this->setBottomText( $opts );
744 }
745
746 function cutoffselector( $options ) {
747 // Cast everything to strings immediately, so that we know all of the values have the same
748 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
749 // default string representation, for example, and would confuse comparisons.
750
751 // Misleadingly, the 'days' option supports hours too.
752 $days = array_map( 'strval', [ 1 / 24, 2 / 24, 6 / 24, 12 / 24, 1, 3, 7 ] );
753
754 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
755 // add the user preference, if it isn't available already
756 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
757 $days[] = $userWatchlistOption;
758 }
759
760 $maxDays = (string)$this->maxDays;
761 // add the maximum possible value, if it isn't available already
762 if ( !in_array( $maxDays, $days ) ) {
763 $days[] = $maxDays;
764 }
765
766 $selected = (string)$options['days'];
767 if ( $selected <= 0 ) {
768 $selected = $maxDays;
769 }
770
771 // add the currently selected value, if it isn't available already
772 if ( !in_array( $selected, $days ) ) {
773 $days[] = $selected;
774 }
775
776 $select = new XmlSelect( 'days', 'days', $selected );
777
778 asort( $days );
779 foreach ( $days as $value ) {
780 if ( $value < 1 ) {
781 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
782 } else {
783 $name = $this->msg( 'days' )->numParams( $value )->text();
784 }
785 $select->addOption( $name, $value );
786 }
787
788 return $select->getHTML() . "\n<br />\n";
789 }
790
791 function setTopText( FormOptions $opts ) {
792 $nondefaults = $opts->getChangedValues();
793 $form = '';
794 $user = $this->getUser();
795
796 $numItems = $this->countItems();
797 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
798
799 // Show watchlist header
800 $form .= "<p>";
801 if ( $numItems == 0 ) {
802 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
803 } else {
804 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
805 if ( $this->getConfig()->get( 'EnotifWatchlist' )
806 && $user->getOption( 'enotifwatchlistpages' )
807 ) {
808 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
809 }
810 if ( $showUpdatedMarker ) {
811 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
812 }
813 }
814 $form .= "</p>";
815
816 if ( $numItems > 0 && $showUpdatedMarker ) {
817 $form .= Xml::openElement( 'form', [ 'method' => 'post',
818 'action' => $this->getPageTitle()->getLocalURL(),
819 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
820 Xml::submitButton( $this->msg( 'enotif_reset' )->text(),
821 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
822 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
823 Html::hidden( 'reset', 'all' ) . "\n";
824 foreach ( $nondefaults as $key => $value ) {
825 $form .= Html::hidden( $key, $value ) . "\n";
826 }
827 $form .= Xml::closeElement( 'form' ) . "\n";
828 }
829
830 $this->getOutput()->addHTML( $form );
831 }
832
833 protected function showHideCheck( $options, $message, $name, $value, $inStructuredUi ) {
834 $options[$name] = 1 - (int)$value;
835
836 $attribs = [ 'class' => 'mw-input-with-label clshowhideoption cloption' ];
837 if ( $inStructuredUi ) {
838 $attribs[ 'data-feature-in-structured-ui' ] = true;
839 }
840
841 return Html::rawElement(
842 'span',
843 $attribs,
844 Xml::checkLabel(
845 $this->msg( $message, '' )->text(),
846 $name,
847 $name,
848 (int)$value
849 )
850 );
851 }
852
853 /**
854 * Count the number of paired items on a user's watchlist.
855 * The assumption made here is that when a subject page is watched a talk page is also watched.
856 * Hence the number of individual items is halved.
857 *
858 * @return int
859 */
860 protected function countItems() {
861 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
862 $count = $store->countWatchedItems( $this->getUser() );
863 return floor( $count / 2 );
864 }
865 }