4 * @ingroup SpecialPage Watchlist
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 * @param $par Parameter passed to the page
27 function wfSpecialWatchlist( $par ) {
28 global $wgUser, $wgOut, $wgLang, $wgRequest;
29 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
32 $wlToken = $wgUser->getOption( 'watchlisttoken' );
34 $wlToken = sha1( mt_rand() . microtime( true ) );
35 $wgUser->setOption( 'watchlisttoken', $wlToken );
36 $wgUser->saveSettings();
39 global $wgFeedClasses;
40 $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
41 'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
42 $feedTemplate = wfScript('api').'?';
44 foreach( $wgFeedClasses as $format => $class ) {
45 $theseParams = $apiParams +
array( 'feedformat' => $format );
46 $url = $feedTemplate . wfArrayToCGI( $theseParams );
47 $wgOut->addFeedLink( $format, $url );
50 $skin = $wgUser->getSkin();
51 $specialTitle = SpecialPage
::getTitleFor( 'Watchlist' );
52 $wgOut->setRobotPolicy( 'noindex,nofollow' );
54 # Anons don't get a watchlist
55 if( $wgUser->isAnon() ) {
56 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
57 $llink = $skin->linkKnown(
58 SpecialPage
::getTitleFor( 'Userlogin' ),
59 wfMsgHtml( 'loginreqlink' ),
61 array( 'returnto' => $specialTitle->getPrefixedText() )
63 $wgOut->addHTML( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
67 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
69 $sub = wfMsgExt( 'watchlistfor2', array( 'parseinline', 'replaceafter' ), $wgUser->getName(), WatchlistEditor
::buildTools( $wgUser->getSkin() ) );
70 $wgOut->setSubtitle( $sub );
72 if( ( $mode = WatchlistEditor
::getMode( $wgRequest, $par ) ) !== false ) {
73 $editor = new WatchlistEditor();
74 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
78 $uid = $wgUser->getId();
79 if( ($wgEnotifWatchlist ||
$wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) &&
80 $wgRequest->wasPosted() )
82 $wgUser->clearAllNotifications( $uid );
83 $wgOut->redirect( $specialTitle->getFullUrl() );
88 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
89 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
90 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
91 /* bool */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
92 /* bool */ 'hideLiu' => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
93 /* bool */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
94 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
95 /* ? */ 'namespace' => 'all',
96 /* ? */ '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( 'watchlisthideanon' );
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
178 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
181 # If the watchlist is relatively short, it's simplest to zip
182 # down its entirety and then sort the results.
184 # If it's relatively long, it may be worth our while to zip
185 # through the time-sorted page list checking for watched items.
187 # Up estimate of watched items by 15% to compensate for talk pages...
191 $conds[] = "rc_user != $uid";
194 $conds[] = 'rc_bot = 0';
197 $conds[] = 'rc_minor = 0';
200 $conds[] = 'rc_user = 0';
203 $conds[] = 'rc_user != 0';
205 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
206 $conds[] = 'rc_patrolled != 1';
208 if( $nameSpaceClause ) {
209 $conds[] = $nameSpaceClause;
212 # Toggle watchlist content (all recent edits or just the latest)
213 if( $wgUser->getOption( 'extendwatchlist' )) {
214 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
217 # Top log Ids for a page are not stored
218 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
223 # Show a message about slave lag, if applicable
224 if( ( $lag = $dbr->getLag() ) > 0 )
225 $wgOut->showLagWarning( $lag );
228 $form = Xml
::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
230 # Show watchlist header
231 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
233 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
234 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
236 if( $wgShowUpdatedMarker ) {
237 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
238 'action' => $specialTitle->getLocalUrl(),
239 'id' => 'mw-watchlist-resetbutton' ) ) .
240 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
241 Xml
::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
242 Xml
::hidden( 'reset', 'all' ) .
243 Xml
::closeElement( 'form' );
247 $tables = array( 'recentchanges', 'watchlist' );
248 $fields = array( "{$recentchanges}.*" );
250 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
252 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
253 if( $wgShowUpdatedMarker ) {
254 $fields[] = 'wl_notificationtimestamp';
256 if( $limitWatchlist ) {
257 $options['LIMIT'] = $limitWatchlist;
260 $rollbacker = $wgUser->isAllowed('rollback');
261 if ( $usePage ||
$rollbacker ) {
263 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
265 $fields[] = 'page_latest';
268 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
269 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
271 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
272 $numRows = $dbr->numRows( $res );
274 /* Start bottom header */
278 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
279 $wgLang->formatNum( $numRows ),
280 $wgLang->formatNum( $days ),
281 $wgLang->timeAndDate( wfTimestampNow(), true ),
282 $wgLang->date( wfTimestampNow(), true ),
283 $wgLang->time( wfTimestampNow(), true )
285 } elseif( $days > 0 ) {
286 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
287 $wgLang->formatNum( $numRows ),
288 $wgLang->formatNum( round($days*24) )
292 $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
294 $thisTitle = SpecialPage
::getTitleFor( 'Watchlist' );
296 # Spit out some control panel links
297 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
298 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
299 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
300 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
301 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
303 if( $wgUser->useRCPatrol() ) {
304 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
307 # Namespace filter and put the whole form together.
309 $form .= $cutofflinks;
310 $form .= $wgLang->pipeList( $links );
311 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
312 $form .= '<hr /><p>';
313 $form .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ';
314 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
315 $form .= Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . ' ';
316 $form .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
317 $form .= Xml
::hidden( 'days', $days );
319 $form .= Xml
::hidden( 'hideMinor', 1 );
321 $form .= Xml
::hidden( 'hideBots', 1 );
323 $form .= Xml
::hidden( 'hideAnons', 1 );
325 $form .= Xml
::hidden( 'hideLiu', 1 );
327 $form .= Xml
::hidden( 'hideOwn', 1 );
328 $form .= Xml
::closeElement( 'form' );
329 $form .= Xml
::closeElement( 'fieldset' );
330 $wgOut->addHTML( $form );
332 $wgOut->addHTML( ChangesList
::flagLegend() );
334 # If there's nothing to show, stop here
335 if( $numRows == 0 ) {
336 $wgOut->addWikiMsg( 'watchnochange' );
340 /* End bottom header */
342 /* Do link batch query */
343 $linkBatch = new LinkBatch
;
344 while ( $row = $dbr->fetchObject( $res ) ) {
345 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
346 if ( $row->rc_user
!= 0 ) {
347 $linkBatch->add( NS_USER
, $userNameUnderscored );
349 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
351 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
353 $linkBatch->execute();
354 $dbr->dataSeek( $res, 0 );
356 $list = ChangesList
::newFromUser( $wgUser );
357 $list->setWatchlistDivs();
359 $s = $list->beginRecentChangesList();
361 while ( $obj = $dbr->fetchObject( $res ) ) {
363 $rc = RecentChange
::newFromRow( $obj );
364 $rc->counter
= $counter++
;
366 if ( $wgShowUpdatedMarker ) {
367 $updated = $obj->wl_notificationtimestamp
;
372 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
373 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
376 'wl_namespace' => $obj->rc_namespace
,
377 'wl_title' => $obj->rc_title
,
381 $rc->numberofWatchingusers
= 0;
384 $s .= $list->recentChangesLine( $rc, $updated, $counter );
386 $s .= $list->endRecentChangesList();
388 $wgOut->addHTML( $s );
391 function wlShowHideLink( $options, $message, $name, $value ) {
394 $showLinktext = wfMsgHtml( 'show' );
395 $hideLinktext = wfMsgHtml( 'hide' );
396 $title = SpecialPage
::getTitleFor( 'Watchlist' );
397 $skin = $wgUser->getSkin();
399 $label = $value ?
$showLinktext : $hideLinktext;
400 $options[$name] = 1 - (int) $value;
402 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
406 function wlHoursLink( $h, $page, $options = array() ) {
407 global $wgUser, $wgLang, $wgContLang;
409 $sk = $wgUser->getSkin();
410 $title = Title
::newFromText( $wgContLang->specialPage( $page ) );
411 $options['days'] = ($h / 24.0);
415 $wgLang->formatNum( $h ),
423 function wlDaysLink( $d, $page, $options = array() ) {
424 global $wgUser, $wgLang, $wgContLang;
426 $sk = $wgUser->getSkin();
427 $title = Title
::newFromText( $wgContLang->specialPage( $page ) );
428 $options['days'] = $d;
429 $message = ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
444 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
447 $hours = array( 1, 2, 6, 12 );
448 $days = array( 1, 3, 7 );
450 foreach( $hours as $h ) {
451 $hours[$i++
] = wlHoursLink( $h, $page, $options );
454 foreach( $days as $d ) {
455 $days[$i++
] = wlDaysLink( $d, $page, $options );
457 return wfMsgExt('wlshowlast',
458 array('parseinline', 'replaceafter'),
459 $wgLang->pipeList( $hours ),
460 $wgLang->pipeList( $days ),
461 wlDaysLink( 0, $page, $options ) );
465 * Count the number of items on a user's watchlist
467 * @param $user User object
468 * @param $talk Boolean: include talk pages
471 function wlCountItems( &$user, $talk = true ) {
472 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
474 # Fetch the raw count
475 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
476 array( 'wl_user' => $user->mId
), 'wlCountItems' );
477 $row = $dbr->fetchObject( $res );
478 $count = $row->count
;
480 # Halve to remove talk pages if needed
482 $count = floor( $count / 2 );