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' );
48 $mode = SpecialEditWatchlist
::getMode( $request, $subpage );
49 if ( $mode !== false ) {
50 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
51 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
52 } elseif ( $mode === SpecialEditWatchlist
::EDIT_CLEAR
) {
53 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'clear' );
55 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
58 $output->redirect( $title->getLocalURL() );
63 $this->checkPermissions();
65 $user = $this->getUser();
66 $opts = $this->getOptions();
68 $config = $this->getConfig();
69 if ( ( $config->get( 'EnotifWatchlist' ) ||
$config->get( 'ShowUpdatedMarker' ) )
70 && $request->getVal( 'reset' )
71 && $request->wasPosted()
73 $user->clearAllNotifications();
74 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
79 parent
::execute( $subpage );
83 * Return an array of subpages that this special page will accept.
85 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
86 * @return string[] subpages
88 public function getSubpagesForPrefixSearch() {
97 * Get a FormOptions object containing the default options
101 public function getDefaultOptions() {
102 $opts = parent
::getDefaultOptions();
103 $user = $this->getUser();
105 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
107 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
108 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
109 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
110 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
111 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
112 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
114 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
115 $opts->add( 'hidecategorization', $user->getBoolOption( 'watchlisthidecategorization' ) );
118 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
124 * Get custom show/hide filters
126 * @return array Map of filter URL param names to properties (msg/default)
128 protected function getCustomFilters() {
129 if ( $this->customFilters
=== null ) {
130 $this->customFilters
= parent
::getCustomFilters();
131 Hooks
::run( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
), '1.23' );
134 return $this->customFilters
;
138 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
140 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
141 * to the current ones.
143 * @param FormOptions $opts
144 * @return FormOptions
146 protected function fetchOptionsFromRequest( $opts ) {
147 static $compatibilityMap = array(
148 'hideMinor' => 'hideminor',
149 'hideBots' => 'hidebots',
150 'hideAnons' => 'hideanons',
151 'hideLiu' => 'hideliu',
152 'hidePatrolled' => 'hidepatrolled',
153 'hideOwn' => 'hidemyself',
156 $params = $this->getRequest()->getValues();
157 foreach ( $compatibilityMap as $from => $to ) {
158 if ( isset( $params[$from] ) ) {
159 $params[$to] = $params[$from];
160 unset( $params[$from] );
164 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
165 // methods defined on WebRequest and removing this dependency would cause some code duplication.
166 $request = new DerivativeRequest( $this->getRequest(), $params );
167 $opts->fetchValuesFromRequest( $request );
173 * Return an array of conditions depending of options set in $opts
175 * @param FormOptions $opts
178 public function buildMainQueryConds( FormOptions
$opts ) {
179 $dbr = $this->getDB();
180 $conds = parent
::buildMainQueryConds( $opts );
183 if ( $opts['days'] > 0 ) {
184 $conds[] = 'rc_timestamp > ' .
185 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
194 * @param array $conds
195 * @param FormOptions $opts
196 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
198 public function doMainQuery( $conds, $opts ) {
199 $dbr = $this->getDB();
200 $user = $this->getUser();
202 # Toggle watchlist content (all recent edits or just the latest)
203 if ( $opts['extended'] ) {
204 $limitWatchlist = $user->getIntOption( 'wllimit' );
207 # Top log Ids for a page are not stored
208 $nonRevisionTypes = array( RC_LOG
);
209 Hooks
::run( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
210 if ( $nonRevisionTypes ) {
211 $conds[] = $dbr->makeList(
213 'rc_this_oldid=page_latest',
214 'rc_type' => $nonRevisionTypes,
223 $tables = array( 'recentchanges', 'watchlist' );
224 $fields = RecentChange
::selectFields();
225 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
227 'watchlist' => array(
230 'wl_user' => $user->getId(),
231 'wl_namespace=rc_namespace',
237 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
238 $fields[] = 'wl_notificationtimestamp';
240 if ( $limitWatchlist ) {
241 $query_options['LIMIT'] = $limitWatchlist;
244 $rollbacker = $user->isAllowed( 'rollback' );
245 if ( $usePage ||
$rollbacker ) {
247 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
249 $fields[] = 'page_latest';
253 // Log entries with DELETED_ACTION must not show up unless the user has
254 // the necessary rights.
255 if ( !$user->isAllowed( 'deletedhistory' ) ) {
256 $bitmask = LogPage
::DELETED_ACTION
;
257 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
258 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
263 $conds[] = $dbr->makeList( array(
264 'rc_type != ' . RC_LOG
,
265 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
269 ChangeTags
::modifyDisplayQuery(
278 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
290 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
293 return parent
::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
295 'SpecialWatchlistQuery',
296 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
302 * Return a IDatabase object for reading
306 protected function getDB() {
307 return wfGetDB( DB_SLAVE
, 'watchlist' );
313 public function outputFeedLinks() {
314 $user = $this->getUser();
315 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
317 $this->addFeedLinks( array(
318 'action' => 'feedwatchlist',
320 'wlowner' => $user->getName(),
321 'wltoken' => $wlToken,
327 * Build and output the actual changes list.
329 * @param ResultWrapper $rows Database rows
330 * @param FormOptions $opts
332 public function outputChangesList( $rows, $opts ) {
333 $dbr = $this->getDB();
334 $user = $this->getUser();
335 $output = $this->getOutput();
337 # Show a message about slave lag, if applicable
338 $lag = wfGetLB()->safeGetLag( $dbr );
340 $output->showLagWarning( $lag );
343 # If no rows to display, show message before try to render the list
344 if ( $rows->numRows() == 0 ) {
345 $output->wrapWikiMsg(
346 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
351 $dbr->dataSeek( $rows, 0 );
353 $list = ChangesList
::newFromContext( $this->getContext() );
354 $list->setWatchlistDivs();
355 $list->initChangesListRows( $rows );
356 $dbr->dataSeek( $rows, 0 );
358 $s = $list->beginRecentChangesList();
360 foreach ( $rows as $obj ) {
362 $rc = RecentChange
::newFromRow( $obj );
363 $rc->counter
= $counter++
;
365 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
366 $updated = $obj->wl_notificationtimestamp
;
371 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
372 && $user->getOption( 'shownumberswatching' )
374 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
377 'wl_namespace' => $obj->rc_namespace
,
378 'wl_title' => $obj->rc_title
,
382 $rc->numberofWatchingusers
= 0;
385 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
386 if ( $changeLine !== false ) {
390 $s .= $list->endRecentChangesList();
392 $output->addHTML( $s );
396 * Set the text to be displayed above the changes
398 * @param FormOptions $opts
399 * @param int $numRows Number of rows in the result to show after this header
401 public function doHeader( $opts, $numRows ) {
402 $user = $this->getUser();
404 $this->getOutput()->addSubtitle(
405 $this->msg( 'watchlistfor2', $user->getName() )
406 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
409 $this->setTopText( $opts );
411 $lang = $this->getLanguage();
413 if ( $opts['days'] > 0 ) {
414 $timestamp = wfTimestampNow();
415 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
416 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
417 )->parse() . "<br />\n";
420 $nondefaults = $opts->getChangedValues();
421 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
423 # Spit out some control panel links
425 'hideminor' => 'rcshowhideminor',
426 'hidebots' => 'rcshowhidebots',
427 'hideanons' => 'rcshowhideanons',
428 'hideliu' => 'rcshowhideliu',
429 'hidemyself' => 'rcshowhidemine',
430 'hidepatrolled' => 'rcshowhidepatr'
433 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
434 $filters['hidecategorization'] = 'rcshowhidecategorization';
437 foreach ( $this->getCustomFilters() as $key => $params ) {
438 $filters[$key] = $params['msg'];
440 // Disable some if needed
441 if ( !$user->useRCPatrol() ) {
442 unset( $filters['hidepatrolled'] );
446 foreach ( $filters as $name => $msg ) {
447 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
450 $hiddenFields = $nondefaults;
451 unset( $hiddenFields['namespace'] );
452 unset( $hiddenFields['invert'] );
453 unset( $hiddenFields['associated'] );
458 # Namespace filter and put the whole form together.
460 $form .= $cutofflinks;
461 $form .= $lang->pipeList( $links ) . "\n";
462 $form .= "<hr />\n<p>";
463 $form .= Html
::namespaceSelector(
465 'selected' => $opts['namespace'],
467 'label' => $this->msg( 'namespace' )->text()
469 'name' => 'namespace',
471 'class' => 'namespaceselector',
474 $form .= Xml
::checkLabel(
475 $this->msg( 'invert' )->text(),
479 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
481 $form .= Xml
::checkLabel(
482 $this->msg( 'namespace_association' )->text(),
486 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
488 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
489 foreach ( $hiddenFields as $key => $value ) {
490 $form .= Html
::hidden( $key, $value ) . "\n";
492 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
493 $form .= Xml
::closeElement( 'form' ) . "\n";
494 $this->getOutput()->addHTML( $form );
496 $this->setBottomText( $opts );
499 function setTopText( FormOptions
$opts ) {
500 $nondefaults = $opts->getChangedValues();
502 $user = $this->getUser();
504 $dbr = $this->getDB();
505 $numItems = $this->countItems( $dbr );
506 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
508 // Show watchlist header
510 if ( $numItems == 0 ) {
511 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
513 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
514 if ( $this->getConfig()->get( 'EnotifWatchlist' )
515 && $user->getOption( 'enotifwatchlistpages' )
517 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
519 if ( $showUpdatedMarker ) {
520 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
525 if ( $numItems > 0 && $showUpdatedMarker ) {
526 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
527 'action' => $this->getPageTitle()->getLocalURL(),
528 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
529 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
530 Html
::hidden( 'reset', 'all' ) . "\n";
531 foreach ( $nondefaults as $key => $value ) {
532 $form .= Html
::hidden( $key, $value ) . "\n";
534 $form .= Xml
::closeElement( 'form' ) . "\n";
537 $form .= Xml
::openElement( 'form', array(
539 'action' => $this->getPageTitle()->getLocalURL(),
540 'id' => 'mw-watchlist-form'
542 $form .= Xml
::fieldset(
543 $this->msg( 'watchlist-options' )->text(),
545 array( 'id' => 'mw-watchlist-options' )
548 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
550 $this->getOutput()->addHTML( $form );
553 protected function showHideLink( $options, $message, $name, $value ) {
554 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
555 $options[$name] = 1 - (int)$value;
557 return $this->msg( $message )
558 ->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
562 protected function hoursLink( $h, $options = array() ) {
563 $options['days'] = ( $h / 24.0 );
565 return Linker
::linkKnown(
566 $this->getPageTitle(),
567 $this->getLanguage()->formatNum( $h ),
573 protected function daysLink( $d, $options = array() ) {
574 $options['days'] = $d;
576 return Linker
::linkKnown(
577 $this->getPageTitle(),
578 $this->getLanguage()->formatNum( $d ),
587 * @param int $days This gets overwritten, so is not used
588 * @param array $options Query parameters for URL
591 protected function cutoffLinks( $days, $options = array() ) {
593 $watchlistMaxDays = ceil( $wgRCMaxAge / ( 3600 * 24 ) );
595 $hours = array( 1, 2, 6, 12 );
596 $days = array( 1, 3, 7, $watchlistMaxDays );
598 foreach ( $hours as $h ) {
599 $hours[$i++
] = $this->hoursLink( $h, $options );
602 foreach ( $days as $d ) {
603 $days[$i++
] = $this->daysLink( $d, $options );
606 return $this->msg( 'wlshowlast' )->rawParams(
607 $this->getLanguage()->pipeList( $hours ),
608 $this->getLanguage()->pipeList( $days ) )->parse();
612 * Count the number of items on a user's watchlist
614 * @param IDatabase $dbr A database connection
617 protected function countItems( $dbr ) {
618 # Fetch the raw count
619 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
620 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
621 $row = $dbr->fetchObject( $rows );
622 $count = $row->count
;
624 return floor( $count / 2 );