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
{
28 public function __construct(){
29 parent
::__construct( 'Watchlist' );
34 * @param $par Parameter passed to the page
36 function execute( $par ) {
37 global $wgUser, $wgOut, $wgLang, $wgRequest;
38 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
41 $wlToken = $wgUser->getOption( 'watchlisttoken' );
43 $wlToken = sha1( mt_rand() . microtime( true ) );
44 $wgUser->setOption( 'watchlisttoken', $wlToken );
45 $wgUser->saveSettings();
48 global $wgFeedClasses;
49 $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
50 'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
51 $feedTemplate = wfScript('api').'?';
53 foreach( $wgFeedClasses as $format => $class ) {
54 $theseParams = $apiParams +
array( 'feedformat' => $format );
55 $url = $feedTemplate . wfArrayToCGI( $theseParams );
56 $wgOut->addFeedLink( $format, $url );
59 $skin = $wgUser->getSkin();
60 $wgOut->setRobotPolicy( 'noindex,nofollow' );
62 # Anons don't get a watchlist
63 if( $wgUser->isAnon() ) {
64 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
65 $llink = $skin->linkKnown(
66 SpecialPage
::getTitleFor( 'Userlogin' ),
67 wfMsgHtml( 'loginreqlink' ),
69 array( 'returnto' => $this->getTitle()->getPrefixedText() )
71 $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
75 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
79 array( 'parseinline', 'replaceafter' ),
81 SpecialEditWatchlist
::buildTools( $wgUser->getSkin() )
83 $wgOut->setSubtitle( $sub );
85 if( ( $mode = SpecialEditWatchlist
::getMode( $wgRequest, $par ) ) !== false ) {
88 case SpecialEditWatchlist
::EDIT_CLEAR
:
91 case SpecialEditWatchlist
::EDIT_RAW
:
97 $title = SpecialPage
::getTitleFor( 'EditWatchlist', $mode );
98 $wgOut->redirect( $title->getLocalUrl() );
102 $uid = $wgUser->getId();
103 if( ($wgEnotifWatchlist ||
$wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) &&
104 $wgRequest->wasPosted() )
106 $wgUser->clearAllNotifications( $uid );
107 $wgOut->redirect( $this->getTitle()->getFullUrl() );
112 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
113 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
114 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
115 /* bool */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
116 /* bool */ 'hideLiu' => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
117 /* bool */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
118 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
119 /* ? */ 'namespace' => 'all',
120 /* ? */ 'invert' => false,
123 # Extract variables from the request, falling back to user preferences or
124 # other default values if these don't exist
125 $prefs['days'] = floatval( $wgUser->getOption( 'watchlistdays' ) );
126 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
127 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
128 $prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanons' );
129 $prefs['hideliu'] = $wgUser->getBoolOption( 'watchlisthideliu' );
130 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
131 $prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );
133 # Get query variables
134 $days = $wgRequest->getVal( 'days' , $prefs['days'] );
135 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
136 $hideBots = $wgRequest->getBool( 'hideBots' , $prefs['hidebots'] );
137 $hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
138 $hideLiu = $wgRequest->getBool( 'hideLiu' , $prefs['hideliu'] );
139 $hideOwn = $wgRequest->getBool( 'hideOwn' , $prefs['hideown'] );
140 $hidePatrolled = $wgRequest->getBool( 'hidePatrolled' , $prefs['hidepatrolled'] );
142 # Get namespace value, if supplied, and prepare a WHERE fragment
143 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
144 $invert = $wgRequest->getIntOrNull( 'invert' );
145 if( !is_null( $nameSpace ) ) {
146 $nameSpace = intval( $nameSpace );
147 if( $invert && $nameSpace !== 'all' )
148 $nameSpaceClause = "rc_namespace != $nameSpace";
150 $nameSpaceClause = "rc_namespace = $nameSpace";
153 $nameSpaceClause = '';
156 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
157 $recentchanges = $dbr->tableName( 'recentchanges' );
159 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
160 array( 'wl_user' => $uid ), __METHOD__
);
161 // Adjust for page X, talk:page X, which are both stored separately,
162 // but treated together
163 $nitems = floor($watchlistCount / 2);
165 if( is_null($days) ||
!is_numeric($days) ) {
166 $big = 1000; /* The magical big */
168 # Set default cutoff shorter
169 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
171 $days = $defaults['days']; # default cutoff for shortlisters
174 $days = floatval($days);
177 // Dump everything here
178 $nondefaults = array();
180 wfAppendToArrayIfNotDefault( 'days' , $days , $defaults, $nondefaults);
181 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
182 wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
183 wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
184 wfAppendToArrayIfNotDefault( 'hideLiu' , (int)$hideLiu , $defaults, $nondefaults );
185 wfAppendToArrayIfNotDefault( 'hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
186 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace , $defaults, $nondefaults);
187 wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
190 $wgOut->addWikiMsg( 'nowatchlist' );
194 # Possible where conditions
198 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
201 # If the watchlist is relatively short, it's simplest to zip
202 # down its entirety and then sort the results.
204 # If it's relatively long, it may be worth our while to zip
205 # through the time-sorted page list checking for watched items.
207 # Up estimate of watched items by 15% to compensate for talk pages...
211 $conds[] = "rc_user != $uid";
214 $conds[] = 'rc_bot = 0';
217 $conds[] = 'rc_minor = 0';
220 $conds[] = 'rc_user = 0';
223 $conds[] = 'rc_user != 0';
225 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
226 $conds[] = 'rc_patrolled != 1';
228 if( $nameSpaceClause ) {
229 $conds[] = $nameSpaceClause;
232 # Toggle watchlist content (all recent edits or just the latest)
233 if( $wgUser->getOption( 'extendwatchlist' )) {
234 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
237 # Top log Ids for a page are not stored
238 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
243 # Show a message about slave lag, if applicable
244 if( ( $lag = $dbr->getLag() ) > 0 )
245 $wgOut->showLagWarning( $lag );
248 $form = Xml
::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
250 # Show watchlist header
251 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
253 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
254 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
256 if( $wgShowUpdatedMarker ) {
257 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
258 'action' => $this->getTitle()->getLocalUrl(),
259 'id' => 'mw-watchlist-resetbutton' ) ) .
260 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
261 Xml
::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
262 Html
::hidden( 'reset', 'all' ) .
263 Xml
::closeElement( 'form' );
267 $tables = array( 'recentchanges', 'watchlist' );
268 $fields = array( "{$recentchanges}.*" );
270 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
272 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
273 if( $wgShowUpdatedMarker ) {
274 $fields[] = 'wl_notificationtimestamp';
276 if( $limitWatchlist ) {
277 $options['LIMIT'] = $limitWatchlist;
280 $rollbacker = $wgUser->isAllowed('rollback');
281 if ( $usePage ||
$rollbacker ) {
283 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
285 $fields[] = 'page_latest';
288 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
289 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
291 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
292 $numRows = $dbr->numRows( $res );
294 /* Start bottom header */
298 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
299 $wgLang->formatNum( $numRows ),
300 $wgLang->formatNum( $days ),
301 $wgLang->timeAndDate( wfTimestampNow(), true ),
302 $wgLang->date( wfTimestampNow(), true ),
303 $wgLang->time( wfTimestampNow(), true )
305 } elseif( $days > 0 ) {
306 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
307 $wgLang->formatNum( $numRows ),
308 $wgLang->formatNum( round($days*24) )
312 $cutofflinks = "\n" . self
::cutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
314 $thisTitle = SpecialPage
::getTitleFor( 'Watchlist' );
316 # Spit out some control panel links
317 $links[] = self
::showHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
318 $links[] = self
::showHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
319 $links[] = self
::showHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
320 $links[] = self
::showHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
321 $links[] = self
::showHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
323 if( $wgUser->useRCPatrol() ) {
324 $links[] = self
::showHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
327 # Namespace filter and put the whole form together.
329 $form .= $cutofflinks;
330 $form .= $wgLang->pipeList( $links );
331 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
332 $form .= '<hr /><p>';
333 $form .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ';
334 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
335 $form .= Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . ' ';
336 $form .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
337 $form .= Html
::hidden( 'days', $days );
339 $form .= Html
::hidden( 'hideMinor', 1 );
341 $form .= Html
::hidden( 'hideBots', 1 );
343 $form .= Html
::hidden( 'hideAnons', 1 );
345 $form .= Html
::hidden( 'hideLiu', 1 );
347 $form .= Html
::hidden( 'hideOwn', 1 );
348 $form .= Xml
::closeElement( 'form' );
349 $form .= Xml
::closeElement( 'fieldset' );
350 $wgOut->addHTML( $form );
352 # If there's nothing to show, stop here
353 if( $numRows == 0 ) {
354 $wgOut->addWikiMsg( 'watchnochange' );
358 /* End bottom header */
360 /* Do link batch query */
361 $linkBatch = new LinkBatch
;
362 foreach ( $res as $row ) {
363 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
364 if ( $row->rc_user
!= 0 ) {
365 $linkBatch->add( NS_USER
, $userNameUnderscored );
367 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
369 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
371 $linkBatch->execute();
372 $dbr->dataSeek( $res, 0 );
374 $list = ChangesList
::newFromUser( $wgUser );
375 $list->setWatchlistDivs();
377 $s = $list->beginRecentChangesList();
379 foreach ( $res as $obj ) {
381 $rc = RecentChange
::newFromRow( $obj );
382 $rc->counter
= $counter++
;
384 if ( $wgShowUpdatedMarker ) {
385 $updated = $obj->wl_notificationtimestamp
;
390 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
391 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
394 'wl_namespace' => $obj->rc_namespace
,
395 'wl_title' => $obj->rc_title
,
399 $rc->numberofWatchingusers
= 0;
402 $s .= $list->recentChangesLine( $rc, $updated, $counter );
404 $s .= $list->endRecentChangesList();
406 $wgOut->addHTML( $s );
409 public static function showHideLink( $options, $message, $name, $value ) {
412 $showLinktext = wfMsgHtml( 'show' );
413 $hideLinktext = wfMsgHtml( 'hide' );
414 $title = SpecialPage
::getTitleFor( 'Watchlist' );
415 $skin = $wgUser->getSkin();
417 $label = $value ?
$showLinktext : $hideLinktext;
418 $options[$name] = 1 - (int) $value;
420 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
424 public static function hoursLink( $h, $page, $options = array() ) {
425 global $wgUser, $wgLang, $wgContLang;
427 $sk = $wgUser->getSkin();
428 $title = Title
::newFromText( $wgContLang->specialPage( $page ) );
429 $options['days'] = ($h / 24.0);
433 $wgLang->formatNum( $h ),
441 public static function daysLink( $d, $page, $options = array() ) {
442 global $wgUser, $wgLang, $wgContLang;
444 $sk = $wgUser->getSkin();
445 $title = Title
::newFromText( $wgContLang->specialPage( $page ) );
446 $options['days'] = $d;
447 $message = ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
462 protected static function cutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
465 $hours = array( 1, 2, 6, 12 );
466 $days = array( 1, 3, 7 );
468 foreach( $hours as $h ) {
469 $hours[$i++
] = self
::hoursLink( $h, $page, $options );
472 foreach( $days as $d ) {
473 $days[$i++
] = self
::daysLink( $d, $page, $options );
475 return wfMsgExt('wlshowlast',
476 array('parseinline', 'replaceafter'),
477 $wgLang->pipeList( $hours ),
478 $wgLang->pipeList( $days ),
479 self
::daysLink( 0, $page, $options ) );
483 * Count the number of items on a user's watchlist
485 * @param $user User object
486 * @param $talk Boolean: include talk pages
489 protected static function countItems( &$user, $talk = true ) {
490 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
492 # Fetch the raw count
493 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
494 array( 'wl_user' => $user->mId
), __METHOD__
);
495 $row = $dbr->fetchObject( $res );
496 $count = $row->count
;
498 # Halve to remove talk pages if needed
500 $count = floor( $count / 2 );