4 * @ingroup SpecialPage Watchlist
10 * @param $par Parameter passed to the page
12 function wfSpecialWatchlist( $par ) {
13 global $wgUser, $wgOut, $wgLang, $wgRequest;
14 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
15 global $wgEnotifWatchlist;
16 $fname = 'wfSpecialWatchlist';
18 $skin = $wgUser->getSkin();
19 $specialTitle = SpecialPage
::getTitleFor( 'Watchlist' );
20 $wgOut->setRobotPolicy( 'noindex,nofollow' );
22 # Anons don't get a watchlist
23 if( $wgUser->isAnon() ) {
24 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
25 $llink = $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Userlogin' ), wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
26 $wgOut->addHtml( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
30 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
32 $sub = wfMsgExt( 'watchlistfor', 'parseinline', $wgUser->getName() );
33 $sub .= '<br />' . WatchlistEditor
::buildTools( $wgUser->getSkin() );
34 $wgOut->setSubtitle( $sub );
36 if( ( $mode = WatchlistEditor
::getMode( $wgRequest, $par ) ) !== false ) {
37 $editor = new WatchlistEditor();
38 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
42 $uid = $wgUser->getId();
43 if( ($wgEnotifWatchlist ||
$wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
44 $wgUser->clearAllNotifications( $uid );
45 $wgOut->redirect( $specialTitle->getFullUrl() );
50 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
51 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
52 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
53 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
54 /* ? */ 'namespace' => 'all',
59 # Extract variables from the request, falling back to user preferences or
60 # other default values if these don't exist
61 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
62 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
63 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
64 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
67 $days = $wgRequest->getVal( 'days', $prefs['days'] );
68 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
69 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
70 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
72 # Get namespace value, if supplied, and prepare a WHERE fragment
73 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
74 if( !is_null( $nameSpace ) ) {
75 $nameSpace = intval( $nameSpace );
76 $nameSpaceClause = " AND rc_namespace = $nameSpace";
79 $nameSpaceClause = '';
82 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
83 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
85 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
86 array( 'wl_user' => $uid ), __METHOD__
);
87 // Adjust for page X, talk:page X, which are both stored separately,
88 // but treated together
89 $nitems = floor($watchlistCount / 2);
91 if( is_null($days) ||
!is_numeric($days) ) {
92 $big = 1000; /* The magical big */
94 # Set default cutoff shorter
95 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
97 $days = $defaults['days']; # default cutoff for shortlisters
100 $days = floatval($days);
103 // Dump everything here
104 $nondefaults = array();
106 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
107 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
108 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
109 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
110 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
113 if( ! wfRunHooks('BeforeWatchlist', array($nondefaults, $wgUser, &$hookSql)) ) {
118 $wgOut->addWikiMsg( 'nowatchlist' );
125 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
127 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
128 $res = $dbr->query( $sql, $fname );
129 $s = $dbr->fetchObject( $res );
134 # If the watchlist is relatively short, it's simplest to zip
135 # down its entirety and then sort the results.
137 # If it's relatively long, it may be worth our while to zip
138 # through the time-sorted page list checking for watched items.
140 # Up estimate of watched items by 15% to compensate for talk pages...
143 $andHideOwn = $hideOwn ?
"AND (rc_user <> $uid)" : '';
144 $andHideBots = $hideBots ?
"AND (rc_bot = 0)" : '';
145 $andHideMinor = $hideMinor ?
'AND rc_minor = 0' : '';
147 # Show watchlist header
149 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
150 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
152 if ( $wgShowUpdatedMarker ) {
153 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
156 # Toggle watchlist content (all recent edits or just the latest)
157 if( $wgUser->getOption( 'extendwatchlist' )) {
159 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
161 # Top log Ids for a page are not stored
162 $andLatest = 'AND (rc_this_oldid=page_latest OR rc_type=' . RC_LOG
. ') ';
163 $limitWatchlist = '';
166 $header .= wfMsgExt( 'watchlist-details', array( 'parsemag' ), $wgLang->formatNum( $nitems ) );
167 $wgOut->addWikiText( $header );
169 # Show a message about slave lag, if applicable
170 if( ( $lag = $dbr->getLag() ) > 0 )
171 $wgOut->showLagWarning( $lag );
173 if ( $wgShowUpdatedMarker ) {
174 $wgOut->addHTML( '<form action="' .
175 $specialTitle->escapeLocalUrl() .
176 '" method="post"><input type="submit" name="dummy" value="' .
177 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
178 '" /><input type="hidden" name="reset" value="all" /></form>' .
181 if ( $wgShowUpdatedMarker ) {
182 $wltsfield = ", ${watchlist}.wl_notificationtimestamp ";
186 $sql = "SELECT ${recentchanges}.* ${wltsfield}
187 FROM $watchlist,$recentchanges
188 LEFT JOIN $page ON rc_cur_id=page_id
190 AND wl_namespace=rc_namespace
191 AND wl_title=rc_title
199 ORDER BY rc_timestamp DESC
202 $res = $dbr->query( $sql, $fname );
203 $numRows = $dbr->numRows( $res );
205 /* Start bottom header */
206 $wgOut->addHTML( "<hr />\n" );
209 $wgOut->addWikiText( wfMsgExt( 'rcnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
210 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
211 } elseif($days > 0) {
212 $wgOut->addWikiText( wfMsgExt( 'wlnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
213 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
216 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
218 # Spit out some control panel links
219 $thisTitle = SpecialPage
::getTitleFor( 'Watchlist' );
220 $skin = $wgUser->getSkin();
222 # Hide/show bot edits
223 $label = $hideBots ?
wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
224 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
225 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
227 # Hide/show own edits
228 $label = $hideOwn ?
wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
229 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
230 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
232 # Hide/show minor edits
233 $label = $hideMinor ?
wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
234 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
235 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
237 $wgOut->addHTML( implode( ' | ', $links ) );
239 # Form for namespace filtering
240 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
242 $form .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ';
243 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
244 $form .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
245 $form .= Xml
::hidden( 'days', $days );
247 $form .= Xml
::hidden( 'hideOwn', 1 );
249 $form .= Xml
::hidden( 'hideBots', 1 );
251 $form .= Xml
::hidden( 'hideMinor', 1 );
252 $form .= Xml
::closeElement( 'form' );
253 $wgOut->addHtml( $form );
255 # If there's nothing to show, stop here
256 if( $numRows == 0 ) {
257 $wgOut->addWikiMsg( 'watchnochange' );
261 /* End bottom header */
263 /* Do link batch query */
264 $linkBatch = new LinkBatch
;
265 while ( $row = $dbr->fetchObject( $res ) ) {
266 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
267 if ( $row->rc_user
!= 0 ) {
268 $linkBatch->add( NS_USER
, $userNameUnderscored );
270 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
272 $linkBatch->execute();
273 $dbr->dataSeek( $res, 0 );
275 $list = ChangesList
::newFromUser( $wgUser );
277 $s = $list->beginRecentChangesList();
279 while ( $obj = $dbr->fetchObject( $res ) ) {
281 $rc = RecentChange
::newFromRow( $obj );
282 $rc->counter
= $counter++
;
284 if ( $wgShowUpdatedMarker ) {
285 $updated = $obj->wl_notificationtimestamp
;
290 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
291 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
294 'wl_namespace' => $obj->rc_namespace
,
295 'wl_title' => $obj->rc_title
,
299 $rc->numberofWatchingusers
= 0;
302 $s .= $list->recentChangesLine( $rc, $updated );
304 $s .= $list->endRecentChangesList();
306 $dbr->freeResult( $res );
307 $wgOut->addHTML( $s );
311 function wlHoursLink( $h, $page, $options = array() ) {
312 global $wgUser, $wgLang, $wgContLang;
313 $sk = $wgUser->getSkin();
314 $s = $sk->makeKnownLink(
315 $wgContLang->specialPage( $page ),
316 $wgLang->formatNum( $h ),
317 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
321 function wlDaysLink( $d, $page, $options = array() ) {
322 global $wgUser, $wgLang, $wgContLang;
323 $sk = $wgUser->getSkin();
324 $s = $sk->makeKnownLink(
325 $wgContLang->specialPage( $page ),
326 ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
327 wfArrayToCGI( array('days' => $d), $options ) );
334 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
335 $hours = array( 1, 2, 6, 12 );
336 $days = array( 1, 3, 7 );
338 foreach( $hours as $h ) {
339 $hours[$i++
] = wlHoursLink( $h, $page, $options );
342 foreach( $days as $d ) {
343 $days[$i++
] = wlDaysLink( $d, $page, $options );
345 return wfMsgExt('wlshowlast',
346 array('parseinline', 'replaceafter'),
347 implode(' | ', $hours),
348 implode(' | ', $days),
349 wlDaysLink( 0, $page, $options ) );
353 * Count the number of items on a user's watchlist
355 * @param $talk Include talk pages
358 function wlCountItems( &$user, $talk = true ) {
359 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
361 # Fetch the raw count
362 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
363 $row = $dbr->fetchObject( $res );
364 $count = $row->count
;
365 $dbr->freeResult( $res );
367 # Halve to remove talk pages if needed
369 $count = floor( $count / 2 );