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 SpecialPage
{
24 protected $customFilters;
29 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
30 parent
::__construct( $page, $restriction );
35 * @param $par Parameter passed to the page
37 function execute( $par ) {
38 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
40 $user = $this->getUser();
41 $output = $this->getOutput();
42 $output->addModuleStyles( 'mediawiki.special.changeslist' );
43 $output->addModules( 'mediawiki.special.changeslist.js' );
45 # Anons don't get a watchlist
46 $this->requireLogin( 'watchlistanontext' );
49 $this->checkPermissions();
52 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
54 $this->addFeedLinks( array(
55 'action' => 'feedwatchlist',
57 'wlowner' => $user->getName(),
58 'wltoken' => $wlToken,
63 $this->outputHeader();
66 $this->msg( 'watchlistfor2', $user->getName() )
67 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
70 $request = $this->getRequest();
72 $mode = SpecialEditWatchlist
::getMode( $request, $par );
73 if ( $mode !== false ) {
74 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
75 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
77 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
80 $output->redirect( $title->getLocalURL() );
84 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
86 $nitems = $this->countItems( $dbr );
88 $output->addWikiMsg( 'nowatchlist' );
92 // @todo use FormOptions!
94 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
95 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
96 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
97 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
98 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
99 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
100 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
101 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
102 /* ? */ 'namespace' => '', //means all
103 /* ? */ 'invert' => false,
104 /* bool */ 'associated' => false,
106 $this->customFilters
= array();
107 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
108 foreach ( $this->customFilters
as $key => $params ) {
109 $defaults[$key] = $params['default'];
112 # Extract variables from the request, falling back to user preferences or
113 # other default values if these don't exist
115 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
116 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
117 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
118 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
119 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
120 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
121 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
122 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
123 foreach ( $this->customFilters
as $key => $params ) {
124 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
127 # Get namespace value, if supplied, and prepare a WHERE fragment
128 $nameSpace = $request->getIntOrNull( 'namespace' );
129 $invert = $request->getBool( 'invert' );
130 $associated = $request->getBool( 'associated' );
131 if ( !is_null( $nameSpace ) ) {
132 $eq_op = $invert ?
'!=' : '=';
133 $bool_op = $invert ?
'AND' : 'OR';
134 $nameSpace = intval( $nameSpace ); // paranioa
135 if ( !$associated ) {
136 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
138 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
140 "rc_namespace $eq_op $nameSpace " .
142 " rc_namespace $eq_op $associatedNS";
146 $nameSpaceClause = '';
148 $values['namespace'] = $nameSpace;
149 $values['invert'] = $invert;
150 $values['associated'] = $associated;
152 // Dump everything here
153 $nondefaults = array();
154 foreach ( $defaults as $name => $defValue ) {
155 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
158 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' )
159 && $request->wasPosted()
161 $user->clearAllNotifications();
162 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
166 # Possible where conditions
169 if ( $values['days'] > 0 ) {
170 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
174 if ( $values['hideOwn'] ) {
175 $conds[] = 'rc_user != ' . $user->getId();
177 if ( $values['hideBots'] ) {
178 $conds[] = 'rc_bot = 0';
180 if ( $values['hideMinor'] ) {
181 $conds[] = 'rc_minor = 0';
183 if ( $values['hideLiu'] ) {
184 $conds[] = 'rc_user = 0';
186 if ( $values['hideAnons'] ) {
187 $conds[] = 'rc_user != 0';
189 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
190 $conds[] = 'rc_patrolled != 1';
192 if ( $nameSpaceClause ) {
193 $conds[] = $nameSpaceClause;
196 # Toggle watchlist content (all recent edits or just the latest)
197 if ( $values['extended'] ) {
198 $limitWatchlist = $user->getIntOption( 'wllimit' );
201 # Top log Ids for a page are not stored
202 $nonRevisionTypes = array( RC_LOG
);
203 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
204 if ( $nonRevisionTypes ) {
205 $conds[] = $dbr->makeList(
207 'rc_this_oldid=page_latest',
208 'rc_type' => $nonRevisionTypes,
217 # Show a message about slave lag, if applicable
218 $lag = wfGetLB()->safeGetLag( $dbr );
220 $output->showLagWarning( $lag );
226 # Show watchlist header
228 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
229 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
230 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
232 if ( $wgShowUpdatedMarker ) {
233 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
237 if ( $wgShowUpdatedMarker ) {
238 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
239 'action' => $this->getTitle()->getLocalURL(),
240 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
241 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
242 Html
::hidden( 'reset', 'all' ) . "\n";
243 foreach ( $nondefaults as $key => $value ) {
244 $form .= Html
::hidden( $key, $value ) . "\n";
246 $form .= Xml
::closeElement( 'form' ) . "\n";
249 $form .= Xml
::openElement( 'form', array(
251 'action' => $this->getTitle()->getLocalURL(),
252 'id' => 'mw-watchlist-form'
254 $form .= Xml
::fieldset(
255 $this->msg( 'watchlist-options' )->text(),
257 array( 'id' => 'mw-watchlist-options' )
260 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
262 $tables = array( 'recentchanges', 'watchlist' );
263 $fields = RecentChange
::selectFields();
265 'watchlist' => array(
268 'wl_user' => $user->getId(),
269 'wl_namespace=rc_namespace',
274 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
275 if ( $wgShowUpdatedMarker ) {
276 $fields[] = 'wl_notificationtimestamp';
278 if ( $limitWatchlist ) {
279 $options['LIMIT'] = $limitWatchlist;
282 $rollbacker = $user->isAllowed( 'rollback' );
283 if ( $usePage ||
$rollbacker ) {
285 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
287 $fields[] = 'page_latest';
291 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
292 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
294 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
295 $numRows = $res->numRows();
297 /* Start bottom header */
299 $lang = $this->getLanguage();
301 if ( $values['days'] > 0 ) {
302 $timestamp = wfTimestampNow();
303 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
304 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
307 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
309 # Spit out some control panel links
311 'hideMinor' => 'rcshowhideminor',
312 'hideBots' => 'rcshowhidebots',
313 'hideAnons' => 'rcshowhideanons',
314 'hideLiu' => 'rcshowhideliu',
315 'hideOwn' => 'rcshowhidemine',
316 'hidePatrolled' => 'rcshowhidepatr'
318 foreach ( $this->customFilters
as $key => $params ) {
319 $filters[$key] = $params['msg'];
321 // Disable some if needed
322 if ( !$user->useNPPatrol() ) {
323 unset( $filters['hidePatrolled'] );
327 foreach ( $filters as $name => $msg ) {
328 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
331 $hiddenFields = $nondefaults;
332 unset( $hiddenFields['namespace'] );
333 unset( $hiddenFields['invert'] );
334 unset( $hiddenFields['associated'] );
336 # Namespace filter and put the whole form together.
338 $form .= $cutofflinks;
339 $form .= $lang->pipeList( $links ) . "\n";
340 $form .= "<hr />\n<p>";
341 $form .= Html
::namespaceSelector(
343 'selected' => $nameSpace,
345 'label' => $this->msg( 'namespace' )->text()
347 'name' => 'namespace',
349 'class' => 'namespaceselector',
352 $form .= Xml
::checkLabel(
353 $this->msg( 'invert' )->text(),
357 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
359 $form .= Xml
::checkLabel(
360 $this->msg( 'namespace_association' )->text(),
364 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
366 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
367 foreach ( $hiddenFields as $key => $value ) {
368 $form .= Html
::hidden( $key, $value ) . "\n";
370 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
371 $form .= Xml
::closeElement( 'form' ) . "\n";
372 $output->addHTML( $form );
374 # If there's nothing to show, stop here
375 if ( $numRows == 0 ) {
376 $output->wrapWikiMsg(
377 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
382 /* End bottom header */
384 /* Do link batch query */
385 $linkBatch = new LinkBatch
;
386 foreach ( $res as $row ) {
387 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
388 if ( $row->rc_user
!= 0 ) {
389 $linkBatch->add( NS_USER
, $userNameUnderscored );
391 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
393 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
395 $linkBatch->execute();
396 $dbr->dataSeek( $res, 0 );
398 $list = ChangesList
::newFromContext( $this->getContext() );
399 $list->setWatchlistDivs();
401 $s = $list->beginRecentChangesList();
403 foreach ( $res as $obj ) {
405 $rc = RecentChange
::newFromRow( $obj );
406 $rc->counter
= $counter++
;
408 if ( $wgShowUpdatedMarker ) {
409 $updated = $obj->wl_notificationtimestamp
;
414 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
415 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
418 'wl_namespace' => $obj->rc_namespace
,
419 'wl_title' => $obj->rc_title
,
423 $rc->numberofWatchingusers
= 0;
426 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
427 if ( $changeLine !== false ) {
431 $s .= $list->endRecentChangesList();
433 $output->addHTML( $s );
436 protected function showHideLink( $options, $message, $name, $value ) {
437 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
438 $options[$name] = 1 - (int)$value;
440 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
443 protected function hoursLink( $h, $options = array() ) {
444 $options['days'] = ( $h / 24.0 );
446 return Linker
::linkKnown(
448 $this->getLanguage()->formatNum( $h ),
454 protected function daysLink( $d, $options = array() ) {
455 $options['days'] = $d;
456 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
458 return Linker
::linkKnown(
469 * @param int $days This gets overwritten, so is not used
470 * @param array $options Query parameters for URL
473 protected function cutoffLinks( $days, $options = array() ) {
474 $hours = array( 1, 2, 6, 12 );
475 $days = array( 1, 3, 7 );
477 foreach ( $hours as $h ) {
478 $hours[$i++
] = $this->hoursLink( $h, $options );
481 foreach ( $days as $d ) {
482 $days[$i++
] = $this->daysLink( $d, $options );
484 return $this->msg( 'wlshowlast' )->rawParams(
485 $this->getLanguage()->pipeList( $hours ),
486 $this->getLanguage()->pipeList( $days ),
487 $this->daysLink( 0, $options ) )->parse();
491 * Count the number of items on a user's watchlist
493 * @param DatabaseBase $dbr A database connection
496 protected function countItems( $dbr ) {
497 # Fetch the raw count
498 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
499 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
500 $row = $dbr->fetchObject( $res );
501 $count = $row->count
;
503 return floor( $count / 2 );
506 protected function getGroupName() {