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
25 * A special page that lists last changes made to the wiki,
26 * limited to user-defined list of titles.
28 * @ingroup SpecialPage
30 class SpecialWatchlist
extends ChangesListSpecialPage
{
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent
::__construct( $page, $restriction );
35 public function doesWrites() {
40 * Main execution point
42 * @param string $subpage
44 function execute( $subpage ) {
45 // Anons don't get a watchlist
46 $this->requireLogin( 'watchlistanontext' );
48 $output = $this->getOutput();
49 $request = $this->getRequest();
50 $this->addHelpLink( 'Help:Watching pages' );
51 $output->addModules( [
52 'mediawiki.special.changeslist.visitedstatus',
55 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
56 if ( $mode !== false ) {
57 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
58 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
59 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
60 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
62 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
65 $output->redirect( $title->getLocalURL() );
70 $this->checkPermissions();
72 $user = $this->getUser();
73 $opts = $this->getOptions();
75 $config = $this->getConfig();
76 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
77 && $request->getVal( 'reset' )
78 && $request->wasPosted()
80 $user->clearAllNotifications();
81 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
86 parent
::execute( $subpage );
90 * Return an array of subpages that this special page will accept.
92 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
93 * @return string[] subpages
95 public function getSubpagesForPrefixSearch() {
104 * Get a FormOptions object containing the default options
106 * @return FormOptions
108 public function getDefaultOptions() {
109 $opts = parent
::getDefaultOptions();
110 $user = $this->getUser();
112 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
113 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
114 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
115 // The user has submitted the form, so we dont need the default values
119 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
120 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
121 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
122 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
123 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
124 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
125 $opts->add( 'hidecategorization', $user->getBoolOption( 'watchlisthidecategorization' ) );
131 * Get custom show/hide filters
133 * @return array Map of filter URL param names to properties (msg/default)
135 protected function getCustomFilters() {
136 if ( $this->customFilters
=== null ) {
137 $this->customFilters
= parent
::getCustomFilters();
138 Hooks
::run( 'SpecialWatchlistFilters', [ $this, &$this->customFilters
], '1.23' );
141 return $this->customFilters
;
145 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
147 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
148 * to the current ones.
150 * @param FormOptions $opts
151 * @return FormOptions
153 protected function fetchOptionsFromRequest( $opts ) {
154 static $compatibilityMap = [
155 'hideMinor' => 'hideminor',
156 'hideBots' => 'hidebots',
157 'hideAnons' => 'hideanons',
158 'hideLiu' => 'hideliu',
159 'hidePatrolled' => 'hidepatrolled',
160 'hideOwn' => 'hidemyself',
163 $params = $this->getRequest()->getValues();
164 foreach ( $compatibilityMap as $from => $to ) {
165 if ( isset( $params[$from] ) ) {
166 $params[$to] = $params[$from];
167 unset( $params[$from] );
171 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
172 // methods defined on WebRequest and removing this dependency would cause some code duplication.
173 $request = new DerivativeRequest( $this->getRequest(), $params );
174 $opts->fetchValuesFromRequest( $request );
180 * Return an array of conditions depending of options set in $opts
182 * @param FormOptions $opts
185 public function buildMainQueryConds( FormOptions
$opts ) {
186 $dbr = $this->getDB();
187 $conds = parent
::buildMainQueryConds( $opts );
190 if ( $opts['days'] > 0 ) {
191 $conds[] = 'rc_timestamp > ' .
192 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
201 * @param array $conds
202 * @param FormOptions $opts
203 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
205 public function doMainQuery( $conds, $opts ) {
206 $dbr = $this->getDB();
207 $user = $this->getUser();
209 # Toggle watchlist content (all recent edits or just the latest)
210 if ( $opts['extended'] ) {
211 $limitWatchlist = $user->getIntOption( 'wllimit' );
214 # Top log Ids for a page are not stored
215 $nonRevisionTypes = [ RC_LOG
];
216 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
217 if ( $nonRevisionTypes ) {
218 $conds[] = $dbr->makeList(
220 'rc_this_oldid=page_latest',
221 'rc_type' => $nonRevisionTypes,
230 $tables = [ 'recentchanges', 'watchlist' ];
231 $fields = RecentChange
::selectFields();
232 $query_options = [ 'ORDER BY' => 'rc_timestamp DESC' ];
237 'wl_user' => $user->getId(),
238 'wl_namespace=rc_namespace',
244 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
245 $fields[] = 'wl_notificationtimestamp';
247 if ( $limitWatchlist ) {
248 $query_options['LIMIT'] = $limitWatchlist;
251 $rollbacker = $user->isAllowed( 'rollback' );
252 if ( $usePage ||
$rollbacker ) {
254 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
256 $fields[] = 'page_latest';
260 // Log entries with DELETED_ACTION must not show up unless the user has
261 // the necessary rights.
262 if ( !$user->isAllowed( 'deletedhistory' ) ) {
263 $bitmask = LogPage
::DELETED_ACTION
;
264 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
265 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
270 $conds[] = $dbr->makeList( [
271 'rc_type != ' . RC_LOG
,
272 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
276 ChangeTags
::modifyDisplayQuery(
285 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
297 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
300 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
302 'SpecialWatchlistQuery',
303 [ &$conds, &$tables, &$join_conds, &$fields, $opts ],
309 * Return a IDatabase object for reading
313 protected function getDB() {
314 return wfGetDB( DB_SLAVE
, 'watchlist' );
320 public function outputFeedLinks() {
321 $user = $this->getUser();
322 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
324 $this->addFeedLinks( [
325 'action' => 'feedwatchlist',
327 'wlowner' => $user->getName(),
328 'wltoken' => $wlToken,
334 * Build and output the actual changes list.
336 * @param ResultWrapper $rows Database rows
337 * @param FormOptions $opts
339 public function outputChangesList( $rows, $opts ) {
340 $dbr = $this->getDB();
341 $user = $this->getUser();
342 $output = $this->getOutput();
344 # Show a message about slave lag, if applicable
345 $lag = wfGetLB()->safeGetLag( $dbr );
347 $output->showLagWarning( $lag );
350 # If no rows to display, show message before try to render the list
351 if ( $rows->numRows() == 0 ) {
352 $output->wrapWikiMsg(
353 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
358 $dbr->dataSeek( $rows, 0 );
360 $list = ChangesList
::newFromContext( $this->getContext() );
361 $list->setWatchlistDivs();
362 $list->initChangesListRows( $rows );
363 $dbr->dataSeek( $rows, 0 );
365 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
366 && $user->getOption( 'shownumberswatching' )
368 $watchedItemStore = WatchedItemStore
::getDefaultInstance();
371 $s = $list->beginRecentChangesList();
372 $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
374 foreach ( $rows as $obj ) {
376 $rc = RecentChange
::newFromRow( $obj );
378 # Skip CatWatch entries for hidden cats based on user preference
380 $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
381 !$userShowHiddenCats &&
382 $rc->getParam( 'hidden-cat' )
387 $rc->counter
= $counter++
;
389 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
390 $updated = $obj->wl_notificationtimestamp
;
395 if ( isset( $watchedItemStore ) ) {
396 $rcTitleValue = new TitleValue( (int)$obj->rc_namespace
, $obj->rc_title
);
397 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
399 $rc->numberofWatchingusers
= 0;
402 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
403 if ( $changeLine !== false ) {
407 $s .= $list->endRecentChangesList();
409 $output->addHTML( $s );
413 * Set the text to be displayed above the changes
415 * @param FormOptions $opts
416 * @param int $numRows Number of rows in the result to show after this header
418 public function doHeader( $opts, $numRows ) {
419 $user = $this->getUser();
420 $out = $this->getOutput();
422 // if the user wishes, that the watchlist is reloaded, whenever a filter changes,
423 // add the module for that
424 if ( $user->getBoolOption( 'watchlistreloadautomatically' ) ) {
425 $out->addModules( [ 'mediawiki.special.watchlist' ] );
429 $this->msg( 'watchlistfor2', $user->getName() )
430 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
433 $this->setTopText( $opts );
435 $lang = $this->getLanguage();
436 if ( $opts['days'] > 0 ) {
437 $days = $opts['days'];
439 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
441 $timestamp = wfTimestampNow();
442 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
443 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
444 )->parse() . "<br />\n";
446 $nondefaults = $opts->getChangedValues();
447 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
449 # Spit out some control panel links
451 'hideminor' => 'wlshowhideminor',
452 'hidebots' => 'wlshowhidebots',
453 'hideanons' => 'wlshowhideanons',
454 'hideliu' => 'wlshowhideliu',
455 'hidemyself' => 'wlshowhidemine',
456 'hidepatrolled' => 'wlshowhidepatr'
459 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
460 $filters['hidecategorization'] = 'wlshowhidecategorization';
463 foreach ( $this->getCustomFilters() as $key => $params ) {
464 $filters[$key] = $params['msg'];
466 // Disable some if needed
467 if ( !$user->useRCPatrol() ) {
468 unset( $filters['hidepatrolled'] );
472 foreach ( $filters as $name => $msg ) {
473 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
476 $hiddenFields = $nondefaults;
477 $hiddenFields['action'] = 'submit';
478 unset( $hiddenFields['namespace'] );
479 unset( $hiddenFields['invert'] );
480 unset( $hiddenFields['associated'] );
481 unset( $hiddenFields['days'] );
482 foreach ( $filters as $key => $value ) {
483 unset( $hiddenFields[$key] );
489 # Namespace filter and put the whole form together.
491 $form .= $cutofflinks;
492 $form .= $this->msg( 'watchlist-hide' ) .
493 $this->msg( 'colon-separator' )->escaped() .
494 implode( ' ', $links );
495 $form .= "\n<br />\n";
496 $form .= Html
::namespaceSelector(
498 'selected' => $opts['namespace'],
500 'label' => $this->msg( 'namespace' )->text()
502 'name' => 'namespace',
504 'class' => 'namespaceselector',
507 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
508 $this->msg( 'invert' )->text(),
512 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
514 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
515 $this->msg( 'namespace_association' )->text(),
519 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
521 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
522 foreach ( $hiddenFields as $key => $value ) {
523 $form .= Html
::hidden( $key, $value ) . "\n";
525 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
526 $form .= Xml
::closeElement( 'form' ) . "\n";
527 $this->getOutput()->addHTML( $form );
529 $this->setBottomText( $opts );
532 function cutoffselector( $options ) {
533 // Cast everything to strings immediately, so that we know all of the values have the same
534 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
535 // default string representation, for example, and would confuse comparisons.
537 // Misleadingly, the 'days' option supports hours too.
538 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
540 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
541 // add the user preference, if it isn't available already
542 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
543 $days[] = $userWatchlistOption;
546 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
547 // add the maximum possible value, if it isn't available already
548 if ( !in_array( $maxDays, $days ) ) {
552 $selected = (string)$options['days'];
553 if ( $selected <= 0 ) {
554 $selected = $maxDays;
557 // add the currently selected value, if it isn't available already
558 if ( !in_array( $selected, $days ) ) {
562 $select = new XmlSelect( 'days', 'days', $selected );
565 foreach ( $days as $value ) {
567 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
569 $name = $this->msg( 'days' )->numParams( $value )->text();
571 $select->addOption( $name, $value );
574 return $select->getHTML() . "\n<br />\n";
577 function setTopText( FormOptions
$opts ) {
578 $nondefaults = $opts->getChangedValues();
580 $user = $this->getUser();
582 $numItems = $this->countItems();
583 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
585 // Show watchlist header
587 if ( $numItems == 0 ) {
588 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
590 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
591 if ( $this->getConfig()->get( 'EnotifWatchlist' )
592 && $user->getOption( 'enotifwatchlistpages' )
594 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
596 if ( $showUpdatedMarker ) {
597 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
602 if ( $numItems > 0 && $showUpdatedMarker ) {
603 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
604 'action' => $this->getPageTitle()->getLocalURL(),
605 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
606 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), [ 'name' => 'dummy' ] ) . "\n" .
607 Html
::hidden( 'reset', 'all' ) . "\n";
608 foreach ( $nondefaults as $key => $value ) {
609 $form .= Html
::hidden( $key, $value ) . "\n";
611 $form .= Xml
::closeElement( 'form' ) . "\n";
614 $form .= Xml
::openElement( 'form', [
616 'action' => $this->getPageTitle()->getLocalURL(),
617 'id' => 'mw-watchlist-form'
619 $form .= Xml
::fieldset(
620 $this->msg( 'watchlist-options' )->text(),
622 [ 'id' => 'mw-watchlist-options' ]
625 $form .= $this->makeLegend();
627 $this->getOutput()->addHTML( $form );
630 protected function showHideCheck( $options, $message, $name, $value ) {
631 $options[$name] = 1 - (int)$value;
633 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
634 $this->msg( $message, '' )->text(),
642 * Count the number of paired items on a user's watchlist.
643 * The assumption made here is that when a subject page is watched a talk page is also watched.
644 * Hence the number of individual items is halved.
648 protected function countItems() {
649 $count = WatchedItemStore
::getDefaultInstance()->countWatchedItems( $this->getUser() );
650 return floor( $count / 2 );