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();
149 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
151 $this->addFeedLinks( array(
152 'action' => 'feedwatchlist',
154 'wlowner' => $user->getName(),
155 'wltoken' => $wlToken,
159 $opts = $this->getOptions();
161 $this->outputHeader();
163 $output->addSubtitle(
164 $this->msg( 'watchlistfor2', $user->getName() )
165 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
168 $request = $this->getRequest();
170 $mode = SpecialEditWatchlist
::getMode( $request, $par );
171 if ( $mode !== false ) {
172 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
173 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
175 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
178 $output->redirect( $title->getLocalURL() );
182 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
184 $nitems = $this->countItems( $dbr );
185 if ( $nitems == 0 ) {
186 $output->addWikiMsg( 'nowatchlist' );
190 # Get namespace value, if supplied, and prepare a WHERE fragment
191 $nameSpace = $opts['namespace'];
192 $invert = $opts['invert'];
193 $associated = $opts['associated'];
195 if ( $nameSpace !== '' ) {
196 $eq_op = $invert ?
'!=' : '=';
197 $bool_op = $invert ?
'AND' : 'OR';
198 if ( !$associated ) {
199 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
201 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
203 "rc_namespace $eq_op $nameSpace " .
205 " rc_namespace $eq_op $associatedNS";
208 $nameSpaceClause = '';
211 // Dump everything here
212 $nondefaults = $opts->getChangedValues();
214 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' )
215 && $request->wasPosted()
217 $user->clearAllNotifications();
218 $output->redirect( $this->getPageTitle()->getFullURL( $nondefaults ) );
222 # Possible where conditions
225 if ( $opts['days'] > 0 ) {
226 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
230 if ( $opts['hidemyself'] ) {
231 $conds[] = 'rc_user != ' . $user->getId();
233 if ( $opts['hidebots'] ) {
234 $conds[] = 'rc_bot = 0';
236 if ( $opts['hideminor'] ) {
237 $conds[] = 'rc_minor = 0';
239 if ( $opts['hideliu'] ) {
240 $conds[] = 'rc_user = 0';
242 if ( $opts['hideanons'] ) {
243 $conds[] = 'rc_user != 0';
245 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
246 $conds[] = 'rc_patrolled != 1';
248 if ( $nameSpaceClause ) {
249 $conds[] = $nameSpaceClause;
252 # Toggle watchlist content (all recent edits or just the latest)
253 if ( $opts['extended'] ) {
254 $limitWatchlist = $user->getIntOption( 'wllimit' );
257 # Top log Ids for a page are not stored
258 $nonRevisionTypes = array( RC_LOG
);
259 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
260 if ( $nonRevisionTypes ) {
261 $conds[] = $dbr->makeList(
263 'rc_this_oldid=page_latest',
264 'rc_type' => $nonRevisionTypes,
273 # Show a message about slave lag, if applicable
274 $lag = wfGetLB()->safeGetLag( $dbr );
276 $output->showLagWarning( $lag );
282 # Show watchlist header
284 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
285 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
286 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
288 if ( $wgShowUpdatedMarker ) {
289 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
293 if ( $wgShowUpdatedMarker ) {
294 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
295 'action' => $this->getPageTitle()->getLocalURL(),
296 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
297 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
298 Html
::hidden( 'reset', 'all' ) . "\n";
299 foreach ( $nondefaults as $key => $value ) {
300 $form .= Html
::hidden( $key, $value ) . "\n";
302 $form .= Xml
::closeElement( 'form' ) . "\n";
305 $form .= Xml
::openElement( 'form', array(
307 'action' => $this->getPageTitle()->getLocalURL(),
308 'id' => 'mw-watchlist-form'
310 $form .= Xml
::fieldset(
311 $this->msg( 'watchlist-options' )->text(),
313 array( 'id' => 'mw-watchlist-options' )
316 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
318 $tables = array( 'recentchanges', 'watchlist' );
319 $fields = RecentChange
::selectFields();
321 'watchlist' => array(
324 'wl_user' => $user->getId(),
325 'wl_namespace=rc_namespace',
330 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
331 if ( $wgShowUpdatedMarker ) {
332 $fields[] = 'wl_notificationtimestamp';
334 if ( $limitWatchlist ) {
335 $options['LIMIT'] = $limitWatchlist;
338 $rollbacker = $user->isAllowed( 'rollback' );
339 if ( $usePage ||
$rollbacker ) {
341 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
343 $fields[] = 'page_latest';
347 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
348 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
350 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
351 $numRows = $res->numRows();
353 /* Start bottom header */
355 $lang = $this->getLanguage();
357 if ( $opts['days'] > 0 ) {
358 $timestamp = wfTimestampNow();
359 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
360 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
363 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
365 # Spit out some control panel links
367 'hideminor' => 'rcshowhideminor',
368 'hidebots' => 'rcshowhidebots',
369 'hideanons' => 'rcshowhideanons',
370 'hideliu' => 'rcshowhideliu',
371 'hidemyself' => 'rcshowhidemine',
372 'hidepatrolled' => 'rcshowhidepatr'
374 foreach ( $this->getCustomFilters() as $key => $params ) {
375 $filters[$key] = $params['msg'];
377 // Disable some if needed
378 if ( !$user->useNPPatrol() ) {
379 unset( $filters['hidepatrolled'] );
383 foreach ( $filters as $name => $msg ) {
384 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
387 $hiddenFields = $nondefaults;
388 unset( $hiddenFields['namespace'] );
389 unset( $hiddenFields['invert'] );
390 unset( $hiddenFields['associated'] );
392 # Namespace filter and put the whole form together.
394 $form .= $cutofflinks;
395 $form .= $lang->pipeList( $links ) . "\n";
396 $form .= "<hr />\n<p>";
397 $form .= Html
::namespaceSelector(
399 'selected' => $nameSpace,
401 'label' => $this->msg( 'namespace' )->text()
403 'name' => 'namespace',
405 'class' => 'namespaceselector',
408 $form .= Xml
::checkLabel(
409 $this->msg( 'invert' )->text(),
413 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
415 $form .= Xml
::checkLabel(
416 $this->msg( 'namespace_association' )->text(),
420 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
422 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
423 foreach ( $hiddenFields as $key => $value ) {
424 $form .= Html
::hidden( $key, $value ) . "\n";
426 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
427 $form .= Xml
::closeElement( 'form' ) . "\n";
428 $output->addHTML( $form );
430 # If there's nothing to show, stop here
431 if ( $numRows == 0 ) {
432 $output->wrapWikiMsg(
433 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
438 /* End bottom header */
440 /* Do link batch query */
441 $linkBatch = new LinkBatch
;
442 foreach ( $res as $row ) {
443 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
444 if ( $row->rc_user
!= 0 ) {
445 $linkBatch->add( NS_USER
, $userNameUnderscored );
447 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
449 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
451 $linkBatch->execute();
452 $dbr->dataSeek( $res, 0 );
454 $list = ChangesList
::newFromContext( $this->getContext() );
455 $list->setWatchlistDivs();
457 $s = $list->beginRecentChangesList();
459 foreach ( $res as $obj ) {
461 $rc = RecentChange
::newFromRow( $obj );
462 $rc->counter
= $counter++
;
464 if ( $wgShowUpdatedMarker ) {
465 $updated = $obj->wl_notificationtimestamp
;
470 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
471 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
474 'wl_namespace' => $obj->rc_namespace
,
475 'wl_title' => $obj->rc_title
,
479 $rc->numberofWatchingusers
= 0;
482 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
483 if ( $changeLine !== false ) {
487 $s .= $list->endRecentChangesList();
489 $output->addHTML( $s );
492 protected function showHideLink( $options, $message, $name, $value ) {
493 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
494 $options[$name] = 1 - (int)$value;
496 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
499 protected function hoursLink( $h, $options = array() ) {
500 $options['days'] = ( $h / 24.0 );
502 return Linker
::linkKnown(
503 $this->getPageTitle(),
504 $this->getLanguage()->formatNum( $h ),
510 protected function daysLink( $d, $options = array() ) {
511 $options['days'] = $d;
512 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
514 return Linker
::linkKnown(
515 $this->getPageTitle(),
525 * @param int $days This gets overwritten, so is not used
526 * @param array $options Query parameters for URL
529 protected function cutoffLinks( $days, $options = array() ) {
530 $hours = array( 1, 2, 6, 12 );
531 $days = array( 1, 3, 7 );
533 foreach ( $hours as $h ) {
534 $hours[$i++
] = $this->hoursLink( $h, $options );
537 foreach ( $days as $d ) {
538 $days[$i++
] = $this->daysLink( $d, $options );
540 return $this->msg( 'wlshowlast' )->rawParams(
541 $this->getLanguage()->pipeList( $hours ),
542 $this->getLanguage()->pipeList( $days ),
543 $this->daysLink( 0, $options ) )->parse();
547 * Count the number of items on a user's watchlist
549 * @param DatabaseBase $dbr A database connection
552 protected function countItems( $dbr ) {
553 # Fetch the raw count
554 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
555 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
556 $row = $dbr->fetchObject( $res );
557 $count = $row->count
;
559 return floor( $count / 2 );