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