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
27 * @param $par Parameter passed to the page
29 function wfSpecialWatchlist( $par ) {
30 global $wgUser, $wgOut, $wgLang, $wgRequest;
31 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
34 $wlToken = $wgUser->getOption( 'watchlisttoken' );
36 $wlToken = sha1( mt_rand() . microtime( true ) );
37 $wgUser->setOption( 'watchlisttoken', $wlToken );
38 $wgUser->saveSettings();
41 global $wgFeedClasses;
42 $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
43 'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
44 $feedTemplate = wfScript('api').'?';
46 foreach( $wgFeedClasses as $format => $class ) {
47 $theseParams = $apiParams +
array( 'feedformat' => $format );
48 $url = $feedTemplate . wfArrayToCGI( $theseParams );
49 $wgOut->addFeedLink( $format, $url );
52 $skin = $wgUser->getSkin();
53 $specialTitle = SpecialPage
::getTitleFor( 'Watchlist' );
54 $wgOut->setRobotPolicy( 'noindex,nofollow' );
56 # Anons don't get a watchlist
57 if( $wgUser->isAnon() ) {
58 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
59 $llink = $skin->linkKnown(
60 SpecialPage
::getTitleFor( 'Userlogin' ),
61 wfMsgHtml( 'loginreqlink' ),
63 array( 'returnto' => $specialTitle->getPrefixedText() )
65 $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
69 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
71 $sub = wfMsgExt( 'watchlistfor2', array( 'parseinline', 'replaceafter' ), $wgUser->getName(), WatchlistEditor
::buildTools( $wgUser->getSkin() ) );
72 $wgOut->setSubtitle( $sub );
74 if( ( $mode = WatchlistEditor
::getMode( $wgRequest, $par ) ) !== false ) {
75 $editor = new WatchlistEditor();
76 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
80 $uid = $wgUser->getId();
81 if( ($wgEnotifWatchlist ||
$wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) &&
82 $wgRequest->wasPosted() )
84 $wgUser->clearAllNotifications( $uid );
85 $wgOut->redirect( $specialTitle->getFullUrl() );
90 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
91 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
92 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
93 /* bool */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
94 /* bool */ 'hideLiu' => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
95 /* bool */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
96 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
97 /* ? */ 'namespace' => 'all',
98 /* ? */ 'invert' => false,
101 # Extract variables from the request, falling back to user preferences or
102 # other default values if these don't exist
103 $prefs['days'] = floatval( $wgUser->getOption( 'watchlistdays' ) );
104 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
105 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
106 $prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanons' );
107 $prefs['hideliu'] = $wgUser->getBoolOption( 'watchlisthideliu' );
108 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
109 $prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );
111 # Get query variables
112 $days = $wgRequest->getVal( 'days' , $prefs['days'] );
113 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
114 $hideBots = $wgRequest->getBool( 'hideBots' , $prefs['hidebots'] );
115 $hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
116 $hideLiu = $wgRequest->getBool( 'hideLiu' , $prefs['hideliu'] );
117 $hideOwn = $wgRequest->getBool( 'hideOwn' , $prefs['hideown'] );
118 $hidePatrolled = $wgRequest->getBool( 'hidePatrolled' , $prefs['hidepatrolled'] );
120 # Get namespace value, if supplied, and prepare a WHERE fragment
121 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
122 $invert = $wgRequest->getIntOrNull( 'invert' );
123 if( !is_null( $nameSpace ) ) {
124 $nameSpace = intval( $nameSpace );
125 if( $invert && $nameSpace !== 'all' )
126 $nameSpaceClause = "rc_namespace != $nameSpace";
128 $nameSpaceClause = "rc_namespace = $nameSpace";
131 $nameSpaceClause = '';
134 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
135 $recentchanges = $dbr->tableName( 'recentchanges' );
137 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
138 array( 'wl_user' => $uid ), __METHOD__
);
139 // Adjust for page X, talk:page X, which are both stored separately,
140 // but treated together
141 $nitems = floor($watchlistCount / 2);
143 if( is_null($days) ||
!is_numeric($days) ) {
144 $big = 1000; /* The magical big */
146 # Set default cutoff shorter
147 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
149 $days = $defaults['days']; # default cutoff for shortlisters
152 $days = floatval($days);
155 // Dump everything here
156 $nondefaults = array();
158 wfAppendToArrayIfNotDefault( 'days' , $days , $defaults, $nondefaults);
159 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
160 wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
161 wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
162 wfAppendToArrayIfNotDefault( 'hideLiu' , (int)$hideLiu , $defaults, $nondefaults );
163 wfAppendToArrayIfNotDefault( 'hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
164 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace , $defaults, $nondefaults);
165 wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
168 $wgOut->addWikiMsg( 'nowatchlist' );
172 # Possible where conditions
176 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
179 # If the watchlist is relatively short, it's simplest to zip
180 # down its entirety and then sort the results.
182 # If it's relatively long, it may be worth our while to zip
183 # through the time-sorted page list checking for watched items.
185 # Up estimate of watched items by 15% to compensate for talk pages...
189 $conds[] = "rc_user != $uid";
192 $conds[] = 'rc_bot = 0';
195 $conds[] = 'rc_minor = 0';
198 $conds[] = 'rc_user = 0';
201 $conds[] = 'rc_user != 0';
203 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
204 $conds[] = 'rc_patrolled != 1';
206 if( $nameSpaceClause ) {
207 $conds[] = $nameSpaceClause;
210 # Toggle watchlist content (all recent edits or just the latest)
211 if( $wgUser->getOption( 'extendwatchlist' )) {
212 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
215 # Top log Ids for a page are not stored
216 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
221 # Show a message about slave lag, if applicable
222 if( ( $lag = $dbr->getLag() ) > 0 )
223 $wgOut->showLagWarning( $lag );
226 $form = Xml
::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
228 # Show watchlist header
229 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
231 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
232 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
234 if( $wgShowUpdatedMarker ) {
235 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
236 'action' => $specialTitle->getLocalUrl(),
237 'id' => 'mw-watchlist-resetbutton' ) ) .
238 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
239 Xml
::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
240 Html
::hidden( 'reset', 'all' ) .
241 Xml
::closeElement( 'form' );
245 $tables = array( 'recentchanges', 'watchlist' );
246 $fields = array( "{$recentchanges}.*" );
248 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
250 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
251 if( $wgShowUpdatedMarker ) {
252 $fields[] = 'wl_notificationtimestamp';
254 if( $limitWatchlist ) {
255 $options['LIMIT'] = $limitWatchlist;
258 $rollbacker = $wgUser->isAllowed('rollback');
259 if ( $usePage ||
$rollbacker ) {
261 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
263 $fields[] = 'page_latest';
266 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
267 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
269 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
270 $numRows = $dbr->numRows( $res );
272 /* Start bottom header */
276 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
277 $wgLang->formatNum( $numRows ),
278 $wgLang->formatNum( $days ),
279 $wgLang->timeAndDate( wfTimestampNow(), true ),
280 $wgLang->date( wfTimestampNow(), true ),
281 $wgLang->time( wfTimestampNow(), true )
283 } elseif( $days > 0 ) {
284 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
285 $wgLang->formatNum( $numRows ),
286 $wgLang->formatNum( round($days*24) )
290 $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
292 $thisTitle = SpecialPage
::getTitleFor( 'Watchlist' );
294 # Spit out some control panel links
295 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
296 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
297 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
298 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
299 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
301 if( $wgUser->useRCPatrol() ) {
302 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
305 # Namespace filter and put the whole form together.
307 $form .= $cutofflinks;
308 $form .= $wgLang->pipeList( $links );
309 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
310 $form .= '<hr /><p>';
311 $form .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ';
312 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
313 $form .= Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . ' ';
314 $form .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
315 $form .= Html
::hidden( 'days', $days );
317 $form .= Html
::hidden( 'hideMinor', 1 );
319 $form .= Html
::hidden( 'hideBots', 1 );
321 $form .= Html
::hidden( 'hideAnons', 1 );
323 $form .= Html
::hidden( 'hideLiu', 1 );
325 $form .= Html
::hidden( 'hideOwn', 1 );
326 $form .= Xml
::closeElement( 'form' );
327 $form .= Xml
::closeElement( 'fieldset' );
328 $wgOut->addHTML( $form );
330 # If there's nothing to show, stop here
331 if( $numRows == 0 ) {
332 $wgOut->addWikiMsg( 'watchnochange' );
336 /* End bottom header */
338 /* Do link batch query */
339 $linkBatch = new LinkBatch
;
340 foreach ( $res as $row ) {
341 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
342 if ( $row->rc_user
!= 0 ) {
343 $linkBatch->add( NS_USER
, $userNameUnderscored );
345 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
347 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
349 $linkBatch->execute();
350 $dbr->dataSeek( $res, 0 );
352 $list = ChangesList
::newFromUser( $wgUser );
353 $list->setWatchlistDivs();
355 $s = $list->beginRecentChangesList();
357 foreach ( $res as $obj ) {
359 $rc = RecentChange
::newFromRow( $obj );
360 $rc->counter
= $counter++
;
362 if ( $wgShowUpdatedMarker ) {
363 $updated = $obj->wl_notificationtimestamp
;
368 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
369 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
372 'wl_namespace' => $obj->rc_namespace
,
373 'wl_title' => $obj->rc_title
,
377 $rc->numberofWatchingusers
= 0;
380 $s .= $list->recentChangesLine( $rc, $updated, $counter );
382 $s .= $list->endRecentChangesList();
384 $wgOut->addHTML( $s );
387 function wlShowHideLink( $options, $message, $name, $value ) {
390 $showLinktext = wfMsgHtml( 'show' );
391 $hideLinktext = wfMsgHtml( 'hide' );
392 $title = SpecialPage
::getTitleFor( 'Watchlist' );
393 $skin = $wgUser->getSkin();
395 $label = $value ?
$showLinktext : $hideLinktext;
396 $options[$name] = 1 - (int) $value;
398 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
402 function wlHoursLink( $h, $page, $options = array() ) {
403 global $wgUser, $wgLang, $wgContLang;
405 $sk = $wgUser->getSkin();
406 $title = Title
::newFromText( $wgContLang->specialPage( $page ) );
407 $options['days'] = ($h / 24.0);
411 $wgLang->formatNum( $h ),
419 function wlDaysLink( $d, $page, $options = array() ) {
420 global $wgUser, $wgLang, $wgContLang;
422 $sk = $wgUser->getSkin();
423 $title = Title
::newFromText( $wgContLang->specialPage( $page ) );
424 $options['days'] = $d;
425 $message = ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
440 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
443 $hours = array( 1, 2, 6, 12 );
444 $days = array( 1, 3, 7 );
446 foreach( $hours as $h ) {
447 $hours[$i++
] = wlHoursLink( $h, $page, $options );
450 foreach( $days as $d ) {
451 $days[$i++
] = wlDaysLink( $d, $page, $options );
453 return wfMsgExt('wlshowlast',
454 array('parseinline', 'replaceafter'),
455 $wgLang->pipeList( $hours ),
456 $wgLang->pipeList( $days ),
457 wlDaysLink( 0, $page, $options ) );
461 * Count the number of items on a user's watchlist
463 * @param $user User object
464 * @param $talk Boolean: include talk pages
467 function wlCountItems( &$user, $talk = true ) {
468 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
470 # Fetch the raw count
471 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
472 array( 'wl_user' => $user->mId
), 'wlCountItems' );
473 $row = $dbr->fetchObject( $res );
474 $count = $row->count
;
476 # Halve to remove talk pages if needed
478 $count = floor( $count / 2 );