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'] ) {
282 # Top log Ids for a page are not stored
283 $nonRevisionTypes = [ RC_LOG
];
284 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
285 if ( $nonRevisionTypes ) {
286 $conds[] = $dbr->makeList(
288 'rc_this_oldid=page_latest',
289 'rc_type' => $nonRevisionTypes,
297 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
298 $fields = array_merge( RecentChange
::selectFields(), $fields );
300 $query_options = array_merge( [
301 'ORDER BY' => 'rc_timestamp DESC',
302 'LIMIT' => $user->getIntOption( 'wllimit' )
304 $join_conds = array_merge(
309 'wl_user' => $user->getId(),
310 'wl_namespace=rc_namespace',
318 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
319 $fields[] = 'wl_notificationtimestamp';
322 $rollbacker = $user->isAllowed( 'rollback' );
323 if ( $usePage ||
$rollbacker ) {
325 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
327 $fields[] = 'page_latest';
331 // Log entries with DELETED_ACTION must not show up unless the user has
332 // the necessary rights.
333 if ( !$user->isAllowed( 'deletedhistory' ) ) {
334 $bitmask = LogPage
::DELETED_ACTION
;
335 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
336 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
341 $conds[] = $dbr->makeList( [
342 'rc_type != ' . RC_LOG
,
343 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
347 ChangeTags
::modifyDisplayQuery(
356 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
368 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
371 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
373 'SpecialWatchlistQuery',
374 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
380 * Return a IDatabase object for reading
384 protected function getDB() {
385 return wfGetDB( DB_REPLICA
, 'watchlist' );
391 public function outputFeedLinks() {
392 $user = $this->getUser();
393 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
395 $this->addFeedLinks( [
396 'action' => 'feedwatchlist',
398 'wlowner' => $user->getName(),
399 'wltoken' => $wlToken,
405 * Build and output the actual changes list.
407 * @param ResultWrapper $rows Database rows
408 * @param FormOptions $opts
410 public function outputChangesList( $rows, $opts ) {
411 $dbr = $this->getDB();
412 $user = $this->getUser();
413 $output = $this->getOutput();
415 # Show a message about replica DB lag, if applicable
416 $lag = wfGetLB()->safeGetLag( $dbr );
418 $output->showLagWarning( $lag );
421 # If no rows to display, show message before try to render the list
422 if ( $rows->numRows() == 0 ) {
423 $output->wrapWikiMsg(
424 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
429 $dbr->dataSeek( $rows, 0 );
431 $list = ChangesList
::newFromContext( $this->getContext(), $this->filterGroups
);
432 $list->setWatchlistDivs();
433 $list->initChangesListRows( $rows );
434 $dbr->dataSeek( $rows, 0 );
436 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
437 && $user->getOption( 'shownumberswatching' )
439 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
442 $s = $list->beginRecentChangesList();
443 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
445 foreach ( $rows as $obj ) {
447 $rc = RecentChange
::newFromRow( $obj );
449 # Skip CatWatch entries for hidden cats based on user preference
451 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
452 !$userShowHiddenCats &&
453 $rc->getParam( 'hidden-cat' )
458 $rc->counter
= $counter++
;
460 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
461 $updated = $obj->wl_notificationtimestamp
;
466 if ( isset( $watchedItemStore ) ) {
467 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
468 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
470 $rc->numberofWatchingusers
= 0;
473 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
474 if ( $changeLine !== false ) {
478 $s .= $list->endRecentChangesList();
480 $output->addHTML( $s );
484 * Set the text to be displayed above the changes
486 * @param FormOptions $opts
487 * @param int $numRows Number of rows in the result to show after this header
489 public function doHeader( $opts, $numRows ) {
490 $user = $this->getUser();
491 $out = $this->getOutput();
494 $this->msg( 'watchlistfor2', $user->getName() )
495 ->rawParams( SpecialEditWatchlist
::buildTools(
496 $this->getLanguage(),
497 $this->getLinkRenderer()
501 $this->setTopText( $opts );
503 $lang = $this->getLanguage();
504 if ( $opts['days'] > 0 ) {
505 $days = $opts['days'];
507 $days = $this->maxDays
;
509 $timestamp = wfTimestampNow();
510 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
511 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
512 )->parse() . "<br />\n";
514 $nondefaults = $opts->getChangedValues();
515 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
517 # Spit out some control panel links
519 $context = $this->getContext();
520 $namesOfDisplayedFilters = [];
521 foreach ( $this->getFilterGroups() as $groupName => $group ) {
522 if ( !$group->isPerGroupRequestParameter() ) {
523 foreach ( $group->getFilters() as $filterName => $filter ) {
524 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
525 $namesOfDisplayedFilters[] = $filterName;
526 $links[] = $this->showHideCheck(
528 $filter->getShowHide(),
537 $hiddenFields = $nondefaults;
538 $hiddenFields['action'] = 'submit';
539 unset( $hiddenFields['namespace'] );
540 unset( $hiddenFields['invert'] );
541 unset( $hiddenFields['associated'] );
542 unset( $hiddenFields['days'] );
543 foreach ( $namesOfDisplayedFilters as $filterName ) {
544 unset( $hiddenFields[$filterName] );
550 # Namespace filter and put the whole form together.
552 $form .= $cutofflinks;
553 $form .= $this->msg( 'watchlist-hide' ) .
554 $this->msg( 'colon-separator' )->escaped() .
555 implode( ' ', $links );
556 $form .= "\n<br />\n";
557 $form .= Html
::namespaceSelector(
559 'selected' => $opts['namespace'],
561 'label' => $this->msg( 'namespace' )->text()
563 'name' => 'namespace',
565 'class' => 'namespaceselector',
568 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
569 $this->msg( 'invert' )->text(),
573 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
575 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
576 $this->msg( 'namespace_association' )->text(),
580 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
582 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
583 foreach ( $hiddenFields as $key => $value ) {
584 $form .= Html
::hidden( $key, $value ) . "\n";
586 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
587 $form .= Xml
::closeElement( 'form' ) . "\n";
588 $this->getOutput()->addHTML( $form );
590 $this->setBottomText( $opts );
593 function cutoffselector( $options ) {
594 // Cast everything to strings immediately, so that we know all of the values have the same
595 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
596 // default string representation, for example, and would confuse comparisons.
598 // Misleadingly, the 'days' option supports hours too.
599 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
601 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
602 // add the user preference, if it isn't available already
603 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
604 $days[] = $userWatchlistOption;
607 $maxDays = (string)$this->maxDays
;
608 // add the maximum possible value, if it isn't available already
609 if ( !in_array( $maxDays, $days ) ) {
613 $selected = (string)$options['days'];
614 if ( $selected <= 0 ) {
615 $selected = $maxDays;
618 // add the currently selected value, if it isn't available already
619 if ( !in_array( $selected, $days ) ) {
623 $select = new XmlSelect( 'days', 'days', $selected );
626 foreach ( $days as $value ) {
628 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
630 $name = $this->msg( 'days' )->numParams( $value )->text();
632 $select->addOption( $name, $value );
635 return $select->getHTML() . "\n<br />\n";
638 function setTopText( FormOptions
$opts ) {
639 $nondefaults = $opts->getChangedValues();
641 $user = $this->getUser();
643 $numItems = $this->countItems();
644 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
646 // Show watchlist header
648 if ( $numItems == 0 ) {
649 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
651 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
652 if ( $this->getConfig()->get( 'EnotifWatchlist' )
653 && $user->getOption( 'enotifwatchlistpages' )
655 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
657 if ( $showUpdatedMarker ) {
658 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
663 if ( $numItems > 0 && $showUpdatedMarker ) {
664 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
665 'action' => $this->getPageTitle()->getLocalURL(),
666 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
667 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
668 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
669 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
670 Html
::hidden( 'reset', 'all' ) . "\n";
671 foreach ( $nondefaults as $key => $value ) {
672 $form .= Html
::hidden( $key, $value ) . "\n";
674 $form .= Xml
::closeElement( 'form' ) . "\n";
677 $form .= Xml
::openElement( 'form', [
679 'action' => wfScript(),
680 'id' => 'mw-watchlist-form'
682 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
683 $form .= Xml
::fieldset(
684 $this->msg( 'watchlist-options' )->text(),
686 [ 'id' => 'mw-watchlist-options' ]
689 $form .= $this->makeLegend();
691 $this->getOutput()->addHTML( $form );
694 protected function showHideCheck( $options, $message, $name, $value ) {
695 $options[$name] = 1 - (int)$value;
697 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
698 $this->msg( $message, '' )->text(),
706 * Count the number of paired items on a user's watchlist.
707 * The assumption made here is that when a subject page is watched a talk page is also watched.
708 * Hence the number of individual items is halved.
712 protected function countItems() {
713 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
714 $count = $store->countWatchedItems( $this->getUser() );
715 return floor( $count / 2 );