3 * Implements Special:Watchlist
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.
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.
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
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\ResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * A special page that lists last changes made to the wiki,
30 * limited to user-defined list of titles.
32 * @ingroup SpecialPage
34 class SpecialWatchlist
extends ChangesListSpecialPage
{
37 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
38 parent
::__construct( $page, $restriction );
40 $this->maxDays
= $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
43 public function doesWrites() {
48 * Main execution point
50 * @param string $subpage
52 function execute( $subpage ) {
53 // Anons don't get a watchlist
54 $this->requireLogin( 'watchlistanontext' );
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',
63 $output->addModuleStyles( [ 'mediawiki.special.watchlist.styles' ] );
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' );
72 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
75 $output->redirect( $title->getLocalURL() );
80 $this->checkPermissions();
82 $user = $this->getUser();
83 $opts = $this->getOptions();
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' ) )
91 $user->clearAllNotifications();
92 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
97 parent
::execute( $subpage );
99 if ( $this->isStructuredFilterUiEnabled() ) {
100 $output->addJsConfigVars( 'wgStructuredChangeFiltersLiveUpdateSupported', false );
101 $output->addJsConfigVars(
102 'wgStructuredChangeFiltersSavedQueriesPreferenceName',
103 'rcfilters-wl-saved-queries'
108 protected function isStructuredFilterUiEnabled() {
109 return parent
::isStructuredFilterUiEnabled()
110 && ( $this->getConfig()->get( 'StructuredChangeFiltersOnWatchlist' )
111 ||
$this->getRequest()->getBool( 'rcfilters' ) );
115 * Return an array of subpages that this special page will accept.
117 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
118 * @return string[] subpages
120 public function getSubpagesForPrefixSearch() {
131 protected function transformFilterDefinition( array $filterDefinition ) {
132 if ( isset( $filterDefinition['showHideSuffix'] ) ) {
133 $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
136 return $filterDefinition;
142 protected function registerFilters() {
143 parent
::registerFilters();
145 $user = $this->getUser();
147 $significance = $this->getFilterGroup( 'significance' );
148 $hideMinor = $significance->getFilter( 'hideminor' );
149 $hideMinor->setDefault( $user->getBoolOption( 'watchlisthideminor' ) );
151 $automated = $this->getFilterGroup( 'automated' );
152 $hideBots = $automated->getFilter( 'hidebots' );
153 $hideBots->setDefault( $user->getBoolOption( 'watchlisthidebots' ) );
155 $registration = $this->getFilterGroup( 'registration' );
156 $hideAnons = $registration->getFilter( 'hideanons' );
157 $hideAnons->setDefault( $user->getBoolOption( 'watchlisthideanons' ) );
158 $hideLiu = $registration->getFilter( 'hideliu' );
159 $hideLiu->setDefault( $user->getBoolOption( 'watchlisthideliu' ) );
161 $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
162 if ( $reviewStatus !== null ) {
163 // Conditional on feature being available and rights
164 $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
165 $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
168 $authorship = $this->getFilterGroup( 'authorship' );
169 $hideMyself = $authorship->getFilter( 'hidemyself' );
170 $hideMyself->setDefault( $user->getBoolOption( 'watchlisthideown' ) );
172 $changeType = $this->getFilterGroup( 'changeType' );
173 $hideCategorization = $changeType->getFilter( 'hidecategorization' );
174 if ( $hideCategorization !== null ) {
175 // Conditional on feature being available
176 $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
181 * Get a FormOptions object containing the default options
183 * @return FormOptions
185 public function getDefaultOptions() {
186 $opts = parent
::getDefaultOptions();
187 $user = $this->getUser();
189 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
190 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
191 $opts->add( 'limit', $user->getIntOption( 'wllimit' ), FormOptions
::INT );
196 public function validateOptions( FormOptions
$opts ) {
197 $opts->validateBounds( 'days', 0, $this->maxDays
);
198 $opts->validateIntBounds( 'limit', 0, 5000 );
199 parent
::validateOptions( $opts );
203 * Get all custom filters
205 * @return array Map of filter URL param names to properties (msg/default)
207 protected function getCustomFilters() {
208 if ( $this->customFilters
=== null ) {
209 $this->customFilters
= parent
::getCustomFilters();
210 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
213 return $this->customFilters
;
217 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
219 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
220 * to the current ones.
222 * @param FormOptions $opts
223 * @return FormOptions
225 protected function fetchOptionsFromRequest( $opts ) {
226 static $compatibilityMap = [
227 'hideMinor' => 'hideminor',
228 'hideBots' => 'hidebots',
229 'hideAnons' => 'hideanons',
230 'hideLiu' => 'hideliu',
231 'hidePatrolled' => 'hidepatrolled',
232 'hideOwn' => 'hidemyself',
235 $params = $this->getRequest()->getValues();
236 foreach ( $compatibilityMap as $from => $to ) {
237 if ( isset( $params[$from] ) ) {
238 $params[$to] = $params[$from];
239 unset( $params[$from] );
243 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
244 $allBooleansFalse = [];
246 // If the user submitted the form, start with a baseline of "all
247 // booleans are false", then change the ones they checked. This
248 // means we ignore the defaults.
250 // This is how we handle the fact that HTML forms don't submit
252 foreach ( $this->filterGroups
as $filterGroup ) {
253 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
254 foreach ( $filterGroup->getFilters() as $filter ) {
255 $allBooleansFalse[$filter->getName()] = false;
260 $params = $params +
$allBooleansFalse;
263 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
264 // methods defined on WebRequest and removing this dependency would cause some code duplication.
265 $request = new DerivativeRequest( $this->getRequest(), $params );
266 $opts->fetchValuesFromRequest( $request );
274 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
275 &$join_conds, FormOptions
$opts
277 $dbr = $this->getDB();
278 parent
::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
282 if ( $opts['days'] > 0 ) {
283 $conds[] = 'rc_timestamp > ' .
284 $dbr->addQuotes( $dbr->timestamp( time() - $opts['days'] * 3600 * 24 ) );
291 protected function doMainQuery( $tables, $fields, $conds, $query_options,
292 $join_conds, FormOptions
$opts
294 $dbr = $this->getDB();
295 $user = $this->getUser();
297 # Toggle watchlist content (all recent edits or just the latest)
298 if ( $opts['extended'] ) {
301 # Top log Ids for a page are not stored
302 $nonRevisionTypes = [ RC_LOG
];
303 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
304 if ( $nonRevisionTypes ) {
305 $conds[] = $dbr->makeList(
307 'rc_this_oldid=page_latest',
308 'rc_type' => $nonRevisionTypes,
316 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
317 $fields = array_merge( RecentChange
::selectFields(), $fields );
319 $query_options = array_merge( [
320 'ORDER BY' => 'rc_timestamp DESC',
321 'LIMIT' => $opts['limit']
323 $join_conds = array_merge(
328 'wl_user' => $user->getId(),
329 'wl_namespace=rc_namespace',
337 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
338 $fields[] = 'wl_notificationtimestamp';
341 $rollbacker = $user->isAllowed( 'rollback' );
342 if ( $usePage ||
$rollbacker ) {
344 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
346 $fields[] = 'page_latest';
350 // Log entries with DELETED_ACTION must not show up unless the user has
351 // the necessary rights.
352 if ( !$user->isAllowed( 'deletedhistory' ) ) {
353 $bitmask = LogPage
::DELETED_ACTION
;
354 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
355 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
360 $conds[] = $dbr->makeList( [
361 'rc_type != ' . RC_LOG
,
362 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
366 ChangeTags
::modifyDisplayQuery(
375 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
387 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
390 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
392 'SpecialWatchlistQuery',
393 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
399 * Return a IDatabase object for reading
403 protected function getDB() {
404 return wfGetDB( DB_REPLICA
, 'watchlist' );
410 public function outputFeedLinks() {
411 $user = $this->getUser();
412 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
414 $this->addFeedLinks( [
415 'action' => 'feedwatchlist',
417 'wlowner' => $user->getName(),
418 'wltoken' => $wlToken,
424 * Build and output the actual changes list.
426 * @param ResultWrapper $rows Database rows
427 * @param FormOptions $opts
429 public function outputChangesList( $rows, $opts ) {
430 $dbr = $this->getDB();
431 $user = $this->getUser();
432 $output = $this->getOutput();
434 # Show a message about replica DB lag, if applicable
435 $lag = wfGetLB()->safeGetLag( $dbr );
437 $output->showLagWarning( $lag );
440 # If no rows to display, show message before try to render the list
441 if ( $rows->numRows() == 0 ) {
442 $output->wrapWikiMsg(
443 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
448 $dbr->dataSeek( $rows, 0 );
450 $list = ChangesList
::newFromContext( $this->getContext(), $this->filterGroups
);
451 $list->setWatchlistDivs();
452 $list->initChangesListRows( $rows );
453 if ( $user->getOption( 'watchlistunwatchlinks' ) ) {
454 $list->setChangeLinePrefixer( function ( RecentChange
$rc, ChangesList
$cl, $grouped ) {
455 // Don't show unwatch link if the line is a grouped log entry using EnhancedChangesList,
456 // since EnhancedChangesList groups log entries by performer rather than by target article
457 if ( $rc->mAttribs
['rc_type'] == RC_LOG
&& $cl instanceof EnhancedChangesList
&&
461 return $this->getLinkRenderer()
462 ->makeKnownLink( $rc->getTitle(),
463 $this->msg( 'watchlist-unwatch' )->text(), [
464 'class' => 'mw-unwatch-link',
465 'title' => $this->msg( 'tooltip-ca-unwatch' )->text()
466 ], [ 'action' => 'unwatch' ] ) . ' ';
470 $dbr->dataSeek( $rows, 0 );
472 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
473 && $user->getOption( 'shownumberswatching' )
475 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
478 $s = $list->beginRecentChangesList();
480 if ( $this->isStructuredFilterUiEnabled() ) {
481 $s .= $this->makeLegend();
484 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
486 foreach ( $rows as $obj ) {
488 $rc = RecentChange
::newFromRow( $obj );
490 # Skip CatWatch entries for hidden cats based on user preference
492 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
493 !$userShowHiddenCats &&
494 $rc->getParam( 'hidden-cat' )
499 $rc->counter
= $counter++
;
501 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
502 $updated = $obj->wl_notificationtimestamp
;
507 if ( isset( $watchedItemStore ) ) {
508 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
509 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
511 $rc->numberofWatchingusers
= 0;
514 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
515 if ( $changeLine !== false ) {
519 $s .= $list->endRecentChangesList();
521 $output->addHTML( $s );
525 * Set the text to be displayed above the changes
527 * @param FormOptions $opts
528 * @param int $numRows Number of rows in the result to show after this header
530 public function doHeader( $opts, $numRows ) {
531 $user = $this->getUser();
532 $out = $this->getOutput();
535 $this->msg( 'watchlistfor2', $user->getName() )
536 ->rawParams( SpecialEditWatchlist
::buildTools(
537 $this->getLanguage(),
538 $this->getLinkRenderer()
542 $this->setTopText( $opts );
546 $form .= Xml
::openElement( 'form', [
548 'action' => wfScript(),
549 'id' => 'mw-watchlist-form'
551 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
552 $form .= Xml
::fieldset(
553 $this->msg( 'watchlist-options' )->text(),
555 [ 'id' => 'mw-watchlist-options', 'class' => 'cloptions' ]
558 if ( !$this->isStructuredFilterUiEnabled() ) {
559 $form .= $this->makeLegend();
562 $lang = $this->getLanguage();
563 if ( $opts['days'] > 0 ) {
564 $days = $opts['days'];
566 $days = $this->maxDays
;
568 $timestamp = wfTimestampNow();
569 $wlInfo = Html
::rawElement(
571 [ 'class' => 'wlinfo' ],
572 $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
573 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
577 $nondefaults = $opts->getChangedValues();
578 $cutofflinks = Html
::rawElement(
580 [ 'class' => 'cldays cloption' ],
581 $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts )
584 # Spit out some control panel links
586 $namesOfDisplayedFilters = [];
587 foreach ( $this->getFilterGroups() as $groupName => $group ) {
588 if ( !$group->isPerGroupRequestParameter() ) {
589 foreach ( $group->getFilters() as $filterName => $filter ) {
590 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
591 $namesOfDisplayedFilters[] = $filterName;
592 $links[] = $this->showHideCheck(
594 $filter->getShowHide(),
597 $filter->isFeatureAvailableOnStructuredUi( $this )
604 $hiddenFields = $nondefaults;
605 $hiddenFields['action'] = 'submit';
606 unset( $hiddenFields['namespace'] );
607 unset( $hiddenFields['invert'] );
608 unset( $hiddenFields['associated'] );
609 unset( $hiddenFields['days'] );
610 foreach ( $namesOfDisplayedFilters as $filterName ) {
611 unset( $hiddenFields[$filterName] );
614 # Namespace filter and put the whole form together.
616 $form .= $cutofflinks;
617 $form .= Html
::rawElement(
619 [ 'class' => 'clshowhide' ],
620 $this->msg( 'watchlist-hide' ) .
621 $this->msg( 'colon-separator' )->escaped() .
622 implode( ' ', $links )
624 $form .= "\n<br />\n";
626 $namespaceForm = Html
::namespaceSelector(
628 'selected' => $opts['namespace'],
630 'label' => $this->msg( 'namespace' )->text()
632 'name' => 'namespace',
634 'class' => 'namespaceselector',
637 $namespaceForm .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
638 $this->msg( 'invert' )->text(),
642 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
644 $namespaceForm .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
645 $this->msg( 'namespace_association' )->text(),
649 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
651 $form .= Html
::rawElement(
653 [ 'class' => 'namespaceForm cloption' ],
657 $form .= Xml
::submitButton(
658 $this->msg( 'watchlist-submit' )->text(),
659 [ 'class' => 'cloption-submit' ]
661 foreach ( $hiddenFields as $key => $value ) {
662 $form .= Html
::hidden( $key, $value ) . "\n";
664 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
665 $form .= Xml
::closeElement( 'form' ) . "\n";
667 // Insert a placeholder for RCFilters
668 if ( $this->isStructuredFilterUiEnabled() ) {
669 $rcfilterContainer = Html
::element(
671 [ 'class' => 'rcfilters-container' ]
674 $loadingContainer = Html
::rawElement(
676 [ 'class' => 'rcfilters-spinner' ],
679 [ 'class' => 'rcfilters-spinner-bounce' ]
683 // Wrap both with rcfilters-head
684 $this->getOutput()->addHTML(
687 [ 'class' => 'rcfilters-head' ],
688 $rcfilterContainer . $form
693 $this->getOutput()->addHTML( $loadingContainer );
695 $this->getOutput()->addHTML( $form );
698 $this->setBottomText( $opts );
701 function cutoffselector( $options ) {
702 // Cast everything to strings immediately, so that we know all of the values have the same
703 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
704 // default string representation, for example, and would confuse comparisons.
706 // Misleadingly, the 'days' option supports hours too.
707 $days = array_map( 'strval', [ 1 / 24, 2 / 24, 6 / 24, 12 / 24, 1, 3, 7 ] );
709 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
710 // add the user preference, if it isn't available already
711 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
712 $days[] = $userWatchlistOption;
715 $maxDays = (string)$this->maxDays
;
716 // add the maximum possible value, if it isn't available already
717 if ( !in_array( $maxDays, $days ) ) {
721 $selected = (string)$options['days'];
722 if ( $selected <= 0 ) {
723 $selected = $maxDays;
726 // add the currently selected value, if it isn't available already
727 if ( !in_array( $selected, $days ) ) {
731 $select = new XmlSelect( 'days', 'days', $selected );
734 foreach ( $days as $value ) {
736 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
738 $name = $this->msg( 'days' )->numParams( $value )->text();
740 $select->addOption( $name, $value );
743 return $select->getHTML() . "\n<br />\n";
746 function setTopText( FormOptions
$opts ) {
747 $nondefaults = $opts->getChangedValues();
749 $user = $this->getUser();
751 $numItems = $this->countItems();
752 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
754 // Show watchlist header
756 if ( $numItems == 0 ) {
757 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
759 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
760 if ( $this->getConfig()->get( 'EnotifWatchlist' )
761 && $user->getOption( 'enotifwatchlistpages' )
763 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
765 if ( $showUpdatedMarker ) {
766 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
771 if ( $numItems > 0 && $showUpdatedMarker ) {
772 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
773 'action' => $this->getPageTitle()->getLocalURL(),
774 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
775 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
776 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
777 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
778 Html
::hidden( 'reset', 'all' ) . "\n";
779 foreach ( $nondefaults as $key => $value ) {
780 $form .= Html
::hidden( $key, $value ) . "\n";
782 $form .= Xml
::closeElement( 'form' ) . "\n";
785 $this->getOutput()->addHTML( $form );
788 protected function showHideCheck( $options, $message, $name, $value, $inStructuredUi ) {
789 $options[$name] = 1 - (int)$value;
791 $attribs = [ 'class' => 'mw-input-with-label clshowhideoption cloption' ];
792 if ( $inStructuredUi ) {
793 $attribs[ 'data-feature-in-structured-ui' ] = true;
796 return Html
::rawElement(
800 $this->msg( $message, '' )->text(),
809 * Count the number of paired items on a user's watchlist.
810 * The assumption made here is that when a subject page is watched a talk page is also watched.
811 * Hence the number of individual items is halved.
815 protected function countItems() {
816 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
817 $count = $store->countWatchedItems( $this->getUser() );
818 return floor( $count / 2 );