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 wfMsgHtml( 'loginreqlink' ),
63 array( 'returnto' => $this->getTitle()->getPrefixedText() )
65 $output->addHTML( wfMessage( 'watchlistanontext' )->rawParams( $llink )->parse() );
70 $this->outputHeader();
72 $output->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->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 );
243 $lang = $this->getLanguage();
246 $form = Xml
::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
248 # Show watchlist header
249 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $lang->formatNum( $nitems ) );
251 if( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
252 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
254 if( $wgShowUpdatedMarker ) {
255 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
256 'action' => $this->getTitle()->getLocalUrl(),
257 'id' => 'mw-watchlist-resetbutton' ) ) .
258 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
259 Xml
::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
260 Html
::hidden( 'reset', 'all' ) .
261 Xml
::closeElement( 'form' );
265 $tables = array( 'recentchanges', 'watchlist' );
266 $fields = array( $dbr->tableName( 'recentchanges' ) . '.*' );
268 'watchlist' => array('INNER JOIN',"wl_user='{$user->getId()}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
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) );
290 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
291 $numRows = $dbr->numRows( $res );
293 /* Start bottom header */
296 if( $values['days'] > 0 ) {
297 $timestamp = wfTimestampNow();
298 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
299 $lang->formatNum( $numRows ),
300 $lang->formatNum( round( $values['days'] * 24 ) ),
301 $lang->date( $timestamp, true ),
302 $lang->time( $timestamp, true )
306 $cutofflinks = "\n" . $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
308 # Spit out some control panel links
310 'hideMinor' => 'rcshowhideminor',
311 'hideBots' => 'rcshowhidebots',
312 'hideAnons' => 'rcshowhideanons',
313 'hideLiu' => 'rcshowhideliu',
314 'hideOwn' => 'rcshowhidemine',
315 'hidePatrolled' => 'rcshowhidepatr'
317 foreach ( $this->customFilters
as $key => $params ) {
318 $filters[$key] = $params['msg'];
320 // Disable some if needed
321 if ( !$user->useNPPatrol() ) {
322 unset( $filters['hidePatrolled'] );
326 foreach( $filters as $name => $msg ) {
327 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
330 # Namespace filter and put the whole form together.
332 $form .= $cutofflinks;
333 $form .= $lang->pipeList( $links );
334 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
335 $form .= '<hr /><p>';
336 $form .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ';
337 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
338 $form .= Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . ' ';
339 $form .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
340 $form .= Html
::hidden( 'days', $values['days'] );
341 foreach ( $filters as $key => $msg ) {
342 if ( $values[$key] ) {
343 $form .= Html
::hidden( $key, 1 );
346 $form .= Xml
::closeElement( 'form' );
347 $form .= Xml
::closeElement( 'fieldset' );
348 $output->addHTML( $form );
350 # If there's nothing to show, stop here
351 if( $numRows == 0 ) {
352 $output->addWikiMsg( 'watchnochange' );
356 /* End bottom header */
358 /* Do link batch query */
359 $linkBatch = new LinkBatch
;
360 foreach ( $res as $row ) {
361 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
362 if ( $row->rc_user
!= 0 ) {
363 $linkBatch->add( NS_USER
, $userNameUnderscored );
365 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
367 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
369 $linkBatch->execute();
370 $dbr->dataSeek( $res, 0 );
372 $list = ChangesList
::newFromContext( $this->getContext() );
373 $list->setWatchlistDivs();
375 $s = $list->beginRecentChangesList();
377 foreach ( $res as $obj ) {
379 $rc = RecentChange
::newFromRow( $obj );
380 $rc->counter
= $counter++
;
382 if ( $wgShowUpdatedMarker ) {
383 $updated = $obj->wl_notificationtimestamp
;
388 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
389 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
392 'wl_namespace' => $obj->rc_namespace
,
393 'wl_title' => $obj->rc_title
,
397 $rc->numberofWatchingusers
= 0;
400 $s .= $list->recentChangesLine( $rc, $updated, $counter );
402 $s .= $list->endRecentChangesList();
404 $output->addHTML( $s );
407 protected function showHideLink( $options, $message, $name, $value ) {
408 $showLinktext = wfMsgHtml( 'show' );
409 $hideLinktext = wfMsgHtml( 'hide' );
411 $label = $value ?
$showLinktext : $hideLinktext;
412 $options[$name] = 1 - (int) $value;
414 return wfMsgHtml( $message, Linker
::linkKnown( $this->getTitle(), $label, array(), $options ) );
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 ) : wfMsgHtml( 'watchlistall2' ) );
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 wfMsgExt('wlshowlast',
457 array('parseinline', 'replaceafter'),
458 $this->getLanguage()->pipeList( $hours ),
459 $this->getLanguage()->pipeList( $days ),
460 $this->daysLink( 0, $options ) );
464 * Count the number of items on a user's watchlist
468 protected function countItems() {
469 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
471 # Fetch the raw count
472 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
473 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
474 $row = $dbr->fetchObject( $res );
475 $count = $row->count
;
477 return floor( $count / 2 );