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 if ( $user->isAnon() ) {
45 $output->setPageTitle( $this->msg( 'watchnologin' ) );
46 $output->setRobotPolicy( 'noindex,nofollow' );
47 $llink = Linker
::linkKnown(
48 SpecialPage
::getTitleFor( 'Userlogin' ),
49 $this->msg( 'loginreqlink' )->escaped(),
51 array( 'returnto' => $this->getTitle()->getPrefixedText() )
53 $output->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
58 $this->checkPermissions();
61 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
63 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
64 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
68 $this->outputHeader();
70 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
71 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
73 $request = $this->getRequest();
75 $mode = SpecialEditWatchlist
::getMode( $request, $par );
76 if ( $mode !== false ) {
79 case SpecialEditWatchlist
::EDIT_CLEAR
:
82 case SpecialEditWatchlist
::EDIT_RAW
:
88 $title = SpecialPage
::getTitleFor( 'EditWatchlist', $mode );
89 $output->redirect( $title->getLocalURL() );
93 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
95 $nitems = $this->countItems( $dbr );
97 $output->addWikiMsg( 'nowatchlist' );
101 // @todo use FormOptions!
103 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
104 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
105 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
106 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
107 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
108 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
109 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
110 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
111 /* ? */ 'namespace' => '', //means all
112 /* ? */ 'invert' => false,
113 /* bool */ 'associated' => false,
115 $this->customFilters
= array();
116 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
117 foreach ( $this->customFilters
as $key => $params ) {
118 $defaults[$key] = $params['default'];
121 # Extract variables from the request, falling back to user preferences or
122 # other default values if these don't exist
124 $values['days'] = $request->getVal( 'days', $defaults['days'] );
125 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
126 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
127 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
128 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
129 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
130 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
131 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
132 foreach ( $this->customFilters
as $key => $params ) {
133 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
136 # Get namespace value, if supplied, and prepare a WHERE fragment
137 $nameSpace = $request->getIntOrNull( 'namespace' );
138 $invert = $request->getBool( 'invert' );
139 $associated = $request->getBool( 'associated' );
140 if ( !is_null( $nameSpace ) ) {
141 $eq_op = $invert ?
'!=' : '=';
142 $bool_op = $invert ?
'AND' : 'OR';
143 $nameSpace = intval( $nameSpace ); // paranioa
144 if ( !$associated ) {
145 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
147 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
149 "rc_namespace $eq_op $nameSpace " .
151 " rc_namespace $eq_op $associatedNS";
155 $nameSpaceClause = '';
157 $values['namespace'] = $nameSpace;
158 $values['invert'] = $invert;
159 $values['associated'] = $associated;
161 if ( is_null( $values['days'] ) ||
!is_numeric( $values['days'] ) ) {
162 $big = 1000; /* The magical big */
163 if ( $nitems > $big ) {
164 # Set default cutoff shorter
165 $values['days'] = $defaults['days'] = ( 12.0 / 24.0 ); # 12 hours...
167 $values['days'] = $defaults['days']; # default cutoff for shortlisters
170 $values['days'] = floatval( $values['days'] );
173 // Dump everything here
174 $nondefaults = array();
175 foreach ( $defaults as $name => $defValue ) {
176 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
179 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
180 $request->wasPosted() )
182 $user->clearAllNotifications();
183 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
187 # Possible where conditions
190 if ( $values['days'] > 0 ) {
191 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
194 # If the watchlist is relatively short, it's simplest to zip
195 # down its entirety and then sort the results.
197 # If it's relatively long, it may be worth our while to zip
198 # through the time-sorted page list checking for watched items.
200 # Up estimate of watched items by 15% to compensate for talk pages...
203 if ( $values['hideOwn'] ) {
204 $conds[] = 'rc_user != ' . $user->getId();
206 if ( $values['hideBots'] ) {
207 $conds[] = 'rc_bot = 0';
209 if ( $values['hideMinor'] ) {
210 $conds[] = 'rc_minor = 0';
212 if ( $values['hideLiu'] ) {
213 $conds[] = 'rc_user = 0';
215 if ( $values['hideAnons'] ) {
216 $conds[] = 'rc_user != 0';
218 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
219 $conds[] = 'rc_patrolled != 1';
221 if ( $nameSpaceClause ) {
222 $conds[] = $nameSpaceClause;
225 # Toggle watchlist content (all recent edits or just the latest)
226 if ( $values['extended'] ) {
227 $limitWatchlist = $user->getIntOption( 'wllimit' );
230 # Top log Ids for a page are not stored
231 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
236 # Show a message about slave lag, if applicable
237 $lag = wfGetLB()->safeGetLag( $dbr );
239 $output->showLagWarning( $lag );
245 # Show watchlist header
247 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
248 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
249 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
251 if ( $wgShowUpdatedMarker ) {
252 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
256 if ( $wgShowUpdatedMarker ) {
257 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
258 'action' => $this->getTitle()->getLocalURL(),
259 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
260 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
261 Html
::hidden( 'reset', 'all' ) . "\n";
262 foreach ( $nondefaults as $key => $value ) {
263 $form .= Html
::hidden( $key, $value ) . "\n";
265 $form .= Xml
::closeElement( 'form' ) . "\n";
268 $form .= Xml
::openElement( 'form', array(
270 'action' => $this->getTitle()->getLocalURL(),
271 'id' => 'mw-watchlist-form'
273 $form .= Xml
::fieldset(
274 $this->msg( 'watchlist-options' )->text(),
276 array( 'id' => 'mw-watchlist-options' )
279 $tables = array( 'recentchanges', 'watchlist' );
280 $fields = RecentChange
::selectFields();
282 'watchlist' => array(
285 'wl_user' => $user->getId(),
286 'wl_namespace=rc_namespace',
291 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
292 if ( $wgShowUpdatedMarker ) {
293 $fields[] = 'wl_notificationtimestamp';
295 if ( $limitWatchlist ) {
296 $options['LIMIT'] = $limitWatchlist;
299 $rollbacker = $user->isAllowed( 'rollback' );
300 if ( $usePage ||
$rollbacker ) {
302 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
304 $fields[] = 'page_latest';
308 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
309 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
311 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
312 $numRows = $res->numRows();
314 /* Start bottom header */
316 $lang = $this->getLanguage();
318 if ( $values['days'] > 0 ) {
319 $timestamp = wfTimestampNow();
320 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
321 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
324 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
326 # Spit out some control panel links
328 'hideMinor' => 'rcshowhideminor',
329 'hideBots' => 'rcshowhidebots',
330 'hideAnons' => 'rcshowhideanons',
331 'hideLiu' => 'rcshowhideliu',
332 'hideOwn' => 'rcshowhidemine',
333 'hidePatrolled' => 'rcshowhidepatr'
335 foreach ( $this->customFilters
as $key => $params ) {
336 $filters[$key] = $params['msg'];
338 // Disable some if needed
339 if ( !$user->useNPPatrol() ) {
340 unset( $filters['hidePatrolled'] );
344 foreach ( $filters as $name => $msg ) {
345 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
348 $hiddenFields = $nondefaults;
349 unset( $hiddenFields['namespace'] );
350 unset( $hiddenFields['invert'] );
351 unset( $hiddenFields['associated'] );
353 # Namespace filter and put the whole form together.
355 $form .= $cutofflinks;
356 $form .= $lang->pipeList( $links ) . "\n";
357 $form .= "<hr />\n<p>";
358 $form .= Html
::namespaceSelector(
360 'selected' => $nameSpace,
362 'label' => $this->msg( 'namespace' )->text()
364 'name' => 'namespace',
366 'class' => 'namespaceselector',
369 $form .= Xml
::checkLabel(
370 $this->msg( 'invert' )->text(),
374 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
376 $form .= Xml
::checkLabel(
377 $this->msg( 'namespace_association' )->text(),
381 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
383 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
384 foreach ( $hiddenFields as $key => $value ) {
385 $form .= Html
::hidden( $key, $value ) . "\n";
387 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
388 $form .= Xml
::closeElement( 'form' ) . "\n";
389 $output->addHTML( $form );
391 # If there's nothing to show, stop here
392 if ( $numRows == 0 ) {
393 $output->wrapWikiMsg(
394 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
399 /* End bottom header */
401 /* Do link batch query */
402 $linkBatch = new LinkBatch
;
403 foreach ( $res as $row ) {
404 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
405 if ( $row->rc_user
!= 0 ) {
406 $linkBatch->add( NS_USER
, $userNameUnderscored );
408 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
410 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
412 $linkBatch->execute();
413 $dbr->dataSeek( $res, 0 );
415 $list = ChangesList
::newFromContext( $this->getContext() );
416 $list->setWatchlistDivs();
418 $s = $list->beginRecentChangesList();
420 foreach ( $res as $obj ) {
422 $rc = RecentChange
::newFromRow( $obj );
423 $rc->counter
= $counter++
;
425 if ( $wgShowUpdatedMarker ) {
426 $updated = $obj->wl_notificationtimestamp
;
431 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
432 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
435 'wl_namespace' => $obj->rc_namespace
,
436 'wl_title' => $obj->rc_title
,
440 $rc->numberofWatchingusers
= 0;
443 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
444 if ( $changeLine !== false ) {
448 $s .= $list->endRecentChangesList();
450 $output->addHTML( $s );
453 protected function showHideLink( $options, $message, $name, $value ) {
454 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
455 $options[$name] = 1 - (int) $value;
457 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
460 protected function hoursLink( $h, $options = array() ) {
461 $options['days'] = ( $h / 24.0 );
463 return Linker
::linkKnown(
465 $this->getLanguage()->formatNum( $h ),
471 protected function daysLink( $d, $options = array() ) {
472 $options['days'] = $d;
473 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
475 return Linker
::linkKnown(
486 * @param int $days This gets overwritten, so is not used
487 * @param array $options Query parameters for URL
490 protected function cutoffLinks( $days, $options = array() ) {
491 $hours = array( 1, 2, 6, 12 );
492 $days = array( 1, 3, 7 );
494 foreach ( $hours as $h ) {
495 $hours[$i++
] = $this->hoursLink( $h, $options );
498 foreach ( $days as $d ) {
499 $days[$i++
] = $this->daysLink( $d, $options );
501 return $this->msg( 'wlshowlast' )->rawParams(
502 $this->getLanguage()->pipeList( $hours ),
503 $this->getLanguage()->pipeList( $days ),
504 $this->daysLink( 0, $options ) )->parse();
508 * Count the number of items on a user's watchlist
510 * @param DatabaseBase $dbr A database connection
513 protected function countItems( $dbr ) {
514 # Fetch the raw count
515 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
516 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
517 $row = $dbr->fetchObject( $res );
518 $count = $row->count
;
520 return floor( $count / 2 );
523 protected function getGroupName() {