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' ) {
30 parent
::__construct( $page );
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 $wlToken = $user->getOption( 'watchlisttoken' );
60 $wlToken = MWCryptRand
::generateHex( 40 );
61 $user->setOption( 'watchlisttoken', $wlToken );
62 $user->saveSettings();
65 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
66 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
69 $this->outputHeader();
71 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
72 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
74 $request = $this->getRequest();
76 $mode = SpecialEditWatchlist
::getMode( $request, $par );
77 if( $mode !== false ) {
80 case SpecialEditWatchlist
::EDIT_CLEAR
:
83 case SpecialEditWatchlist
::EDIT_RAW
:
89 $title = SpecialPage
::getTitleFor( 'EditWatchlist', $mode );
90 $output->redirect( $title->getLocalUrl() );
94 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
96 $nitems = $this->countItems( $dbr );
98 $output->addWikiMsg( 'nowatchlist' );
102 // @TODO: use FormOptions!
104 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
105 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
106 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
107 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
108 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
109 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
110 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
111 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
112 /* ? */ 'namespace' => '', //means all
113 /* ? */ 'invert' => false,
114 /* bool */ 'associated' => false,
116 $this->customFilters
= array();
117 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
118 foreach( $this->customFilters
as $key => $params ) {
119 $defaults[$key] = $params['msg'];
122 # Extract variables from the request, falling back to user preferences or
123 # other default values if these don't exist
125 $values['days'] = $request->getVal( 'days', $defaults['days'] );
126 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
127 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
128 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
129 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
130 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
131 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
132 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
133 foreach( $this->customFilters
as $key => $params ) {
134 $values[$key] = (int)$request->getBool( $key );
137 # Get namespace value, if supplied, and prepare a WHERE fragment
138 $nameSpace = $request->getIntOrNull( 'namespace' );
139 $invert = $request->getBool( 'invert' );
140 $associated = $request->getBool( 'associated' );
141 if ( !is_null( $nameSpace ) ) {
142 $eq_op = $invert ?
'!=' : '=';
143 $bool_op = $invert ?
'AND' : 'OR';
144 $nameSpace = intval( $nameSpace ); // paranioa
145 if ( !$associated ) {
146 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
148 $associatedNS = MWNamespace
::getAssociated( $nameSpace );
150 "rc_namespace $eq_op $nameSpace " .
152 " rc_namespace $eq_op $associatedNS";
156 $nameSpaceClause = '';
158 $values['namespace'] = $nameSpace;
159 $values['invert'] = $invert;
160 $values['associated'] = $associated;
162 if( is_null( $values['days'] ) ||
!is_numeric( $values['days'] ) ) {
163 $big = 1000; /* The magical big */
164 if( $nitems > $big ) {
165 # Set default cutoff shorter
166 $values['days'] = $defaults['days'] = (12.0 / 24.0); # 12 hours...
168 $values['days'] = $defaults['days']; # default cutoff for shortlisters
171 $values['days'] = floatval( $values['days'] );
174 // Dump everything here
175 $nondefaults = array();
176 foreach ( $defaults as $name => $defValue ) {
177 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
180 if( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
181 $request->wasPosted() )
183 $user->clearAllNotifications();
184 $output->redirect( $this->getTitle()->getFullUrl( $nondefaults ) );
188 # Possible where conditions
191 if( $values['days'] > 0 ) {
192 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
195 # If the watchlist is relatively short, it's simplest to zip
196 # down its entirety and then sort the results.
198 # If it's relatively long, it may be worth our while to zip
199 # through the time-sorted page list checking for watched items.
201 # Up estimate of watched items by 15% to compensate for talk pages...
205 if( $values['hideOwn'] ) {
206 $conds[] = 'rc_user != ' . $user->getId();
208 if( $values['hideBots'] ) {
209 $conds[] = 'rc_bot = 0';
211 if( $values['hideMinor'] ) {
212 $conds[] = 'rc_minor = 0';
214 if( $values['hideLiu'] ) {
215 $conds[] = 'rc_user = 0';
217 if( $values['hideAnons'] ) {
218 $conds[] = 'rc_user != 0';
220 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
221 $conds[] = 'rc_patrolled != 1';
223 if ( $nameSpaceClause ) {
224 $conds[] = $nameSpaceClause;
227 # Toggle watchlist content (all recent edits or just the latest)
228 if( $values['extended'] ) {
229 $limitWatchlist = intval( $user->getOption( 'wllimit' ) );
232 # Top log Ids for a page are not stored
233 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
238 # Show a message about slave lag, if applicable
239 $lag = wfGetLB()->safeGetLag( $dbr );
241 $output->showLagWarning( $lag );
245 $form = Xml
::fieldset( $this->msg( 'watchlist-options' )->text(), false, array( 'id' => 'mw-watchlist-options' ) );
247 # Show watchlist header
248 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
250 if( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
251 $form .= $this->msg( 'wlheader-enotif' )->parseAsBlock() . "\n";
253 if( $wgShowUpdatedMarker ) {
254 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
255 'action' => $this->getTitle()->getLocalUrl(),
256 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
257 $this->msg( 'wlheader-showupdated' )->parse() .
258 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
259 Html
::hidden( 'reset', 'all' ) . "\n";
260 foreach ( $nondefaults as $key => $value ) {
261 $form .= Html
::hidden( $key, $value ) . "\n";
263 $form .= Xml
::closeElement( 'form' ) . "\n";
267 $tables = array( 'recentchanges', 'watchlist' );
268 $fields = RecentChange
::selectFields();
270 'watchlist' => array(
273 'wl_user' => $user->getId(),
274 'wl_namespace=rc_namespace',
279 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
280 if( $wgShowUpdatedMarker ) {
281 $fields[] = 'wl_notificationtimestamp';
283 if( $limitWatchlist ) {
284 $options['LIMIT'] = $limitWatchlist;
287 $rollbacker = $user->isAllowed( 'rollback' );
288 if ( $usePage ||
$rollbacker ) {
290 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
292 $fields[] = 'page_latest';
296 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
297 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields ) );
299 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
300 $numRows = $res->numRows();
302 /* Start bottom header */
304 $lang = $this->getLanguage();
306 if( $values['days'] > 0 ) {
307 $timestamp = wfTimestampNow();
308 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
309 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
312 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
314 # Spit out some control panel links
316 'hideMinor' => 'rcshowhideminor',
317 'hideBots' => 'rcshowhidebots',
318 'hideAnons' => 'rcshowhideanons',
319 'hideLiu' => 'rcshowhideliu',
320 'hideOwn' => 'rcshowhidemine',
321 'hidePatrolled' => 'rcshowhidepatr'
323 foreach ( $this->customFilters
as $key => $params ) {
324 $filters[$key] = $params['msg'];
326 // Disable some if needed
327 if ( !$user->useNPPatrol() ) {
328 unset( $filters['hidePatrolled'] );
332 foreach( $filters as $name => $msg ) {
333 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
336 $hiddenFields = $nondefaults;
337 unset( $hiddenFields['namespace'] );
338 unset( $hiddenFields['invert'] );
339 unset( $hiddenFields['associated'] );
341 # Namespace filter and put the whole form together.
343 $form .= $cutofflinks;
344 $form .= $lang->pipeList( $links ) . "\n";
345 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) ) . "\n";
346 $form .= "<hr />\n<p>";
347 $form .= Html
::namespaceSelector(
349 'selected' => $nameSpace,
351 'label' => $this->msg( 'namespace' )->text()
353 'name' => 'namespace',
355 'class' => 'namespaceselector',
358 $form .= Xml
::checkLabel(
359 $this->msg( 'invert' )->text(),
363 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
365 $form .= Xml
::checkLabel(
366 $this->msg( 'namespace_association' )->text(),
370 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
372 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
373 foreach ( $hiddenFields as $key => $value ) {
374 $form .= Html
::hidden( $key, $value ) . "\n";
376 $form .= Xml
::closeElement( 'form' ) . "\n";
377 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
378 $output->addHTML( $form );
380 # If there's nothing to show, stop here
381 if( $numRows == 0 ) {
382 $output->addWikiMsg( 'watchnochange' );
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->getTitle(), $label, array(), $options ) )->escaped();
447 protected function hoursLink( $h, $options = array() ) {
448 $options['days'] = ( $h / 24.0 );
450 return Linker
::linkKnown(
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(
475 protected function cutoffLinks( $days, $options = array() ) {
476 $hours = array( 1, 2, 6, 12 );
477 $days = array( 1, 3, 7 );
479 foreach( $hours as $h ) {
480 $hours[$i++
] = $this->hoursLink( $h, $options );
483 foreach( $days as $d ) {
484 $days[$i++
] = $this->daysLink( $d, $options );
486 return $this->msg( 'wlshowlast' )->rawParams(
487 $this->getLanguage()->pipeList( $hours ),
488 $this->getLanguage()->pipeList( $days ),
489 $this->daysLink( 0, $options ) )->parse();
493 * Count the number of items on a user's watchlist
495 * @param $dbr A database connection
498 protected function countItems( $dbr ) {
499 # Fetch the raw count
500 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
501 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
502 $row = $dbr->fetchObject( $res );
503 $count = $row->count
;
505 return floor( $count / 2 );