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