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