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();
43 # Anons don't get a watchlist
44 $this->requireLogin( 'watchlistanontext', 'watchnologin' );
47 $this->checkPermissions();
50 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
52 $this->addFeedLinks( array(
53 'action' => 'feedwatchlist',
55 'wlowner' => $user->getName(),
56 'wltoken' => $wlToken,
61 $this->outputHeader();
64 $this->msg( 'watchlistfor2', $user->getName() )
65 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
68 $request = $this->getRequest();
70 $mode = SpecialEditWatchlist
::getMode( $request, $par );
71 if ( $mode !== false ) {
72 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
73 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
75 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
78 $output->redirect( $title->getLocalURL() );
82 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
84 $nitems = $this->countItems( $dbr );
86 $output->addWikiMsg( 'nowatchlist' );
90 // @todo use FormOptions!
92 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
93 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
94 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
95 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
96 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
97 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
98 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
99 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
100 /* ? */ 'namespace' => '', //means all
101 /* ? */ 'invert' => false,
102 /* bool */ 'associated' => false,
104 $this->customFilters
= array();
105 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
106 foreach ( $this->customFilters
as $key => $params ) {
107 $defaults[$key] = $params['default'];
110 # Extract variables from the request, falling back to user preferences or
111 # other default values if these don't exist
113 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
114 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
115 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
116 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
117 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
118 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
119 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
120 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
121 foreach ( $this->customFilters
as $key => $params ) {
122 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
125 # Get namespace value, if supplied, and prepare a WHERE fragment
126 $nameSpace = $request->getIntOrNull( 'namespace' );
127 $invert = $request->getBool( 'invert' );
128 $associated = $request->getBool( 'associated' );
129 if ( !is_null( $nameSpace ) ) {
130 $eq_op = $invert ?
'!=' : '=';
131 $bool_op = $invert ?
'AND' : 'OR';
132 $nameSpace = intval( $nameSpace ); // paranioa
133 if ( !$associated ) {
134 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
136 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
138 "rc_namespace $eq_op $nameSpace " .
140 " rc_namespace $eq_op $associatedNS";
144 $nameSpaceClause = '';
146 $values['namespace'] = $nameSpace;
147 $values['invert'] = $invert;
148 $values['associated'] = $associated;
150 // Dump everything here
151 $nondefaults = array();
152 foreach ( $defaults as $name => $defValue ) {
153 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
156 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
157 $request->wasPosted() )
159 $user->clearAllNotifications();
160 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
164 # Possible where conditions
167 if ( $values['days'] > 0 ) {
168 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
172 if ( $values['hideOwn'] ) {
173 $conds[] = 'rc_user != ' . $user->getId();
175 if ( $values['hideBots'] ) {
176 $conds[] = 'rc_bot = 0';
178 if ( $values['hideMinor'] ) {
179 $conds[] = 'rc_minor = 0';
181 if ( $values['hideLiu'] ) {
182 $conds[] = 'rc_user = 0';
184 if ( $values['hideAnons'] ) {
185 $conds[] = 'rc_user != 0';
187 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
188 $conds[] = 'rc_patrolled != 1';
190 if ( $nameSpaceClause ) {
191 $conds[] = $nameSpaceClause;
194 # Toggle watchlist content (all recent edits or just the latest)
195 if ( $values['extended'] ) {
196 $limitWatchlist = $user->getIntOption( 'wllimit' );
199 # Top log Ids for a page are not stored
200 $nonRevisionTypes = array( RC_LOG
);
201 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
202 if ( $nonRevisionTypes ) {
203 $conds[] = $dbr->makeList(
205 'rc_this_oldid=page_latest',
206 'rc_type' => $nonRevisionTypes,
215 # Show a message about slave lag, if applicable
216 $lag = wfGetLB()->safeGetLag( $dbr );
218 $output->showLagWarning( $lag );
224 # Show watchlist header
226 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
227 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
228 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
230 if ( $wgShowUpdatedMarker ) {
231 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
235 if ( $wgShowUpdatedMarker ) {
236 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
237 'action' => $this->getTitle()->getLocalURL(),
238 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
239 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
240 Html
::hidden( 'reset', 'all' ) . "\n";
241 foreach ( $nondefaults as $key => $value ) {
242 $form .= Html
::hidden( $key, $value ) . "\n";
244 $form .= Xml
::closeElement( 'form' ) . "\n";
247 $form .= Xml
::openElement( 'form', array(
249 'action' => $this->getTitle()->getLocalURL(),
250 'id' => 'mw-watchlist-form'
252 $form .= Xml
::fieldset(
253 $this->msg( 'watchlist-options' )->text(),
255 array( 'id' => 'mw-watchlist-options' )
258 $tables = array( 'recentchanges', 'watchlist' );
259 $fields = RecentChange
::selectFields();
261 'watchlist' => array(
264 'wl_user' => $user->getId(),
265 'wl_namespace=rc_namespace',
270 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
271 if ( $wgShowUpdatedMarker ) {
272 $fields[] = 'wl_notificationtimestamp';
274 if ( $limitWatchlist ) {
275 $options['LIMIT'] = $limitWatchlist;
278 $rollbacker = $user->isAllowed( 'rollback' );
279 if ( $usePage ||
$rollbacker ) {
281 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
283 $fields[] = 'page_latest';
287 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
288 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
290 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
291 $numRows = $res->numRows();
293 /* Start bottom header */
295 $lang = $this->getLanguage();
297 if ( $values['days'] > 0 ) {
298 $timestamp = wfTimestampNow();
299 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
300 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
303 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
305 # Spit out some control panel links
307 'hideMinor' => 'rcshowhideminor',
308 'hideBots' => 'rcshowhidebots',
309 'hideAnons' => 'rcshowhideanons',
310 'hideLiu' => 'rcshowhideliu',
311 'hideOwn' => 'rcshowhidemine',
312 'hidePatrolled' => 'rcshowhidepatr'
314 foreach ( $this->customFilters
as $key => $params ) {
315 $filters[$key] = $params['msg'];
317 // Disable some if needed
318 if ( !$user->useNPPatrol() ) {
319 unset( $filters['hidePatrolled'] );
323 foreach ( $filters as $name => $msg ) {
324 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
327 $hiddenFields = $nondefaults;
328 unset( $hiddenFields['namespace'] );
329 unset( $hiddenFields['invert'] );
330 unset( $hiddenFields['associated'] );
332 # Namespace filter and put the whole form together.
334 $form .= $cutofflinks;
335 $form .= $lang->pipeList( $links ) . "\n";
336 $form .= "<hr />\n<p>";
337 $form .= Html
::namespaceSelector(
339 'selected' => $nameSpace,
341 'label' => $this->msg( 'namespace' )->text()
343 'name' => 'namespace',
345 'class' => 'namespaceselector',
348 $form .= Xml
::checkLabel(
349 $this->msg( 'invert' )->text(),
353 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
355 $form .= Xml
::checkLabel(
356 $this->msg( 'namespace_association' )->text(),
360 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
362 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
363 foreach ( $hiddenFields as $key => $value ) {
364 $form .= Html
::hidden( $key, $value ) . "\n";
366 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
367 $form .= Xml
::closeElement( 'form' ) . "\n";
368 $output->addHTML( $form );
370 # If there's nothing to show, stop here
371 if ( $numRows == 0 ) {
372 $output->wrapWikiMsg(
373 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
378 /* End bottom header */
380 /* Do link batch query */
381 $linkBatch = new LinkBatch
;
382 foreach ( $res as $row ) {
383 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
384 if ( $row->rc_user
!= 0 ) {
385 $linkBatch->add( NS_USER
, $userNameUnderscored );
387 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
389 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
391 $linkBatch->execute();
392 $dbr->dataSeek( $res, 0 );
394 $list = ChangesList
::newFromContext( $this->getContext() );
395 $list->setWatchlistDivs();
397 $s = $list->beginRecentChangesList();
399 foreach ( $res as $obj ) {
401 $rc = RecentChange
::newFromRow( $obj );
402 $rc->counter
= $counter++
;
404 if ( $wgShowUpdatedMarker ) {
405 $updated = $obj->wl_notificationtimestamp
;
410 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
411 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
414 'wl_namespace' => $obj->rc_namespace
,
415 'wl_title' => $obj->rc_title
,
419 $rc->numberofWatchingusers
= 0;
422 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
423 if ( $changeLine !== false ) {
427 $s .= $list->endRecentChangesList();
429 $output->addHTML( $s );
432 protected function showHideLink( $options, $message, $name, $value ) {
433 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
434 $options[$name] = 1 - (int)$value;
436 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
439 protected function hoursLink( $h, $options = array() ) {
440 $options['days'] = ( $h / 24.0 );
442 return Linker
::linkKnown(
444 $this->getLanguage()->formatNum( $h ),
450 protected function daysLink( $d, $options = array() ) {
451 $options['days'] = $d;
452 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
454 return Linker
::linkKnown(
465 * @param int $days This gets overwritten, so is not used
466 * @param array $options Query parameters for URL
469 protected function cutoffLinks( $days, $options = array() ) {
470 $hours = array( 1, 2, 6, 12 );
471 $days = array( 1, 3, 7 );
473 foreach ( $hours as $h ) {
474 $hours[$i++
] = $this->hoursLink( $h, $options );
477 foreach ( $days as $d ) {
478 $days[$i++
] = $this->daysLink( $d, $options );
480 return $this->msg( 'wlshowlast' )->rawParams(
481 $this->getLanguage()->pipeList( $hours ),
482 $this->getLanguage()->pipeList( $days ),
483 $this->daysLink( 0, $options ) )->parse();
487 * Count the number of items on a user's watchlist
489 * @param DatabaseBase $dbr A database connection
492 protected function countItems( $dbr ) {
493 # Fetch the raw count
494 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
495 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
496 $row = $dbr->fetchObject( $res );
497 $count = $row->count
;
499 return floor( $count / 2 );
502 protected function getGroupName() {