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