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 $s = $list->beginRecentChangesList();
367 foreach ( $rows as $obj ) {
369 $rc = RecentChange
::newFromRow( $obj );
370 $rc->counter
= $counter++
;
372 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
373 $updated = $obj->wl_notificationtimestamp
;
378 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
379 && $user->getOption( 'shownumberswatching' )
381 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
384 'wl_namespace' => $obj->rc_namespace
,
385 'wl_title' => $obj->rc_title
,
389 $rc->numberofWatchingusers
= 0;
392 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
393 if ( $changeLine !== false ) {
397 $s .= $list->endRecentChangesList();
399 $output->addHTML( $s );
403 * Set the text to be displayed above the changes
405 * @param FormOptions $opts
406 * @param int $numRows Number of rows in the result to show after this header
408 public function doHeader( $opts, $numRows ) {
409 $user = $this->getUser();
410 $out = $this->getOutput();
412 // if the user wishes, that the watchlist is reloaded, whenever a filter changes,
413 // add the module for that
414 if ( $user->getBoolOption( 'watchlistreloadautomatically' ) ) {
415 $out->addModules( [ 'mediawiki.special.watchlist' ] );
419 $this->msg( 'watchlistfor2', $user->getName() )
420 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
423 $this->setTopText( $opts );
425 $lang = $this->getLanguage();
426 if ( $opts['days'] > 0 ) {
427 $days = $opts['days'];
429 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
431 $timestamp = wfTimestampNow();
432 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
433 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
434 )->parse() . "<br />\n";
436 $nondefaults = $opts->getChangedValues();
437 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
439 # Spit out some control panel links
441 'hideminor' => 'wlshowhideminor',
442 'hidebots' => 'wlshowhidebots',
443 'hideanons' => 'wlshowhideanons',
444 'hideliu' => 'wlshowhideliu',
445 'hidemyself' => 'wlshowhidemine',
446 'hidepatrolled' => 'wlshowhidepatr'
449 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
450 $filters['hidecategorization'] = 'wlshowhidecategorization';
453 foreach ( $this->getCustomFilters() as $key => $params ) {
454 $filters[$key] = $params['msg'];
456 // Disable some if needed
457 if ( !$user->useRCPatrol() ) {
458 unset( $filters['hidepatrolled'] );
462 foreach ( $filters as $name => $msg ) {
463 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
466 $hiddenFields = $nondefaults;
467 $hiddenFields['action'] = 'submit';
468 unset( $hiddenFields['namespace'] );
469 unset( $hiddenFields['invert'] );
470 unset( $hiddenFields['associated'] );
471 unset( $hiddenFields['days'] );
472 foreach ( $filters as $key => $value ) {
473 unset( $hiddenFields[$key] );
479 # Namespace filter and put the whole form together.
481 $form .= $cutofflinks;
482 $form .= $this->msg( 'watchlist-hide' ) .
483 $this->msg( 'colon-separator' )->escaped() .
484 implode( ' ', $links );
485 $form .= "\n<br />\n";
486 $form .= Html
::namespaceSelector(
488 'selected' => $opts['namespace'],
490 'label' => $this->msg( 'namespace' )->text()
492 'name' => 'namespace',
494 'class' => 'namespaceselector',
497 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
498 $this->msg( 'invert' )->text(),
502 [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
504 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
505 $this->msg( 'namespace_association' )->text(),
509 [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
511 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
512 foreach ( $hiddenFields as $key => $value ) {
513 $form .= Html
::hidden( $key, $value ) . "\n";
515 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
516 $form .= Xml
::closeElement( 'form' ) . "\n";
517 $this->getOutput()->addHTML( $form );
519 $this->setBottomText( $opts );
522 function cutoffselector( $options ) {
523 // Cast everything to strings immediately, so that we know all of the values have the same
524 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
525 // default string representation, for example, and would confuse comparisons.
527 // Misleadingly, the 'days' option supports hours too.
528 $days = array_map( 'strval', [ 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ] );
530 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
531 // add the user preference, if it isn't available already
532 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
533 $days[] = $userWatchlistOption;
536 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
537 // add the maximum possible value, if it isn't available already
538 if ( !in_array( $maxDays, $days ) ) {
542 $selected = (string)$options['days'];
543 if ( $selected <= 0 ) {
544 $selected = $maxDays;
547 // add the currently selected value, if it isn't available already
548 if ( !in_array( $selected, $days ) ) {
552 $select = new XmlSelect( 'days', 'days', $selected );
555 foreach ( $days as $value ) {
557 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
559 $name = $this->msg( 'days' )->numParams( $value )->text();
561 $select->addOption( $name, $value );
564 return $select->getHTML() . "\n<br />\n";
567 function setTopText( FormOptions
$opts ) {
568 $nondefaults = $opts->getChangedValues();
570 $user = $this->getUser();
572 $dbr = $this->getDB();
573 $numItems = $this->countItems( $dbr );
574 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
576 // Show watchlist header
578 if ( $numItems == 0 ) {
579 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
581 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
582 if ( $this->getConfig()->get( 'EnotifWatchlist' )
583 && $user->getOption( 'enotifwatchlistpages' )
585 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
587 if ( $showUpdatedMarker ) {
588 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
593 if ( $numItems > 0 && $showUpdatedMarker ) {
594 $form .= Xml
::openElement( 'form', [ 'method' => 'post',
595 'action' => $this->getPageTitle()->getLocalURL(),
596 'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
597 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), [ 'name' => 'dummy' ] ) . "\n" .
598 Html
::hidden( 'reset', 'all' ) . "\n";
599 foreach ( $nondefaults as $key => $value ) {
600 $form .= Html
::hidden( $key, $value ) . "\n";
602 $form .= Xml
::closeElement( 'form' ) . "\n";
605 $form .= Xml
::openElement( 'form', [
607 'action' => $this->getPageTitle()->getLocalURL(),
608 'id' => 'mw-watchlist-form'
610 $form .= Xml
::fieldset(
611 $this->msg( 'watchlist-options' )->text(),
613 [ 'id' => 'mw-watchlist-options' ]
616 $form .= $this->makeLegend();
618 $this->getOutput()->addHTML( $form );
621 protected function showHideCheck( $options, $message, $name, $value ) {
622 $options[$name] = 1 - (int)$value;
624 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
625 $this->msg( $message, '' )->text(),
633 * Count the number of items on a user's watchlist
635 * @param IDatabase $dbr A database connection
638 protected function countItems( $dbr ) {
639 # Fetch the raw count
640 $rows = $dbr->select( 'watchlist', [ 'count' => 'COUNT(*)' ],
641 [ 'wl_user' => $this->getUser()->getId() ], __METHOD__
);
642 $row = $dbr->fetchObject( $rows );
643 $count = $row->count
;
645 return floor( $count / 2 );