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() {
39 * @param $par Parameter passed to the page
41 function execute( $par ) {
42 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
44 $user = $this->getUser();
45 $output = $this->getOutput();
46 $output->addModuleStyles( 'mediawiki.special.changeslist' );
47 $output->addModules( 'mediawiki.special.changeslist.js' );
49 # Anons don't get a watchlist
50 $this->requireLogin( 'watchlistanontext' );
53 $this->checkPermissions();
56 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
58 $this->addFeedLinks( array(
59 'action' => 'feedwatchlist',
61 'wlowner' => $user->getName(),
62 'wltoken' => $wlToken,
67 $this->outputHeader();
70 $this->msg( 'watchlistfor2', $user->getName() )
71 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
74 $request = $this->getRequest();
76 $mode = SpecialEditWatchlist
::getMode( $request, $par );
77 if ( $mode !== false ) {
78 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
79 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
81 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
84 $output->redirect( $title->getLocalURL() );
88 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
90 $nitems = $this->countItems( $dbr );
92 $output->addWikiMsg( 'nowatchlist' );
96 // @todo use FormOptions!
98 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
99 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
100 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
101 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
102 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
103 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
104 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
105 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
106 /* ? */ 'namespace' => '', //means all
107 /* ? */ 'invert' => false,
108 /* bool */ 'associated' => false,
110 $this->customFilters
= array();
111 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
112 foreach ( $this->customFilters
as $key => $params ) {
113 $defaults[$key] = $params['default'];
116 # Extract variables from the request, falling back to user preferences or
117 # other default values if these don't exist
119 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
120 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
121 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
122 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
123 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
124 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
125 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
126 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
127 foreach ( $this->customFilters
as $key => $params ) {
128 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
131 # Get namespace value, if supplied, and prepare a WHERE fragment
132 $nameSpace = $request->getIntOrNull( 'namespace' );
133 $invert = $request->getBool( 'invert' );
134 $associated = $request->getBool( 'associated' );
135 if ( !is_null( $nameSpace ) ) {
136 $eq_op = $invert ?
'!=' : '=';
137 $bool_op = $invert ?
'AND' : 'OR';
138 $nameSpace = intval( $nameSpace ); // paranioa
139 if ( !$associated ) {
140 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
142 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
144 "rc_namespace $eq_op $nameSpace " .
146 " rc_namespace $eq_op $associatedNS";
150 $nameSpaceClause = '';
152 $values['namespace'] = $nameSpace;
153 $values['invert'] = $invert;
154 $values['associated'] = $associated;
156 // Dump everything here
157 $nondefaults = array();
158 foreach ( $defaults as $name => $defValue ) {
159 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
162 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' )
163 && $request->wasPosted()
165 $user->clearAllNotifications();
166 $output->redirect( $this->getPageTitle()->getFullURL( $nondefaults ) );
170 # Possible where conditions
173 if ( $values['days'] > 0 ) {
174 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
178 if ( $values['hideOwn'] ) {
179 $conds[] = 'rc_user != ' . $user->getId();
181 if ( $values['hideBots'] ) {
182 $conds[] = 'rc_bot = 0';
184 if ( $values['hideMinor'] ) {
185 $conds[] = 'rc_minor = 0';
187 if ( $values['hideLiu'] ) {
188 $conds[] = 'rc_user = 0';
190 if ( $values['hideAnons'] ) {
191 $conds[] = 'rc_user != 0';
193 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
194 $conds[] = 'rc_patrolled != 1';
196 if ( $nameSpaceClause ) {
197 $conds[] = $nameSpaceClause;
200 # Toggle watchlist content (all recent edits or just the latest)
201 if ( $values['extended'] ) {
202 $limitWatchlist = $user->getIntOption( 'wllimit' );
205 # Top log Ids for a page are not stored
206 $nonRevisionTypes = array( RC_LOG
);
207 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
208 if ( $nonRevisionTypes ) {
209 $conds[] = $dbr->makeList(
211 'rc_this_oldid=page_latest',
212 'rc_type' => $nonRevisionTypes,
221 # Show a message about slave lag, if applicable
222 $lag = wfGetLB()->safeGetLag( $dbr );
224 $output->showLagWarning( $lag );
230 # Show watchlist header
232 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
233 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
234 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
236 if ( $wgShowUpdatedMarker ) {
237 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
241 if ( $wgShowUpdatedMarker ) {
242 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
243 'action' => $this->getPageTitle()->getLocalURL(),
244 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
245 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
246 Html
::hidden( 'reset', 'all' ) . "\n";
247 foreach ( $nondefaults as $key => $value ) {
248 $form .= Html
::hidden( $key, $value ) . "\n";
250 $form .= Xml
::closeElement( 'form' ) . "\n";
253 $form .= Xml
::openElement( 'form', array(
255 'action' => $this->getPageTitle()->getLocalURL(),
256 'id' => 'mw-watchlist-form'
258 $form .= Xml
::fieldset(
259 $this->msg( 'watchlist-options' )->text(),
261 array( 'id' => 'mw-watchlist-options' )
264 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
266 $tables = array( 'recentchanges', 'watchlist' );
267 $fields = RecentChange
::selectFields();
269 'watchlist' => array(
272 'wl_user' => $user->getId(),
273 'wl_namespace=rc_namespace',
278 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
279 if ( $wgShowUpdatedMarker ) {
280 $fields[] = 'wl_notificationtimestamp';
282 if ( $limitWatchlist ) {
283 $options['LIMIT'] = $limitWatchlist;
286 $rollbacker = $user->isAllowed( 'rollback' );
287 if ( $usePage ||
$rollbacker ) {
289 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
291 $fields[] = 'page_latest';
295 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
296 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
298 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
299 $numRows = $res->numRows();
301 /* Start bottom header */
303 $lang = $this->getLanguage();
305 if ( $values['days'] > 0 ) {
306 $timestamp = wfTimestampNow();
307 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
308 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
311 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
313 # Spit out some control panel links
315 'hideMinor' => 'rcshowhideminor',
316 'hideBots' => 'rcshowhidebots',
317 'hideAnons' => 'rcshowhideanons',
318 'hideLiu' => 'rcshowhideliu',
319 'hideOwn' => 'rcshowhidemine',
320 'hidePatrolled' => 'rcshowhidepatr'
322 foreach ( $this->customFilters
as $key => $params ) {
323 $filters[$key] = $params['msg'];
325 // Disable some if needed
326 if ( !$user->useNPPatrol() ) {
327 unset( $filters['hidePatrolled'] );
331 foreach ( $filters as $name => $msg ) {
332 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
335 $hiddenFields = $nondefaults;
336 unset( $hiddenFields['namespace'] );
337 unset( $hiddenFields['invert'] );
338 unset( $hiddenFields['associated'] );
340 # Namespace filter and put the whole form together.
342 $form .= $cutofflinks;
343 $form .= $lang->pipeList( $links ) . "\n";
344 $form .= "<hr />\n<p>";
345 $form .= Html
::namespaceSelector(
347 'selected' => $nameSpace,
349 'label' => $this->msg( 'namespace' )->text()
351 'name' => 'namespace',
353 'class' => 'namespaceselector',
356 $form .= Xml
::checkLabel(
357 $this->msg( 'invert' )->text(),
361 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
363 $form .= Xml
::checkLabel(
364 $this->msg( 'namespace_association' )->text(),
368 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
370 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
371 foreach ( $hiddenFields as $key => $value ) {
372 $form .= Html
::hidden( $key, $value ) . "\n";
374 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
375 $form .= Xml
::closeElement( 'form' ) . "\n";
376 $output->addHTML( $form );
378 # If there's nothing to show, stop here
379 if ( $numRows == 0 ) {
380 $output->wrapWikiMsg(
381 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
386 /* End bottom header */
388 /* Do link batch query */
389 $linkBatch = new LinkBatch
;
390 foreach ( $res as $row ) {
391 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
392 if ( $row->rc_user
!= 0 ) {
393 $linkBatch->add( NS_USER
, $userNameUnderscored );
395 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
397 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
399 $linkBatch->execute();
400 $dbr->dataSeek( $res, 0 );
402 $list = ChangesList
::newFromContext( $this->getContext() );
403 $list->setWatchlistDivs();
405 $s = $list->beginRecentChangesList();
407 foreach ( $res as $obj ) {
409 $rc = RecentChange
::newFromRow( $obj );
410 $rc->counter
= $counter++
;
412 if ( $wgShowUpdatedMarker ) {
413 $updated = $obj->wl_notificationtimestamp
;
418 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
419 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
422 'wl_namespace' => $obj->rc_namespace
,
423 'wl_title' => $obj->rc_title
,
427 $rc->numberofWatchingusers
= 0;
430 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
431 if ( $changeLine !== false ) {
435 $s .= $list->endRecentChangesList();
437 $output->addHTML( $s );
440 protected function showHideLink( $options, $message, $name, $value ) {
441 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
442 $options[$name] = 1 - (int)$value;
444 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
447 protected function hoursLink( $h, $options = array() ) {
448 $options['days'] = ( $h / 24.0 );
450 return Linker
::linkKnown(
451 $this->getPageTitle(),
452 $this->getLanguage()->formatNum( $h ),
458 protected function daysLink( $d, $options = array() ) {
459 $options['days'] = $d;
460 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
462 return Linker
::linkKnown(
463 $this->getPageTitle(),
473 * @param int $days This gets overwritten, so is not used
474 * @param array $options Query parameters for URL
477 protected function cutoffLinks( $days, $options = array() ) {
478 $hours = array( 1, 2, 6, 12 );
479 $days = array( 1, 3, 7 );
481 foreach ( $hours as $h ) {
482 $hours[$i++
] = $this->hoursLink( $h, $options );
485 foreach ( $days as $d ) {
486 $days[$i++
] = $this->daysLink( $d, $options );
488 return $this->msg( 'wlshowlast' )->rawParams(
489 $this->getLanguage()->pipeList( $hours ),
490 $this->getLanguage()->pipeList( $days ),
491 $this->daysLink( 0, $options ) )->parse();
495 * Count the number of items on a user's watchlist
497 * @param DatabaseBase $dbr A database connection
500 protected function countItems( $dbr ) {
501 # Fetch the raw count
502 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
503 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
504 $row = $dbr->fetchObject( $res );
505 $count = $row->count
;
507 return floor( $count / 2 );