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
;
28 * A special page that lists last changes made to the wiki,
29 * limited to user-defined list of titles.
31 * @ingroup SpecialPage
33 class SpecialWatchlist
extends ChangesListSpecialPage
{
34 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
35 parent
::__construct( $page, $restriction );
38 public function doesWrites() {
43 * Main execution point
45 * @param string $subpage
47 function execute( $subpage ) {
48 // Anons don't get a watchlist
49 $this->requireLogin( 'watchlistanontext' );
51 $output = $this->getOutput();
52 $request = $this->getRequest();
53 $this->addHelpLink( 'Help:Watching pages' );
54 $output->addModules( [
55 'mediawiki.special.changeslist.visitedstatus',
56 'mediawiki.special.watchlist',
59 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
60 if ( $mode !== false ) {
61 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
62 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
63 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
64 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
66 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
69 $output->redirect( $title->getLocalURL() );
74 $this->checkPermissions();
76 $user = $this->getUser();
77 $opts = $this->getOptions();
79 $config = $this->getConfig();
80 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
81 && $request->getVal( 'reset' )
82 && $request->wasPosted()
84 $user->clearAllNotifications();
85 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
90 parent
::execute( $subpage );
94 * Return an array of subpages that this special page will accept.
96 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
97 * @return string[] subpages
99 public function getSubpagesForPrefixSearch() {
110 protected function registerFiltersFromDefinitions( array $definition ) {
111 foreach ( $definition as $groupName => &$groupDefinition ) {
112 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
113 if ( isset( $filterDefinition['showHideSuffix'] ) ) {
114 $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
119 parent
::registerFiltersFromDefinitions( $definition );
125 protected function registerFilters() {
126 parent
::registerFilters();
128 $user = $this->getUser();
130 $significance = $this->getFilterGroup( 'significance' );
131 $hideMinor = $significance->getFilter( 'hideminor' );
132 $hideMinor->setDefault( $user->getBoolOption( 'watchlisthideminor' ) );
134 $automated = $this->getFilterGroup( 'automated' );
135 $hideBots = $automated->getFilter( 'hidebots' );
136 $hideBots->setDefault( $user->getBoolOption( 'watchlisthidebots' ) );
138 $registration = $this->getFilterGroup( 'registration' );
139 $hideAnons = $registration->getFilter( 'hideanons' );
140 $hideAnons->setDefault( $user->getBoolOption( 'watchlisthideanons' ) );
141 $hideLiu = $registration->getFilter( 'hideliu' );
142 $hideLiu->setDefault( $user->getBoolOption( 'watchlisthideliu' ) );
144 $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
145 $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
146 $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
148 $authorship = $this->getFilterGroup( 'authorship' );
149 $hideMyself = $authorship->getFilter( 'hidemyself' );
150 $hideMyself->setDefault( $user->getBoolOption( 'watchlisthideown' ) );
152 $changeType = $this->getFilterGroup( 'changeType' );
153 $hideCategorization = $changeType->getFilter( 'hidecategorization' );
154 $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
158 * Get a FormOptions object containing the default options
160 * @return FormOptions
162 public function getDefaultOptions() {
163 $opts = parent
::getDefaultOptions();
164 $user = $this->getUser();
166 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
167 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
173 * Get all custom filters
175 * @return array Map of filter URL param names to properties (msg/default)
177 protected function getCustomFilters() {
178 if ( $this->customFilters
=== null ) {
179 $this->customFilters
= parent
::getCustomFilters();
180 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
183 return $this->customFilters
;
187 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
189 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
190 * to the current ones.
192 * @param FormOptions $opts
193 * @return FormOptions
195 protected function fetchOptionsFromRequest( $opts ) {
196 static $compatibilityMap = [
197 'hideMinor' => 'hideminor',
198 'hideBots' => 'hidebots',
199 'hideAnons' => 'hideanons',
200 'hideLiu' => 'hideliu',
201 'hidePatrolled' => 'hidepatrolled',
202 'hideOwn' => 'hidemyself',
205 $params = $this->getRequest()->getValues();
206 foreach ( $compatibilityMap as $from => $to ) {
207 if ( isset( $params[$from] ) ) {
208 $params[$to] = $params[$from];
209 unset( $params[$from] );
213 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
214 $allBooleansFalse = [];
216 // If the user submitted the form, start with a baseline of "all
217 // booleans are false", then change the ones they checked. This
218 // means we ignore the defaults.
220 // This is how we handle the fact that HTML forms don't submit
222 foreach ( $this->filterGroups
as $filterGroup ) {
223 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
224 foreach ( $filterGroup->getFilters() as $filter ) {
225 $allBooleansFalse[$filter->getName()] = false;
230 $params = $params +
$allBooleansFalse;
233 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
234 // methods defined on WebRequest and removing this dependency would cause some code duplication.
235 $request = new DerivativeRequest( $this->getRequest(), $params );
236 $opts->fetchValuesFromRequest( $request );
244 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
245 &$join_conds, FormOptions
$opts ) {
247 $dbr = $this->getDB();
248 parent
::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
252 if ( $opts['days'] > 0 ) {
253 $conds[] = 'rc_timestamp > ' .
254 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
261 protected function doMainQuery( $tables, $fields, $conds, $query_options,
262 $join_conds, FormOptions
$opts ) {
264 $dbr = $this->getDB();
265 $user = $this->getUser();
267 # Toggle watchlist content (all recent edits or just the latest)
268 if ( $opts['extended'] ) {
269 $limitWatchlist = $user->getIntOption( 'wllimit' );
272 # Top log Ids for a page are not stored
273 $nonRevisionTypes = [ RC_LOG
];
274 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
275 if ( $nonRevisionTypes ) {
276 $conds[] = $dbr->makeList(
278 'rc_this_oldid=page_latest',
279 'rc_type' => $nonRevisionTypes,
288 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
289 $fields = array_merge( RecentChange
::selectFields(), $fields );
291 $query_options = array_merge( [ 'ORDER BY' => 'rc_timestamp DESC' ], $query_options );
292 $join_conds = array_merge(
297 'wl_user' => $user->getId(),
298 'wl_namespace=rc_namespace',
306 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
307 $fields[] = 'wl_notificationtimestamp';
309 if ( $limitWatchlist ) {
310 $query_options['LIMIT'] = $limitWatchlist;
313 $rollbacker = $user->isAllowed( 'rollback' );
314 if ( $usePage ||
$rollbacker ) {
316 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
318 $fields[] = 'page_latest';
322 // Log entries with DELETED_ACTION must not show up unless the user has
323 // the necessary rights.
324 if ( !$user->isAllowed( 'deletedhistory' ) ) {
325 $bitmask = LogPage
::DELETED_ACTION
;
326 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
327 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
332 $conds[] = $dbr->makeList( [
333 'rc_type != ' . RC_LOG
,
334 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
338 ChangeTags
::modifyDisplayQuery(
347 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
359 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
362 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
364 'SpecialWatchlistQuery',
365 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
371 * Return a IDatabase object for reading
375 protected function getDB() {
376 return wfGetDB( DB_REPLICA
, 'watchlist' );
382 public function outputFeedLinks() {
383 $user = $this->getUser();
384 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
386 $this->addFeedLinks( [
387 'action' => 'feedwatchlist',
389 'wlowner' => $user->getName(),
390 'wltoken' => $wlToken,
396 * Build and output the actual changes list.
398 * @param ResultWrapper $rows Database rows
399 * @param FormOptions $opts
401 public function outputChangesList( $rows, $opts ) {
402 $dbr = $this->getDB();
403 $user = $this->getUser();
404 $output = $this->getOutput();
406 # Show a message about replica DB lag, if applicable
407 $lag = wfGetLB()->safeGetLag( $dbr );
409 $output->showLagWarning( $lag );
412 # If no rows to display, show message before try to render the list
413 if ( $rows->numRows() == 0 ) {
414 $output->wrapWikiMsg(
415 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
420 $dbr->dataSeek( $rows, 0 );
422 $list = ChangesList
::newFromContext( $this->getContext(), $this->filterGroups
);
423 $list->setWatchlistDivs();
424 $list->initChangesListRows( $rows );
425 $dbr->dataSeek( $rows, 0 );
427 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
428 && $user->getOption( 'shownumberswatching' )
430 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
433 $s = $list->beginRecentChangesList();
434 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
436 foreach ( $rows as $obj ) {
438 $rc = RecentChange
::newFromRow( $obj );
440 # Skip CatWatch entries for hidden cats based on user preference
442 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
443 !$userShowHiddenCats &&
444 $rc->getParam( 'hidden-cat' )
449 $rc->counter
= $counter++
;
451 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
452 $updated = $obj->wl_notificationtimestamp
;
457 if ( isset( $watchedItemStore ) ) {
458 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
459 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
461 $rc->numberofWatchingusers
= 0;
464 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
465 if ( $changeLine !== false ) {
469 $s .= $list->endRecentChangesList();
471 $output->addHTML( $s );
475 * Set the text to be displayed above the changes
477 * @param FormOptions $opts
478 * @param int $numRows Number of rows in the result to show after this header
480 public function doHeader( $opts, $numRows ) {
481 $user = $this->getUser();
482 $out = $this->getOutput();
485 $this->msg( 'watchlistfor2', $user->getName() )
486 ->rawParams( SpecialEditWatchlist
::buildTools(
487 $this->getLanguage(),
488 $this->getLinkRenderer()
492 $this->setTopText( $opts );
494 $lang = $this->getLanguage();
495 if ( $opts['days'] > 0 ) {
496 $days = $opts['days'];
498 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
500 $timestamp = wfTimestampNow();
501 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
502 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
503 )->parse() . "<br />\n";
505 $nondefaults = $opts->getChangedValues();
506 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
508 # Spit out some control panel links
510 $context = $this->getContext();
511 $namesOfDisplayedFilters = [];
512 foreach ( $this->getFilterGroups() as $groupName => $group ) {
513 if ( !$group->isPerGroupRequestParameter() ) {
514 foreach ( $group->getFilters() as $filterName => $filter ) {
515 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
516 $namesOfDisplayedFilters[] = $filterName;
517 $links[] = $this->showHideCheck(
519 $filter->getShowHide(),
528 $hiddenFields = $nondefaults;
529 $hiddenFields['action'] = 'submit';
530 unset( $hiddenFields['namespace'] );
531 unset( $hiddenFields['invert'] );
532 unset( $hiddenFields['associated'] );
533 unset( $hiddenFields['days'] );
534 foreach ( $namesOfDisplayedFilters as $filterName ) {
535 unset( $hiddenFields[$filterName] );
541 # Namespace filter and put the whole form together.
543 $form .= $cutofflinks;
544 $form .= $this->msg( 'watchlist-hide' ) .
545 $this->msg( 'colon-separator' )->escaped() .
546 implode( ' ', $links );
547 $form .= "\n<br />\n";
548 $form .= Html
::namespaceSelector(
550 'selected' => $opts['namespace'],
552 'label' => $this->msg( 'namespace' )->text()
554 'name' => 'namespace',
556 'class' => 'namespaceselector',
559 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
560 $this->msg( 'invert' )->text(),
564 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
566 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
567 $this->msg( 'namespace_association' )->text(),
571 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
573 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
574 foreach ( $hiddenFields as $key => $value ) {
575 $form .= Html
::hidden( $key, $value ) . "\n";
577 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
578 $form .= Xml
::closeElement( 'form' ) . "\n";
579 $this->getOutput()->addHTML( $form );
581 $this->setBottomText( $opts );
584 function cutoffselector( $options ) {
585 // Cast everything to strings immediately, so that we know all of the values have the same
586 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
587 // default string representation, for example, and would confuse comparisons.
589 // Misleadingly, the 'days' option supports hours too.
590 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
592 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
593 // add the user preference, if it isn't available already
594 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
595 $days[] = $userWatchlistOption;
598 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
599 // add the maximum possible value, if it isn't available already
600 if ( !in_array( $maxDays, $days ) ) {
604 $selected = (string)$options['days'];
605 if ( $selected <= 0 ) {
606 $selected = $maxDays;
609 // add the currently selected value, if it isn't available already
610 if ( !in_array( $selected, $days ) ) {
614 $select = new XmlSelect( 'days', 'days', $selected );
617 foreach ( $days as $value ) {
619 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
621 $name = $this->msg( 'days' )->numParams( $value )->text();
623 $select->addOption( $name, $value );
626 return $select->getHTML() . "\n<br />\n";
629 function setTopText( FormOptions
$opts ) {
630 $nondefaults = $opts->getChangedValues();
632 $user = $this->getUser();
634 $numItems = $this->countItems();
635 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
637 // Show watchlist header
639 if ( $numItems == 0 ) {
640 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
642 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
643 if ( $this->getConfig()->get( 'EnotifWatchlist' )
644 && $user->getOption( 'enotifwatchlistpages' )
646 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
648 if ( $showUpdatedMarker ) {
649 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
654 if ( $numItems > 0 && $showUpdatedMarker ) {
655 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
656 'action' => $this->getPageTitle()->getLocalURL(),
657 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
658 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
659 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
660 Html
::hidden( 'reset', 'all' ) . "\n";
661 foreach ( $nondefaults as $key => $value ) {
662 $form .= Html
::hidden( $key, $value ) . "\n";
664 $form .= Xml
::closeElement( 'form' ) . "\n";
667 $form .= Xml
::openElement( 'form', [
669 'action' => wfScript(),
670 'id' => 'mw-watchlist-form'
672 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
673 $form .= Xml
::fieldset(
674 $this->msg( 'watchlist-options' )->text(),
676 [ 'id' => 'mw-watchlist-options' ]
679 $form .= $this->makeLegend();
681 $this->getOutput()->addHTML( $form );
684 protected function showHideCheck( $options, $message, $name, $value ) {
685 $options[$name] = 1 - (int)$value;
687 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
688 $this->msg( $message, '' )->text(),
696 * Count the number of paired items on a user's watchlist.
697 * The assumption made here is that when a subject page is watched a talk page is also watched.
698 * Hence the number of individual items is halved.
702 protected function countItems() {
703 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
704 $count = $store->countWatchedItems( $this->getUser() );
705 return floor( $count / 2 );