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' ) );
168 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
169 // The user has submitted the form, so we dont need the default values
177 * Get all custom filters
179 * @return array Map of filter URL param names to properties (msg/default)
181 protected function getCustomFilters() {
182 if ( $this->customFilters
=== null ) {
183 $this->customFilters
= parent
::getCustomFilters();
184 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
187 return $this->customFilters
;
191 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
193 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
194 * to the current ones.
196 * @param FormOptions $opts
197 * @return FormOptions
199 protected function fetchOptionsFromRequest( $opts ) {
200 static $compatibilityMap = [
201 'hideMinor' => 'hideminor',
202 'hideBots' => 'hidebots',
203 'hideAnons' => 'hideanons',
204 'hideLiu' => 'hideliu',
205 'hidePatrolled' => 'hidepatrolled',
206 'hideOwn' => 'hidemyself',
209 $params = $this->getRequest()->getValues();
210 foreach ( $compatibilityMap as $from => $to ) {
211 if ( isset( $params[$from] ) ) {
212 $params[$to] = $params[$from];
213 unset( $params[$from] );
217 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
218 // methods defined on WebRequest and removing this dependency would cause some code duplication.
219 $request = new DerivativeRequest( $this->getRequest(), $params );
220 $opts->fetchValuesFromRequest( $request );
228 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
229 &$join_conds, FormOptions
$opts ) {
231 $dbr = $this->getDB();
232 parent
::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
236 if ( $opts['days'] > 0 ) {
237 $conds[] = 'rc_timestamp > ' .
238 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
245 protected function doMainQuery( $tables, $fields, $conds, $query_options,
246 $join_conds, FormOptions
$opts ) {
248 $dbr = $this->getDB();
249 $user = $this->getUser();
251 # Toggle watchlist content (all recent edits or just the latest)
252 if ( $opts['extended'] ) {
253 $limitWatchlist = $user->getIntOption( 'wllimit' );
256 # Top log Ids for a page are not stored
257 $nonRevisionTypes = [ RC_LOG
];
258 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
259 if ( $nonRevisionTypes ) {
260 $conds[] = $dbr->makeList(
262 'rc_this_oldid=page_latest',
263 'rc_type' => $nonRevisionTypes,
272 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
273 $fields = array_merge( RecentChange
::selectFields(), $fields );
275 $query_options = array_merge( [ 'ORDER BY' => 'rc_timestamp DESC' ], $query_options );
276 $join_conds = array_merge(
281 'wl_user' => $user->getId(),
282 'wl_namespace=rc_namespace',
290 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
291 $fields[] = 'wl_notificationtimestamp';
293 if ( $limitWatchlist ) {
294 $query_options['LIMIT'] = $limitWatchlist;
297 $rollbacker = $user->isAllowed( 'rollback' );
298 if ( $usePage ||
$rollbacker ) {
300 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
302 $fields[] = 'page_latest';
306 // Log entries with DELETED_ACTION must not show up unless the user has
307 // the necessary rights.
308 if ( !$user->isAllowed( 'deletedhistory' ) ) {
309 $bitmask = LogPage
::DELETED_ACTION
;
310 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
311 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
316 $conds[] = $dbr->makeList( [
317 'rc_type != ' . RC_LOG
,
318 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
322 ChangeTags
::modifyDisplayQuery(
331 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
343 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
346 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
348 'SpecialWatchlistQuery',
349 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
355 * Return a IDatabase object for reading
359 protected function getDB() {
360 return wfGetDB( DB_REPLICA
, 'watchlist' );
366 public function outputFeedLinks() {
367 $user = $this->getUser();
368 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
370 $this->addFeedLinks( [
371 'action' => 'feedwatchlist',
373 'wlowner' => $user->getName(),
374 'wltoken' => $wlToken,
380 * Build and output the actual changes list.
382 * @param ResultWrapper $rows Database rows
383 * @param FormOptions $opts
385 public function outputChangesList( $rows, $opts ) {
386 $dbr = $this->getDB();
387 $user = $this->getUser();
388 $output = $this->getOutput();
390 # Show a message about replica DB lag, if applicable
391 $lag = wfGetLB()->safeGetLag( $dbr );
393 $output->showLagWarning( $lag );
396 # If no rows to display, show message before try to render the list
397 if ( $rows->numRows() == 0 ) {
398 $output->wrapWikiMsg(
399 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
404 $dbr->dataSeek( $rows, 0 );
406 $list = ChangesList
::newFromContext( $this->getContext(), $this->filterGroups
);
407 $list->setWatchlistDivs();
408 $list->initChangesListRows( $rows );
409 $dbr->dataSeek( $rows, 0 );
411 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
412 && $user->getOption( 'shownumberswatching' )
414 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
417 $s = $list->beginRecentChangesList();
418 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
420 foreach ( $rows as $obj ) {
422 $rc = RecentChange
::newFromRow( $obj );
424 # Skip CatWatch entries for hidden cats based on user preference
426 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
427 !$userShowHiddenCats &&
428 $rc->getParam( 'hidden-cat' )
433 $rc->counter
= $counter++
;
435 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
436 $updated = $obj->wl_notificationtimestamp
;
441 if ( isset( $watchedItemStore ) ) {
442 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
443 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
445 $rc->numberofWatchingusers
= 0;
448 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
449 if ( $changeLine !== false ) {
453 $s .= $list->endRecentChangesList();
455 $output->addHTML( $s );
459 * Set the text to be displayed above the changes
461 * @param FormOptions $opts
462 * @param int $numRows Number of rows in the result to show after this header
464 public function doHeader( $opts, $numRows ) {
465 $user = $this->getUser();
466 $out = $this->getOutput();
469 $this->msg( 'watchlistfor2', $user->getName() )
470 ->rawParams( SpecialEditWatchlist
::buildTools(
471 $this->getLanguage(),
472 $this->getLinkRenderer()
476 $this->setTopText( $opts );
478 $lang = $this->getLanguage();
479 if ( $opts['days'] > 0 ) {
480 $days = $opts['days'];
482 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
484 $timestamp = wfTimestampNow();
485 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
486 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
487 )->parse() . "<br />\n";
489 $nondefaults = $opts->getChangedValues();
490 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
492 # Spit out some control panel links
494 $context = $this->getContext();
495 $namesOfDisplayedFilters = [];
496 foreach ( $this->getFilterGroups() as $groupName => $group ) {
497 if ( !$group->isPerGroupRequestParameter() ) {
498 foreach ( $group->getFilters() as $filterName => $filter ) {
499 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
500 $namesOfDisplayedFilters[] = $filterName;
501 $links[] = $this->showHideCheck(
503 $filter->getShowHide(),
512 $hiddenFields = $nondefaults;
513 $hiddenFields['action'] = 'submit';
514 unset( $hiddenFields['namespace'] );
515 unset( $hiddenFields['invert'] );
516 unset( $hiddenFields['associated'] );
517 unset( $hiddenFields['days'] );
518 foreach ( $namesOfDisplayedFilters as $filterName ) {
519 unset( $hiddenFields[$filterName] );
525 # Namespace filter and put the whole form together.
527 $form .= $cutofflinks;
528 $form .= $this->msg( 'watchlist-hide' ) .
529 $this->msg( 'colon-separator' )->escaped() .
530 implode( ' ', $links );
531 $form .= "\n<br />\n";
532 $form .= Html
::namespaceSelector(
534 'selected' => $opts['namespace'],
536 'label' => $this->msg( 'namespace' )->text()
538 'name' => 'namespace',
540 'class' => 'namespaceselector',
543 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
544 $this->msg( 'invert' )->text(),
548 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
550 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
551 $this->msg( 'namespace_association' )->text(),
555 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
557 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
558 foreach ( $hiddenFields as $key => $value ) {
559 $form .= Html
::hidden( $key, $value ) . "\n";
561 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
562 $form .= Xml
::closeElement( 'form' ) . "\n";
563 $this->getOutput()->addHTML( $form );
565 $this->setBottomText( $opts );
568 function cutoffselector( $options ) {
569 // Cast everything to strings immediately, so that we know all of the values have the same
570 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
571 // default string representation, for example, and would confuse comparisons.
573 // Misleadingly, the 'days' option supports hours too.
574 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
576 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
577 // add the user preference, if it isn't available already
578 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
579 $days[] = $userWatchlistOption;
582 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
583 // add the maximum possible value, if it isn't available already
584 if ( !in_array( $maxDays, $days ) ) {
588 $selected = (string)$options['days'];
589 if ( $selected <= 0 ) {
590 $selected = $maxDays;
593 // add the currently selected value, if it isn't available already
594 if ( !in_array( $selected, $days ) ) {
598 $select = new XmlSelect( 'days', 'days', $selected );
601 foreach ( $days as $value ) {
603 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
605 $name = $this->msg( 'days' )->numParams( $value )->text();
607 $select->addOption( $name, $value );
610 return $select->getHTML() . "\n<br />\n";
613 function setTopText( FormOptions
$opts ) {
614 $nondefaults = $opts->getChangedValues();
616 $user = $this->getUser();
618 $numItems = $this->countItems();
619 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
621 // Show watchlist header
623 if ( $numItems == 0 ) {
624 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
626 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
627 if ( $this->getConfig()->get( 'EnotifWatchlist' )
628 && $user->getOption( 'enotifwatchlistpages' )
630 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
632 if ( $showUpdatedMarker ) {
633 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
638 if ( $numItems > 0 && $showUpdatedMarker ) {
639 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
640 'action' => $this->getPageTitle()->getLocalURL(),
641 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
642 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
643 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
644 Html
::hidden( 'reset', 'all' ) . "\n";
645 foreach ( $nondefaults as $key => $value ) {
646 $form .= Html
::hidden( $key, $value ) . "\n";
648 $form .= Xml
::closeElement( 'form' ) . "\n";
651 $form .= Xml
::openElement( 'form', [
653 'action' => wfScript(),
654 'id' => 'mw-watchlist-form'
656 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
657 $form .= Xml
::fieldset(
658 $this->msg( 'watchlist-options' )->text(),
660 [ 'id' => 'mw-watchlist-options' ]
663 $form .= $this->makeLegend();
665 $this->getOutput()->addHTML( $form );
668 protected function showHideCheck( $options, $message, $name, $value ) {
669 $options[$name] = 1 - (int)$value;
671 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
672 $this->msg( $message, '' )->text(),
680 * Count the number of paired items on a user's watchlist.
681 * The assumption made here is that when a subject page is watched a talk page is also watched.
682 * Hence the number of individual items is halved.
686 protected function countItems() {
687 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
688 $count = $store->countWatchedItems( $this->getUser() );
689 return floor( $count / 2 );