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