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