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