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();
373 foreach ( $rows as $obj ) {
375 $rc = RecentChange
::newFromRow( $obj );
376 $rc->counter
= $counter++
;
378 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
379 $updated = $obj->wl_notificationtimestamp
;
384 if ( isset( $watchedItemStore ) ) {
385 $rcTitleValue = new TitleValue( $obj->rc_namespace
, $obj->rc_title
);
386 $rc->numberofWatchingusers
= $watchedItemStore->countWatchers( $rcTitleValue );
388 $rc->numberofWatchingusers
= 0;
391 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
392 if ( $changeLine !== false ) {
396 $s .= $list->endRecentChangesList();
398 $output->addHTML( $s );
402 * Set the text to be displayed above the changes
404 * @param FormOptions $opts
405 * @param int $numRows Number of rows in the result to show after this header
407 public function doHeader( $opts, $numRows ) {
408 $user = $this->getUser();
409 $out = $this->getOutput();
411 // if the user wishes, that the watchlist is reloaded, whenever a filter changes,
412 // add the module for that
413 if ( $user->getBoolOption( 'watchlistreloadautomatically' ) ) {
414 $out->addModules( [ 'mediawiki.special.watchlist' ] );
418 $this->msg( 'watchlistfor2', $user->getName() )
419 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
422 $this->setTopText( $opts );
424 $lang = $this->getLanguage();
425 if ( $opts['days'] > 0 ) {
426 $days = $opts['days'];
428 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
430 $timestamp = wfTimestampNow();
431 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
432 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
433 )->parse() . "<br />\n";
435 $nondefaults = $opts->getChangedValues();
436 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
438 # Spit out some control panel links
440 'hideminor' => 'wlshowhideminor',
441 'hidebots' => 'wlshowhidebots',
442 'hideanons' => 'wlshowhideanons',
443 'hideliu' => 'wlshowhideliu',
444 'hidemyself' => 'wlshowhidemine',
445 'hidepatrolled' => 'wlshowhidepatr'
448 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
449 $filters['hidecategorization'] = 'wlshowhidecategorization';
452 foreach ( $this->getCustomFilters() as $key => $params ) {
453 $filters[$key] = $params['msg'];
455 // Disable some if needed
456 if ( !$user->useRCPatrol() ) {
457 unset( $filters['hidepatrolled'] );
461 foreach ( $filters as $name => $msg ) {
462 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
465 $hiddenFields = $nondefaults;
466 $hiddenFields['action'] = 'submit';
467 unset( $hiddenFields['namespace'] );
468 unset( $hiddenFields['invert'] );
469 unset( $hiddenFields['associated'] );
470 unset( $hiddenFields['days'] );
471 foreach ( $filters as $key => $value ) {
472 unset( $hiddenFields[$key] );
478 # Namespace filter and put the whole form together.
480 $form .= $cutofflinks;
481 $form .= $this->msg( 'watchlist-hide' ) .
482 $this->msg( 'colon-separator' )->escaped() .
483 implode( ' ', $links );
484 $form .= "\n<br />\n";
485 $form .= Html
::namespaceSelector(
487 'selected' => $opts['namespace'],
489 'label' => $this->msg( 'namespace' )->text()
491 'name' => 'namespace',
493 'class' => 'namespaceselector',
496 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
497 $this->msg( 'invert' )->text(),
501 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
503 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
504 $this->msg( 'namespace_association' )->text(),
508 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
510 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
511 foreach ( $hiddenFields as $key => $value ) {
512 $form .= Html
::hidden( $key, $value ) . "\n";
514 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
515 $form .= Xml
::closeElement( 'form' ) . "\n";
516 $this->getOutput()->addHTML( $form );
518 $this->setBottomText( $opts );
521 function cutoffselector( $options ) {
522 // Cast everything to strings immediately, so that we know all of the values have the same
523 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
524 // default string representation, for example, and would confuse comparisons.
526 // Misleadingly, the 'days' option supports hours too.
527 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
529 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
530 // add the user preference, if it isn't available already
531 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
532 $days[] = $userWatchlistOption;
535 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
536 // add the maximum possible value, if it isn't available already
537 if ( !in_array( $maxDays, $days ) ) {
541 $selected = (string)$options['days'];
542 if ( $selected <= 0 ) {
543 $selected = $maxDays;
546 // add the currently selected value, if it isn't available already
547 if ( !in_array( $selected, $days ) ) {
551 $select = new XmlSelect( 'days', 'days', $selected );
554 foreach ( $days as $value ) {
556 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
558 $name = $this->msg( 'days' )->numParams( $value )->text();
560 $select->addOption( $name, $value );
563 return $select->getHTML() . "\n<br />\n";
566 function setTopText( FormOptions
$opts ) {
567 $nondefaults = $opts->getChangedValues();
569 $user = $this->getUser();
571 $numItems = $this->countItems();
572 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
574 // Show watchlist header
576 if ( $numItems == 0 ) {
577 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
579 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
580 if ( $this->getConfig()->get( 'EnotifWatchlist' )
581 && $user->getOption( 'enotifwatchlistpages' )
583 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
585 if ( $showUpdatedMarker ) {
586 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
591 if ( $numItems > 0 && $showUpdatedMarker ) {
592 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
593 'action' => $this->getPageTitle()->getLocalURL(),
594 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
595 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), [ 'name' => 'dummy' ] ) . "\n" .
596 Html
::hidden( 'reset', 'all' ) . "\n";
597 foreach ( $nondefaults as $key => $value ) {
598 $form .= Html
::hidden( $key, $value ) . "\n";
600 $form .= Xml
::closeElement( 'form' ) . "\n";
603 $form .= Xml
::openElement( 'form', [
605 'action' => $this->getPageTitle()->getLocalURL(),
606 'id' => 'mw-watchlist-form'
608 $form .= Xml
::fieldset(
609 $this->msg( 'watchlist-options' )->text(),
611 [ 'id' => 'mw-watchlist-options' ]
614 $form .= $this->makeLegend();
616 $this->getOutput()->addHTML( $form );
619 protected function showHideCheck( $options, $message, $name, $value ) {
620 $options[$name] = 1 - (int)$value;
622 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
623 $this->msg( $message, '' )->text(),
631 * Count the number of paired items on a user's watchlist.
632 * The assumption made here is that when a subject page is watched a talk page is also watched.
633 * Hence the number of individual items is halved.
637 protected function countItems() {
638 $count = WatchedItemStore
::getDefaultInstance()->countWatchedItems( $this->getUser() );
639 return floor( $count / 2 );