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