5 * @subpackage SpecialPage
11 require_once( 'SpecialRecentchanges.php' );
12 require_once( 'WatchedItem.php' );
17 function wfSpecialWatchlist( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgMemc, $wgRequest, $wgContLang;
19 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname;
20 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
21 global $wgEnotifWatchlist;
22 $fname = 'wfSpecialWatchlist';
24 $wgOut->setPagetitle( wfMsg( 'watchlist' ) );
25 $sub = htmlspecialchars( wfMsg( 'watchlistsub', $wgUser->getName() ) );
26 $wgOut->setSubtitle( $sub );
27 $wgOut->setRobotpolicy( 'noindex,nofollow' );
29 $specialTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
31 if( $wgUser->isAnon() ) {
32 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
36 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
41 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
42 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
43 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
49 # Extract variables from the request, falling back to user preferences or
50 # other default values if these don't exist
51 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
52 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
53 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
56 $days = $wgRequest->getVal( 'days', $prefs['days'] );
57 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
58 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
60 # Get namespace value, if supplied, and prepare a WHERE fragment
61 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
62 if( !is_null( $nameSpace ) ) {
63 $nameSpace = intval( $nameSpace );
64 $nameSpaceClause = " AND rc_namespace = $nameSpace";
67 $nameSpaceClause = '';
71 $action = $wgRequest->getVal( 'action' );
72 $remove = $wgRequest->getVal( 'remove' );
73 $id = $wgRequest->getArray( 'id' );
75 $uid = $wgUser->getID();
76 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
77 $wgUser->clearAllNotifications( $uid );
80 # Deleting items from watchlist
81 if(($action == 'submit') && isset($remove) && is_array($id)) {
82 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
83 $wgOut->addHTML( '<p>' );
84 foreach($id as $one) {
85 $t = Title
::newFromURL( $one );
86 if( !is_null( $t ) ) {
87 $wl = WatchedItem
::fromUserTitle( $wgUser, $t );
88 if( $wl->removeWatch() === false ) {
89 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
91 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
94 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
97 $wgOut->addHTML( "done.</p>\n" );
100 if ( $wgUseWatchlistCache ) {
101 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
102 $cache_s = @$wgMemc->get( $memckey );
104 $wgOut->addWikiText( wfMsg('wlsaved') );
105 $wgOut->addHTML( $cache_s );
110 $dbr =& wfGetDB( DB_SLAVE
);
111 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
113 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
114 $res = $dbr->query( $sql, $fname );
115 $s = $dbr->fetchObject( $res );
117 # Patch *** A1 *** (see A2 below)
118 # adjust for page X, talk:page X, which are both stored separately, but treated together
119 $nitems = floor($s->n
/ 2);
123 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
127 if( is_null($days) ||
!is_numeric($days) ) {
128 $big = 1000; /* The magical big */
130 # Set default cutoff shorter
131 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
133 $days = $defaults['days']; # default cutoff for shortlisters
136 $days = floatval($days);
139 // Dump everything here
140 $nondefaults = array();
142 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
143 wfAppendToArrayIfNotDefault( 'hideOwn', (int)$hideOwn, $defaults, $nondefaults);
144 wfAppendToArrayIfNotDefault( 'hideBots', (int)$hideBots, $defaults, $nondefaults);
145 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
150 $npages = wfMsg( 'watchlistall1' );
152 $docutoff = "AND rev_timestamp > '" .
153 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
156 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
157 $res = $dbr->query( $sql, $fname );
158 $s = $dbr->fetchObject( $res );
161 $npages = 40000 * $days;
165 /* Edit watchlist form */
166 if($wgRequest->getBool('edit') ||
$par == 'edit' ) {
167 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
168 "\n\n" . wfMsg( 'watcheditlist' ) );
170 $wgOut->addHTML( '<form action=\'' .
171 $specialTitle->escapeLocalUrl( 'action=submit' ) .
172 "' method='post'>\n" );
175 # The following was proposed by KTurner 07.11.2004 to T.Gries
176 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
177 $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";
179 $res = $dbr->query( $sql, $fname );
181 # Batch existence check
182 $linkBatch = new LinkBatch();
183 while( $row = $dbr->fetchObject( $res ) )
184 $linkBatch->addObj( Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
) );
185 $linkBatch->execute();
186 if( $dbr->numRows( $res ) > 0 )
187 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
189 $sk = $wgUser->getSkin();
192 while( $s = $dbr->fetchObject( $res ) ) {
193 $list[$s->wl_namespace
][$s->wl_title
] = $s->page_is_redirect
;
196 // TODO: Display a TOC
197 foreach($list as $ns => $titles) {
198 if (Namespace::isTalk($ns))
201 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
202 $wgOut->addHTML( '<ul>' );
203 foreach( $titles as $title => $redir ) {
204 $titleObj = Title
::makeTitle( $ns, $title );
205 if( is_null( $titleObj ) ) {
208 htmlspecialchars( $s->wl_title
) . '" in namespace ' . $s->wl_namespace
. " -->\n"
212 $toolLinks = array();
213 $titleText = $titleObj->getPrefixedText();
214 $pageLink = $sk->makeLinkObj( $titleObj );
215 $toolLinks[] = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK
) );
216 if( $titleObj->exists() )
217 $toolLinks[] = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'history_short' ), 'action=history' );
218 $toolLinks = '(' . implode( ' | ', $toolLinks ) . ')';
219 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ?
'‏' : '‎' );
221 $spanopen = '<span class="watchlistredir">';
222 $spanclosed = '</span>';
224 $spanopen = $spanclosed = '';
227 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
230 $wgOut->addHTML( '</ul>' );
233 "<input type='submit' name='remove' value=\"" .
234 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
241 # If the watchlist is relatively short, it's simplest to zip
242 # down its entirety and then sort the results.
244 # If it's relatively long, it may be worth our while to zip
245 # through the time-sorted page list checking for watched items.
247 # Up estimate of watched items by 15% to compensate for talk pages...
250 $andHideOwn = $hideOwn ?
"AND (rc_user <> $uid)" : '';
251 $andHideBots = $hideBots ?
"AND (rc_bot = 0)" : '';
253 # Show watchlist header
255 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
256 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
258 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
259 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
262 # Toggle watchlist content (all recent edits or just the latest)
263 if( $wgUser->getOption( 'extendwatchlist' )) {
265 $limitWatchlist = 'LIMIT ' . $wgUser->getOption( 'wllimit' );
267 $andLatest= 'AND rc_this_oldid=page_latest';
268 $limitWatchlist = '';
271 # TODO: Consider removing the third parameter
272 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
273 $wgLang->formatNum( $npages ), '',
274 $specialTitle->getFullUrl( 'edit=yes' ) );
275 $wgOut->addWikiText( $header );
277 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
278 $wgOut->addHTML( '<form action="' .
279 $specialTitle->escapeLocalUrl() .
280 '" method="post"><input type="submit" name="dummy" value="' .
281 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
282 '" /><input type="hidden" name="reset" value="all" /></form>' .
287 rc_namespace AS page_namespace, rc_title AS page_title,
288 rc_comment AS rev_comment, rc_cur_id AS page_id,
289 rc_user AS rev_user, rc_user_text AS rev_user_text,
290 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
291 rc_this_oldid AS rev_id,
293 rc_new AS page_is_new,wl_notificationtimestamp
294 FROM $watchlist,$recentchanges,$page
296 AND wl_namespace=rc_namespace
297 AND wl_title=rc_title
298 AND rc_timestamp > '$cutoff'
299 AND rc_cur_id=page_id
304 ORDER BY rc_timestamp DESC
307 $res = $dbr->query( $sql, $fname );
308 $numRows = $dbr->numRows( $res );
310 /* Start bottom header */
311 $wgOut->addHTML( "<hr />\n<p>" );
314 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
315 $wgLang->formatNum( $days ) ) . '<br />' , false );
317 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
318 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
320 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
322 # Spit out some control panel links
323 $thisTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
324 $skin = $wgUser->getSkin();
325 $linkElements = array( 'hideOwn' => 'wlhideshowown', 'hideBots' => 'wlhideshowbots' );
327 # Problems encountered using the fancier method
328 $label = $hideBots ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
329 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
330 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
331 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
333 $label = $hideOwn ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
334 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
335 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
336 $links[] = wfMsgHtml( 'wlhideshowown', $link );
338 $wgOut->addHTML( implode( ' | ', $links ) );
340 # Form for namespace filtering
341 $thisAction = $thisTitle->escapeLocalUrl();
342 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
343 $nsForm .= "<label for=\"namespace\">" . wfMsg( 'namespace' ) . "</label> ";
344 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
345 $nsForm .= ( $hideOwn ?
"<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
346 $nsForm .= ( $hideBots ?
"<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
347 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
348 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
349 $nsForm .= "</form>\n";
350 $wgOut->addHTML( $nsForm );
352 if ( $numRows == 0 ) {
353 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
354 $wgOut->addHTML( "</p>\n" );
358 $wgOut->addHTML( "</p>\n" );
359 /* End bottom header */
361 $list = ChangesList
::newFromUser( $wgUser );
363 $s = $list->beginRecentChangesList();
365 while ( $obj = $dbr->fetchObject( $res ) ) {
367 $rc = RecentChange
::newFromCurRow( $obj, $obj->rc_last_oldid
);
368 $rc->counter
= $counter++
;
370 if ( $wgShowUpdatedMarker ) {
371 $updated = $obj->wl_notificationtimestamp
;
373 // Same visual appearance as MW 1.4
377 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
378 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title
). "' AND wl_namespace='{$obj->page_namespace}'" ;
379 $res3 = $dbr->query( $sql3, DB_READ
, $fname );
380 $x = $dbr->fetchObject( $res3 );
381 $rc->numberofWatchingusers
= $x->n
;
383 $rc->numberofWatchingusers
= 0;
386 $s .= $list->recentChangesLine( $rc, $updated );
388 $s .= $list->endRecentChangesList();
390 $dbr->freeResult( $res );
391 $wgOut->addHTML( $s );
393 if ( $wgUseWatchlistCache ) {
394 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
399 function wlHoursLink( $h, $page, $options = array() ) {
400 global $wgUser, $wgLang, $wgContLang;
401 $sk = $wgUser->getSkin();
402 $s = $sk->makeKnownLink(
403 $wgContLang->specialPage( $page ),
404 $wgLang->formatNum( $h ),
405 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
409 function wlDaysLink( $d, $page, $options = array() ) {
410 global $wgUser, $wgLang, $wgContLang;
411 $sk = $wgUser->getSkin();
412 $s = $sk->makeKnownLink(
413 $wgContLang->specialPage( $page ),
414 ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
415 wfArrayToCGI( array('days' => $d), $options ) );
419 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
420 $hours = array( 1, 2, 6, 12 );
421 $days = array( 1, 3, 7 );
424 foreach( $hours as $h ) {
425 $hours[$i++
] = wlHoursLink( $h, $page, $options );
428 foreach( $days as $d ) {
429 $days[$i++
] = wlDaysLink( $d, $page, $options );
431 return wfMsg ('wlshowlast',
432 implode(' | ', $hours),
433 implode(' | ', $days),
434 wlDaysLink( 0, $page, $options ) );
438 * Count the number of items on a user's watchlist
440 * @param $talk Include talk pages
443 function wlCountItems( &$user, $talk = true ) {
444 $dbr =& wfGetDB( DB_SLAVE
);
446 # Fetch the raw count
447 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
448 $row = $dbr->fetchObject( $res );
449 $count = $row->count
;
450 $dbr->freeResult( $res );
452 # Halve to remove talk pages if needed
454 $count = floor( $count / 2 );
460 * Allow the user to clear their watchlist
462 * @param $out Output object
463 * @param $request Request object
464 * @param $par Parameters passed to the watchlist page
465 * @return bool True if it's been taken care of; false indicates the watchlist
466 * code needs to do something further
468 function wlHandleClear( &$out, &$request, $par ) {
469 # Check this function has something to do
470 if( $request->getText( 'action' ) == 'clear' ||
$par == 'clear' ) {
472 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
473 $count = wlCountItems( $wgUser );
475 # See if we're clearing or confirming
476 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
477 # Clearing, so do it and report the result
478 $dbw =& wfGetDB( DB_MASTER
);
479 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId
), 'wlHandleClear' );
480 $out->addWikiText( wfMsg( 'watchlistcleardone', $count ) );
481 $out->returnToMain();
483 # Confirming, so show a form
484 $wlTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
485 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
486 $out->addWikiText( wfMsg( 'watchlistcount', $count ) );
487 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
488 $out->addHTML( wfElement( 'input', array( 'type' => 'hidden', 'name' => 'token', 'value' => $wgUser->editToken( 'clearwatchlist' ) ), '' ) );
489 $out->addHTML( wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) );
490 $out->addHTML( wfCloseElement( 'form' ) );
494 # Nothing on the watchlist; nothing to do here
495 $out->addWikiText( wfMsg( 'nowatchlist' ) );
496 $out->returnToMain();