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 if( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
95 $request->wasPosted() )
97 $user->clearAllNotifications();
98 $output->redirect( $this->getTitle()->getFullUrl() );
102 $nitems = $this->countItems();
103 if ( $nitems == 0 ) {
104 $output->addWikiMsg( 'nowatchlist' );
108 // @TODO: use FormOptions!
110 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
111 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
112 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
113 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
114 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
115 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
116 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
117 /* ? */ 'namespace' => 'all',
118 /* ? */ 'invert' => false,
120 $this->customFilters
= array();
121 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
122 foreach( $this->customFilters
as $key => $params ) {
123 $defaults[$key] = $params['msg'];
126 # Extract variables from the request, falling back to user preferences or
127 # other default values if these don't exist
128 $prefs['days'] = floatval( $user->getOption( 'watchlistdays' ) );
129 $prefs['hideminor'] = $user->getBoolOption( 'watchlisthideminor' );
130 $prefs['hidebots'] = $user->getBoolOption( 'watchlisthidebots' );
131 $prefs['hideanons'] = $user->getBoolOption( 'watchlisthideanons' );
132 $prefs['hideliu'] = $user->getBoolOption( 'watchlisthideliu' );
133 $prefs['hideown' ] = $user->getBoolOption( 'watchlisthideown' );
134 $prefs['hidepatrolled' ] = $user->getBoolOption( 'watchlisthidepatrolled' );
136 # Get query variables
138 $values['days'] = $request->getVal( 'days', $prefs['days'] );
139 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $prefs['hideminor'] );
140 $values['hideBots'] = (int)$request->getBool( 'hideBots' , $prefs['hidebots'] );
141 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $prefs['hideanons'] );
142 $values['hideLiu'] = (int)$request->getBool( 'hideLiu' , $prefs['hideliu'] );
143 $values['hideOwn'] = (int)$request->getBool( 'hideOwn' , $prefs['hideown'] );
144 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $prefs['hidepatrolled'] );
145 foreach( $this->customFilters
as $key => $params ) {
146 $values[$key] = (int)$request->getBool( $key );
149 # Get namespace value, if supplied, and prepare a WHERE fragment
150 $nameSpace = $request->getIntOrNull( 'namespace' );
151 $invert = $request->getIntOrNull( 'invert' );
152 if ( !is_null( $nameSpace ) ) {
153 $nameSpace = intval( $nameSpace ); // paranioa
155 $nameSpaceClause = "rc_namespace != $nameSpace";
157 $nameSpaceClause = "rc_namespace = $nameSpace";
161 $nameSpaceClause = '';
163 $values['namespace'] = $nameSpace;
164 $values['invert'] = $invert;
166 if( is_null( $values['days'] ) ||
!is_numeric( $values['days'] ) ) {
167 $big = 1000; /* The magical big */
168 if( $nitems > $big ) {
169 # Set default cutoff shorter
170 $values['days'] = $defaults['days'] = (12.0 / 24.0); # 12 hours...
172 $values['days'] = $defaults['days']; # default cutoff for shortlisters
175 $values['days'] = floatval( $values['days'] );
178 // Dump everything here
179 $nondefaults = array();
180 foreach ( $defaults as $name => $defValue ) {
181 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
184 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
186 # Possible where conditions
189 if( $values['days'] > 0 ) {
190 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $values['days'] * 86400 ) )."'";
193 # If the watchlist is relatively short, it's simplest to zip
194 # down its entirety and then sort the results.
196 # If it's relatively long, it may be worth our while to zip
197 # through the time-sorted page list checking for watched items.
199 # 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( $user->getOption( 'extendwatchlist' ) ) {
227 $limitWatchlist = intval( $user->getOption( '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 );
243 $form = Xml
::fieldset( $this->msg( 'watchlist-options' )->text(), false, array( 'id' => 'mw-watchlist-options' ) );
245 # Show watchlist header
246 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse();
248 if( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
249 $form .= $this->msg( 'wlheader-enotif' )->parseAsBlock() . "\n";
251 if( $wgShowUpdatedMarker ) {
252 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
253 'action' => $this->getTitle()->getLocalUrl(),
254 'id' => 'mw-watchlist-resetbutton' ) ) .
255 $this->msg( 'wlheader-showupdated' )->parse() . ' ' .
256 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) .
257 Html
::hidden( 'reset', 'all' ) .
258 Xml
::closeElement( 'form' );
262 $tables = array( 'recentchanges', 'watchlist' );
263 $fields = array( $dbr->tableName( 'recentchanges' ) . '.*' );
265 'watchlist' => array('INNER JOIN',"wl_user='{$user->getId()}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
267 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
268 if( $wgShowUpdatedMarker ) {
269 $fields[] = 'wl_notificationtimestamp';
271 if( $limitWatchlist ) {
272 $options['LIMIT'] = $limitWatchlist;
275 $rollbacker = $user->isAllowed('rollback');
276 if ( $usePage ||
$rollbacker ) {
278 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
280 $fields[] = 'page_latest';
284 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
285 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
287 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
288 $numRows = $res->numRows();
290 /* Start bottom header */
292 $lang = $this->getLanguage();
294 if( $values['days'] > 0 ) {
295 $timestamp = wfTimestampNow();
296 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
297 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . '<br />';
300 $cutofflinks = "\n" . $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
302 # Spit out some control panel links
304 'hideMinor' => 'rcshowhideminor',
305 'hideBots' => 'rcshowhidebots',
306 'hideAnons' => 'rcshowhideanons',
307 'hideLiu' => 'rcshowhideliu',
308 'hideOwn' => 'rcshowhidemine',
309 'hidePatrolled' => 'rcshowhidepatr'
311 foreach ( $this->customFilters
as $key => $params ) {
312 $filters[$key] = $params['msg'];
314 // Disable some if needed
315 if ( !$user->useNPPatrol() ) {
316 unset( $filters['hidePatrolled'] );
320 foreach( $filters as $name => $msg ) {
321 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
324 # Namespace filter and put the whole form together.
326 $form .= $cutofflinks;
327 $form .= $lang->pipeList( $links );
328 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
329 $form .= '<hr /><p>';
330 $form .= Html
::namespaceSelector(
332 'selected' => $nameSpace,
334 'label' => $this->msg( 'namespace' )->text()
336 'name' => 'namespace',
338 'class' => 'namespaceselector',
341 $form .= Xml
::checkLabel( $this->msg( 'invert' )->text(), 'invert', 'nsinvert', $invert ) . ' ';
342 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . '</p>';
343 $form .= Html
::hidden( 'days', $values['days'] );
344 foreach ( $filters as $key => $msg ) {
345 if ( $values[$key] ) {
346 $form .= Html
::hidden( $key, 1 );
349 $form .= Xml
::closeElement( 'form' );
350 $form .= Xml
::closeElement( 'fieldset' );
351 $output->addHTML( $form );
353 # If there's nothing to show, stop here
354 if( $numRows == 0 ) {
355 $output->addWikiMsg( 'watchnochange' );
359 /* End bottom header */
361 /* Do link batch query */
362 $linkBatch = new LinkBatch
;
363 foreach ( $res as $row ) {
364 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
365 if ( $row->rc_user
!= 0 ) {
366 $linkBatch->add( NS_USER
, $userNameUnderscored );
368 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
370 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
372 $linkBatch->execute();
373 $dbr->dataSeek( $res, 0 );
375 $list = ChangesList
::newFromContext( $this->getContext() );
376 $list->setWatchlistDivs();
378 $s = $list->beginRecentChangesList();
380 foreach ( $res as $obj ) {
382 $rc = RecentChange
::newFromRow( $obj );
383 $rc->counter
= $counter++
;
385 if ( $wgShowUpdatedMarker ) {
386 $updated = $obj->wl_notificationtimestamp
;
391 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
392 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
395 'wl_namespace' => $obj->rc_namespace
,
396 'wl_title' => $obj->rc_title
,
400 $rc->numberofWatchingusers
= 0;
403 $s .= $list->recentChangesLine( $rc, $updated, $counter );
405 $s .= $list->endRecentChangesList();
407 $output->addHTML( $s );
410 protected function showHideLink( $options, $message, $name, $value ) {
411 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
412 $options[$name] = 1 - (int) $value;
414 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
417 protected function hoursLink( $h, $options = array() ) {
418 $options['days'] = ( $h / 24.0 );
420 return Linker
::linkKnown(
422 $this->getLanguage()->formatNum( $h ),
428 protected function daysLink( $d, $options = array() ) {
429 $options['days'] = $d;
430 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
432 return Linker
::linkKnown(
445 protected function cutoffLinks( $days, $options = array() ) {
446 $hours = array( 1, 2, 6, 12 );
447 $days = array( 1, 3, 7 );
449 foreach( $hours as $h ) {
450 $hours[$i++
] = $this->hoursLink( $h, $options );
453 foreach( $days as $d ) {
454 $days[$i++
] = $this->daysLink( $d, $options );
456 return $this->msg( 'wlshowlast' )->rawParams(
457 $this->getLanguage()->pipeList( $hours ),
458 $this->getLanguage()->pipeList( $days ),
459 $this->daysLink( 0, $options ) )->parse();
463 * Count the number of items on a user's watchlist
467 protected function countItems() {
468 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
470 # Fetch the raw count
471 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
472 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
473 $row = $dbr->fetchObject( $res );
474 $count = $row->count
;
476 return floor( $count / 2 );