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