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
{
35 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
36 parent
::__construct( $page, $restriction );
39 public function doesWrites() {
44 * Main execution point
46 * @param string $subpage
48 function execute( $subpage ) {
49 // Anons don't get a watchlist
50 $this->requireLogin( 'watchlistanontext' );
52 $output = $this->getOutput();
53 $request = $this->getRequest();
54 $this->addHelpLink( 'Help:Watching pages' );
55 $output->addModules( [
56 'mediawiki.special.changeslist.visitedstatus',
57 'mediawiki.special.watchlist',
60 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
61 if ( $mode !== false ) {
62 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
63 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
64 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
65 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
67 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
70 $output->redirect( $title->getLocalURL() );
75 $this->checkPermissions();
77 $user = $this->getUser();
78 $opts = $this->getOptions();
80 $config = $this->getConfig();
81 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
82 && $request->getVal( 'reset' )
83 && $request->wasPosted()
85 $user->clearAllNotifications();
86 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
91 parent
::execute( $subpage );
95 * Return an array of subpages that this special page will accept.
97 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
98 * @return string[] subpages
100 public function getSubpagesForPrefixSearch() {
111 protected function registerFiltersFromDefinitions( array $definition ) {
112 foreach ( $definition as $groupName => &$groupDefinition ) {
113 foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
114 if ( isset( $filterDefinition['showHideSuffix'] ) ) {
115 $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
120 parent
::registerFiltersFromDefinitions( $definition );
126 protected function registerFilters() {
127 parent
::registerFilters();
129 $user = $this->getUser();
131 $significance = $this->getFilterGroup( 'significance' );
132 $hideMinor = $significance->getFilter( 'hideminor' );
133 $hideMinor->setDefault( $user->getBoolOption( 'watchlisthideminor' ) );
135 $automated = $this->getFilterGroup( 'automated' );
136 $hideBots = $automated->getFilter( 'hidebots' );
137 $hideBots->setDefault( $user->getBoolOption( 'watchlisthidebots' ) );
139 $registration = $this->getFilterGroup( 'registration' );
140 $hideAnons = $registration->getFilter( 'hideanons' );
141 $hideAnons->setDefault( $user->getBoolOption( 'watchlisthideanons' ) );
142 $hideLiu = $registration->getFilter( 'hideliu' );
143 $hideLiu->setDefault( $user->getBoolOption( 'watchlisthideliu' ) );
145 $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
146 $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
147 $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
149 $authorship = $this->getFilterGroup( 'authorship' );
150 $hideMyself = $authorship->getFilter( 'hidemyself' );
151 $hideMyself->setDefault( $user->getBoolOption( 'watchlisthideown' ) );
153 $changeType = $this->getFilterGroup( 'changeType' );
154 $hideCategorization = $changeType->getFilter( 'hidecategorization' );
155 $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
159 * Get a FormOptions object containing the default options
161 * @return FormOptions
163 public function getDefaultOptions() {
164 $opts = parent
::getDefaultOptions();
165 $user = $this->getUser();
167 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
168 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
174 * Get all custom filters
176 * @return array Map of filter URL param names to properties (msg/default)
178 protected function getCustomFilters() {
179 if ( $this->customFilters
=== null ) {
180 $this->customFilters
= parent
::getCustomFilters();
181 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
184 return $this->customFilters
;
188 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
190 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
191 * to the current ones.
193 * @param FormOptions $opts
194 * @return FormOptions
196 protected function fetchOptionsFromRequest( $opts ) {
197 static $compatibilityMap = [
198 'hideMinor' => 'hideminor',
199 'hideBots' => 'hidebots',
200 'hideAnons' => 'hideanons',
201 'hideLiu' => 'hideliu',
202 'hidePatrolled' => 'hidepatrolled',
203 'hideOwn' => 'hidemyself',
206 $params = $this->getRequest()->getValues();
207 foreach ( $compatibilityMap as $from => $to ) {
208 if ( isset( $params[$from] ) ) {
209 $params[$to] = $params[$from];
210 unset( $params[$from] );
214 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
215 $allBooleansFalse = [];
217 // If the user submitted the form, start with a baseline of "all
218 // booleans are false", then change the ones they checked. This
219 // means we ignore the defaults.
221 // This is how we handle the fact that HTML forms don't submit
223 foreach ( $this->filterGroups
as $filterGroup ) {
224 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
225 foreach ( $filterGroup->getFilters() as $filter ) {
226 $allBooleansFalse[$filter->getName()] = false;
231 $params = $params +
$allBooleansFalse;
234 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
235 // methods defined on WebRequest and removing this dependency would cause some code duplication.
236 $request = new DerivativeRequest( $this->getRequest(), $params );
237 $opts->fetchValuesFromRequest( $request );
245 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
246 &$join_conds, FormOptions
$opts ) {
248 $dbr = $this->getDB();
249 parent
::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
253 if ( $opts['days'] > 0 ) {
254 $conds[] = 'rc_timestamp > ' .
255 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
262 protected function doMainQuery( $tables, $fields, $conds, $query_options,
263 $join_conds, FormOptions
$opts ) {
265 $dbr = $this->getDB();
266 $user = $this->getUser();
268 # Toggle watchlist content (all recent edits or just the latest)
269 if ( $opts['extended'] ) {
270 $limitWatchlist = $user->getIntOption( 'wllimit' );
273 # Top log Ids for a page are not stored
274 $nonRevisionTypes = [ RC_LOG
];
275 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
276 if ( $nonRevisionTypes ) {
277 $conds[] = $dbr->makeList(
279 'rc_this_oldid=page_latest',
280 'rc_type' => $nonRevisionTypes,
289 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
290 $fields = array_merge( RecentChange
::selectFields(), $fields );
292 $query_options = array_merge( [ 'ORDER BY' => 'rc_timestamp DESC' ], $query_options );
293 $join_conds = array_merge(
298 'wl_user' => $user->getId(),
299 'wl_namespace=rc_namespace',
307 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
308 $fields[] = 'wl_notificationtimestamp';
310 if ( $limitWatchlist ) {
311 $query_options['LIMIT'] = $limitWatchlist;
314 $rollbacker = $user->isAllowed( 'rollback' );
315 if ( $usePage ||
$rollbacker ) {
317 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
319 $fields[] = 'page_latest';
323 // Log entries with DELETED_ACTION must not show up unless the user has
324 // the necessary rights.
325 if ( !$user->isAllowed( 'deletedhistory' ) ) {
326 $bitmask = LogPage
::DELETED_ACTION
;
327 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
328 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
333 $conds[] = $dbr->makeList( [
334 'rc_type != ' . RC_LOG
,
335 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
339 ChangeTags
::modifyDisplayQuery(
348 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
360 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
363 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
365 'SpecialWatchlistQuery',
366 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
372 * Return a IDatabase object for reading
376 protected function getDB() {
377 return wfGetDB( DB_REPLICA
, 'watchlist' );
383 public function outputFeedLinks() {
384 $user = $this->getUser();
385 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
387 $this->addFeedLinks( [
388 'action' => 'feedwatchlist',
390 'wlowner' => $user->getName(),
391 'wltoken' => $wlToken,
397 * Build and output the actual changes list.
399 * @param ResultWrapper $rows Database rows
400 * @param FormOptions $opts
402 public function outputChangesList( $rows, $opts ) {
403 $dbr = $this->getDB();
404 $user = $this->getUser();
405 $output = $this->getOutput();
407 # Show a message about replica DB lag, if applicable
408 $lag = wfGetLB()->safeGetLag( $dbr );
410 $output->showLagWarning( $lag );
413 # If no rows to display, show message before try to render the list
414 if ( $rows->numRows() == 0 ) {
415 $output->wrapWikiMsg(
416 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
421 $dbr->dataSeek( $rows, 0 );
423 $list = ChangesList
::newFromContext( $this->getContext(), $this->filterGroups
);
424 $list->setWatchlistDivs();
425 $list->initChangesListRows( $rows );
426 $dbr->dataSeek( $rows, 0 );
428 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
429 && $user->getOption( 'shownumberswatching' )
431 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
434 $s = $list->beginRecentChangesList();
435 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
437 foreach ( $rows as $obj ) {
439 $rc = RecentChange
::newFromRow( $obj );
441 # Skip CatWatch entries for hidden cats based on user preference
443 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
444 !$userShowHiddenCats &&
445 $rc->getParam( 'hidden-cat' )
450 $rc->counter
= $counter++
;
452 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
453 $updated = $obj->wl_notificationtimestamp
;
458 if ( isset( $watchedItemStore ) ) {
459 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
460 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
462 $rc->numberofWatchingusers
= 0;
465 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
466 if ( $changeLine !== false ) {
470 $s .= $list->endRecentChangesList();
472 $output->addHTML( $s );
476 * Set the text to be displayed above the changes
478 * @param FormOptions $opts
479 * @param int $numRows Number of rows in the result to show after this header
481 public function doHeader( $opts, $numRows ) {
482 $user = $this->getUser();
483 $out = $this->getOutput();
486 $this->msg( 'watchlistfor2', $user->getName() )
487 ->rawParams( SpecialEditWatchlist
::buildTools(
488 $this->getLanguage(),
489 $this->getLinkRenderer()
493 $this->setTopText( $opts );
495 $lang = $this->getLanguage();
496 if ( $opts['days'] > 0 ) {
497 $days = $opts['days'];
499 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
501 $timestamp = wfTimestampNow();
502 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
503 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
504 )->parse() . "<br />\n";
506 $nondefaults = $opts->getChangedValues();
507 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
509 # Spit out some control panel links
511 $context = $this->getContext();
512 $namesOfDisplayedFilters = [];
513 foreach ( $this->getFilterGroups() as $groupName => $group ) {
514 if ( !$group->isPerGroupRequestParameter() ) {
515 foreach ( $group->getFilters() as $filterName => $filter ) {
516 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
517 $namesOfDisplayedFilters[] = $filterName;
518 $links[] = $this->showHideCheck(
520 $filter->getShowHide(),
529 $hiddenFields = $nondefaults;
530 $hiddenFields['action'] = 'submit';
531 unset( $hiddenFields['namespace'] );
532 unset( $hiddenFields['invert'] );
533 unset( $hiddenFields['associated'] );
534 unset( $hiddenFields['days'] );
535 foreach ( $namesOfDisplayedFilters as $filterName ) {
536 unset( $hiddenFields[$filterName] );
542 # Namespace filter and put the whole form together.
544 $form .= $cutofflinks;
545 $form .= $this->msg( 'watchlist-hide' ) .
546 $this->msg( 'colon-separator' )->escaped() .
547 implode( ' ', $links );
548 $form .= "\n<br />\n";
549 $form .= Html
::namespaceSelector(
551 'selected' => $opts['namespace'],
553 'label' => $this->msg( 'namespace' )->text()
555 'name' => 'namespace',
557 'class' => 'namespaceselector',
560 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
561 $this->msg( 'invert' )->text(),
565 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
567 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
568 $this->msg( 'namespace_association' )->text(),
572 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
574 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
575 foreach ( $hiddenFields as $key => $value ) {
576 $form .= Html
::hidden( $key, $value ) . "\n";
578 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
579 $form .= Xml
::closeElement( 'form' ) . "\n";
580 $this->getOutput()->addHTML( $form );
582 $this->setBottomText( $opts );
585 function cutoffselector( $options ) {
586 // Cast everything to strings immediately, so that we know all of the values have the same
587 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
588 // default string representation, for example, and would confuse comparisons.
590 // Misleadingly, the 'days' option supports hours too.
591 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
593 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
594 // add the user preference, if it isn't available already
595 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
596 $days[] = $userWatchlistOption;
599 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
600 // add the maximum possible value, if it isn't available already
601 if ( !in_array( $maxDays, $days ) ) {
605 $selected = (string)$options['days'];
606 if ( $selected <= 0 ) {
607 $selected = $maxDays;
610 // add the currently selected value, if it isn't available already
611 if ( !in_array( $selected, $days ) ) {
615 $select = new XmlSelect( 'days', 'days', $selected );
618 foreach ( $days as $value ) {
620 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
622 $name = $this->msg( 'days' )->numParams( $value )->text();
624 $select->addOption( $name, $value );
627 return $select->getHTML() . "\n<br />\n";
630 function setTopText( FormOptions
$opts ) {
631 $nondefaults = $opts->getChangedValues();
633 $user = $this->getUser();
635 $numItems = $this->countItems();
636 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
638 // Show watchlist header
640 if ( $numItems == 0 ) {
641 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
643 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
644 if ( $this->getConfig()->get( 'EnotifWatchlist' )
645 && $user->getOption( 'enotifwatchlistpages' )
647 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
649 if ( $showUpdatedMarker ) {
650 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
655 if ( $numItems > 0 && $showUpdatedMarker ) {
656 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
657 'action' => $this->getPageTitle()->getLocalURL(),
658 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
659 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
660 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
661 Html
::hidden( 'reset', 'all' ) . "\n";
662 foreach ( $nondefaults as $key => $value ) {
663 $form .= Html
::hidden( $key, $value ) . "\n";
665 $form .= Xml
::closeElement( 'form' ) . "\n";
668 $form .= Xml
::openElement( 'form', [
670 'action' => wfScript(),
671 'id' => 'mw-watchlist-form'
673 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
674 $form .= Xml
::fieldset(
675 $this->msg( 'watchlist-options' )->text(),
677 [ 'id' => 'mw-watchlist-options' ]
680 $form .= $this->makeLegend();
682 $this->getOutput()->addHTML( $form );
685 protected function showHideCheck( $options, $message, $name, $value ) {
686 $options[$name] = 1 - (int)$value;
688 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
689 $this->msg( $message, '' )->text(),
697 * Count the number of paired items on a user's watchlist.
698 * The assumption made here is that when a subject page is watched a talk page is also watched.
699 * Hence the number of individual items is halved.
703 protected function countItems() {
704 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
705 $count = $store->countWatchedItems( $this->getUser() );
706 return floor( $count / 2 );