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