fdc42e0e3f086cbeef2d6bf66d6ad7862b3ff444
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' => $wgUser->getBoolOption( 'watchlisthideown' ),
43 /* bool */ 'hideBots' => $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', $hideOwn, $defaults, $nondefaults);
144 wfAppendToArrayIfNotDefault( 'hideBots', $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 # TODO: Consider removing the third parameter
263 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
264 $wgLang->formatNum( $npages ), '',
265 $specialTitle->getFullUrl( 'edit=yes' ) );
266 $wgOut->addWikiText( $header );
268 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
269 $wgOut->addHTML( '<form action="' .
270 $specialTitle->escapeLocalUrl() .
271 '" method="post"><input type="submit" name="dummy" value="' .
272 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
273 '" /><input type="hidden" name="reset" value="all" /></form>' .
278 rc_namespace AS page_namespace, rc_title AS page_title,
279 rc_comment AS rev_comment, rc_cur_id AS page_id,
280 rc_user AS rev_user, rc_user_text AS rev_user_text,
281 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
282 rc_this_oldid AS rev_id,
284 rc_new AS page_is_new,wl_notificationtimestamp
285 FROM $watchlist,$recentchanges,$page
287 AND wl_namespace=rc_namespace
288 AND wl_title=rc_title
289 AND rc_timestamp > '$cutoff'
290 AND rc_cur_id=page_id
294 ORDER BY rc_timestamp DESC";
296 $res = $dbr->query( $sql, $fname );
297 $numRows = $dbr->numRows( $res );
299 /* Start bottom header */
300 $wgOut->addHTML( "<hr />\n<p>" );
303 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
304 $wgLang->formatNum( $days ) ) . '<br />' , false );
306 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
307 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
309 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
311 # Spit out some control panel links
312 $thisTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
313 $skin = $wgUser->getSkin();
314 $linkElements = array( 'hideOwn' => 'wlhideshowown', 'hideBots' => 'wlhideshowbots' );
316 # Problems encountered using the fancier method
317 $label = $hideBots ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
318 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
319 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
320 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
322 $label = $hideOwn ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
323 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
324 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
325 $links[] = wfMsgHtml( 'wlhideshowown', $link );
327 $wgOut->addHTML( implode( ' | ', $links ) );
329 # Form for namespace filtering
330 $thisAction = $thisTitle->escapeLocalUrl();
331 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
332 $nsForm .= "<label for=\"namespace\">" . wfMsg( 'namespace' ) . "</label> ";
333 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
334 $nsForm .= ( $hideOwn ?
"<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
335 $nsForm .= ( $hideBots ?
"<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
336 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
337 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
338 $nsForm .= "</form>\n";
339 $wgOut->addHTML( $nsForm );
341 if ( $numRows == 0 ) {
342 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
343 $wgOut->addHTML( "</p>\n" );
347 $wgOut->addHTML( "</p>\n" );
348 /* End bottom header */
350 $list = ChangesList
::newFromUser( $wgUser );
352 $s = $list->beginRecentChangesList();
354 while ( $obj = $dbr->fetchObject( $res ) ) {
356 $rc = RecentChange
::newFromCurRow( $obj, $obj->rc_last_oldid
);
357 $rc->counter
= $counter++
;
359 if ( $wgShowUpdatedMarker ) {
360 $updated = $obj->wl_notificationtimestamp
;
362 // Same visual appearance as MW 1.4
366 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
367 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title
). "' AND wl_namespace='{$obj->page_namespace}'" ;
368 $res3 = $dbr->query( $sql3, DB_READ
, $fname );
369 $x = $dbr->fetchObject( $res3 );
370 $rc->numberofWatchingusers
= $x->n
;
372 $rc->numberofWatchingusers
= 0;
375 $s .= $list->recentChangesLine( $rc, $updated );
377 $s .= $list->endRecentChangesList();
379 $dbr->freeResult( $res );
380 $wgOut->addHTML( $s );
382 if ( $wgUseWatchlistCache ) {
383 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
388 function wlHoursLink( $h, $page, $options = array() ) {
389 global $wgUser, $wgLang, $wgContLang;
390 $sk = $wgUser->getSkin();
391 $s = $sk->makeKnownLink(
392 $wgContLang->specialPage( $page ),
393 $wgLang->formatNum( $h ),
394 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
398 function wlDaysLink( $d, $page, $options = array() ) {
399 global $wgUser, $wgLang, $wgContLang;
400 $sk = $wgUser->getSkin();
401 $s = $sk->makeKnownLink(
402 $wgContLang->specialPage( $page ),
403 ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
404 wfArrayToCGI( array('days' => $d), $options ) );
408 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
409 $hours = array( 1, 2, 6, 12 );
410 $days = array( 1, 3, 7 );
413 foreach( $hours as $h ) {
414 $hours[$i++
] = wlHoursLink( $h, $page, $options );
417 foreach( $days as $d ) {
418 $days[$i++
] = wlDaysLink( $d, $page, $options );
420 return wfMsg ('wlshowlast',
421 implode(' | ', $hours),
422 implode(' | ', $days),
423 wlDaysLink( 0, $page, $options ) );
427 * Count the number of items on a user's watchlist
429 * @param $talk Include talk pages
432 function wlCountItems( &$user, $talk = true ) {
433 $dbr =& wfGetDB( DB_SLAVE
);
435 # Fetch the raw count
436 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
437 $row = $dbr->fetchObject( $res );
438 $count = $row->count
;
439 $dbr->freeResult( $res );
441 # Halve to remove talk pages if needed
443 $count = floor( $count / 2 );
449 * Allow the user to clear their watchlist
451 * @param $out Output object
452 * @param $request Request object
453 * @param $par Parameters passed to the watchlist page
454 * @return bool True if it's been taken care of; false indicates the watchlist
455 * code needs to do something further
457 function wlHandleClear( &$out, &$request, $par ) {
458 # Check this function has something to do
459 if( $request->getText( 'action' ) == 'clear' ||
$par == 'clear' ) {
461 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
462 $count = wlCountItems( $wgUser );
464 # See if we're clearing or confirming
465 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
466 # Clearing, so do it and report the result
467 $dbw =& wfGetDB( DB_MASTER
);
468 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId
), 'wlHandleClear' );
469 $out->addWikiText( wfMsg( 'watchlistcleardone', $count ) );
470 $out->returnToMain();
472 # Confirming, so show a form
473 $wlTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
474 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
475 $out->addWikiText( wfMsg( 'watchlistcount', $count ) );
476 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
477 $out->addHTML( wfElement( 'input', array( 'type' => 'hidden', 'name' => 'token', 'value' => $wgUser->editToken( 'clearwatchlist' ) ), '' ) );
478 $out->addHTML( wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) );
479 $out->addHTML( wfCloseElement( 'form' ) );
483 # Nothing on the watchlist; nothing to do here
484 $out->addWikiText( wfMsg( 'nowatchlist' ) );
485 $out->returnToMain();