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