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();
47 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
48 if ( $mode !== false ) {
49 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
50 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
51 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
52 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
54 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
57 $output->redirect( $title->getLocalURL() );
62 $this->checkPermissions();
64 $user = $this->getUser();
65 $opts = $this->getOptions();
67 $config = $this->getConfig();
68 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
69 && $request->getVal( 'reset' )
70 && $request->wasPosted()
72 $user->clearAllNotifications();
73 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
78 parent
::execute( $subpage );
82 * Return an array of subpages beginning with $search that this special page will accept.
84 * @param string $search Prefix to search for
85 * @param int $limit Maximum number of results to return
86 * @return string[] Matching subpages
88 public function prefixSearchSubpages( $search, $limit = 10 ) {
89 // See also SpecialEditWatchlist::prefixSearchSubpages
90 return self
::prefixSearchArray(
102 * Get a FormOptions object containing the default options
104 * @return FormOptions
106 public function getDefaultOptions() {
107 $opts = parent
::getDefaultOptions();
108 $user = $this->getUser();
110 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
112 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
113 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
114 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
115 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
116 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
117 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
119 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
125 * Get custom show/hide filters
127 * @return array Map of filter URL param names to properties (msg/default)
129 protected function getCustomFilters() {
130 if ( $this->customFilters
=== null ) {
131 $this->customFilters
= parent
::getCustomFilters();
132 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
), '1.23' );
135 return $this->customFilters
;
139 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
141 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
142 * to the current ones.
144 * @param FormOptions $opts
145 * @return FormOptions
147 protected function fetchOptionsFromRequest( $opts ) {
148 static $compatibilityMap = array(
149 'hideMinor' => 'hideminor',
150 'hideBots' => 'hidebots',
151 'hideAnons' => 'hideanons',
152 'hideLiu' => 'hideliu',
153 'hidePatrolled' => 'hidepatrolled',
154 'hideOwn' => 'hidemyself',
157 $params = $this->getRequest()->getValues();
158 foreach ( $compatibilityMap as $from => $to ) {
159 if ( isset( $params[$from] ) ) {
160 $params[$to] = $params[$from];
161 unset( $params[$from] );
165 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
166 // methods defined on WebRequest and removing this dependency would cause some code duplication.
167 $request = new DerivativeRequest( $this->getRequest(), $params );
168 $opts->fetchValuesFromRequest( $request );
174 * Return an array of conditions depending of options set in $opts
176 * @param FormOptions $opts
179 public function buildMainQueryConds( FormOptions
$opts ) {
180 $dbr = $this->getDB();
181 $conds = parent
::buildMainQueryConds( $opts );
184 if ( $opts['days'] > 0 ) {
185 $conds[] = 'rc_timestamp > ' .
186 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
195 * @param array $conds
196 * @param FormOptions $opts
197 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
199 public function doMainQuery( $conds, $opts ) {
200 $dbr = $this->getDB();
201 $user = $this->getUser();
203 # Toggle watchlist content (all recent edits or just the latest)
204 if ( $opts['extended'] ) {
205 $limitWatchlist = $user->getIntOption( 'wllimit' );
208 # Top log Ids for a page are not stored
209 $nonRevisionTypes = array( RC_LOG
);
210 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
211 if ( $nonRevisionTypes ) {
212 $conds[] = $dbr->makeList(
214 'rc_this_oldid=page_latest',
215 'rc_type' => $nonRevisionTypes,
224 $tables = array( 'recentchanges', 'watchlist' );
225 $fields = RecentChange
::selectFields();
226 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
228 'watchlist' => array(
231 'wl_user' => $user->getId(),
232 'wl_namespace=rc_namespace',
238 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
239 $fields[] = 'wl_notificationtimestamp';
241 if ( $limitWatchlist ) {
242 $query_options['LIMIT'] = $limitWatchlist;
245 $rollbacker = $user->isAllowed( 'rollback' );
246 if ( $usePage ||
$rollbacker ) {
248 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
250 $fields[] = 'page_latest';
254 // Log entries with DELETED_ACTION must not show up unless the user has
255 // the necessary rights.
256 if ( !$user->isAllowed( 'deletedhistory' ) ) {
257 $bitmask = LogPage
::DELETED_ACTION
;
258 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
259 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
264 $conds[] = $dbr->makeList( array(
265 'rc_type != ' . RC_LOG
,
266 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
270 ChangeTags
::modifyDisplayQuery(
279 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
291 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
294 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
296 'SpecialWatchlistQuery',
297 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
303 * Return a DatabaseBase object for reading
305 * @return DatabaseBase
307 protected function getDB() {
308 return wfGetDB( DB_SLAVE
, 'watchlist' );
314 public function outputFeedLinks() {
315 $user = $this->getUser();
316 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
318 $this->addFeedLinks( array(
319 'action' => 'feedwatchlist',
321 'wlowner' => $user->getName(),
322 'wltoken' => $wlToken,
328 * Build and output the actual changes list.
330 * @param ResultWrapper $rows Database rows
331 * @param FormOptions $opts
333 public function outputChangesList( $rows, $opts ) {
334 $dbr = $this->getDB();
335 $user = $this->getUser();
336 $output = $this->getOutput();
338 # Show a message about slave lag, if applicable
339 $lag = wfGetLB()->safeGetLag( $dbr );
341 $output->showLagWarning( $lag );
344 # If no rows to display, show message before try to render the list
345 if ( $rows->numRows() == 0 ) {
346 $output->wrapWikiMsg(
347 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
352 $dbr->dataSeek( $rows, 0 );
354 $list = ChangesList
::newFromContext( $this->getContext() );
355 $list->setWatchlistDivs();
356 $list->initChangesListRows( $rows );
357 $dbr->dataSeek( $rows, 0 );
359 $s = $list->beginRecentChangesList();
361 foreach ( $rows as $obj ) {
363 $rc = RecentChange
::newFromRow( $obj );
364 $rc->counter
= $counter++
;
366 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
367 $updated = $obj->wl_notificationtimestamp
;
372 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
373 && $user->getOption( 'shownumberswatching' )
375 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
378 'wl_namespace' => $obj->rc_namespace
,
379 'wl_title' => $obj->rc_title
,
383 $rc->numberofWatchingusers
= 0;
386 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
387 if ( $changeLine !== false ) {
391 $s .= $list->endRecentChangesList();
393 $output->addHTML( $s );
397 * Set the text to be displayed above the changes
399 * @param FormOptions $opts
400 * @param int $numRows Number of rows in the result to show after this header
402 public function doHeader( $opts, $numRows ) {
403 $user = $this->getUser();
405 $this->getOutput()->addSubtitle(
406 $this->msg( 'watchlistfor2', $user->getName() )
407 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
410 $this->setTopText( $opts );
412 $lang = $this->getLanguage();
414 if ( $opts['days'] > 0 ) {
415 $timestamp = wfTimestampNow();
416 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
417 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
418 )->parse() . "<br />\n";
421 $nondefaults = $opts->getChangedValues();
422 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
424 # Spit out some control panel links
426 'hideminor' => 'rcshowhideminor',
427 'hidebots' => 'rcshowhidebots',
428 'hideanons' => 'rcshowhideanons',
429 'hideliu' => 'rcshowhideliu',
430 'hidemyself' => 'rcshowhidemine',
431 'hidepatrolled' => 'rcshowhidepatr'
433 foreach ( $this->getCustomFilters() as $key => $params ) {
434 $filters[$key] = $params['msg'];
436 // Disable some if needed
437 if ( !$user->useNPPatrol() ) {
438 unset( $filters['hidepatrolled'] );
442 foreach ( $filters as $name => $msg ) {
443 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
446 $hiddenFields = $nondefaults;
447 unset( $hiddenFields['namespace'] );
448 unset( $hiddenFields['invert'] );
449 unset( $hiddenFields['associated'] );
454 # Namespace filter and put the whole form together.
456 $form .= $cutofflinks;
457 $form .= $lang->pipeList( $links ) . "\n";
458 $form .= "<hr />\n<p>";
459 $form .= Html
::namespaceSelector(
461 'selected' => $opts['namespace'],
463 'label' => $this->msg( 'namespace' )->text()
465 'name' => 'namespace',
467 'class' => 'namespaceselector',
470 $form .= Xml
::checkLabel(
471 $this->msg( 'invert' )->text(),
475 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
477 $form .= Xml
::checkLabel(
478 $this->msg( 'namespace_association' )->text(),
482 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
484 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
485 foreach ( $hiddenFields as $key => $value ) {
486 $form .= Html
::hidden( $key, $value ) . "\n";
488 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
489 $form .= Xml
::closeElement( 'form' ) . "\n";
490 $this->getOutput()->addHTML( $form );
492 $this->setBottomText( $opts );
495 function setTopText( FormOptions
$opts ) {
496 $nondefaults = $opts->getChangedValues();
498 $user = $this->getUser();
500 $dbr = $this->getDB();
501 $numItems = $this->countItems( $dbr );
502 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
504 // Show watchlist header
506 if ( $numItems == 0 ) {
507 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
509 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
510 if ( $this->getConfig()->get( 'EnotifWatchlist' )
511 && $user->getOption( 'enotifwatchlistpages' )
513 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
515 if ( $showUpdatedMarker ) {
516 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
521 if ( $numItems > 0 && $showUpdatedMarker ) {
522 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
523 'action' => $this->getPageTitle()->getLocalURL(),
524 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
525 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
526 Html
::hidden( 'reset', 'all' ) . "\n";
527 foreach ( $nondefaults as $key => $value ) {
528 $form .= Html
::hidden( $key, $value ) . "\n";
530 $form .= Xml
::closeElement( 'form' ) . "\n";
533 $form .= Xml
::openElement( 'form', array(
535 'action' => $this->getPageTitle()->getLocalURL(),
536 'id' => 'mw-watchlist-form'
538 $form .= Xml
::fieldset(
539 $this->msg( 'watchlist-options' )->text(),
541 array( 'id' => 'mw-watchlist-options' )
544 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
546 $this->getOutput()->addHTML( $form );
549 protected function showHideLink( $options, $message, $name, $value ) {
550 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
551 $options[$name] = 1 - (int)$value;
553 return $this->msg( $message )
554 ->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
558 protected function hoursLink( $h, $options = array() ) {
559 $options['days'] = ( $h / 24.0 );
561 return Linker
::linkKnown(
562 $this->getPageTitle(),
563 $this->getLanguage()->formatNum( $h ),
569 protected function daysLink( $d, $options = array() ) {
570 $options['days'] = $d;
572 return Linker
::linkKnown(
573 $this->getPageTitle(),
574 $this->getLanguage()->formatNum( $d ),
583 * @param int $days This gets overwritten, so is not used
584 * @param array $options Query parameters for URL
587 protected function cutoffLinks( $days, $options = array() ) {
589 $watchlistMaxDays = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
591 $hours = array( 1, 2, 6, 12 );
592 $days = array( 1, 3, 7, $watchlistMaxDays );
594 foreach ( $hours as $h ) {
595 $hours[$i++
] = $this->hoursLink( $h, $options );
598 foreach ( $days as $d ) {
599 $days[$i++
] = $this->daysLink( $d, $options );
602 return $this->msg( 'wlshowlast' )->rawParams(
603 $this->getLanguage()->pipeList( $hours ),
604 $this->getLanguage()->pipeList( $days ) )->parse();
608 * Count the number of items on a user's watchlist
610 * @param DatabaseBase $dbr A database connection
613 protected function countItems( $dbr ) {
614 # Fetch the raw count
615 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
616 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
617 $row = $dbr->fetchObject( $rows );
618 $count = $row->count
;
620 return floor( $count / 2 );