RCFilters: display timestamp of new changes in refresh link
[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 $now = $lang->userTimeAndDate( $timestamp, $user );
602 $wlInfo = Html::rawElement(
603 'span',
604 [
605 'class' => 'wlinfo',
606 'data-params' => json_encode( [ 'from' => $timestamp, 'fromFormatted' => $now ] ),
607 ],
608 $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
609 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
610 )->parse()
611 ) . "<br />\n";
612
613 $nondefaults = $opts->getChangedValues();
614 $cutofflinks = Html::rawElement(
615 'span',
616 [ 'class' => 'cldays cloption' ],
617 $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts )
618 );
619
620 # Spit out some control panel links
621 $links = [];
622 $namesOfDisplayedFilters = [];
623 foreach ( $this->getLegacyShowHideFilters() as $filterName => $filter ) {
624 $namesOfDisplayedFilters[] = $filterName;
625 $links[] = $this->showHideCheck(
626 $nondefaults,
627 $filter->getShowHide(),
628 $filterName,
629 $opts[ $filterName ],
630 $filter->isFeatureAvailableOnStructuredUi( $this )
631 );
632 }
633
634 $hiddenFields = $nondefaults;
635 $hiddenFields['action'] = 'submit';
636 unset( $hiddenFields['namespace'] );
637 unset( $hiddenFields['invert'] );
638 unset( $hiddenFields['associated'] );
639 unset( $hiddenFields['days'] );
640 foreach ( $namesOfDisplayedFilters as $filterName ) {
641 unset( $hiddenFields[$filterName] );
642 }
643
644 # Namespace filter and put the whole form together.
645 $form .= $wlInfo;
646 $form .= $cutofflinks;
647 $form .= Html::rawElement(
648 'span',
649 [ 'class' => 'clshowhide' ],
650 $this->msg( 'watchlist-hide' ) .
651 $this->msg( 'colon-separator' )->escaped() .
652 implode( ' ', $links )
653 );
654 $form .= "\n<br />\n";
655
656 $namespaceForm = Html::namespaceSelector(
657 [
658 'selected' => $opts['namespace'],
659 'all' => '',
660 'label' => $this->msg( 'namespace' )->text(),
661 'in-user-lang' => true,
662 ], [
663 'name' => 'namespace',
664 'id' => 'namespace',
665 'class' => 'namespaceselector',
666 ]
667 ) . "\n";
668 $hidden = $opts['namespace'] === '' ? ' mw-input-hidden' : '';
669 $namespaceForm .= '<span class="mw-input-with-label' . $hidden . '">' . Xml::checkLabel(
670 $this->msg( 'invert' )->text(),
671 'invert',
672 'nsinvert',
673 $opts['invert'],
674 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
675 ) . "</span>\n";
676 $namespaceForm .= '<span class="mw-input-with-label' . $hidden . '">' . Xml::checkLabel(
677 $this->msg( 'namespace_association' )->text(),
678 'associated',
679 'nsassociated',
680 $opts['associated'],
681 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
682 ) . "</span>\n";
683 $form .= Html::rawElement(
684 'span',
685 [ 'class' => 'namespaceForm cloption' ],
686 $namespaceForm
687 );
688
689 $form .= Xml::submitButton(
690 $this->msg( 'watchlist-submit' )->text(),
691 [ 'class' => 'cloption-submit' ]
692 ) . "\n";
693 foreach ( $hiddenFields as $key => $value ) {
694 $form .= Html::hidden( $key, $value ) . "\n";
695 }
696 $form .= Xml::closeElement( 'fieldset' ) . "\n";
697 $form .= Xml::closeElement( 'form' ) . "\n";
698
699 // Insert a placeholder for RCFilters
700 if ( $this->isStructuredFilterUiEnabled() ) {
701 $rcfilterContainer = Html::element(
702 'div',
703 // TODO: Remove deprecated rcfilters-container class
704 [ 'class' => 'rcfilters-container mw-rcfilters-container' ]
705 );
706
707 $loadingContainer = Html::rawElement(
708 'div',
709 [ 'class' => 'mw-rcfilters-spinner' ],
710 Html::element(
711 'div',
712 [ 'class' => 'mw-rcfilters-spinner-bounce' ]
713 )
714 );
715
716 // Wrap both with rcfilters-head
717 $this->getOutput()->addHTML(
718 Html::rawElement(
719 'div',
720 // TODO: Remove deprecated rcfilters-head class
721 [ 'class' => 'rcfilters-head mw-rcfilters-head' ],
722 $rcfilterContainer . $form
723 )
724 );
725
726 // Add spinner
727 $this->getOutput()->addHTML( $loadingContainer );
728 } else {
729 $this->getOutput()->addHTML( $form );
730 }
731
732 $this->setBottomText( $opts );
733 }
734
735 function cutoffselector( $options ) {
736 $selected = (float)$options['days'];
737 if ( $selected <= 0 ) {
738 $selected = $this->maxDays;
739 }
740
741 $selectedHours = round( $selected * 24 );
742
743 $hours = array_unique( array_filter( [
744 1,
745 2,
746 6,
747 12,
748 24,
749 72,
750 168,
751 24 * (float)$this->getUser()->getOption( 'watchlistdays', 0 ),
752 24 * $this->maxDays,
753 $selectedHours
754 ] ) );
755 asort( $hours );
756
757 $select = new XmlSelect( 'days', 'days', (float)( $selectedHours / 24 ) );
758
759 foreach ( $hours as $value ) {
760 if ( $value < 24 ) {
761 $name = $this->msg( 'hours' )->numParams( $value )->text();
762 } else {
763 $name = $this->msg( 'days' )->numParams( $value / 24 )->text();
764 }
765 $select->addOption( $name, (float)( $value / 24 ) );
766 }
767
768 return $select->getHTML() . "\n<br />\n";
769 }
770
771 function setTopText( FormOptions $opts ) {
772 $nondefaults = $opts->getChangedValues();
773 $form = '';
774 $user = $this->getUser();
775
776 $numItems = $this->countItems();
777 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
778
779 // Show watchlist header
780 $watchlistHeader = '';
781 if ( $numItems == 0 ) {
782 $watchlistHeader = $this->msg( 'nowatchlist' )->parse();
783 } else {
784 $watchlistHeader .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
785 if ( $this->getConfig()->get( 'EnotifWatchlist' )
786 && $user->getOption( 'enotifwatchlistpages' )
787 ) {
788 $watchlistHeader .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
789 }
790 if ( $showUpdatedMarker ) {
791 $watchlistHeader .= $this->msg(
792 $this->isStructuredFilterUiEnabled() ?
793 'rcfilters-watchlist-showupdated' :
794 'wlheader-showupdated'
795 )->parse() . "\n";
796 }
797 }
798 $form .= Html::rawElement(
799 'div',
800 [ 'class' => 'watchlistDetails' ],
801 $watchlistHeader
802 );
803
804 if ( $numItems > 0 && $showUpdatedMarker ) {
805 $form .= Xml::openElement( 'form', [ 'method' => 'post',
806 'action' => $this->getPageTitle()->getLocalURL(),
807 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
808 Xml::submitButton( $this->msg( 'enotif_reset' )->text(),
809 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
810 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
811 Html::hidden( 'reset', 'all' ) . "\n";
812 foreach ( $nondefaults as $key => $value ) {
813 $form .= Html::hidden( $key, $value ) . "\n";
814 }
815 $form .= Xml::closeElement( 'form' ) . "\n";
816 }
817
818 $this->getOutput()->addHTML( $form );
819 }
820
821 protected function showHideCheck( $options, $message, $name, $value, $inStructuredUi ) {
822 $options[$name] = 1 - (int)$value;
823
824 $attribs = [ 'class' => 'mw-input-with-label clshowhideoption cloption' ];
825 if ( $inStructuredUi ) {
826 $attribs[ 'data-feature-in-structured-ui' ] = true;
827 }
828
829 return Html::rawElement(
830 'span',
831 $attribs,
832 // not using Html::checkLabel because that would escape the contents
833 Html::check( $name, (int)$value, [ 'id' => $name ] ) . Html::rawElement(
834 'label',
835 $attribs + [ 'for' => $name ],
836 // <nowiki/> at beginning to avoid messages with "$1 ..." being parsed as pre tags
837 $this->msg( $message, '<nowiki/>' )->parse()
838 )
839 );
840 }
841
842 /**
843 * Count the number of paired items on a user's watchlist.
844 * The assumption made here is that when a subject page is watched a talk page is also watched.
845 * Hence the number of individual items is halved.
846 *
847 * @return int
848 */
849 protected function countItems() {
850 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
851 $count = $store->countWatchedItems( $this->getUser() );
852 return floor( $count / 2 );
853 }
854 }