4 * @addtogroup SpecialPage
10 require_once( dirname(__FILE__
) . '/SpecialRecentchanges.php' );
15 * @param $par Parameter passed to the page
17 function wfSpecialWatchlist( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgRequest, $wgContLang;
19 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
20 global $wgEnotifWatchlist;
21 $fname = 'wfSpecialWatchlist';
23 $skin = $wgUser->getSkin();
24 $specialTitle = SpecialPage
::getTitleFor( 'Watchlist' );
25 $wgOut->setRobotPolicy( 'noindex,nofollow' );
27 # Anons don't get a watchlist
28 if( $wgUser->isAnon() ) {
29 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
30 $llink = $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Userlogin' ), wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
31 $wgOut->addHtml( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
34 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
35 $wgOut->setSubtitle( wfMsgWikiHtml( 'watchlistfor', htmlspecialchars( $wgUser->getName() ) ) );
38 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
43 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
44 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
45 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
46 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
47 /* ? */ 'namespace' => 'all',
52 # Extract variables from the request, falling back to user preferences or
53 # other default values if these don't exist
54 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
55 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
56 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
57 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
60 $days = $wgRequest->getVal( 'days', $prefs['days'] );
61 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
62 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
63 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
65 # Get namespace value, if supplied, and prepare a WHERE fragment
66 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
67 if( !is_null( $nameSpace ) ) {
68 $nameSpace = intval( $nameSpace );
69 $nameSpaceClause = " AND rc_namespace = $nameSpace";
72 $nameSpaceClause = '';
76 $action = $wgRequest->getVal( 'action' );
77 $remove = $wgRequest->getVal( 'remove' );
78 $id = $wgRequest->getArray( 'id' );
80 $uid = $wgUser->getID();
81 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
82 $wgUser->clearAllNotifications( $uid );
85 # Deleting items from watchlist
86 if(($action == 'submit') && isset($remove) && is_array($id)) {
87 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
88 $wgOut->addHTML( '<p>' );
89 foreach($id as $one) {
90 $t = Title
::newFromURL( $one );
91 if( !is_null( $t ) ) {
92 $wl = WatchedItem
::fromUserTitle( $wgUser, $t );
93 if( $wl->removeWatch() === false ) {
94 $wgOut->addHTML( wfMsg( 'couldntremove', htmlspecialchars($one) ) . "<br />\n" );
96 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
97 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
100 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
103 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
106 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
107 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
109 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
110 $res = $dbr->query( $sql, $fname );
111 $s = $dbr->fetchObject( $res );
113 # Patch *** A1 *** (see A2 below)
114 # adjust for page X, talk:page X, which are both stored separately, but treated together
115 $nitems = floor($s->n
/ 2);
119 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
123 if( is_null($days) ||
!is_numeric($days) ) {
124 $big = 1000; /* The magical big */
126 # Set default cutoff shorter
127 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
129 $days = $defaults['days']; # default cutoff for shortlisters
132 $days = floatval($days);
135 // Dump everything here
136 $nondefaults = array();
138 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
139 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
140 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
141 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
142 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
146 $npages = wfMsg( 'watchlistall1' );
148 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
150 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
151 $res = $dbr->query( $sql, $fname );
152 $s = $dbr->fetchObject( $res );
155 $npages = 40000 * $days;
158 /* Edit watchlist form */
159 if($wgRequest->getBool('edit') ||
$par == 'edit' ) {
160 $wgOut->addWikiText( wfMsgExt( 'watchlistcontains', array( 'parseinline' ), $wgLang->formatNum( $nitems ) ) .
161 "\n\n" . wfMsg( 'watcheditlist' ) );
163 $wgOut->addHTML( '<form action=\'' .
164 $specialTitle->escapeLocalUrl( 'action=submit' ) .
165 "' method='post'>\n" );
168 # The following was proposed by KTurner 07.11.2004 to T.Gries
169 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
170 $sql = "SELECT wl_namespace, wl_title, page_is_redirect FROM $watchlist LEFT JOIN $page ON wl_namespace = page_namespace AND wl_title = page_title WHERE wl_user=$uid";
172 $res = $dbr->query( $sql, $fname );
174 # Batch existence check
175 $linkBatch = new LinkBatch();
176 while( $row = $dbr->fetchObject( $res ) )
177 $linkBatch->addObj( Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
) );
178 $linkBatch->execute();
180 if( $dbr->numRows( $res ) > 0 )
181 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
183 $sk = $wgUser->getSkin();
186 while( $s = $dbr->fetchObject( $res ) ) {
187 $list[$s->wl_namespace
][$s->wl_title
] = $s->page_is_redirect
;
190 // TODO: Display a TOC
191 foreach($list as $ns => $titles) {
192 if (Namespace::isTalk($ns))
195 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
196 $wgOut->addHTML( '<ul>' );
197 foreach( $titles as $title => $redir ) {
198 $titleObj = Title
::makeTitle( $ns, $title );
199 if( is_null( $titleObj ) ) {
202 htmlspecialchars( $s->wl_title
) . '" in namespace ' . $s->wl_namespace
. " -->\n"
206 $toolLinks = array();
207 $pageLink = $sk->makeLinkObj( $titleObj );
208 $toolLinks[] = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK
) );
209 if( $titleObj->exists() )
210 $toolLinks[] = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'history_short' ), 'action=history' );
211 $toolLinks = '(' . implode( ' | ', $toolLinks ) . ')';
212 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ?
'‏' : '‎' );
214 $spanopen = '<span class="watchlistredir">';
215 $spanclosed = '</span>';
217 $spanopen = $spanclosed = '';
220 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
223 $wgOut->addHTML( '</ul>' );
226 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
233 # If the watchlist is relatively short, it's simplest to zip
234 # down its entirety and then sort the results.
236 # If it's relatively long, it may be worth our while to zip
237 # through the time-sorted page list checking for watched items.
239 # Up estimate of watched items by 15% to compensate for talk pages...
242 $andHideOwn = $hideOwn ?
"AND (rc_user <> $uid)" : '';
243 $andHideBots = $hideBots ?
"AND (rc_bot = 0)" : '';
244 $andHideMinor = $hideMinor ?
'AND rc_minor = 0' : '';
246 # Show watchlist header
248 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
249 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
251 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
252 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
255 # Toggle watchlist content (all recent edits or just the latest)
256 if( $wgUser->getOption( 'extendwatchlist' )) {
258 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
260 $andLatest= 'AND rc_this_oldid=page_latest';
261 $limitWatchlist = '';
264 # TODO: Consider removing the third parameter
265 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
266 $wgLang->formatNum( $npages ), '',
267 $specialTitle->getFullUrl( 'edit=yes' ) );
268 $wgOut->addWikiText( $header );
270 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
271 $wgOut->addHTML( '<form action="' .
272 $specialTitle->escapeLocalUrl() .
273 '" method="post"><input type="submit" name="dummy" value="' .
274 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
275 '" /><input type="hidden" name="reset" value="all" /></form>' .
280 FROM $watchlist,$recentchanges,$page
282 AND wl_namespace=rc_namespace
283 AND wl_title=rc_title
284 AND rc_cur_id=page_id
291 ORDER BY rc_timestamp DESC
294 $res = $dbr->query( $sql, $fname );
295 $numRows = $dbr->numRows( $res );
297 /* Start bottom header */
298 $wgOut->addHTML( "<hr />\n" );
301 $wgOut->addWikiText( wfMsgExt( 'rcnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
302 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
303 } elseif($days > 0) {
304 $wgOut->addWikiText( wfMsgExt( 'wlnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
305 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
308 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
310 # Spit out some control panel links
311 $thisTitle = SpecialPage
::getTitleFor( 'Watchlist' );
312 $skin = $wgUser->getSkin();
314 # Hide/show bot edits
315 $label = $hideBots ?
wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
316 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
317 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
319 # Hide/show own edits
320 $label = $hideOwn ?
wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
321 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
322 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
324 # Hide/show minor edits
325 $label = $hideMinor ?
wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
326 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
327 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
329 $wgOut->addHTML( implode( ' | ', $links ) );
331 # Form for namespace filtering
332 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
334 $form .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ';
335 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
336 $form .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
337 $form .= Xml
::hidden( 'days', $days );
339 $form .= Xml
::hidden( 'hideOwn', 1 );
341 $form .= Xml
::hidden( 'hideBots', 1 );
343 $form .= Xml
::hidden( 'hideMinor', 1 );
344 $form .= Xml
::closeElement( 'form' );
345 $wgOut->addHtml( $form );
347 # If there's nothing to show, stop here
348 if( $numRows == 0 ) {
349 $wgOut->addWikiText( wfMsgNoTrans( 'watchnochange' ) );
353 /* End bottom header */
355 /* Do link batch query */
356 $linkBatch = new LinkBatch
;
357 while ( $row = $dbr->fetchObject( $res ) ) {
358 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
359 if ( $row->rc_user
!= 0 ) {
360 $linkBatch->add( NS_USER
, $userNameUnderscored );
362 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
364 $linkBatch->execute();
365 $dbr->dataSeek( $res, 0 );
367 $list = ChangesList
::newFromUser( $wgUser );
369 $s = $list->beginRecentChangesList();
371 while ( $obj = $dbr->fetchObject( $res ) ) {
373 $rc = RecentChange
::newFromRow( $obj );
374 $rc->counter
= $counter++
;
376 if ( $wgShowUpdatedMarker ) {
377 $updated = $obj->wl_notificationtimestamp
;
379 // Same visual appearance as MW 1.4
383 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
384 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .$dbr->strencode($obj->page_title
). "' AND wl_namespace='{$obj->page_namespace}'" ;
385 $res3 = $dbr->query( $sql3, $fname );
386 $x = $dbr->fetchObject( $res3 );
387 $rc->numberofWatchingusers
= $x->n
;
389 $rc->numberofWatchingusers
= 0;
392 $s .= $list->recentChangesLine( $rc, $updated );
394 $s .= $list->endRecentChangesList();
396 $dbr->freeResult( $res );
397 $wgOut->addHTML( $s );
401 function wlHoursLink( $h, $page, $options = array() ) {
402 global $wgUser, $wgLang, $wgContLang;
403 $sk = $wgUser->getSkin();
404 $s = $sk->makeKnownLink(
405 $wgContLang->specialPage( $page ),
406 $wgLang->formatNum( $h ),
407 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
411 function wlDaysLink( $d, $page, $options = array() ) {
412 global $wgUser, $wgLang, $wgContLang;
413 $sk = $wgUser->getSkin();
414 $s = $sk->makeKnownLink(
415 $wgContLang->specialPage( $page ),
416 ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
417 wfArrayToCGI( array('days' => $d), $options ) );
424 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
425 $hours = array( 1, 2, 6, 12 );
426 $days = array( 1, 3, 7 );
428 foreach( $hours as $h ) {
429 $hours[$i++
] = wlHoursLink( $h, $page, $options );
432 foreach( $days as $d ) {
433 $days[$i++
] = wlDaysLink( $d, $page, $options );
435 return wfMsgExt('wlshowlast',
436 array('parseinline', 'replaceafter'),
437 implode(' | ', $hours),
438 implode(' | ', $days),
439 wlDaysLink( 0, $page, $options ) );
443 * Count the number of items on a user's watchlist
445 * @param $talk Include talk pages
448 function wlCountItems( &$user, $talk = true ) {
449 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
451 # Fetch the raw count
452 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
453 $row = $dbr->fetchObject( $res );
454 $count = $row->count
;
455 $dbr->freeResult( $res );
457 # Halve to remove talk pages if needed
459 $count = floor( $count / 2 );
465 * Allow the user to clear their watchlist
467 * @param $out Output object
468 * @param $request Request object
469 * @param $par Parameters passed to the watchlist page
470 * @return bool True if it's been taken care of; false indicates the watchlist
471 * code needs to do something further
473 function wlHandleClear( &$out, &$request, $par ) {
476 # Check this function has something to do
477 if( $request->getText( 'action' ) == 'clear' ||
$par == 'clear' ) {
479 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
480 $count = wlCountItems( $wgUser );
482 # See if we're clearing or confirming
483 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
484 # Clearing, so do it and report the result
485 $dbw = wfGetDB( DB_MASTER
);
486 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId
), 'wlHandleClear' );
487 $out->addWikiText( wfMsgExt( 'watchlistcleardone', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
488 $out->returnToMain();
490 # Confirming, so show a form
491 $wlTitle = SpecialPage
::getTitleFor( 'Watchlist' );
492 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
493 $out->addWikiText( wfMsgExt( 'watchlistcount', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
494 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
496 wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
497 wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
498 wfCloseElement( 'form' )
503 # Nothing on the watchlist; nothing to do here
504 $out->addWikiText( wfMsg( 'nowatchlist' ) );
505 $out->returnToMain();