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 );
38 $this->maxDays
= $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
41 public function doesWrites() {
46 * Main execution point
48 * @param string $subpage
50 function execute( $subpage ) {
51 // Anons don't get a watchlist
52 $this->requireLogin( 'watchlistanontext' );
54 $output = $this->getOutput();
55 $request = $this->getRequest();
56 $this->addHelpLink( 'Help:Watching pages' );
57 $output->addModules( [
58 'mediawiki.special.changeslist.visitedstatus',
59 'mediawiki.special.watchlist',
62 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
63 if ( $mode !== false ) {
64 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
65 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
66 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
67 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
69 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
72 $output->redirect( $title->getLocalURL() );
77 $this->checkPermissions();
79 $user = $this->getUser();
80 $opts = $this->getOptions();
82 $config = $this->getConfig();
83 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
84 && $request->getVal( 'reset' )
85 && $request->wasPosted()
86 && $user->matchEditToken( $request->getVal( 'token' ) )
88 $user->clearAllNotifications();
89 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
94 parent
::execute( $subpage );
98 * Return an array of subpages that this special page will accept.
100 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
101 * @return string[] subpages
103 public function getSubpagesForPrefixSearch() {
114 protected function transformFilterDefinition( array $filterDefinition ) {
115 if ( isset( $filterDefinition['showHideSuffix'] ) ) {
116 $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
119 return $filterDefinition;
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 if ( $reviewStatus !== null ) {
146 // Conditional on feature being available and rights
147 $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
148 $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
151 $authorship = $this->getFilterGroup( 'authorship' );
152 $hideMyself = $authorship->getFilter( 'hidemyself' );
153 $hideMyself->setDefault( $user->getBoolOption( 'watchlisthideown' ) );
155 $changeType = $this->getFilterGroup( 'changeType' );
156 $hideCategorization = $changeType->getFilter( 'hidecategorization' );
157 if ( $hideCategorization !== null ) {
158 // Conditional on feature being available
159 $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
164 * Get a FormOptions object containing the default options
166 * @return FormOptions
168 public function getDefaultOptions() {
169 $opts = parent
::getDefaultOptions();
170 $user = $this->getUser();
172 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
173 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
178 public function validateOptions( FormOptions
$opts ) {
179 $opts->validateBounds( 'days', 0, $this->maxDays
);
180 parent
::validateOptions( $opts );
184 * Get all custom filters
186 * @return array Map of filter URL param names to properties (msg/default)
188 protected function getCustomFilters() {
189 if ( $this->customFilters
=== null ) {
190 $this->customFilters
= parent
::getCustomFilters();
191 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
194 return $this->customFilters
;
198 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
200 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
201 * to the current ones.
203 * @param FormOptions $opts
204 * @return FormOptions
206 protected function fetchOptionsFromRequest( $opts ) {
207 static $compatibilityMap = [
208 'hideMinor' => 'hideminor',
209 'hideBots' => 'hidebots',
210 'hideAnons' => 'hideanons',
211 'hideLiu' => 'hideliu',
212 'hidePatrolled' => 'hidepatrolled',
213 'hideOwn' => 'hidemyself',
216 $params = $this->getRequest()->getValues();
217 foreach ( $compatibilityMap as $from => $to ) {
218 if ( isset( $params[$from] ) ) {
219 $params[$to] = $params[$from];
220 unset( $params[$from] );
224 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
225 $allBooleansFalse = [];
227 // If the user submitted the form, start with a baseline of "all
228 // booleans are false", then change the ones they checked. This
229 // means we ignore the defaults.
231 // This is how we handle the fact that HTML forms don't submit
233 foreach ( $this->filterGroups
as $filterGroup ) {
234 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
235 foreach ( $filterGroup->getFilters() as $filter ) {
236 $allBooleansFalse[$filter->getName()] = false;
241 $params = $params +
$allBooleansFalse;
244 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
245 // methods defined on WebRequest and removing this dependency would cause some code duplication.
246 $request = new DerivativeRequest( $this->getRequest(), $params );
247 $opts->fetchValuesFromRequest( $request );
255 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
256 &$join_conds, FormOptions
$opts
258 $dbr = $this->getDB();
259 parent
::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
263 if ( $opts['days'] > 0 ) {
264 $conds[] = 'rc_timestamp > ' .
265 $dbr->addQuotes( $dbr->timestamp( time() - $opts['days'] * 3600 * 24 ) );
272 protected function doMainQuery( $tables, $fields, $conds, $query_options,
273 $join_conds, FormOptions
$opts
275 $dbr = $this->getDB();
276 $user = $this->getUser();
278 # Toggle watchlist content (all recent edits or just the latest)
279 if ( $opts['extended'] ) {
280 $limitWatchlist = $user->getIntOption( 'wllimit' );
283 # Top log Ids for a page are not stored
284 $nonRevisionTypes = [ RC_LOG
];
285 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
286 if ( $nonRevisionTypes ) {
287 $conds[] = $dbr->makeList(
289 'rc_this_oldid=page_latest',
290 'rc_type' => $nonRevisionTypes,
299 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
300 $fields = array_merge( RecentChange
::selectFields(), $fields );
302 $query_options = array_merge( [ 'ORDER BY' => 'rc_timestamp DESC' ], $query_options );
303 $join_conds = array_merge(
308 'wl_user' => $user->getId(),
309 'wl_namespace=rc_namespace',
317 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
318 $fields[] = 'wl_notificationtimestamp';
320 if ( $limitWatchlist ) {
321 $query_options['LIMIT'] = $limitWatchlist;
324 $rollbacker = $user->isAllowed( 'rollback' );
325 if ( $usePage ||
$rollbacker ) {
327 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
329 $fields[] = 'page_latest';
333 // Log entries with DELETED_ACTION must not show up unless the user has
334 // the necessary rights.
335 if ( !$user->isAllowed( 'deletedhistory' ) ) {
336 $bitmask = LogPage
::DELETED_ACTION
;
337 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
338 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
343 $conds[] = $dbr->makeList( [
344 'rc_type != ' . RC_LOG
,
345 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
349 ChangeTags
::modifyDisplayQuery(
358 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
370 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
373 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
375 'SpecialWatchlistQuery',
376 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
382 * Return a IDatabase object for reading
386 protected function getDB() {
387 return wfGetDB( DB_REPLICA
, 'watchlist' );
393 public function outputFeedLinks() {
394 $user = $this->getUser();
395 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
397 $this->addFeedLinks( [
398 'action' => 'feedwatchlist',
400 'wlowner' => $user->getName(),
401 'wltoken' => $wlToken,
407 * Build and output the actual changes list.
409 * @param ResultWrapper $rows Database rows
410 * @param FormOptions $opts
412 public function outputChangesList( $rows, $opts ) {
413 $dbr = $this->getDB();
414 $user = $this->getUser();
415 $output = $this->getOutput();
417 # Show a message about replica DB lag, if applicable
418 $lag = wfGetLB()->safeGetLag( $dbr );
420 $output->showLagWarning( $lag );
423 # If no rows to display, show message before try to render the list
424 if ( $rows->numRows() == 0 ) {
425 $output->wrapWikiMsg(
426 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
431 $dbr->dataSeek( $rows, 0 );
433 $list = ChangesList
::newFromContext( $this->getContext(), $this->filterGroups
);
434 $list->setWatchlistDivs();
435 $list->initChangesListRows( $rows );
436 $dbr->dataSeek( $rows, 0 );
438 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
439 && $user->getOption( 'shownumberswatching' )
441 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
444 $s = $list->beginRecentChangesList();
445 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
447 foreach ( $rows as $obj ) {
449 $rc = RecentChange
::newFromRow( $obj );
451 # Skip CatWatch entries for hidden cats based on user preference
453 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
454 !$userShowHiddenCats &&
455 $rc->getParam( 'hidden-cat' )
460 $rc->counter
= $counter++
;
462 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
463 $updated = $obj->wl_notificationtimestamp
;
468 if ( isset( $watchedItemStore ) ) {
469 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
470 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
472 $rc->numberofWatchingusers
= 0;
475 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
476 if ( $changeLine !== false ) {
480 $s .= $list->endRecentChangesList();
482 $output->addHTML( $s );
486 * Set the text to be displayed above the changes
488 * @param FormOptions $opts
489 * @param int $numRows Number of rows in the result to show after this header
491 public function doHeader( $opts, $numRows ) {
492 $user = $this->getUser();
493 $out = $this->getOutput();
496 $this->msg( 'watchlistfor2', $user->getName() )
497 ->rawParams( SpecialEditWatchlist
::buildTools(
498 $this->getLanguage(),
499 $this->getLinkRenderer()
503 $this->setTopText( $opts );
505 $lang = $this->getLanguage();
506 if ( $opts['days'] > 0 ) {
507 $days = $opts['days'];
509 $days = $this->maxDays
;
511 $timestamp = wfTimestampNow();
512 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
513 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
514 )->parse() . "<br />\n";
516 $nondefaults = $opts->getChangedValues();
517 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
519 # Spit out some control panel links
521 $context = $this->getContext();
522 $namesOfDisplayedFilters = [];
523 foreach ( $this->getFilterGroups() as $groupName => $group ) {
524 if ( !$group->isPerGroupRequestParameter() ) {
525 foreach ( $group->getFilters() as $filterName => $filter ) {
526 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
527 $namesOfDisplayedFilters[] = $filterName;
528 $links[] = $this->showHideCheck(
530 $filter->getShowHide(),
539 $hiddenFields = $nondefaults;
540 $hiddenFields['action'] = 'submit';
541 unset( $hiddenFields['namespace'] );
542 unset( $hiddenFields['invert'] );
543 unset( $hiddenFields['associated'] );
544 unset( $hiddenFields['days'] );
545 foreach ( $namesOfDisplayedFilters as $filterName ) {
546 unset( $hiddenFields[$filterName] );
552 # Namespace filter and put the whole form together.
554 $form .= $cutofflinks;
555 $form .= $this->msg( 'watchlist-hide' ) .
556 $this->msg( 'colon-separator' )->escaped() .
557 implode( ' ', $links );
558 $form .= "\n<br />\n";
559 $form .= Html
::namespaceSelector(
561 'selected' => $opts['namespace'],
563 'label' => $this->msg( 'namespace' )->text()
565 'name' => 'namespace',
567 'class' => 'namespaceselector',
570 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
571 $this->msg( 'invert' )->text(),
575 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
577 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
578 $this->msg( 'namespace_association' )->text(),
582 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
584 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
585 foreach ( $hiddenFields as $key => $value ) {
586 $form .= Html
::hidden( $key, $value ) . "\n";
588 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
589 $form .= Xml
::closeElement( 'form' ) . "\n";
590 $this->getOutput()->addHTML( $form );
592 $this->setBottomText( $opts );
595 function cutoffselector( $options ) {
596 // Cast everything to strings immediately, so that we know all of the values have the same
597 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
598 // default string representation, for example, and would confuse comparisons.
600 // Misleadingly, the 'days' option supports hours too.
601 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
603 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
604 // add the user preference, if it isn't available already
605 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
606 $days[] = $userWatchlistOption;
609 $maxDays = (string)$this->maxDays
;
610 // add the maximum possible value, if it isn't available already
611 if ( !in_array( $maxDays, $days ) ) {
615 $selected = (string)$options['days'];
616 if ( $selected <= 0 ) {
617 $selected = $maxDays;
620 // add the currently selected value, if it isn't available already
621 if ( !in_array( $selected, $days ) ) {
625 $select = new XmlSelect( 'days', 'days', $selected );
628 foreach ( $days as $value ) {
630 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
632 $name = $this->msg( 'days' )->numParams( $value )->text();
634 $select->addOption( $name, $value );
637 return $select->getHTML() . "\n<br />\n";
640 function setTopText( FormOptions
$opts ) {
641 $nondefaults = $opts->getChangedValues();
643 $user = $this->getUser();
645 $numItems = $this->countItems();
646 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
648 // Show watchlist header
650 if ( $numItems == 0 ) {
651 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
653 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
654 if ( $this->getConfig()->get( 'EnotifWatchlist' )
655 && $user->getOption( 'enotifwatchlistpages' )
657 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
659 if ( $showUpdatedMarker ) {
660 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
665 if ( $numItems > 0 && $showUpdatedMarker ) {
666 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
667 'action' => $this->getPageTitle()->getLocalURL(),
668 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
669 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
670 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
671 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
672 Html
::hidden( 'reset', 'all' ) . "\n";
673 foreach ( $nondefaults as $key => $value ) {
674 $form .= Html
::hidden( $key, $value ) . "\n";
676 $form .= Xml
::closeElement( 'form' ) . "\n";
679 $form .= Xml
::openElement( 'form', [
681 'action' => wfScript(),
682 'id' => 'mw-watchlist-form'
684 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
685 $form .= Xml
::fieldset(
686 $this->msg( 'watchlist-options' )->text(),
688 [ 'id' => 'mw-watchlist-options' ]
691 $form .= $this->makeLegend();
693 $this->getOutput()->addHTML( $form );
696 protected function showHideCheck( $options, $message, $name, $value ) {
697 $options[$name] = 1 - (int)$value;
699 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
700 $this->msg( $message, '' )->text(),
708 * Count the number of paired items on a user's watchlist.
709 * The assumption made here is that when a subject page is watched a talk page is also watched.
710 * Hence the number of individual items is halved.
714 protected function countItems() {
715 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
716 $count = $store->countWatchedItems( $this->getUser() );
717 return floor( $count / 2 );