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