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