Merge "Change 'editfont' default preference to 'monospace'"
[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 return $dbr->select(
419 $tables,
420 $fields,
421 $conds,
422 __METHOD__,
423 $query_options,
424 $join_conds
425 );
426 }
427
428 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
429 &$join_conds, $opts
430 ) {
431 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
432 && Hooks::run(
433 'SpecialWatchlistQuery',
434 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
435 '1.23'
436 );
437 }
438
439 /**
440 * Return a IDatabase object for reading
441 *
442 * @return IDatabase
443 */
444 protected function getDB() {
445 return wfGetDB( DB_REPLICA, 'watchlist' );
446 }
447
448 /**
449 * Output feed links.
450 */
451 public function outputFeedLinks() {
452 $user = $this->getUser();
453 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
454 if ( $wlToken ) {
455 $this->addFeedLinks( [
456 'action' => 'feedwatchlist',
457 'allrev' => 1,
458 'wlowner' => $user->getName(),
459 'wltoken' => $wlToken,
460 ] );
461 }
462 }
463
464 /**
465 * Build and output the actual changes list.
466 *
467 * @param ResultWrapper $rows Database rows
468 * @param FormOptions $opts
469 */
470 public function outputChangesList( $rows, $opts ) {
471 $dbr = $this->getDB();
472 $user = $this->getUser();
473 $output = $this->getOutput();
474
475 # Show a message about replica DB lag, if applicable
476 $lag = wfGetLB()->safeGetLag( $dbr );
477 if ( $lag > 0 ) {
478 $output->showLagWarning( $lag );
479 }
480
481 # If no rows to display, show message before try to render the list
482 if ( $rows->numRows() == 0 ) {
483 $output->wrapWikiMsg(
484 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
485 );
486 return;
487 }
488
489 $dbr->dataSeek( $rows, 0 );
490
491 $list = ChangesList::newFromContext( $this->getContext(), $this->filterGroups );
492 $list->setWatchlistDivs();
493 $list->initChangesListRows( $rows );
494 if ( $user->getOption( 'watchlistunwatchlinks' ) ) {
495 $list->setChangeLinePrefixer( function ( RecentChange $rc, ChangesList $cl, $grouped ) {
496 // Don't show unwatch link if the line is a grouped log entry using EnhancedChangesList,
497 // since EnhancedChangesList groups log entries by performer rather than by target article
498 if ( $rc->mAttribs['rc_type'] == RC_LOG && $cl instanceof EnhancedChangesList &&
499 $grouped ) {
500 return '';
501 } else {
502 return $this->getLinkRenderer()
503 ->makeKnownLink( $rc->getTitle(),
504 $this->msg( 'watchlist-unwatch' )->text(), [
505 'class' => 'mw-unwatch-link',
506 'title' => $this->msg( 'tooltip-ca-unwatch' )->text()
507 ], [ 'action' => 'unwatch' ] ) . '&#160;';
508 }
509 } );
510 }
511 $dbr->dataSeek( $rows, 0 );
512
513 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
514 && $user->getOption( 'shownumberswatching' )
515 ) {
516 $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
517 }
518
519 $s = $list->beginRecentChangesList();
520
521 if ( $this->isStructuredFilterUiEnabled() ) {
522 $s .= $this->makeLegend();
523 }
524
525 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
526 $counter = 1;
527 foreach ( $rows as $obj ) {
528 # Make RC entry
529 $rc = RecentChange::newFromRow( $obj );
530
531 # Skip CatWatch entries for hidden cats based on user preference
532 if (
533 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE &&
534 !$userShowHiddenCats &&
535 $rc->getParam( 'hidden-cat' )
536 ) {
537 continue;
538 }
539
540 $rc->counter = $counter++;
541
542 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
543 $updated = $obj->wl_notificationtimestamp;
544 } else {
545 $updated = false;
546 }
547
548 if ( isset( $watchedItemStore ) ) {
549 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace, $obj->rc_title );
550 $rc->numberofWatchingusers = $watchedItemStore->countWatchers( $rcTitleValue );
551 } else {
552 $rc->numberofWatchingusers = 0;
553 }
554
555 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
556 if ( $changeLine !== false ) {
557 $s .= $changeLine;
558 }
559 }
560 $s .= $list->endRecentChangesList();
561
562 $output->addHTML( $s );
563 }
564
565 /**
566 * Set the text to be displayed above the changes
567 *
568 * @param FormOptions $opts
569 * @param int $numRows Number of rows in the result to show after this header
570 */
571 public function doHeader( $opts, $numRows ) {
572 $user = $this->getUser();
573 $out = $this->getOutput();
574
575 $out->addSubtitle(
576 $this->msg( 'watchlistfor2', $user->getName() )
577 ->rawParams( SpecialEditWatchlist::buildTools(
578 $this->getLanguage(),
579 $this->getLinkRenderer()
580 ) )
581 );
582
583 $this->setTopText( $opts );
584
585 $form = '';
586
587 $form .= Xml::openElement( 'form', [
588 'method' => 'get',
589 'action' => wfScript(),
590 'id' => 'mw-watchlist-form'
591 ] );
592 $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
593 $form .= Xml::fieldset(
594 $this->msg( 'watchlist-options' )->text(),
595 false,
596 [ 'id' => 'mw-watchlist-options', 'class' => 'cloptions' ]
597 );
598
599 if ( !$this->isStructuredFilterUiEnabled() ) {
600 $form .= $this->makeLegend();
601 }
602
603 $lang = $this->getLanguage();
604 if ( $opts['days'] > 0 ) {
605 $days = $opts['days'];
606 } else {
607 $days = $this->maxDays;
608 }
609 $timestamp = wfTimestampNow();
610 $wlInfo = Html::rawElement(
611 'span',
612 [ 'class' => 'wlinfo' ],
613 $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
614 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
615 )->parse()
616 ) . "<br />\n";
617
618 $nondefaults = $opts->getChangedValues();
619 $cutofflinks = Html::rawElement(
620 'span',
621 [ 'class' => 'cldays cloption' ],
622 $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts )
623 );
624
625 # Spit out some control panel links
626 $links = [];
627 $namesOfDisplayedFilters = [];
628 foreach ( $this->getFilterGroups() as $groupName => $group ) {
629 if ( !$group->isPerGroupRequestParameter() ) {
630 foreach ( $group->getFilters() as $filterName => $filter ) {
631 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
632 $namesOfDisplayedFilters[] = $filterName;
633 $links[] = $this->showHideCheck(
634 $nondefaults,
635 $filter->getShowHide(),
636 $filterName,
637 $opts[$filterName],
638 $filter->isFeatureAvailableOnStructuredUi( $this )
639 );
640 }
641 }
642 }
643 }
644
645 $hiddenFields = $nondefaults;
646 $hiddenFields['action'] = 'submit';
647 unset( $hiddenFields['namespace'] );
648 unset( $hiddenFields['invert'] );
649 unset( $hiddenFields['associated'] );
650 unset( $hiddenFields['days'] );
651 foreach ( $namesOfDisplayedFilters as $filterName ) {
652 unset( $hiddenFields[$filterName] );
653 }
654
655 # Namespace filter and put the whole form together.
656 $form .= $wlInfo;
657 $form .= $cutofflinks;
658 $form .= Html::rawElement(
659 'span',
660 [ 'class' => 'clshowhide' ],
661 $this->msg( 'watchlist-hide' ) .
662 $this->msg( 'colon-separator' )->escaped() .
663 implode( ' ', $links )
664 );
665 $form .= "\n<br />\n";
666
667 $namespaceForm = Html::namespaceSelector(
668 [
669 'selected' => $opts['namespace'],
670 'all' => '',
671 'label' => $this->msg( 'namespace' )->text()
672 ], [
673 'name' => 'namespace',
674 'id' => 'namespace',
675 'class' => 'namespaceselector',
676 ]
677 ) . "\n";
678 $namespaceForm .= '<span class="mw-input-with-label">' . Xml::checkLabel(
679 $this->msg( 'invert' )->text(),
680 'invert',
681 'nsinvert',
682 $opts['invert'],
683 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
684 ) . "</span>\n";
685 $namespaceForm .= '<span class="mw-input-with-label">' . Xml::checkLabel(
686 $this->msg( 'namespace_association' )->text(),
687 'associated',
688 'nsassociated',
689 $opts['associated'],
690 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
691 ) . "</span>\n";
692 $form .= Html::rawElement(
693 'span',
694 [ 'class' => 'namespaceForm cloption' ],
695 $namespaceForm
696 );
697
698 $form .= Xml::submitButton(
699 $this->msg( 'watchlist-submit' )->text(),
700 [ 'class' => 'cloption-submit' ]
701 ) . "\n";
702 foreach ( $hiddenFields as $key => $value ) {
703 $form .= Html::hidden( $key, $value ) . "\n";
704 }
705 $form .= Xml::closeElement( 'fieldset' ) . "\n";
706 $form .= Xml::closeElement( 'form' ) . "\n";
707
708 // Insert a placeholder for RCFilters
709 if ( $this->isStructuredFilterUiEnabled() ) {
710 $rcfilterContainer = Html::element(
711 'div',
712 [ 'class' => 'rcfilters-container' ]
713 );
714
715 $loadingContainer = Html::rawElement(
716 'div',
717 [ 'class' => 'rcfilters-spinner' ],
718 Html::element(
719 'div',
720 [ 'class' => 'rcfilters-spinner-bounce' ]
721 )
722 );
723
724 // Wrap both with rcfilters-head
725 $this->getOutput()->addHTML(
726 Html::rawElement(
727 'div',
728 [ 'class' => 'rcfilters-head' ],
729 $rcfilterContainer . $form
730 )
731 );
732
733 // Add spinner
734 $this->getOutput()->addHTML( $loadingContainer );
735 } else {
736 $this->getOutput()->addHTML( $form );
737 }
738
739 $this->setBottomText( $opts );
740 }
741
742 function cutoffselector( $options ) {
743 // Cast everything to strings immediately, so that we know all of the values have the same
744 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
745 // default string representation, for example, and would confuse comparisons.
746
747 // Misleadingly, the 'days' option supports hours too.
748 $days = array_map( 'strval', [ 1 / 24, 2 / 24, 6 / 24, 12 / 24, 1, 3, 7 ] );
749
750 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
751 // add the user preference, if it isn't available already
752 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
753 $days[] = $userWatchlistOption;
754 }
755
756 $maxDays = (string)$this->maxDays;
757 // add the maximum possible value, if it isn't available already
758 if ( !in_array( $maxDays, $days ) ) {
759 $days[] = $maxDays;
760 }
761
762 $selected = (string)$options['days'];
763 if ( $selected <= 0 ) {
764 $selected = $maxDays;
765 }
766
767 // add the currently selected value, if it isn't available already
768 if ( !in_array( $selected, $days ) ) {
769 $days[] = $selected;
770 }
771
772 $select = new XmlSelect( 'days', 'days', $selected );
773
774 asort( $days );
775 foreach ( $days as $value ) {
776 if ( $value < 1 ) {
777 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
778 } else {
779 $name = $this->msg( 'days' )->numParams( $value )->text();
780 }
781 $select->addOption( $name, $value );
782 }
783
784 return $select->getHTML() . "\n<br />\n";
785 }
786
787 function setTopText( FormOptions $opts ) {
788 $nondefaults = $opts->getChangedValues();
789 $form = '';
790 $user = $this->getUser();
791
792 $numItems = $this->countItems();
793 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
794
795 // Show watchlist header
796 $form .= "<p>";
797 if ( $numItems == 0 ) {
798 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
799 } else {
800 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
801 if ( $this->getConfig()->get( 'EnotifWatchlist' )
802 && $user->getOption( 'enotifwatchlistpages' )
803 ) {
804 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
805 }
806 if ( $showUpdatedMarker ) {
807 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
808 }
809 }
810 $form .= "</p>";
811
812 if ( $numItems > 0 && $showUpdatedMarker ) {
813 $form .= Xml::openElement( 'form', [ 'method' => 'post',
814 'action' => $this->getPageTitle()->getLocalURL(),
815 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
816 Xml::submitButton( $this->msg( 'enotif_reset' )->text(),
817 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
818 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
819 Html::hidden( 'reset', 'all' ) . "\n";
820 foreach ( $nondefaults as $key => $value ) {
821 $form .= Html::hidden( $key, $value ) . "\n";
822 }
823 $form .= Xml::closeElement( 'form' ) . "\n";
824 }
825
826 $this->getOutput()->addHTML( $form );
827 }
828
829 protected function showHideCheck( $options, $message, $name, $value, $inStructuredUi ) {
830 $options[$name] = 1 - (int)$value;
831
832 $attribs = [ 'class' => 'mw-input-with-label clshowhideoption cloption' ];
833 if ( $inStructuredUi ) {
834 $attribs[ 'data-feature-in-structured-ui' ] = true;
835 }
836
837 return Html::rawElement(
838 'span',
839 $attribs,
840 Xml::checkLabel(
841 $this->msg( $message, '' )->text(),
842 $name,
843 $name,
844 (int)$value
845 )
846 );
847 }
848
849 /**
850 * Count the number of paired items on a user's watchlist.
851 * The assumption made here is that when a subject page is watched a talk page is also watched.
852 * Hence the number of individual items is halved.
853 *
854 * @return int
855 */
856 protected function countItems() {
857 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
858 $count = $store->countWatchedItems( $this->getUser() );
859 return floor( $count / 2 );
860 }
861 }