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() {
108 * Get a FormOptions object containing the default options
110 * @return FormOptions
112 public function getDefaultOptions() {
113 $opts = parent
::getDefaultOptions();
114 $user = $this->getUser();
116 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
117 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
118 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
119 // The user has submitted the form, so we dont need the default values
123 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
124 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
125 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
126 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
127 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
128 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
129 $opts->add( 'hidecategorization', $user->getBoolOption( 'watchlisthidecategorization' ) );
135 * Get all custom filters
137 * @return array Map of filter URL param names to properties (msg/default)
139 protected function getCustomFilters() {
140 if ( $this->customFilters
=== null ) {
141 $this->customFilters
= parent
::getCustomFilters();
142 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
145 return $this->customFilters
;
149 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
151 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
152 * to the current ones.
154 * @param FormOptions $opts
155 * @return FormOptions
157 protected function fetchOptionsFromRequest( $opts ) {
158 static $compatibilityMap = [
159 'hideMinor' => 'hideminor',
160 'hideBots' => 'hidebots',
161 'hideAnons' => 'hideanons',
162 'hideLiu' => 'hideliu',
163 'hidePatrolled' => 'hidepatrolled',
164 'hideOwn' => 'hidemyself',
167 $params = $this->getRequest()->getValues();
168 foreach ( $compatibilityMap as $from => $to ) {
169 if ( isset( $params[$from] ) ) {
170 $params[$to] = $params[$from];
171 unset( $params[$from] );
175 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
176 // methods defined on WebRequest and removing this dependency would cause some code duplication.
177 $request = new DerivativeRequest( $this->getRequest(), $params );
178 $opts->fetchValuesFromRequest( $request );
184 * Return an array of conditions depending of options set in $opts
186 * @param FormOptions $opts
189 public function buildMainQueryConds( FormOptions
$opts ) {
190 $dbr = $this->getDB();
191 $conds = parent
::buildMainQueryConds( $opts );
194 if ( $opts['days'] > 0 ) {
195 $conds[] = 'rc_timestamp > ' .
196 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
205 * @param array $conds
206 * @param FormOptions $opts
207 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
209 public function doMainQuery( $conds, $opts ) {
210 $dbr = $this->getDB();
211 $user = $this->getUser();
213 # Toggle watchlist content (all recent edits or just the latest)
214 if ( $opts['extended'] ) {
215 $limitWatchlist = $user->getIntOption( 'wllimit' );
218 # Top log Ids for a page are not stored
219 $nonRevisionTypes = [ RC_LOG
];
220 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
221 if ( $nonRevisionTypes ) {
222 $conds[] = $dbr->makeList(
224 'rc_this_oldid=page_latest',
225 'rc_type' => $nonRevisionTypes,
234 $tables = [ 'recentchanges', 'watchlist' ];
235 $fields = RecentChange
::selectFields();
236 $query_options = [ 'ORDER BY' => 'rc_timestamp DESC' ];
241 'wl_user' => $user->getId(),
242 'wl_namespace=rc_namespace',
248 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
249 $fields[] = 'wl_notificationtimestamp';
251 if ( $limitWatchlist ) {
252 $query_options['LIMIT'] = $limitWatchlist;
255 $rollbacker = $user->isAllowed( 'rollback' );
256 if ( $usePage ||
$rollbacker ) {
258 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
260 $fields[] = 'page_latest';
264 // Log entries with DELETED_ACTION must not show up unless the user has
265 // the necessary rights.
266 if ( !$user->isAllowed( 'deletedhistory' ) ) {
267 $bitmask = LogPage
::DELETED_ACTION
;
268 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
269 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
274 $conds[] = $dbr->makeList( [
275 'rc_type != ' . RC_LOG
,
276 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
280 ChangeTags
::modifyDisplayQuery(
289 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
301 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
304 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
306 'SpecialWatchlistQuery',
307 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
313 * Return a IDatabase object for reading
317 protected function getDB() {
318 return wfGetDB( DB_REPLICA
, 'watchlist' );
324 public function outputFeedLinks() {
325 $user = $this->getUser();
326 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
328 $this->addFeedLinks( [
329 'action' => 'feedwatchlist',
331 'wlowner' => $user->getName(),
332 'wltoken' => $wlToken,
338 * Build and output the actual changes list.
340 * @param ResultWrapper $rows Database rows
341 * @param FormOptions $opts
343 public function outputChangesList( $rows, $opts ) {
344 $dbr = $this->getDB();
345 $user = $this->getUser();
346 $output = $this->getOutput();
348 # Show a message about replica DB lag, if applicable
349 $lag = wfGetLB()->safeGetLag( $dbr );
351 $output->showLagWarning( $lag );
354 # If no rows to display, show message before try to render the list
355 if ( $rows->numRows() == 0 ) {
356 $output->wrapWikiMsg(
357 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
362 $dbr->dataSeek( $rows, 0 );
364 $list = ChangesList
::newFromContext( $this->getContext() );
365 $list->setWatchlistDivs();
366 $list->initChangesListRows( $rows );
367 $dbr->dataSeek( $rows, 0 );
369 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
370 && $user->getOption( 'shownumberswatching' )
372 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
375 $s = $list->beginRecentChangesList();
376 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
378 foreach ( $rows as $obj ) {
380 $rc = RecentChange
::newFromRow( $obj );
382 # Skip CatWatch entries for hidden cats based on user preference
384 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
385 !$userShowHiddenCats &&
386 $rc->getParam( 'hidden-cat' )
391 $rc->counter
= $counter++
;
393 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
394 $updated = $obj->wl_notificationtimestamp
;
399 if ( isset( $watchedItemStore ) ) {
400 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
401 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
403 $rc->numberofWatchingusers
= 0;
406 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
407 if ( $changeLine !== false ) {
411 $s .= $list->endRecentChangesList();
413 $output->addHTML( $s );
417 * Set the text to be displayed above the changes
419 * @param FormOptions $opts
420 * @param int $numRows Number of rows in the result to show after this header
422 public function doHeader( $opts, $numRows ) {
423 $user = $this->getUser();
424 $out = $this->getOutput();
427 $this->msg( 'watchlistfor2', $user->getName() )
428 ->rawParams( SpecialEditWatchlist
::buildTools(
429 $this->getLanguage(),
430 $this->getLinkRenderer()
434 $this->setTopText( $opts );
436 $lang = $this->getLanguage();
437 if ( $opts['days'] > 0 ) {
438 $days = $opts['days'];
440 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
442 $timestamp = wfTimestampNow();
443 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
444 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
445 )->parse() . "<br />\n";
447 $nondefaults = $opts->getChangedValues();
448 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
450 # Spit out some control panel links
452 'hideminor' => 'wlshowhideminor',
453 'hidebots' => 'wlshowhidebots',
454 'hideanons' => 'wlshowhideanons',
455 'hideliu' => 'wlshowhideliu',
456 'hidemyself' => 'wlshowhidemine',
457 'hidepatrolled' => 'wlshowhidepatr'
460 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
461 $filters['hidecategorization'] = 'wlshowhidecategorization';
464 foreach ( $this->getRenderableCustomFilters( $this->getCustomFilters() ) as $key => $params ) {
465 $filters[$key] = $params['msg'];
468 // Disable some if needed
469 if ( !$user->useRCPatrol() ) {
470 unset( $filters['hidepatrolled'] );
474 foreach ( $filters as $name => $msg ) {
475 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
478 $hiddenFields = $nondefaults;
479 $hiddenFields['action'] = 'submit';
480 unset( $hiddenFields['namespace'] );
481 unset( $hiddenFields['invert'] );
482 unset( $hiddenFields['associated'] );
483 unset( $hiddenFields['days'] );
484 foreach ( $filters as $key => $value ) {
485 unset( $hiddenFields[$key] );
491 # Namespace filter and put the whole form together.
493 $form .= $cutofflinks;
494 $form .= $this->msg( 'watchlist-hide' ) .
495 $this->msg( 'colon-separator' )->escaped() .
496 implode( ' ', $links );
497 $form .= "\n<br />\n";
498 $form .= Html
::namespaceSelector(
500 'selected' => $opts['namespace'],
502 'label' => $this->msg( 'namespace' )->text()
504 'name' => 'namespace',
506 'class' => 'namespaceselector',
509 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
510 $this->msg( 'invert' )->text(),
514 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
516 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
517 $this->msg( 'namespace_association' )->text(),
521 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
523 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
524 foreach ( $hiddenFields as $key => $value ) {
525 $form .= Html
::hidden( $key, $value ) . "\n";
527 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
528 $form .= Xml
::closeElement( 'form' ) . "\n";
529 $this->getOutput()->addHTML( $form );
531 $this->setBottomText( $opts );
534 function cutoffselector( $options ) {
535 // Cast everything to strings immediately, so that we know all of the values have the same
536 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
537 // default string representation, for example, and would confuse comparisons.
539 // Misleadingly, the 'days' option supports hours too.
540 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
542 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
543 // add the user preference, if it isn't available already
544 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
545 $days[] = $userWatchlistOption;
548 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
549 // add the maximum possible value, if it isn't available already
550 if ( !in_array( $maxDays, $days ) ) {
554 $selected = (string)$options['days'];
555 if ( $selected <= 0 ) {
556 $selected = $maxDays;
559 // add the currently selected value, if it isn't available already
560 if ( !in_array( $selected, $days ) ) {
564 $select = new XmlSelect( 'days', 'days', $selected );
567 foreach ( $days as $value ) {
569 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
571 $name = $this->msg( 'days' )->numParams( $value )->text();
573 $select->addOption( $name, $value );
576 return $select->getHTML() . "\n<br />\n";
579 function setTopText( FormOptions
$opts ) {
580 $nondefaults = $opts->getChangedValues();
582 $user = $this->getUser();
584 $numItems = $this->countItems();
585 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
587 // Show watchlist header
589 if ( $numItems == 0 ) {
590 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
592 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
593 if ( $this->getConfig()->get( 'EnotifWatchlist' )
594 && $user->getOption( 'enotifwatchlistpages' )
596 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
598 if ( $showUpdatedMarker ) {
599 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
604 if ( $numItems > 0 && $showUpdatedMarker ) {
605 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
606 'action' => $this->getPageTitle()->getLocalURL(),
607 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
608 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(),
609 [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
610 Html
::hidden( 'reset', 'all' ) . "\n";
611 foreach ( $nondefaults as $key => $value ) {
612 $form .= Html
::hidden( $key, $value ) . "\n";
614 $form .= Xml
::closeElement( 'form' ) . "\n";
617 $form .= Xml
::openElement( 'form', [
619 'action' => wfScript(),
620 'id' => 'mw-watchlist-form'
622 $form .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
623 $form .= Xml
::fieldset(
624 $this->msg( 'watchlist-options' )->text(),
626 [ 'id' => 'mw-watchlist-options' ]
629 $form .= $this->makeLegend();
631 $this->getOutput()->addHTML( $form );
634 protected function showHideCheck( $options, $message, $name, $value ) {
635 $options[$name] = 1 - (int)$value;
637 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
638 $this->msg( $message, '' )->text(),
646 * Count the number of paired items on a user's watchlist.
647 * The assumption made here is that when a subject page is watched a talk page is also watched.
648 * Hence the number of individual items is halved.
652 protected function countItems() {
653 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
654 $count = $store->countWatchedItems( $this->getUser() );
655 return floor( $count / 2 );