5 * @subpackage SpecialPage
11 require_once( 'SpecialRecentchanges.php' );
15 * @todo Document $par parameter.
16 * @param $par String: FIXME
18 function wfSpecialWatchlist( $par ) {
19 global $wgUser, $wgOut, $wgLang, $wgMemc, $wgRequest, $wgContLang;
20 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname;
21 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
22 global $wgEnotifWatchlist;
23 $fname = 'wfSpecialWatchlist';
25 $wgOut->setPagetitle( wfMsg( 'watchlist' ) );
26 $sub = htmlspecialchars( wfMsg( 'watchlistsub', $wgUser->getName() ) );
27 $wgOut->setSubtitle( $sub );
28 $wgOut->setRobotpolicy( 'noindex,nofollow' );
30 $specialTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
32 if( $wgUser->isAnon() ) {
33 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
37 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
42 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
43 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
44 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
45 /* ? */ 'namespace' => 'all',
50 # Extract variables from the request, falling back to user preferences or
51 # other default values if these don't exist
52 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
53 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
54 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
57 $days = $wgRequest->getVal( 'days', $prefs['days'] );
58 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
59 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
61 # Get namespace value, if supplied, and prepare a WHERE fragment
62 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
63 if( !is_null( $nameSpace ) ) {
64 $nameSpace = intval( $nameSpace );
65 $nameSpaceClause = " AND rc_namespace = $nameSpace";
68 $nameSpaceClause = '';
72 $action = $wgRequest->getVal( 'action' );
73 $remove = $wgRequest->getVal( 'remove' );
74 $id = $wgRequest->getArray( 'id' );
76 $uid = $wgUser->getID();
77 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
78 $wgUser->clearAllNotifications( $uid );
81 # Deleting items from watchlist
82 if(($action == 'submit') && isset($remove) && is_array($id)) {
83 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
84 $wgOut->addHTML( '<p>' );
85 foreach($id as $one) {
86 $t = Title
::newFromURL( $one );
87 if( !is_null( $t ) ) {
88 $wl = WatchedItem
::fromUserTitle( $wgUser, $t );
89 if( $wl->removeWatch() === false ) {
90 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
92 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
93 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
96 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
99 $wgOut->addHTML( "<br />\n" . wfMsg( 'wldone' ) . "</p>\n" );
102 if ( $wgUseWatchlistCache ) {
103 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
104 $cache_s = @$wgMemc->get( $memckey );
106 $wgOut->addWikiText( wfMsg('wlsaved') );
107 $wgOut->addHTML( $cache_s );
112 $dbr =& wfGetDB( DB_SLAVE
);
113 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
115 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
116 $res = $dbr->query( $sql, $fname );
117 $s = $dbr->fetchObject( $res );
119 # Patch *** A1 *** (see A2 below)
120 # adjust for page X, talk:page X, which are both stored separately, but treated together
121 $nitems = floor($s->n
/ 2);
125 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
129 if( is_null($days) ||
!is_numeric($days) ) {
130 $big = 1000; /* The magical big */
132 # Set default cutoff shorter
133 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
135 $days = $defaults['days']; # default cutoff for shortlisters
138 $days = floatval($days);
141 // Dump everything here
142 $nondefaults = array();
144 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
145 wfAppendToArrayIfNotDefault( 'hideOwn', (int)$hideOwn, $defaults, $nondefaults);
146 wfAppendToArrayIfNotDefault( 'hideBots', (int)$hideBots, $defaults, $nondefaults);
147 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
152 $npages = wfMsg( 'watchlistall1' );
154 $docutoff = "AND rev_timestamp > '" .
155 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
158 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
159 $res = $dbr->query( $sql, $fname );
160 $s = $dbr->fetchObject( $res );
163 $npages = 40000 * $days;
167 /* Edit watchlist form */
168 if($wgRequest->getBool('edit') ||
$par == 'edit' ) {
169 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
170 "\n\n" . wfMsg( 'watcheditlist' ) );
172 $wgOut->addHTML( '<form action=\'' .
173 $specialTitle->escapeLocalUrl( 'action=submit' ) .
174 "' method='post'>\n" );
177 # The following was proposed by KTurner 07.11.2004 to T.Gries
178 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
179 $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";
181 $res = $dbr->query( $sql, $fname );
183 # Batch existence check
184 $linkBatch = new LinkBatch();
185 while( $row = $dbr->fetchObject( $res ) )
186 $linkBatch->addObj( Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
) );
187 $linkBatch->execute();
188 if( $dbr->numRows( $res ) > 0 )
189 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
191 $sk = $wgUser->getSkin();
194 while( $s = $dbr->fetchObject( $res ) ) {
195 $list[$s->wl_namespace
][$s->wl_title
] = $s->page_is_redirect
;
198 // TODO: Display a TOC
199 foreach($list as $ns => $titles) {
200 if (Namespace::isTalk($ns))
203 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
204 $wgOut->addHTML( '<ul>' );
205 foreach( $titles as $title => $redir ) {
206 $titleObj = Title
::makeTitle( $ns, $title );
207 if( is_null( $titleObj ) ) {
210 htmlspecialchars( $s->wl_title
) . '" in namespace ' . $s->wl_namespace
. " -->\n"
214 $toolLinks = array();
215 $titleText = $titleObj->getPrefixedText();
216 $pageLink = $sk->makeLinkObj( $titleObj );
217 $toolLinks[] = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK
) );
218 if( $titleObj->exists() )
219 $toolLinks[] = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'history_short' ), 'action=history' );
220 $toolLinks = '(' . implode( ' | ', $toolLinks ) . ')';
221 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ?
'‏' : '‎' );
223 $spanopen = '<span class="watchlistredir">';
224 $spanclosed = '</span>';
226 $spanopen = $spanclosed = '';
229 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
232 $wgOut->addHTML( '</ul>' );
235 "<input type='submit' name='remove' value=\"" .
236 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
243 # If the watchlist is relatively short, it's simplest to zip
244 # down its entirety and then sort the results.
246 # If it's relatively long, it may be worth our while to zip
247 # through the time-sorted page list checking for watched items.
249 # Up estimate of watched items by 15% to compensate for talk pages...
252 $andHideOwn = $hideOwn ?
"AND (rc_user <> $uid)" : '';
253 $andHideBots = $hideBots ?
"AND (rc_bot = 0)" : '';
255 # Show watchlist header
257 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
258 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
260 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
261 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
264 # Toggle watchlist content (all recent edits or just the latest)
265 if( $wgUser->getOption( 'extendwatchlist' )) {
267 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
269 $andLatest= 'AND rc_this_oldid=page_latest';
270 $limitWatchlist = '';
273 # TODO: Consider removing the third parameter
274 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
275 $wgLang->formatNum( $npages ), '',
276 $specialTitle->getFullUrl( 'edit=yes' ) );
277 $wgOut->addWikiText( $header );
279 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
280 $wgOut->addHTML( '<form action="' .
281 $specialTitle->escapeLocalUrl() .
282 '" method="post"><input type="submit" name="dummy" value="' .
283 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
284 '" /><input type="hidden" name="reset" value="all" /></form>' .
289 rc_namespace AS page_namespace, rc_title AS page_title,
290 rc_comment AS rev_comment, rc_cur_id AS page_id,
291 rc_user AS rev_user, rc_user_text AS rev_user_text,
292 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
293 rc_this_oldid AS rev_id,
294 rc_last_oldid, rc_id, rc_patrolled,
295 rc_new AS page_is_new,wl_notificationtimestamp
296 FROM $watchlist,$recentchanges,$page
298 AND wl_namespace=rc_namespace
299 AND wl_title=rc_title
300 AND rc_timestamp > '$cutoff'
301 AND rc_cur_id=page_id
306 ORDER BY rc_timestamp DESC
309 $res = $dbr->query( $sql, $fname );
310 $numRows = $dbr->numRows( $res );
312 /* Start bottom header */
313 $wgOut->addHTML( "<hr />\n<p>" );
316 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
317 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
319 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
320 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
322 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
324 # Spit out some control panel links
325 $thisTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
326 $skin = $wgUser->getSkin();
327 $linkElements = array( 'hideOwn' => 'wlhideshowown', 'hideBots' => 'wlhideshowbots' );
329 # Problems encountered using the fancier method
330 $label = $hideBots ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
331 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
332 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
333 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
335 $label = $hideOwn ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
336 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
337 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
338 $links[] = wfMsgHtml( 'wlhideshowown', $link );
340 $wgOut->addHTML( implode( ' | ', $links ) );
342 # Form for namespace filtering
343 $thisAction = $thisTitle->escapeLocalUrl();
344 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
345 $nsForm .= "<label for=\"namespace\">" . wfMsgExt( 'namespace', array( 'parseinline') ) . "</label> ";
346 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
347 $nsForm .= ( $hideOwn ?
"<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
348 $nsForm .= ( $hideBots ?
"<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
349 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
350 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgExt( 'allpagessubmit', array( 'escape') ) . "\" />\n";
351 $nsForm .= "</form>\n";
352 $wgOut->addHTML( $nsForm );
354 if ( $numRows == 0 ) {
355 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
356 $wgOut->addHTML( "</p>\n" );
360 $wgOut->addHTML( "</p>\n" );
361 /* End bottom header */
363 $list = ChangesList
::newFromUser( $wgUser );
365 $s = $list->beginRecentChangesList();
367 while ( $obj = $dbr->fetchObject( $res ) ) {
369 $rc = RecentChange
::newFromCurRow( $obj, $obj->rc_last_oldid
);
370 $rc->counter
= $counter++
;
372 if ( $wgShowUpdatedMarker ) {
373 $updated = $obj->wl_notificationtimestamp
;
375 // Same visual appearance as MW 1.4
379 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
380 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title
). "' AND wl_namespace='{$obj->page_namespace}'" ;
381 $res3 = $dbr->query( $sql3, DB_READ
, $fname );
382 $x = $dbr->fetchObject( $res3 );
383 $rc->numberofWatchingusers
= $x->n
;
385 $rc->numberofWatchingusers
= 0;
388 $s .= $list->recentChangesLine( $rc, $updated );
390 $s .= $list->endRecentChangesList();
392 $dbr->freeResult( $res );
393 $wgOut->addHTML( $s );
395 if ( $wgUseWatchlistCache ) {
396 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
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 );
429 foreach( $hours as $h ) {
430 $hours[$i++
] = wlHoursLink( $h, $page, $options );
433 foreach( $days as $d ) {
434 $days[$i++
] = wlDaysLink( $d, $page, $options );
436 return wfMsgExt('wlshowlast',
437 array('parseinline', 'replaceafter'),
438 implode(' | ', $hours),
439 implode(' | ', $days),
440 wlDaysLink( 0, $page, $options ) );
444 * Count the number of items on a user's watchlist
446 * @param $talk Include talk pages
449 function wlCountItems( &$user, $talk = true ) {
450 $dbr =& wfGetDB( DB_SLAVE
);
452 # Fetch the raw count
453 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
454 $row = $dbr->fetchObject( $res );
455 $count = $row->count
;
456 $dbr->freeResult( $res );
458 # Halve to remove talk pages if needed
460 $count = floor( $count / 2 );
466 * Allow the user to clear their watchlist
468 * @param $out Output object
469 * @param $request Request object
470 * @param $par Parameters passed to the watchlist page
471 * @return bool True if it's been taken care of; false indicates the watchlist
472 * code needs to do something further
474 function wlHandleClear( &$out, &$request, $par ) {
475 # Check this function has something to do
476 if( $request->getText( 'action' ) == 'clear' ||
$par == 'clear' ) {
478 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
479 $count = wlCountItems( $wgUser );
481 # See if we're clearing or confirming
482 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
483 # Clearing, so do it and report the result
484 $dbw =& wfGetDB( DB_MASTER
);
485 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId
), 'wlHandleClear' );
486 $out->addWikiText( wfMsg( 'watchlistcleardone', $count ) );
487 $out->returnToMain();
489 # Confirming, so show a form
490 $wlTitle = Title
::makeTitle( NS_SPECIAL
, 'Watchlist' );
491 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
492 $out->addWikiText( wfMsg( 'watchlistcount', $count ) );
493 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
494 $out->addHTML( wfElement( 'input', array( 'type' => 'hidden', 'name' => 'token', 'value' => $wgUser->editToken( 'clearwatchlist' ) ), '' ) );
495 $out->addHTML( wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) );
496 $out->addHTML( wfCloseElement( 'form' ) );
500 # Nothing on the watchlist; nothing to do here
501 $out->addWikiText( wfMsg( 'nowatchlist' ) );
502 $out->returnToMain();