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 );
36 * Main execution point
38 * @param string $subpage
40 function execute( $subpage ) {
41 // Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
44 $output = $this->getOutput();
45 $request = $this->getRequest();
46 $this->addHelpLink( 'Help:Watching pages' );
47 $output->addModules( array(
48 'mediawiki.special.changeslist.visitedstatus',
51 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
52 if ( $mode !== false ) {
53 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
54 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
55 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
56 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
58 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
61 $output->redirect( $title->getLocalURL() );
66 $this->checkPermissions();
68 $user = $this->getUser();
69 $opts = $this->getOptions();
71 $config = $this->getConfig();
72 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
73 && $request->getVal( 'reset' )
74 && $request->wasPosted()
76 $user->clearAllNotifications();
77 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
82 parent
::execute( $subpage );
86 * Return an array of subpages that this special page will accept.
88 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
89 * @return string[] subpages
91 public function getSubpagesForPrefixSearch() {
100 * Get a FormOptions object containing the default options
102 * @return FormOptions
104 public function getDefaultOptions() {
105 $opts = parent
::getDefaultOptions();
106 $user = $this->getUser();
108 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
109 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
110 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
111 // The user has submitted the form, so we dont need the default values
115 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
116 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
117 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
118 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
119 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
120 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
121 $opts->add( 'hidecategorization', $user->getBoolOption( 'watchlisthidecategorization' ) );
127 * Get custom show/hide filters
129 * @return array Map of filter URL param names to properties (msg/default)
131 protected function getCustomFilters() {
132 if ( $this->customFilters
=== null ) {
133 $this->customFilters
= parent
::getCustomFilters();
134 Hooks
::run( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
), '1.23' );
137 return $this->customFilters
;
141 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
143 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
144 * to the current ones.
146 * @param FormOptions $opts
147 * @return FormOptions
149 protected function fetchOptionsFromRequest( $opts ) {
150 static $compatibilityMap = array(
151 'hideMinor' => 'hideminor',
152 'hideBots' => 'hidebots',
153 'hideAnons' => 'hideanons',
154 'hideLiu' => 'hideliu',
155 'hidePatrolled' => 'hidepatrolled',
156 'hideOwn' => 'hidemyself',
159 $params = $this->getRequest()->getValues();
160 foreach ( $compatibilityMap as $from => $to ) {
161 if ( isset( $params[$from] ) ) {
162 $params[$to] = $params[$from];
163 unset( $params[$from] );
167 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
168 // methods defined on WebRequest and removing this dependency would cause some code duplication.
169 $request = new DerivativeRequest( $this->getRequest(), $params );
170 $opts->fetchValuesFromRequest( $request );
176 * Return an array of conditions depending of options set in $opts
178 * @param FormOptions $opts
181 public function buildMainQueryConds( FormOptions
$opts ) {
182 $dbr = $this->getDB();
183 $conds = parent
::buildMainQueryConds( $opts );
186 if ( $opts['days'] > 0 ) {
187 $conds[] = 'rc_timestamp > ' .
188 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
197 * @param array $conds
198 * @param FormOptions $opts
199 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
201 public function doMainQuery( $conds, $opts ) {
202 $dbr = $this->getDB();
203 $user = $this->getUser();
205 # Toggle watchlist content (all recent edits or just the latest)
206 if ( $opts['extended'] ) {
207 $limitWatchlist = $user->getIntOption( 'wllimit' );
210 # Top log Ids for a page are not stored
211 $nonRevisionTypes = array( RC_LOG
);
212 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
213 if ( $nonRevisionTypes ) {
214 $conds[] = $dbr->makeList(
216 'rc_this_oldid=page_latest',
217 'rc_type' => $nonRevisionTypes,
226 $tables = array( 'recentchanges', 'watchlist' );
227 $fields = RecentChange
::selectFields();
228 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
230 'watchlist' => array(
233 'wl_user' => $user->getId(),
234 'wl_namespace=rc_namespace',
240 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
241 $fields[] = 'wl_notificationtimestamp';
243 if ( $limitWatchlist ) {
244 $query_options['LIMIT'] = $limitWatchlist;
247 $rollbacker = $user->isAllowed( 'rollback' );
248 if ( $usePage ||
$rollbacker ) {
250 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
252 $fields[] = 'page_latest';
256 // Log entries with DELETED_ACTION must not show up unless the user has
257 // the necessary rights.
258 if ( !$user->isAllowed( 'deletedhistory' ) ) {
259 $bitmask = LogPage
::DELETED_ACTION
;
260 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
261 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
266 $conds[] = $dbr->makeList( array(
267 'rc_type != ' . RC_LOG
,
268 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
272 ChangeTags
::modifyDisplayQuery(
281 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
293 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
296 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
298 'SpecialWatchlistQuery',
299 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
305 * Return a IDatabase object for reading
309 protected function getDB() {
310 return wfGetDB( DB_SLAVE
, 'watchlist' );
316 public function outputFeedLinks() {
317 $user = $this->getUser();
318 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
320 $this->addFeedLinks( array(
321 'action' => 'feedwatchlist',
323 'wlowner' => $user->getName(),
324 'wltoken' => $wlToken,
330 * Build and output the actual changes list.
332 * @param ResultWrapper $rows Database rows
333 * @param FormOptions $opts
335 public function outputChangesList( $rows, $opts ) {
336 $dbr = $this->getDB();
337 $user = $this->getUser();
338 $output = $this->getOutput();
340 # Show a message about slave lag, if applicable
341 $lag = wfGetLB()->safeGetLag( $dbr );
343 $output->showLagWarning( $lag );
346 # If no rows to display, show message before try to render the list
347 if ( $rows->numRows() == 0 ) {
348 $output->wrapWikiMsg(
349 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
354 $dbr->dataSeek( $rows, 0 );
356 $list = ChangesList
::newFromContext( $this->getContext() );
357 $list->setWatchlistDivs();
358 $list->initChangesListRows( $rows );
359 $dbr->dataSeek( $rows, 0 );
361 $s = $list->beginRecentChangesList();
363 foreach ( $rows as $obj ) {
365 $rc = RecentChange
::newFromRow( $obj );
366 $rc->counter
= $counter++
;
368 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
369 $updated = $obj->wl_notificationtimestamp
;
374 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
375 && $user->getOption( 'shownumberswatching' )
377 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
380 'wl_namespace' => $obj->rc_namespace
,
381 'wl_title' => $obj->rc_title
,
385 $rc->numberofWatchingusers
= 0;
388 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
389 if ( $changeLine !== false ) {
393 $s .= $list->endRecentChangesList();
395 $output->addHTML( $s );
399 * Set the text to be displayed above the changes
401 * @param FormOptions $opts
402 * @param int $numRows Number of rows in the result to show after this header
404 public function doHeader( $opts, $numRows ) {
405 $user = $this->getUser();
406 $out = $this->getOutput();
408 // if the user wishes, that the watchlist is reloaded, whenever a filter changes,
409 // add the module for that
410 if ( $user->getBoolOption( 'watchlistreloadautomatically' ) ) {
411 $out->addModules( array( 'mediawiki.special.watchlist' ) );
415 $this->msg( 'watchlistfor2', $user->getName() )
416 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
419 $this->setTopText( $opts );
421 $lang = $this->getLanguage();
422 if ( $opts['days'] > 0 ) {
423 $days = $opts['days'];
425 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
427 $timestamp = wfTimestampNow();
428 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
429 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
430 )->parse() . "<br />\n";
432 $nondefaults = $opts->getChangedValues();
433 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
435 # Spit out some control panel links
437 'hideminor' => 'wlshowhideminor',
438 'hidebots' => 'wlshowhidebots',
439 'hideanons' => 'wlshowhideanons',
440 'hideliu' => 'wlshowhideliu',
441 'hidemyself' => 'wlshowhidemine',
442 'hidepatrolled' => 'wlshowhidepatr'
445 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
446 $filters['hidecategorization'] = 'wlshowhidecategorization';
449 foreach ( $this->getCustomFilters() as $key => $params ) {
450 $filters[$key] = $params['msg'];
452 // Disable some if needed
453 if ( !$user->useRCPatrol() ) {
454 unset( $filters['hidepatrolled'] );
458 foreach ( $filters as $name => $msg ) {
459 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
462 $hiddenFields = $nondefaults;
463 $hiddenFields['action'] = 'submit';
464 unset( $hiddenFields['namespace'] );
465 unset( $hiddenFields['invert'] );
466 unset( $hiddenFields['associated'] );
467 unset( $hiddenFields['days'] );
468 foreach ( $filters as $key => $value ) {
469 unset( $hiddenFields[$key] );
475 # Namespace filter and put the whole form together.
477 $form .= $cutofflinks;
478 $form .= $this->msg( 'watchlist-hide' ) .
479 $this->msg( 'colon-separator' )->escaped() .
480 implode( ' ', $links );
481 $form .= "\n<br />\n";
482 $form .= Html
::namespaceSelector(
484 'selected' => $opts['namespace'],
486 'label' => $this->msg( 'namespace' )->text()
488 'name' => 'namespace',
490 'class' => 'namespaceselector',
493 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
494 $this->msg( 'invert' )->text(),
498 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
500 $form .= '<span class="mw-input-with-label">' . Xml
::checkLabel(
501 $this->msg( 'namespace_association' )->text(),
505 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
507 $form .= Xml
::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
508 foreach ( $hiddenFields as $key => $value ) {
509 $form .= Html
::hidden( $key, $value ) . "\n";
511 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
512 $form .= Xml
::closeElement( 'form' ) . "\n";
513 $this->getOutput()->addHTML( $form );
515 $this->setBottomText( $opts );
518 function cutoffselector( $options ) {
519 // Cast everything to strings immediately, so that we know all of the values have the same
520 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
521 // default string representation, for example, and would confuse comparisons.
523 // Misleadingly, the 'days' option supports hours too.
524 $days = array_map( 'strval', array( 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ) );
526 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
527 // add the user preference, if it isn't available already
528 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
529 $days[] = $userWatchlistOption;
532 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
533 // add the maximum possible value, if it isn't available already
534 if ( !in_array( $maxDays, $days ) ) {
538 $selected = (string)$options['days'];
539 if ( $selected <= 0 ) {
540 $selected = $maxDays;
543 // add the currently selected value, if it isn't available already
544 if ( !in_array( $selected, $days ) ) {
548 $select = new XmlSelect( 'days', 'days', $selected );
551 foreach ( $days as $value ) {
553 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
555 $name = $this->msg( 'days' )->numParams( $value )->text();
557 $select->addOption( $name, $value );
560 return $select->getHTML() . "\n<br />\n";
563 function setTopText( FormOptions
$opts ) {
564 $nondefaults = $opts->getChangedValues();
566 $user = $this->getUser();
568 $dbr = $this->getDB();
569 $numItems = $this->countItems( $dbr );
570 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
572 // Show watchlist header
574 if ( $numItems == 0 ) {
575 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
577 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
578 if ( $this->getConfig()->get( 'EnotifWatchlist' )
579 && $user->getOption( 'enotifwatchlistpages' )
581 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
583 if ( $showUpdatedMarker ) {
584 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
589 if ( $numItems > 0 && $showUpdatedMarker ) {
590 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
591 'action' => $this->getPageTitle()->getLocalURL(),
592 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
593 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
594 Html
::hidden( 'reset', 'all' ) . "\n";
595 foreach ( $nondefaults as $key => $value ) {
596 $form .= Html
::hidden( $key, $value ) . "\n";
598 $form .= Xml
::closeElement( 'form' ) . "\n";
601 $form .= Xml
::openElement( 'form', array(
603 'action' => $this->getPageTitle()->getLocalURL(),
604 'id' => 'mw-watchlist-form'
606 $form .= Xml
::fieldset(
607 $this->msg( 'watchlist-options' )->text(),
609 array( 'id' => 'mw-watchlist-options' )
612 $form .= $this->makeLegend();
614 $this->getOutput()->addHTML( $form );
617 protected function showHideCheck( $options, $message, $name, $value ) {
618 $options[$name] = 1 - (int)$value;
620 return '<span class="mw-input-with-label">' . Xml
::checkLabel(
621 $this->msg( $message, '' )->text(),
629 * Count the number of items on a user's watchlist
631 * @param IDatabase $dbr A database connection
634 protected function countItems( $dbr ) {
635 # Fetch the raw count
636 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
637 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
638 $row = $dbr->fetchObject( $rows );
639 $count = $row->count
;
641 return floor( $count / 2 );