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