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',
61 $output->addModuleStyles( [ 'mediawiki.special.watchlist.styles' ] );
63 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
64 if ( $mode !== false ) {
65 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
66 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
67 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
68 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
70 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
73 $output->redirect( $title->getLocalURL() );
78 $this->checkPermissions();
80 $user = $this->getUser();
81 $opts = $this->getOptions();
83 $config = $this->getConfig();
84 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
85 && $request->getVal( 'reset' )
86 && $request->wasPosted()
87 && $user->matchEditToken( $request->getVal( 'token' ) )
89 $user->clearAllNotifications();
90 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
95 parent
::execute( $subpage );
99 * Return an array of subpages that this special page will accept.
101 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
102 * @return string[] subpages
104 public function getSubpagesForPrefixSearch() {
115 protected function transformFilterDefinition( array $filterDefinition ) {
116 if ( isset( $filterDefinition['showHideSuffix'] ) ) {
117 $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
120 return $filterDefinition;
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 if ( $reviewStatus !== null ) {
147 // Conditional on feature being available and rights
148 $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
149 $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
152 $authorship = $this->getFilterGroup( 'authorship' );
153 $hideMyself = $authorship->getFilter( 'hidemyself' );
154 $hideMyself->setDefault( $user->getBoolOption( 'watchlisthideown' ) );
156 $changeType = $this->getFilterGroup( 'changeType' );
157 $hideCategorization = $changeType->getFilter( 'hidecategorization' );
158 if ( $hideCategorization !== null ) {
159 // Conditional on feature being available
160 $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
165 * Get a FormOptions object containing the default options
167 * @return FormOptions
169 public function getDefaultOptions() {
170 $opts = parent
::getDefaultOptions();
171 $user = $this->getUser();
173 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
174 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
179 public function validateOptions( FormOptions
$opts ) {
180 $opts->validateBounds( 'days', 0, $this->maxDays
);
181 parent
::validateOptions( $opts );
185 * Get all custom filters
187 * @return array Map of filter URL param names to properties (msg/default)
189 protected function getCustomFilters() {
190 if ( $this->customFilters
=== null ) {
191 $this->customFilters
= parent
::getCustomFilters();
192 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
195 return $this->customFilters
;
199 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
201 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
202 * to the current ones.
204 * @param FormOptions $opts
205 * @return FormOptions
207 protected function fetchOptionsFromRequest( $opts ) {
208 static $compatibilityMap = [
209 'hideMinor' => 'hideminor',
210 'hideBots' => 'hidebots',
211 'hideAnons' => 'hideanons',
212 'hideLiu' => 'hideliu',
213 'hidePatrolled' => 'hidepatrolled',
214 'hideOwn' => 'hidemyself',
217 $params = $this->getRequest()->getValues();
218 foreach ( $compatibilityMap as $from => $to ) {
219 if ( isset( $params[$from] ) ) {
220 $params[$to] = $params[$from];
221 unset( $params[$from] );
225 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
226 $allBooleansFalse = [];
228 // If the user submitted the form, start with a baseline of "all
229 // booleans are false", then change the ones they checked. This
230 // means we ignore the defaults.
232 // This is how we handle the fact that HTML forms don't submit
234 foreach ( $this->filterGroups
as $filterGroup ) {
235 if ( $filterGroup instanceof ChangesListBooleanFilterGroup
) {
236 foreach ( $filterGroup->getFilters() as $filter ) {
237 $allBooleansFalse[$filter->getName()] = false;
242 $params = $params +
$allBooleansFalse;
245 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
246 // methods defined on WebRequest and removing this dependency would cause some code duplication.
247 $request = new DerivativeRequest( $this->getRequest(), $params );
248 $opts->fetchValuesFromRequest( $request );
256 protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
257 &$join_conds, FormOptions
$opts
259 $dbr = $this->getDB();
260 parent
::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
264 if ( $opts['days'] > 0 ) {
265 $conds[] = 'rc_timestamp > ' .
266 $dbr->addQuotes( $dbr->timestamp( time() - $opts['days'] * 3600 * 24 ) );
273 protected function doMainQuery( $tables, $fields, $conds, $query_options,
274 $join_conds, FormOptions
$opts
276 $dbr = $this->getDB();
277 $user = $this->getUser();
279 # Toggle watchlist content (all recent edits or just the latest)
280 if ( $opts['extended'] ) {
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,
298 $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
299 $fields = array_merge( RecentChange
::selectFields(), $fields );
301 $query_options = array_merge( [
302 'ORDER BY' => 'rc_timestamp DESC',
303 'LIMIT' => $user->getIntOption( 'wllimit' )
305 $join_conds = array_merge(
310 'wl_user' => $user->getId(),
311 'wl_namespace=rc_namespace',
319 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
320 $fields[] = 'wl_notificationtimestamp';
323 $rollbacker = $user->isAllowed( 'rollback' );
324 if ( $usePage ||
$rollbacker ) {
326 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
328 $fields[] = 'page_latest';
332 // Log entries with DELETED_ACTION must not show up unless the user has
333 // the necessary rights.
334 if ( !$user->isAllowed( 'deletedhistory' ) ) {
335 $bitmask = LogPage
::DELETED_ACTION
;
336 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
337 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
342 $conds[] = $dbr->makeList( [
343 'rc_type != ' . RC_LOG
,
344 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
348 ChangeTags
::modifyDisplayQuery(
357 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
369 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
372 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
374 'SpecialWatchlistQuery',
375 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
381 * Return a IDatabase object for reading
385 protected function getDB() {
386 return wfGetDB( DB_REPLICA
, 'watchlist' );
392 public function outputFeedLinks() {
393 $user = $this->getUser();
394 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
396 $this->addFeedLinks( [
397 'action' => 'feedwatchlist',
399 'wlowner' => $user->getName(),
400 'wltoken' => $wlToken,
406 * Build and output the actual changes list.
408 * @param ResultWrapper $rows Database rows
409 * @param FormOptions $opts
411 public function outputChangesList( $rows, $opts ) {
412 $dbr = $this->getDB();
413 $user = $this->getUser();
414 $output = $this->getOutput();
416 # Show a message about replica DB lag, if applicable
417 $lag = wfGetLB()->safeGetLag( $dbr );
419 $output->showLagWarning( $lag );
422 # If no rows to display, show message before try to render the list
423 if ( $rows->numRows() == 0 ) {
424 $output->wrapWikiMsg(
425 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
430 $dbr->dataSeek( $rows, 0 );
432 $list = ChangesList
::newFromContext( $this->getContext(), $this->filterGroups
);
433 $list->setWatchlistDivs();
434 $list->initChangesListRows( $rows );
435 if ( $user->getOption( 'watchlistunwatchlinks' ) ) {
436 $list->setChangeLinePrefixer( function ( RecentChange
$rc, ChangesList
$cl, $grouped ) {
437 // Don't show unwatch link if the line is a grouped log entry using EnhancedChangesList,
438 // since EnhancedChangesList groups log entries by performer rather than by target article
439 if ( $rc->mAttribs
['rc_type'] == RC_LOG
&& $cl instanceof EnhancedChangesList
&&
443 return $this->getLinkRenderer()
444 ->makeKnownLink( $rc->getTitle(),
445 $this->msg( 'watchlist-unwatch' )->text(), [
446 'class' => 'mw-unwatch-link',
447 'title' => $this->msg( 'tooltip-ca-unwatch' )->text()
448 ], [ 'action' => 'unwatch' ] ) . ' ';
452 $dbr->dataSeek( $rows, 0 );
454 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
455 && $user->getOption( 'shownumberswatching' )
457 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
460 $s = $list->beginRecentChangesList();
461 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
463 foreach ( $rows as $obj ) {
465 $rc = RecentChange
::newFromRow( $obj );
467 # Skip CatWatch entries for hidden cats based on user preference
469 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
470 !$userShowHiddenCats &&
471 $rc->getParam( 'hidden-cat' )
476 $rc->counter
= $counter++
;
478 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
479 $updated = $obj->wl_notificationtimestamp
;
484 if ( isset( $watchedItemStore ) ) {
485 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
486 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
488 $rc->numberofWatchingusers
= 0;
491 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
492 if ( $changeLine !== false ) {
496 $s .= $list->endRecentChangesList();
498 $output->addHTML( $s );
502 * Set the text to be displayed above the changes
504 * @param FormOptions $opts
505 * @param int $numRows Number of rows in the result to show after this header
507 public function doHeader( $opts, $numRows ) {
508 $user = $this->getUser();
509 $out = $this->getOutput();
512 $this->msg( 'watchlistfor2', $user->getName() )
513 ->rawParams( SpecialEditWatchlist
::buildTools(
514 $this->getLanguage(),
515 $this->getLinkRenderer()
519 $this->setTopText( $opts );
521 $lang = $this->getLanguage();
522 if ( $opts['days'] > 0 ) {
523 $days = $opts['days'];
525 $days = $this->maxDays
;
527 $timestamp = wfTimestampNow();
528 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
529 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
530 )->parse() . "<br />\n";
532 $nondefaults = $opts->getChangedValues();
533 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
535 # Spit out some control panel links
537 $context = $this->getContext();
538 $namesOfDisplayedFilters = [];
539 foreach ( $this->getFilterGroups() as $groupName => $group ) {
540 if ( !$group->isPerGroupRequestParameter() ) {
541 foreach ( $group->getFilters() as $filterName => $filter ) {
542 if ( $filter->displaysOnUnstructuredUi( $this ) ) {
543 $namesOfDisplayedFilters[] = $filterName;
544 $links[] = $this->showHideCheck(
546 $filter->getShowHide(),
555 $hiddenFields = $nondefaults;
556 $hiddenFields['action'] = 'submit';
557 unset( $hiddenFields['namespace'] );
558 unset( $hiddenFields['invert'] );
559 unset( $hiddenFields['associated'] );
560 unset( $hiddenFields['days'] );
561 foreach ( $namesOfDisplayedFilters as $filterName ) {
562 unset( $hiddenFields[$filterName] );
568 # Namespace filter and put the whole form together.
570 $form .= $cutofflinks;
571 $form .= $this->msg( 'watchlist-hide' ) .
572 $this->msg( 'colon-separator' )->escaped() .
573 implode( ' ', $links );
574 $form .= "\n<br />\n";
575 $form .= Html
::namespaceSelector(
577 'selected' => $opts['namespace'],
579 'label' => $this->msg( 'namespace' )->text()
581 'name' => 'namespace',
583 'class' => 'namespaceselector',
586 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
587 $this->msg( 'invert' )->text(),
591 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
593 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
594 $this->msg( 'namespace_association' )->text(),
598 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
600 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
601 foreach ( $hiddenFields as $key => $value ) {
602 $form .= Html
::hidden( $key, $value ) . "\n";
604 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
605 $form .= Xml
::closeElement( 'form' ) . "\n";
606 $this->getOutput()->addHTML( $form );
608 $this->setBottomText( $opts );
611 function cutoffselector( $options ) {
612 // Cast everything to strings immediately, so that we know all of the values have the same
613 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
614 // default string representation, for example, and would confuse comparisons.
616 // Misleadingly, the 'days' option supports hours too.
617 $days = array_map( 'strval', [ 1 / 24, 2 / 24, 6 / 24, 12 / 24, 1, 3, 7 ] );
619 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
620 // add the user preference, if it isn't available already
621 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
622 $days[] = $userWatchlistOption;
625 $maxDays = (string)$this->maxDays
;
626 // add the maximum possible value, if it isn't available already
627 if ( !in_array( $maxDays, $days ) ) {
631 $selected = (string)$options['days'];
632 if ( $selected <= 0 ) {
633 $selected = $maxDays;
636 // add the currently selected value, if it isn't available already
637 if ( !in_array( $selected, $days ) ) {
641 $select = new XmlSelect( 'days', 'days', $selected );
644 foreach ( $days as $value ) {
646 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
648 $name = $this->msg( 'days' )->numParams( $value )->text();
650 $select->addOption( $name, $value );
653 return $select->getHTML() . "\n<br />\n";
656 function setTopText( FormOptions
$opts ) {
657 $nondefaults = $opts->getChangedValues();
659 $user = $this->getUser();
661 $numItems = $this->countItems();
662 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
664 // Show watchlist header
666 if ( $numItems == 0 ) {
667 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
669 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
670 if ( $this->getConfig()->get( 'EnotifWatchlist' )
671 && $user->getOption( 'enotifwatchlistpages' )
673 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
675 if ( $showUpdatedMarker ) {
676 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
681 if ( $numItems > 0 && $showUpdatedMarker ) {
682 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
683 'action' => $this->getPageTitle()->getLocalURL(),
684 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
685 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
686 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
687 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
688 Html
::hidden( 'reset', 'all' ) . "\n";
689 foreach ( $nondefaults as $key => $value ) {
690 $form .= Html
::hidden( $key, $value ) . "\n";
692 $form .= Xml
::closeElement( 'form' ) . "\n";
695 $form .= Xml
::openElement( 'form', [
697 'action' => wfScript(),
698 'id' => 'mw-watchlist-form'
700 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
701 $form .= Xml
::fieldset(
702 $this->msg( 'watchlist-options' )->text(),
704 [ 'id' => 'mw-watchlist-options' ]
707 $form .= $this->makeLegend();
709 $this->getOutput()->addHTML( $form );
712 protected function showHideCheck( $options, $message, $name, $value ) {
713 $options[$name] = 1 - (int)$value;
715 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
716 $this->msg( $message, '' )->text(),
724 * Count the number of paired items on a user's watchlist.
725 * The assumption made here is that when a subject page is watched a talk page is also watched.
726 * Hence the number of individual items is halved.
730 protected function countItems() {
731 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
732 $count = $store->countWatchedItems( $this->getUser() );
733 return floor( $count / 2 );