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();
44 $wlToken = $user->getOption( 'watchlisttoken' );
46 $wlToken = sha1( mt_rand() . microtime( true ) );
47 $user->setOption( 'watchlisttoken', $wlToken );
48 $user->saveSettings();
51 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
52 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
54 $output->setRobotPolicy( 'noindex,nofollow' );
56 # Anons don't get a watchlist
57 if( $user->isAnon() ) {
58 $output->setPageTitle( $this->msg( 'watchnologin' ) );
59 $llink = Linker
::linkKnown(
60 SpecialPage
::getTitleFor( 'Userlogin' ),
61 $this->msg( 'loginreqlink' )->escaped(),
63 array( 'returnto' => $this->getTitle()->getPrefixedText() )
65 $output->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
70 $this->outputHeader();
72 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
73 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
75 $request = $this->getRequest();
77 $mode = SpecialEditWatchlist
::getMode( $request, $par );
78 if( $mode !== false ) {
81 case SpecialEditWatchlist
::EDIT_CLEAR
:
84 case SpecialEditWatchlist
::EDIT_RAW
:
90 $title = SpecialPage
::getTitleFor( 'EditWatchlist', $mode );
91 $output->redirect( $title->getLocalUrl() );
95 if( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
96 $request->wasPosted() )
98 $user->clearAllNotifications();
99 $output->redirect( $this->getTitle()->getFullUrl() );
103 $nitems = $this->countItems();
104 if ( $nitems == 0 ) {
105 $output->addWikiMsg( 'nowatchlist' );
109 // @TODO: use FormOptions!
111 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
112 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
113 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
114 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
115 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
116 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
117 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
118 /* ? */ 'namespace' => 'all',
119 /* ? */ 'invert' => false,
121 $this->customFilters
= array();
122 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
123 foreach( $this->customFilters
as $key => $params ) {
124 $defaults[$key] = $params['msg'];
127 # Extract variables from the request, falling back to user preferences or
128 # other default values if these don't exist
129 $prefs['days'] = floatval( $user->getOption( 'watchlistdays' ) );
130 $prefs['hideminor'] = $user->getBoolOption( 'watchlisthideminor' );
131 $prefs['hidebots'] = $user->getBoolOption( 'watchlisthidebots' );
132 $prefs['hideanons'] = $user->getBoolOption( 'watchlisthideanons' );
133 $prefs['hideliu'] = $user->getBoolOption( 'watchlisthideliu' );
134 $prefs['hideown' ] = $user->getBoolOption( 'watchlisthideown' );
135 $prefs['hidepatrolled' ] = $user->getBoolOption( 'watchlisthidepatrolled' );
137 # Get query variables
139 $values['days'] = $request->getVal( 'days', $prefs['days'] );
140 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $prefs['hideminor'] );
141 $values['hideBots'] = (int)$request->getBool( 'hideBots' , $prefs['hidebots'] );
142 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $prefs['hideanons'] );
143 $values['hideLiu'] = (int)$request->getBool( 'hideLiu' , $prefs['hideliu'] );
144 $values['hideOwn'] = (int)$request->getBool( 'hideOwn' , $prefs['hideown'] );
145 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $prefs['hidepatrolled'] );
146 foreach( $this->customFilters
as $key => $params ) {
147 $values[$key] = (int)$request->getBool( $key );
150 # Get namespace value, if supplied, and prepare a WHERE fragment
151 $nameSpace = $request->getIntOrNull( 'namespace' );
152 $invert = $request->getIntOrNull( 'invert' );
153 if ( !is_null( $nameSpace ) ) {
154 $nameSpace = intval( $nameSpace ); // paranioa
156 $nameSpaceClause = "rc_namespace != $nameSpace";
158 $nameSpaceClause = "rc_namespace = $nameSpace";
162 $nameSpaceClause = '';
164 $values['namespace'] = $nameSpace;
165 $values['invert'] = $invert;
167 if( is_null( $values['days'] ) ||
!is_numeric( $values['days'] ) ) {
168 $big = 1000; /* The magical big */
169 if( $nitems > $big ) {
170 # Set default cutoff shorter
171 $values['days'] = $defaults['days'] = (12.0 / 24.0); # 12 hours...
173 $values['days'] = $defaults['days']; # default cutoff for shortlisters
176 $values['days'] = floatval( $values['days'] );
179 // Dump everything here
180 $nondefaults = array();
181 foreach ( $defaults as $name => $defValue ) {
182 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
185 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
187 # Possible where conditions
190 if( $values['days'] > 0 ) {
191 $conds[] = "rc_timestamp > '".$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...
204 if( $values['hideOwn'] ) {
205 $conds[] = 'rc_user != ' . $user->getId();
207 if( $values['hideBots'] ) {
208 $conds[] = 'rc_bot = 0';
210 if( $values['hideMinor'] ) {
211 $conds[] = 'rc_minor = 0';
213 if( $values['hideLiu'] ) {
214 $conds[] = 'rc_user = 0';
216 if( $values['hideAnons'] ) {
217 $conds[] = 'rc_user != 0';
219 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
220 $conds[] = 'rc_patrolled != 1';
222 if ( $nameSpaceClause ) {
223 $conds[] = $nameSpaceClause;
226 # Toggle watchlist content (all recent edits or just the latest)
227 if( $user->getOption( 'extendwatchlist' ) ) {
228 $limitWatchlist = intval( $user->getOption( 'wllimit' ) );
231 # Top log Ids for a page are not stored
232 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
237 # Show a message about slave lag, if applicable
238 $lag = wfGetLB()->safeGetLag( $dbr );
240 $output->showLagWarning( $lag );
244 $form = Xml
::fieldset( $this->msg( 'watchlist-options' )->text(), false, array( 'id' => 'mw-watchlist-options' ) );
246 # Show watchlist header
247 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse();
249 if( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
250 $form .= $this->msg( 'wlheader-enotif' )->parseAsBlock() . "\n";
252 if( $wgShowUpdatedMarker ) {
253 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
254 'action' => $this->getTitle()->getLocalUrl(),
255 'id' => 'mw-watchlist-resetbutton' ) ) .
256 $this->msg( 'wlheader-showupdated' )->parse() . ' ' .
257 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) .
258 Html
::hidden( 'reset', 'all' ) .
259 Xml
::closeElement( 'form' );
263 $tables = array( 'recentchanges', 'watchlist' );
264 $fields = array( $dbr->tableName( 'recentchanges' ) . '.*' );
266 'watchlist' => array('INNER JOIN',"wl_user='{$user->getId()}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
268 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
269 if( $wgShowUpdatedMarker ) {
270 $fields[] = 'wl_notificationtimestamp';
272 if( $limitWatchlist ) {
273 $options['LIMIT'] = $limitWatchlist;
276 $rollbacker = $user->isAllowed('rollback');
277 if ( $usePage ||
$rollbacker ) {
279 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
281 $fields[] = 'page_latest';
285 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
286 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
288 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
289 $numRows = $dbr->numRows( $res );
291 /* Start bottom header */
293 $lang = $this->getLanguage();
295 if( $values['days'] > 0 ) {
296 $timestamp = wfTimestampNow();
297 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
298 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . '<br />';
301 $cutofflinks = "\n" . $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
303 # Spit out some control panel links
305 'hideMinor' => 'rcshowhideminor',
306 'hideBots' => 'rcshowhidebots',
307 'hideAnons' => 'rcshowhideanons',
308 'hideLiu' => 'rcshowhideliu',
309 'hideOwn' => 'rcshowhidemine',
310 'hidePatrolled' => 'rcshowhidepatr'
312 foreach ( $this->customFilters
as $key => $params ) {
313 $filters[$key] = $params['msg'];
315 // Disable some if needed
316 if ( !$user->useNPPatrol() ) {
317 unset( $filters['hidePatrolled'] );
321 foreach( $filters as $name => $msg ) {
322 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
325 # Namespace filter and put the whole form together.
327 $form .= $cutofflinks;
328 $form .= $lang->pipeList( $links );
329 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
330 $form .= '<hr /><p>';
331 $form .= Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) . ' ';
332 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
333 $form .= Xml
::checkLabel( $this->msg( 'invert' )->text(), 'invert', 'nsinvert', $invert ) . ' ';
334 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . '</p>';
335 $form .= Html
::hidden( 'days', $values['days'] );
336 foreach ( $filters as $key => $msg ) {
337 if ( $values[$key] ) {
338 $form .= Html
::hidden( $key, 1 );
341 $form .= Xml
::closeElement( 'form' );
342 $form .= Xml
::closeElement( 'fieldset' );
343 $output->addHTML( $form );
345 # If there's nothing to show, stop here
346 if( $numRows == 0 ) {
347 $output->addWikiMsg( 'watchnochange' );
351 /* End bottom header */
353 /* Do link batch query */
354 $linkBatch = new LinkBatch
;
355 foreach ( $res as $row ) {
356 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
357 if ( $row->rc_user
!= 0 ) {
358 $linkBatch->add( NS_USER
, $userNameUnderscored );
360 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
362 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
364 $linkBatch->execute();
365 $dbr->dataSeek( $res, 0 );
367 $list = ChangesList
::newFromContext( $this->getContext() );
368 $list->setWatchlistDivs();
370 $s = $list->beginRecentChangesList();
372 foreach ( $res as $obj ) {
374 $rc = RecentChange
::newFromRow( $obj );
375 $rc->counter
= $counter++
;
377 if ( $wgShowUpdatedMarker ) {
378 $updated = $obj->wl_notificationtimestamp
;
383 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
384 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
387 'wl_namespace' => $obj->rc_namespace
,
388 'wl_title' => $obj->rc_title
,
392 $rc->numberofWatchingusers
= 0;
395 $s .= $list->recentChangesLine( $rc, $updated, $counter );
397 $s .= $list->endRecentChangesList();
399 $output->addHTML( $s );
402 protected function showHideLink( $options, $message, $name, $value ) {
403 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
404 $options[$name] = 1 - (int) $value;
406 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
409 protected function hoursLink( $h, $options = array() ) {
410 $options['days'] = ( $h / 24.0 );
412 return Linker
::linkKnown(
414 $this->getLanguage()->formatNum( $h ),
420 protected function daysLink( $d, $options = array() ) {
421 $options['days'] = $d;
422 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
424 return Linker
::linkKnown(
437 protected function cutoffLinks( $days, $options = array() ) {
438 $hours = array( 1, 2, 6, 12 );
439 $days = array( 1, 3, 7 );
441 foreach( $hours as $h ) {
442 $hours[$i++
] = $this->hoursLink( $h, $options );
445 foreach( $days as $d ) {
446 $days[$i++
] = $this->daysLink( $d, $options );
448 return $this->msg( 'wlshowlast' )->rawParams(
449 $this->getLanguage()->pipeList( $hours ),
450 $this->getLanguage()->pipeList( $days ),
451 $this->daysLink( 0, $options ) )->parse();
455 * Count the number of items on a user's watchlist
459 protected function countItems() {
460 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
462 # Fetch the raw count
463 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
464 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
465 $row = $dbr->fetchObject( $res );
466 $count = $row->count
;
468 return floor( $count / 2 );