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 Watchlist
23 class SpecialWatchlist
extends SpecialRecentChanges
{
24 protected $customFilters;
29 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
30 parent
::__construct( $page, $restriction );
33 public function isIncludable() {
38 * Get a FormOptions object containing the default options
42 public function getDefaultOptions() {
43 $opts = parent
::getDefaultOptions();
44 $user = $this->getUser();
46 // Overwrite RC options with Watchlist options
47 // (calling #add() again is okay)
48 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
49 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
50 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
51 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
52 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
53 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
54 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
57 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
63 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
65 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
66 * to the current ones.
68 * @param FormOptions $parameters
71 protected function fetchOptionsFromRequest( $opts ) {
72 static $compatibilityMap = array(
73 'hideMinor' => 'hideminor',
74 'hideBots' => 'hidebots',
75 'hideAnons' => 'hideanons',
76 'hideLiu' => 'hideliu',
77 'hidePatrolled' => 'hidepatrolled',
78 'hideOwn' => 'hidemyself',
81 $params = $this->getRequest()->getValues();
82 foreach ( $compatibilityMap as $from => $to ) {
83 if ( isset( $params[$from] ) ) {
84 $params[$to] = $params[$from];
85 unset( $params[$from] );
89 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
90 // methods defined on WebRequest and removing this dependency would cause some code duplication.
91 $request = new DerivativeRequest( $this->getRequest(), $params );
92 $opts->fetchValuesFromRequest( $request );
97 * Get custom show/hide filters
99 * @return array Map of filter URL param names to properties (msg/default)
101 protected function getCustomFilters() {
102 if ( $this->customFilters
=== null ) {
103 $this->customFilters
= array();
104 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
107 return $this->customFilters
;
111 * Process $par and put options found if $opts. Not used for Watchlist.
114 * @param FormOptions $opts
116 public function parseParameters( $par, FormOptions
$opts ) {
120 * Get the current FormOptions for this request
122 public function getOptions() {
123 if ( $this->rcOptions
=== null ) {
124 $this->rcOptions
= $this->setup( null );
127 return $this->rcOptions
;
132 * @param $par Parameter passed to the page
134 function execute( $par ) {
135 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
137 $user = $this->getUser();
138 $output = $this->getOutput();
139 $output->addModuleStyles( 'mediawiki.special.changeslist' );
140 $output->addModules( 'mediawiki.special.changeslist.js' );
142 # Anons don't get a watchlist
143 $this->requireLogin( 'watchlistanontext' );
146 $this->checkPermissions();
148 $request = $this->getRequest();
149 $opts = $this->getOptions();
151 $mode = SpecialEditWatchlist
::getMode( $request, $par );
152 if ( $mode !== false ) {
153 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
154 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
156 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
159 $output->redirect( $title->getLocalURL() );
163 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
164 $request->wasPosted() )
166 $user->clearAllNotifications();
167 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
172 $this->outputHeader();
176 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
178 $this->addFeedLinks( array(
179 'action' => 'feedwatchlist',
181 'wlowner' => $user->getName(),
182 'wltoken' => $wlToken,
186 $output->addSubtitle(
187 $this->msg( 'watchlistfor2', $user->getName() )
188 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
191 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
193 # Show a message about slave lag, if applicable
194 $lag = wfGetLB()->safeGetLag( $dbr );
196 $output->showLagWarning( $lag );
199 $nitems = $this->countItems( $dbr );
200 if ( $nitems == 0 ) {
201 $output->addWikiMsg( 'nowatchlist' );
205 # Possible where conditions
208 if ( $opts['days'] > 0 ) {
209 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
213 if ( $opts['hidemyself'] ) {
214 $conds[] = 'rc_user != ' . $user->getId();
216 if ( $opts['hidebots'] ) {
217 $conds[] = 'rc_bot = 0';
219 if ( $opts['hideminor'] ) {
220 $conds[] = 'rc_minor = 0';
222 if ( $opts['hideliu'] ) {
223 $conds[] = 'rc_user = 0';
225 if ( $opts['hideanons'] ) {
226 $conds[] = 'rc_user != 0';
228 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
229 $conds[] = 'rc_patrolled != 1';
231 # Namespace filtering
232 if ( $opts['namespace'] !== '' ) {
233 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
234 $operator = $opts['invert'] ?
'!=' : '=';
235 $boolean = $opts['invert'] ?
'AND' : 'OR';
237 # namespace association (bug 2429)
238 if ( !$opts['associated'] ) {
239 $condition = "rc_namespace $operator $selectedNS";
241 # Also add the associated namespace
242 $associatedNS = $dbr->addQuotes(
243 MWNamespace
::getAssociated( $opts['namespace'] )
245 $condition = "(rc_namespace $operator $selectedNS "
247 . " rc_namespace $operator $associatedNS)";
250 $conds[] = $condition;
253 # Toggle watchlist content (all recent edits or just the latest)
254 if ( $opts['extended'] ) {
255 $limitWatchlist = $user->getIntOption( 'wllimit' );
258 # Top log Ids for a page are not stored
259 $nonRevisionTypes = array( RC_LOG
);
260 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
261 if ( $nonRevisionTypes ) {
262 $conds[] = $dbr->makeList(
264 'rc_this_oldid=page_latest',
265 'rc_type' => $nonRevisionTypes,
274 $tables = array( 'recentchanges', 'watchlist' );
275 $fields = RecentChange
::selectFields();
277 'watchlist' => array(
280 'wl_user' => $user->getId(),
281 'wl_namespace=rc_namespace',
286 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
287 if ( $wgShowUpdatedMarker ) {
288 $fields[] = 'wl_notificationtimestamp';
290 if ( $limitWatchlist ) {
291 $options['LIMIT'] = $limitWatchlist;
294 $rollbacker = $user->isAllowed( 'rollback' );
295 if ( $usePage ||
$rollbacker ) {
297 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
299 $fields[] = 'page_latest';
303 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
304 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
306 $rows = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
307 $numRows = $rows->numRows();
309 /* Start bottom header */
311 $lang = $this->getLanguage();
313 if ( $opts['days'] > 0 ) {
314 $timestamp = wfTimestampNow();
315 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
316 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
319 $nondefaults = $opts->getChangedValues();
320 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
322 # Spit out some control panel links
324 'hideminor' => 'rcshowhideminor',
325 'hidebots' => 'rcshowhidebots',
326 'hideanons' => 'rcshowhideanons',
327 'hideliu' => 'rcshowhideliu',
328 'hidemyself' => 'rcshowhidemine',
329 'hidepatrolled' => 'rcshowhidepatr'
331 foreach ( $this->getCustomFilters() as $key => $params ) {
332 $filters[$key] = $params['msg'];
334 // Disable some if needed
335 if ( !$user->useNPPatrol() ) {
336 unset( $filters['hidepatrolled'] );
340 foreach ( $filters as $name => $msg ) {
341 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
344 $hiddenFields = $nondefaults;
345 unset( $hiddenFields['namespace'] );
346 unset( $hiddenFields['invert'] );
347 unset( $hiddenFields['associated'] );
352 # Show watchlist header
354 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
355 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
356 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
358 if ( $wgShowUpdatedMarker ) {
359 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
363 if ( $wgShowUpdatedMarker ) {
364 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
365 'action' => $this->getPageTitle()->getLocalURL(),
366 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
367 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
368 Html
::hidden( 'reset', 'all' ) . "\n";
369 foreach ( $nondefaults as $key => $value ) {
370 $form .= Html
::hidden( $key, $value ) . "\n";
372 $form .= Xml
::closeElement( 'form' ) . "\n";
375 $form .= Xml
::openElement( 'form', array(
377 'action' => $this->getPageTitle()->getLocalURL(),
378 'id' => 'mw-watchlist-form'
380 $form .= Xml
::fieldset(
381 $this->msg( 'watchlist-options' )->text(),
383 array( 'id' => 'mw-watchlist-options' )
386 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
388 # Namespace filter and put the whole form together.
390 $form .= $cutofflinks;
391 $form .= $lang->pipeList( $links ) . "\n";
392 $form .= "<hr />\n<p>";
393 $form .= Html
::namespaceSelector(
395 'selected' => $opts['namespace'],
397 'label' => $this->msg( 'namespace' )->text()
399 'name' => 'namespace',
401 'class' => 'namespaceselector',
404 $form .= Xml
::checkLabel(
405 $this->msg( 'invert' )->text(),
409 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
411 $form .= Xml
::checkLabel(
412 $this->msg( 'namespace_association' )->text(),
416 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
418 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
419 foreach ( $hiddenFields as $key => $value ) {
420 $form .= Html
::hidden( $key, $value ) . "\n";
422 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
423 $form .= Xml
::closeElement( 'form' ) . "\n";
424 $output->addHTML( $form );
426 # If there's nothing to show, stop here
427 if ( $numRows == 0 ) {
428 $output->wrapWikiMsg(
429 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
434 /* End bottom header */
436 /* Do link batch query */
437 $batch = new LinkBatch
;
438 foreach ( $rows as $row ) {
439 $batch->add( NS_USER
, $row->rc_user_text
);
440 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
441 $batch->add( $row->rc_namespace
, $row->rc_title
);
445 $dbr->dataSeek( $rows, 0 );
447 $list = ChangesList
::newFromContext( $this->getContext() );
448 $list->setWatchlistDivs();
450 $s = $list->beginRecentChangesList();
452 foreach ( $rows as $obj ) {
454 $rc = RecentChange
::newFromRow( $obj );
455 $rc->counter
= $counter++
;
457 if ( $wgShowUpdatedMarker ) {
458 $updated = $obj->wl_notificationtimestamp
;
463 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
464 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
467 'wl_namespace' => $obj->rc_namespace
,
468 'wl_title' => $obj->rc_title
,
472 $rc->numberofWatchingusers
= 0;
475 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
476 if ( $changeLine !== false ) {
480 $s .= $list->endRecentChangesList();
482 $output->addHTML( $s );
485 protected function showHideLink( $options, $message, $name, $value ) {
486 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
487 $options[$name] = 1 - (int)$value;
489 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
492 protected function hoursLink( $h, $options = array() ) {
493 $options['days'] = ( $h / 24.0 );
495 return Linker
::linkKnown(
496 $this->getPageTitle(),
497 $this->getLanguage()->formatNum( $h ),
503 protected function daysLink( $d, $options = array() ) {
504 $options['days'] = $d;
505 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
507 return Linker
::linkKnown(
508 $this->getPageTitle(),
518 * @param int $days This gets overwritten, so is not used
519 * @param array $options Query parameters for URL
522 protected function cutoffLinks( $days, $options = array() ) {
523 $hours = array( 1, 2, 6, 12 );
524 $days = array( 1, 3, 7 );
526 foreach ( $hours as $h ) {
527 $hours[$i++
] = $this->hoursLink( $h, $options );
530 foreach ( $days as $d ) {
531 $days[$i++
] = $this->daysLink( $d, $options );
533 return $this->msg( 'wlshowlast' )->rawParams(
534 $this->getLanguage()->pipeList( $hours ),
535 $this->getLanguage()->pipeList( $days ),
536 $this->daysLink( 0, $options ) )->parse();
540 * Count the number of items on a user's watchlist
542 * @param DatabaseBase $dbr A database connection
545 protected function countItems( $dbr ) {
546 # Fetch the raw count
547 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
548 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
549 $row = $dbr->fetchObject( $rows );
550 $count = $row->count
;
552 return floor( $count / 2 );