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, $wgFilterRobotsWL;
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' => 3.0, /* or 0.5, watch further below */
42 /* bool */ 'hideOwn' => false,
43 /* bool */ 'hideBots' => false,
50 $days = $wgRequest->getVal( 'days' );
51 $hideOwn = $wgRequest->getBool( 'hideOwn' );
52 $hideBots = $wgRequest->getBool( 'hideBots' );
54 # Get namespace value, if supplied, and prepare a WHERE fragment
55 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
56 if( !is_null( $nameSpace ) ) {
57 $nameSpace = intval( $nameSpace );
58 $nameSpaceClause = " AND rc_namespace = $nameSpace";
61 $nameSpaceClause = '';
65 $action = $wgRequest->getVal( 'action' );
66 $remove = $wgRequest->getVal( 'remove' );
67 $id = $wgRequest->getArray( 'id' );
69 $uid = $wgUser->getID();
70 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
71 $wgUser->clearAllNotifications( $uid );
74 # Deleting items from watchlist
75 if(($action == 'submit') && isset($remove) && is_array($id)) {
76 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
77 $wgOut->addHTML( '<p>' );
78 foreach($id as $one) {
79 $t = Title
::newFromURL( $one );
80 if( !is_null( $t ) ) {
81 $wl = WatchedItem
::fromUserTitle( $wgUser, $t );
82 if( $wl->removeWatch() === false ) {
83 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
85 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
88 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
91 $wgOut->addHTML( "done.</p>\n" );
94 if ( $wgUseWatchlistCache ) {
95 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
96 $cache_s = @$wgMemc->get( $memckey );
98 $wgOut->addWikiText( wfMsg('wlsaved') );
99 $wgOut->addHTML( $cache_s );
104 $dbr =& wfGetDB( DB_SLAVE
);
105 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
107 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
108 $res = $dbr->query( $sql, $fname );
109 $s = $dbr->fetchObject( $res );
111 # Patch *** A1 *** (see A2 below)
112 # adjust for page X, talk:page X, which are both stored separately, but treated together
113 $nitems = floor($s->n
/ 2);
117 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
121 if( is_null($days) ||
!is_numeric($days) ) {
122 $big = 1000; /* The magical big */
124 # Set default cutoff shorter
125 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
127 $days = $defaults['days']; # default cutoff for shortlisters
130 $days = floatval($days);
133 // Dump everything here
134 $nondefaults = array();
136 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
137 wfAppendToArrayIfNotDefault( 'hideOwn', $hideOwn, $defaults, $nondefaults);
138 wfAppendToArrayIfNotDefault( 'hideBots', $hideBots, $defaults, $nondefaults);
139 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
144 $npages = wfMsg( 'watchlistall1' );
146 $docutoff = "AND rev_timestamp > '" .
147 ( $cutoff = $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;
159 /* Edit watchlist form */
160 if($wgRequest->getBool('edit') ||
$par == 'edit' ) {
161 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
162 "\n\n" . wfMsg( 'watcheditlist' ) );
164 $wgOut->addHTML( '<form action=\'' .
165 $specialTitle->escapeLocalUrl( 'action=submit' ) .
166 "' method='post'>\n" );
169 # The following was proposed by KTurner 07.11.2004 to T.Gries
170 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
171 $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";
173 $res = $dbr->query( $sql, $fname );
175 # Batch existence check
176 $linkBatch = new LinkBatch();
177 while( $row = $dbr->fetchObject( $res ) )
178 $linkBatch->addObj( Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
) );
179 $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 $titleText = $titleObj->getPrefixedText();
208 $pageLink = $sk->makeLinkObj( $titleObj );
209 $toolLinks[] = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK
) );
210 if( $titleObj->exists() )
211 $toolLinks[] = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'history_short' ), 'action=history' );
212 $toolLinks = '(' . implode( ' | ', $toolLinks ) . ')';
213 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ?
'‏' : '‎' );
215 $spanopen = '<span class="watchlistredir">';
216 $spanclosed = '</span>';
218 $spanopen = $spanclosed = '';
221 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
224 $wgOut->addHTML( '</ul>' );
227 "<input type='submit' name='remove' value=\"" .
228 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
235 # If the watchlist is relatively short, it's simplest to zip
236 # down its entirety and then sort the results.
238 # If it's relatively long, it may be worth our while to zip
239 # through the time-sorted page list checking for watched items.
241 # Up estimate of watched items by 15% to compensate for talk pages...
243 $andHideOwn = $hideOwn ?
"AND (rc_user <> $uid)" : '';
244 if( $wgFilterRobotsWL ) {
245 $andHideBotsOptional = $hideBots ?
"AND (rc_bot = 0)" : '';
247 $andHideBotsOptional = "AND rc_this_oldid=page_latest";
251 # Show watchlist header
253 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
254 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
256 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
257 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
260 # TODO: Consider removing the third parameter
261 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
262 $wgLang->formatNum( $npages ), '',
263 $specialTitle->getFullUrl( 'edit=yes' ) );
264 $wgOut->addWikiText( $header );
266 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
267 $wgOut->addHTML( '<form action="' .
268 $specialTitle->escapeLocalUrl() .
269 '" method="post"><input type="submit" name="dummy" value="' .
270 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
271 '" /><input type="hidden" name="reset" value="all" /></form>' .
276 rc_namespace AS page_namespace, rc_title AS page_title,
277 rc_comment AS rev_comment, rc_cur_id AS page_id,
278 rc_user AS rev_user, rc_user_text AS rev_user_text,
279 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
280 rc_this_oldid AS rev_id,
282 rc_new AS page_is_new,wl_notificationtimestamp
283 FROM $watchlist,$recentchanges,$page
285 AND wl_namespace=rc_namespace
286 AND wl_title=rc_title
287 AND rc_timestamp > '$cutoff'
288 AND rc_cur_id=page_id
292 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<p>" );
301 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
302 $wgLang->formatNum( $days ) ) . '<br />' , false );
304 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
305 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
307 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
309 $sk = $wgUser->getSkin();
310 $s = $sk->makeKnownLink(
311 $wgContLang->specialPage( 'Watchlist' ),
312 (0 == $hideOwn) ?
wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
313 wfArrayToCGI( array('hideOwn' => 1-$hideOwn ), $nondefaults ) );
314 $wgOut->addHTML( wfMsgHtml( "wlhideshowown", $s ) );
316 if( $wgFilterRobotsWL ) {
317 $s = $sk->makeKnownLink(
318 $wgContLang->specialPage( 'Watchlist' ),
319 (0 == $hideBots) ?
wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
320 wfArrayToCGI( array('hideBots' => 1-$hideBots ), $nondefaults ) );
321 $wgOut->addHTML( wfMsgHtml( "wlhideshowbots", " $s" ) );
324 # Form for namespace filtering
325 $thisTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
326 $thisAction = $thisTitle->escapeLocalUrl();
327 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
328 $nsForm .= "<label for=\"namespace\">" . wfMsg( 'namespace' ) . "</label> ";
329 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
330 $nsForm .= ( $hideOwn ?
"<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
331 $nsForm .= ( $hideBots ?
"<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
332 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
333 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
334 $nsForm .= "</form>\n";
335 $wgOut->addHTML( $nsForm );
337 if ( $numRows == 0 ) {
338 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
339 $wgOut->addHTML( "</p>\n" );
343 $wgOut->addHTML( "</p>\n" );
344 /* End bottom header */
346 $list = ChangesList
::newFromUser( $wgUser );
348 $s = $list->beginRecentChangesList();
350 while ( $obj = $dbr->fetchObject( $res ) ) {
352 $rc = RecentChange
::newFromCurRow( $obj, $obj->rc_last_oldid
);
353 $rc->counter
= $counter++
;
355 if ( $wgShowUpdatedMarker ) {
356 $updated = $obj->wl_notificationtimestamp
;
358 // Same visual appearance as MW 1.4
362 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
363 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title
). "' AND wl_namespace='{$obj->page_namespace}'" ;
364 $res3 = $dbr->query( $sql3, DB_READ
, $fname );
365 $x = $dbr->fetchObject( $res3 );
366 $rc->numberofWatchingusers
= $x->n
;
368 $rc->numberofWatchingusers
= 0;
371 $s .= $list->recentChangesLine( $rc, $updated );
373 $s .= $list->endRecentChangesList();
375 $dbr->freeResult( $res );
376 $wgOut->addHTML( $s );
378 if ( $wgUseWatchlistCache ) {
379 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
384 function wlHoursLink( $h, $page, $options = array() ) {
385 global $wgUser, $wgLang, $wgContLang;
386 $sk = $wgUser->getSkin();
387 $s = $sk->makeKnownLink(
388 $wgContLang->specialPage( $page ),
389 $wgLang->formatNum( $h ),
390 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
394 function wlDaysLink( $d, $page, $options = array() ) {
395 global $wgUser, $wgLang, $wgContLang;
396 $sk = $wgUser->getSkin();
397 $s = $sk->makeKnownLink(
398 $wgContLang->specialPage( $page ),
399 ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
400 wfArrayToCGI( array('days' => $d), $options ) );
404 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
405 $hours = array( 1, 2, 6, 12 );
406 $days = array( 1, 3, 7 );
409 foreach( $hours as $h ) {
410 $hours[$i++
] = wlHoursLink( $h, $page, $options );
413 foreach( $days as $d ) {
414 $days[$i++
] = wlDaysLink( $d, $page, $options );
416 return wfMsg ('wlshowlast',
417 implode(' | ', $hours),
418 implode(' | ', $days),
419 wlDaysLink( 0, $page, $options ) );
423 * Count the number of items on a user's watchlist
425 * @param $talk Include talk pages
428 function wlCountItems( &$user, $talk = true ) {
429 $dbr =& wfGetDB( DB_SLAVE
);
431 # Fetch the raw count
432 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
433 $row = $dbr->fetchObject( $res );
434 $count = $row->count
;
435 $dbr->freeResult( $res );
437 # Halve to remove talk pages if needed
439 $count = floor( $count / 2 );
445 * Allow the user to clear their watchlist
447 * @param $out Output object
448 * @param $request Request object
449 * @param $par Parameters passed to the watchlist page
450 * @return bool True if it's been taken care of; false indicates the watchlist
451 * code needs to do something further
453 function wlHandleClear( &$out, &$request, $par ) {
454 # Check this function has something to do
455 if( $request->getText( 'action' ) == 'clear' ||
$par == 'clear' ) {
457 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
458 $count = wlCountItems( $wgUser );
460 # See if we're clearing or confirming
461 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
462 # Clearing, so do it and report the result
463 $dbw =& wfGetDB( DB_MASTER
);
464 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId
), 'wlHandleClear' );
465 $out->addWikiText( wfMsg( 'watchlistcleardone', $count ) );
466 $out->returnToMain();
468 # Confirming, so show a form
469 $wlTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
470 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
471 $out->addWikiText( wfMsg( 'watchlistcount', $count ) );
472 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
473 $out->addHTML( wfElement( 'input', array( 'type' => 'hidden', 'name' => 'token', 'value' => $wgUser->editToken( 'clearwatchlist' ) ), '' ) );
474 $out->addHTML( wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) );
475 $out->addHTML( wfCloseElement( 'form' ) );
479 # Nothing on the watchlist; nothing to do here
480 $out->addWikiText( wfMsg( 'nowatchlist' ) );
481 $out->returnToMain();