Merge "maintenance: Fix bogus IDatabase method calls in sqlite.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 ( !MediaWikiServices::getInstance()
386 ->getPermissionManager()
387 ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
388 ) {
389 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
390 } else {
391 $bitmask = 0;
392 }
393 if ( $bitmask ) {
394 $conds[] = $dbr->makeList( [
395 'rc_type != ' . RC_LOG,
396 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
397 ], LIST_OR );
398 }
399
400 $tagFilter = $opts['tagfilter'] ? explode( '|', $opts['tagfilter'] ) : [];
401 ChangeTags::modifyDisplayQuery(
402 $tables,
403 $fields,
404 $conds,
405 $join_conds,
406 $query_options,
407 $tagFilter
408 );
409
410 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
411
412 if ( $this->areFiltersInConflict() ) {
413 return false;
414 }
415
416 $orderByAndLimit = [
417 'ORDER BY' => 'rc_timestamp DESC',
418 'LIMIT' => $opts['limit']
419 ];
420 if ( in_array( 'DISTINCT', $query_options ) ) {
421 // ChangeTags::modifyDisplayQuery() adds DISTINCT when filtering on multiple tags.
422 // In order to prevent DISTINCT from causing query performance problems,
423 // we have to GROUP BY the primary key. This in turn requires us to add
424 // the primary key to the end of the ORDER BY, and the old ORDER BY to the
425 // start of the GROUP BY
426 $orderByAndLimit['ORDER BY'] = 'rc_timestamp DESC, rc_id DESC';
427 $orderByAndLimit['GROUP BY'] = 'rc_timestamp, rc_id';
428 }
429 // array_merge() is used intentionally here so that hooks can, should
430 // they so desire, override the ORDER BY / LIMIT condition(s)
431 $query_options = array_merge( $orderByAndLimit, $query_options );
432
433 return $dbr->select(
434 $tables,
435 $fields,
436 $conds,
437 __METHOD__,
438 $query_options,
439 $join_conds
440 );
441 }
442
443 /**
444 * Return a IDatabase object for reading
445 *
446 * @return IDatabase
447 */
448 protected function getDB() {
449 return wfGetDB( DB_REPLICA, 'watchlist' );
450 }
451
452 /**
453 * Output feed links.
454 */
455 public function outputFeedLinks() {
456 $user = $this->getUser();
457 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
458 if ( $wlToken ) {
459 $this->addFeedLinks( [
460 'action' => 'feedwatchlist',
461 'allrev' => 1,
462 'wlowner' => $user->getName(),
463 'wltoken' => $wlToken,
464 ] );
465 }
466 }
467
468 /**
469 * Build and output the actual changes list.
470 *
471 * @param IResultWrapper $rows Database rows
472 * @param FormOptions $opts
473 */
474 public function outputChangesList( $rows, $opts ) {
475 $dbr = $this->getDB();
476 $user = $this->getUser();
477 $output = $this->getOutput();
478 $services = MediaWikiServices::getInstance();
479
480 # Show a message about replica DB lag, if applicable
481 $lag = $dbr->getSessionLagStatus()['lag'];
482 if ( $lag > 0 ) {
483 $output->showLagWarning( $lag );
484 }
485
486 # If no rows to display, show message before try to render the list
487 if ( $rows->numRows() == 0 ) {
488 $output->wrapWikiMsg(
489 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
490 );
491 return;
492 }
493
494 $dbr->dataSeek( $rows, 0 );
495
496 $list = ChangesList::newFromContext( $this->getContext(), $this->filterGroups );
497 $list->setWatchlistDivs();
498 $list->initChangesListRows( $rows );
499 if ( $user->getOption( 'watchlistunwatchlinks' ) ) {
500 $list->setChangeLinePrefixer( function ( RecentChange $rc, ChangesList $cl, $grouped ) {
501 // Don't show unwatch link if the line is a grouped log entry using EnhancedChangesList,
502 // since EnhancedChangesList groups log entries by performer rather than by target article
503 if ( $rc->mAttribs['rc_type'] == RC_LOG && $cl instanceof EnhancedChangesList &&
504 $grouped ) {
505 return '';
506 } else {
507 return $this->getLinkRenderer()
508 ->makeKnownLink( $rc->getTitle(),
509 $this->msg( 'watchlist-unwatch' )->text(), [
510 'class' => 'mw-unwatch-link',
511 'title' => $this->msg( 'tooltip-ca-unwatch' )->text()
512 ], [ 'action' => 'unwatch' ] ) . "\u{00A0}";
513 }
514 } );
515 }
516 $dbr->dataSeek( $rows, 0 );
517
518 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
519 && $user->getOption( 'shownumberswatching' )
520 ) {
521 $watchedItemStore = $services->getWatchedItemStore();
522 }
523
524 $s = $list->beginRecentChangesList();
525
526 if ( $this->isStructuredFilterUiEnabled() ) {
527 $s .= $this->makeLegend();
528 }
529
530 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
531 $counter = 1;
532 foreach ( $rows as $obj ) {
533 # Make RC entry
534 $rc = RecentChange::newFromRow( $obj );
535
536 # Skip CatWatch entries for hidden cats based on user preference
537 if (
538 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE &&
539 !$userShowHiddenCats &&
540 $rc->getParam( 'hidden-cat' )
541 ) {
542 continue;
543 }
544
545 $rc->counter = $counter++;
546
547 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
548 $unseen = !$this->isChangeEffectivelySeen( $rc );
549 } else {
550 $unseen = false;
551 }
552
553 if ( isset( $watchedItemStore ) ) {
554 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace, $obj->rc_title );
555 $rc->numberofWatchingusers = $watchedItemStore->countWatchers( $rcTitleValue );
556 } else {
557 $rc->numberofWatchingusers = 0;
558 }
559
560 // XXX: this treats pages with no unseen changes as "not on the watchlist" since
561 // everything is on the watchlist and it is an easy way to make pages with unseen
562 // changes appear bold. @TODO: clean this up.
563 $changeLine = $list->recentChangesLine( $rc, $unseen, $counter );
564 if ( $changeLine !== false ) {
565 $s .= $changeLine;
566 }
567 }
568 $s .= $list->endRecentChangesList();
569
570 $output->addHTML( $s );
571 }
572
573 /**
574 * Set the text to be displayed above the changes
575 *
576 * @param FormOptions $opts
577 * @param int $numRows Number of rows in the result to show after this header
578 */
579 public function doHeader( $opts, $numRows ) {
580 $user = $this->getUser();
581 $out = $this->getOutput();
582
583 $out->addSubtitle(
584 $this->msg( 'watchlistfor2', $user->getName() )
585 ->rawParams( SpecialEditWatchlist::buildTools(
586 $this->getLanguage(),
587 $this->getLinkRenderer()
588 ) )
589 );
590
591 $this->setTopText( $opts );
592
593 $form = '';
594
595 $form .= Xml::openElement( 'form', [
596 'method' => 'get',
597 'action' => wfScript(),
598 'id' => 'mw-watchlist-form'
599 ] );
600 $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
601 $form .= Xml::openElement(
602 'fieldset',
603 [ 'id' => 'mw-watchlist-options', 'class' => 'cloptions' ]
604 );
605 $form .= Xml::element(
606 'legend', null, $this->msg( 'watchlist-options' )->text()
607 );
608
609 if ( !$this->isStructuredFilterUiEnabled() ) {
610 $form .= $this->makeLegend();
611 }
612
613 $lang = $this->getLanguage();
614 $timestamp = wfTimestampNow();
615 $now = $lang->userTimeAndDate( $timestamp, $user );
616 $wlInfo = Html::rawElement(
617 'span',
618 [
619 'class' => 'wlinfo',
620 'data-params' => json_encode( [ 'from' => $timestamp, 'fromFormatted' => $now ] ),
621 ],
622 $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
623 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
624 )->parse()
625 ) . "<br />\n";
626
627 $nondefaults = $opts->getChangedValues();
628 $cutofflinks = Html::rawElement(
629 'span',
630 [ 'class' => 'cldays cloption' ],
631 $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts )
632 );
633
634 # Spit out some control panel links
635 $links = [];
636 $namesOfDisplayedFilters = [];
637 foreach ( $this->getLegacyShowHideFilters() as $filterName => $filter ) {
638 $namesOfDisplayedFilters[] = $filterName;
639 $links[] = $this->showHideCheck(
640 $nondefaults,
641 $filter->getShowHide(),
642 $filterName,
643 $opts[ $filterName ],
644 $filter->isFeatureAvailableOnStructuredUi( $this )
645 );
646 }
647
648 $hiddenFields = $nondefaults;
649 $hiddenFields['action'] = 'submit';
650 unset( $hiddenFields['namespace'] );
651 unset( $hiddenFields['invert'] );
652 unset( $hiddenFields['associated'] );
653 unset( $hiddenFields['days'] );
654 foreach ( $namesOfDisplayedFilters as $filterName ) {
655 unset( $hiddenFields[$filterName] );
656 }
657
658 # Namespace filter and put the whole form together.
659 $form .= $wlInfo;
660 $form .= $cutofflinks;
661 $form .= Html::rawElement(
662 'span',
663 [ 'class' => 'clshowhide' ],
664 $this->msg( 'watchlist-hide' ) .
665 $this->msg( 'colon-separator' )->escaped() .
666 implode( ' ', $links )
667 );
668 $form .= "\n<br />\n";
669
670 $namespaceForm = Html::namespaceSelector(
671 [
672 'selected' => $opts['namespace'],
673 'all' => '',
674 'label' => $this->msg( 'namespace' )->text(),
675 'in-user-lang' => true,
676 ], [
677 'name' => 'namespace',
678 'id' => 'namespace',
679 'class' => 'namespaceselector',
680 ]
681 ) . "\n";
682 $hidden = $opts['namespace'] === '' ? ' mw-input-hidden' : '';
683 $namespaceForm .= '<span class="mw-input-with-label' . $hidden . '">' . Xml::checkLabel(
684 $this->msg( 'invert' )->text(),
685 'invert',
686 'nsinvert',
687 $opts['invert'],
688 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
689 ) . "</span>\n";
690 $namespaceForm .= '<span class="mw-input-with-label' . $hidden . '">' . Xml::checkLabel(
691 $this->msg( 'namespace_association' )->text(),
692 'associated',
693 'nsassociated',
694 $opts['associated'],
695 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
696 ) . "</span>\n";
697 $form .= Html::rawElement(
698 'span',
699 [ 'class' => 'namespaceForm cloption' ],
700 $namespaceForm
701 );
702
703 $form .= Xml::submitButton(
704 $this->msg( 'watchlist-submit' )->text(),
705 [ 'class' => 'cloption-submit' ]
706 ) . "\n";
707 foreach ( $hiddenFields as $key => $value ) {
708 $form .= Html::hidden( $key, $value ) . "\n";
709 }
710 $form .= Xml::closeElement( 'fieldset' ) . "\n";
711 $form .= Xml::closeElement( 'form' ) . "\n";
712
713 // Insert a placeholder for RCFilters
714 if ( $this->isStructuredFilterUiEnabled() ) {
715 $rcfilterContainer = Html::element(
716 'div',
717 // TODO: Remove deprecated rcfilters-container class
718 [ 'class' => 'rcfilters-container mw-rcfilters-container' ]
719 );
720
721 $loadingContainer = Html::rawElement(
722 'div',
723 [ 'class' => 'mw-rcfilters-spinner' ],
724 Html::element(
725 'div',
726 [ 'class' => 'mw-rcfilters-spinner-bounce' ]
727 )
728 );
729
730 // Wrap both with rcfilters-head
731 $this->getOutput()->addHTML(
732 Html::rawElement(
733 'div',
734 // TODO: Remove deprecated rcfilters-head class
735 [ 'class' => 'rcfilters-head mw-rcfilters-head' ],
736 $rcfilterContainer . $form
737 )
738 );
739
740 // Add spinner
741 $this->getOutput()->addHTML( $loadingContainer );
742 } else {
743 $this->getOutput()->addHTML( $form );
744 }
745
746 $this->setBottomText( $opts );
747 }
748
749 function cutoffselector( $options ) {
750 $selected = (float)$options['days'];
751 if ( $selected <= 0 ) {
752 $selected = $this->maxDays;
753 }
754
755 $selectedHours = round( $selected * 24 );
756
757 $hours = array_unique( array_filter( [
758 1,
759 2,
760 6,
761 12,
762 24,
763 72,
764 168,
765 24 * (float)$this->getUser()->getOption( 'watchlistdays', 0 ),
766 24 * $this->maxDays,
767 $selectedHours
768 ] ) );
769 asort( $hours );
770
771 $select = new XmlSelect( 'days', 'days', (float)( $selectedHours / 24 ) );
772
773 foreach ( $hours as $value ) {
774 if ( $value < 24 ) {
775 $name = $this->msg( 'hours' )->numParams( $value )->text();
776 } else {
777 $name = $this->msg( 'days' )->numParams( $value / 24 )->text();
778 }
779 $select->addOption( $name, (float)( $value / 24 ) );
780 }
781
782 return $select->getHTML() . "\n<br />\n";
783 }
784
785 function setTopText( FormOptions $opts ) {
786 $nondefaults = $opts->getChangedValues();
787 $form = '';
788 $user = $this->getUser();
789
790 $numItems = $this->countItems();
791 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
792
793 // Show watchlist header
794 $watchlistHeader = '';
795 if ( $numItems == 0 ) {
796 $watchlistHeader = $this->msg( 'nowatchlist' )->parse();
797 } else {
798 $watchlistHeader .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
799 if ( $this->getConfig()->get( 'EnotifWatchlist' )
800 && $user->getOption( 'enotifwatchlistpages' )
801 ) {
802 $watchlistHeader .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
803 }
804 if ( $showUpdatedMarker ) {
805 $watchlistHeader .= $this->msg(
806 $this->isStructuredFilterUiEnabled() ?
807 'rcfilters-watchlist-showupdated' :
808 'wlheader-showupdated'
809 )->parse() . "\n";
810 }
811 }
812 $form .= Html::rawElement(
813 'div',
814 [ 'class' => 'watchlistDetails' ],
815 $watchlistHeader
816 );
817
818 if ( $numItems > 0 && $showUpdatedMarker ) {
819 $form .= Xml::openElement( 'form', [ 'method' => 'post',
820 'action' => $this->getPageTitle()->getLocalURL(),
821 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
822 Xml::submitButton( $this->msg( 'enotif_reset' )->text(),
823 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
824 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
825 Html::hidden( 'reset', 'all' ) . "\n";
826 foreach ( $nondefaults as $key => $value ) {
827 $form .= Html::hidden( $key, $value ) . "\n";
828 }
829 $form .= Xml::closeElement( 'form' ) . "\n";
830 }
831
832 $this->getOutput()->addHTML( $form );
833 }
834
835 protected function showHideCheck( $options, $message, $name, $value, $inStructuredUi ) {
836 $options[$name] = 1 - (int)$value;
837
838 $attribs = [ 'class' => 'mw-input-with-label clshowhideoption cloption' ];
839 if ( $inStructuredUi ) {
840 $attribs[ 'data-feature-in-structured-ui' ] = true;
841 }
842
843 return Html::rawElement(
844 'span',
845 $attribs,
846 // not using Html::checkLabel because that would escape the contents
847 Html::check( $name, (int)$value, [ 'id' => $name ] ) . Html::rawElement(
848 'label',
849 $attribs + [ 'for' => $name ],
850 // <nowiki/> at beginning to avoid messages with "$1 ..." being parsed as pre tags
851 $this->msg( $message, '<nowiki/>' )->parse()
852 )
853 );
854 }
855
856 /**
857 * Count the number of paired items on a user's watchlist.
858 * The assumption made here is that when a subject page is watched a talk page is also watched.
859 * Hence the number of individual items is halved.
860 *
861 * @return int
862 */
863 protected function countItems() {
864 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
865 $count = $store->countWatchedItems( $this->getUser() );
866 return floor( $count / 2 );
867 }
868
869 /**
870 * @param RecentChange $rc
871 * @return bool User viewed the revision or a newer one
872 */
873 protected function isChangeEffectivelySeen( RecentChange $rc ) {
874 $firstUnseen = $this->getLatestNotificationTimestamp( $rc );
875
876 return ( $firstUnseen === null || $firstUnseen > $rc->getAttribute( 'rc_timestamp' ) );
877 }
878
879 /**
880 * @param RecentChange $rc
881 * @return string|null TS_MW timestamp of first unseen revision or null if there isn't one
882 */
883 private function getLatestNotificationTimestamp( RecentChange $rc ) {
884 return $this->watchStore->getLatestNotificationTimestamp(
885 $rc->getAttribute( 'wl_notificationtimestamp' ),
886 $this->getUser(),
887 $rc->getTitle()
888 );
889 }
890 }