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