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;
21 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
22 global $wgEnotifWatchlist;
23 $fname = 'wfSpecialWatchlist';
25 $skin =& $wgUser->getSkin();
26 $specialTitle = SpecialPage
::getTitleFor( 'Watchlist' );
27 $wgOut->setRobotPolicy( 'noindex,nofollow' );
29 # Anons don't get a watchlist
30 if( $wgUser->isAnon() ) {
31 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
32 $llink = $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Userlogin' ), wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
33 $wgOut->addHtml( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
36 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
37 $wgOut->setSubtitle( wfMsgWikiHtml( 'watchlistfor', htmlspecialchars( $wgUser->getName() ) ) );
40 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
45 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
46 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
47 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
48 /* ? */ 'namespace' => 'all',
53 # Extract variables from the request, falling back to user preferences or
54 # other default values if these don't exist
55 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
56 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
57 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
60 $days = $wgRequest->getVal( 'days', $prefs['days'] );
61 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
62 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
64 # Get namespace value, if supplied, and prepare a WHERE fragment
65 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
66 if( !is_null( $nameSpace ) ) {
67 $nameSpace = intval( $nameSpace );
68 $nameSpaceClause = " AND rc_namespace = $nameSpace";
71 $nameSpaceClause = '';
75 $action = $wgRequest->getVal( 'action' );
76 $remove = $wgRequest->getVal( 'remove' );
77 $id = $wgRequest->getArray( 'id' );
79 $uid = $wgUser->getID();
80 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
81 $wgUser->clearAllNotifications( $uid );
84 # Deleting items from watchlist
85 if(($action == 'submit') && isset($remove) && is_array($id)) {
86 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
87 $wgOut->addHTML( '<p>' );
88 foreach($id as $one) {
89 $t = Title
::newFromURL( $one );
90 if( !is_null( $t ) ) {
91 $wl = WatchedItem
::fromUserTitle( $wgUser, $t );
92 if( $wl->removeWatch() === false ) {
93 $wgOut->addHTML( wfMsg( 'couldntremove', htmlspecialchars($one) ) . "<br />\n" );
95 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
96 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
99 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
102 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
105 if ( $wgUseWatchlistCache ) {
106 $memckey = wfMemcKey( 'watchlist', 'id', $wgUser->getId() );
107 $cache_s = @$wgMemc->get( $memckey );
109 $wgOut->addWikiText( wfMsg('wlsaved') );
110 $wgOut->addHTML( $cache_s );
115 $dbr =& wfGetDB( DB_SLAVE
);
116 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
118 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
119 $res = $dbr->query( $sql, $fname );
120 $s = $dbr->fetchObject( $res );
122 # Patch *** A1 *** (see A2 below)
123 # adjust for page X, talk:page X, which are both stored separately, but treated together
124 $nitems = floor($s->n
/ 2);
128 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
132 if( is_null($days) ||
!is_numeric($days) ) {
133 $big = 1000; /* The magical big */
135 # Set default cutoff shorter
136 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
138 $days = $defaults['days']; # default cutoff for shortlisters
141 $days = floatval($days);
144 // Dump everything here
145 $nondefaults = array();
147 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
148 wfAppendToArrayIfNotDefault( 'hideOwn', (int)$hideOwn, $defaults, $nondefaults);
149 wfAppendToArrayIfNotDefault( 'hideBots', (int)$hideBots, $defaults, $nondefaults);
150 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
154 $npages = wfMsg( 'watchlistall1' );
156 $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;
166 /* Edit watchlist form */
167 if($wgRequest->getBool('edit') ||
$par == 'edit' ) {
168 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
169 "\n\n" . wfMsg( 'watcheditlist' ) );
171 $wgOut->addHTML( '<form action=\'' .
172 $specialTitle->escapeLocalUrl( 'action=submit' ) .
173 "' method='post'>\n" );
176 # The following was proposed by KTurner 07.11.2004 to T.Gries
177 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
178 $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";
180 $res = $dbr->query( $sql, $fname );
182 # Batch existence check
183 $linkBatch = new LinkBatch();
184 while( $row = $dbr->fetchObject( $res ) )
185 $linkBatch->addObj( Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
) );
186 $linkBatch->execute();
187 if( $dbr->numRows( $res ) > 0 )
188 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
190 $sk = $wgUser->getSkin();
193 while( $s = $dbr->fetchObject( $res ) ) {
194 $list[$s->wl_namespace
][$s->wl_title
] = $s->page_is_redirect
;
197 // TODO: Display a TOC
198 foreach($list as $ns => $titles) {
199 if (Namespace::isTalk($ns))
202 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
203 $wgOut->addHTML( '<ul>' );
204 foreach( $titles as $title => $redir ) {
205 $titleObj = Title
::makeTitle( $ns, $title );
206 if( is_null( $titleObj ) ) {
209 htmlspecialchars( $s->wl_title
) . '" in namespace ' . $s->wl_namespace
. " -->\n"
213 $toolLinks = array();
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 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
240 # If the watchlist is relatively short, it's simplest to zip
241 # down its entirety and then sort the results.
243 # If it's relatively long, it may be worth our while to zip
244 # through the time-sorted page list checking for watched items.
246 # Up estimate of watched items by 15% to compensate for talk pages...
249 $andHideOwn = $hideOwn ?
"AND (rc_user <> $uid)" : '';
250 $andHideBots = $hideBots ?
"AND (rc_bot = 0)" : '';
252 # Show watchlist header
254 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
255 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
257 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
258 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
261 # Toggle watchlist content (all recent edits or just the latest)
262 if( $wgUser->getOption( 'extendwatchlist' )) {
264 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
266 $andLatest= 'AND rc_this_oldid=page_latest';
267 $limitWatchlist = '';
270 # TODO: Consider removing the third parameter
271 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
272 $wgLang->formatNum( $npages ), '',
273 $specialTitle->getFullUrl( 'edit=yes' ) );
274 $wgOut->addWikiText( $header );
276 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
277 $wgOut->addHTML( '<form action="' .
278 $specialTitle->escapeLocalUrl() .
279 '" method="post"><input type="submit" name="dummy" value="' .
280 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
281 '" /><input type="hidden" name="reset" value="all" /></form>' .
286 rc_namespace AS page_namespace, rc_title AS page_title,
287 rc_comment AS rev_comment, rc_cur_id AS page_id,
288 rc_user AS rev_user, rc_user_text AS rev_user_text,
289 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
290 rc_this_oldid AS rev_id,
291 rc_last_oldid, rc_id, rc_patrolled,
292 rc_new AS page_is_new,wl_notificationtimestamp
293 FROM $watchlist,$recentchanges,$page
295 AND wl_namespace=rc_namespace
296 AND wl_title=rc_title
297 AND rc_timestamp > '$cutoff'
298 AND rc_cur_id=page_id
303 ORDER BY rc_timestamp DESC
306 $res = $dbr->query( $sql, $fname );
307 $numRows = $dbr->numRows( $res );
309 /* Start bottom header */
310 $wgOut->addHTML( "<hr />\n" );
313 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
314 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
315 } elseif($days > 0) {
316 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
317 $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 = SpecialPage
::getTitleFor( 'Watchlist' );
324 $skin = $wgUser->getSkin();
326 # Problems encountered using the fancier method
327 $label = $hideBots ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
328 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
329 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
330 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
332 $label = $hideOwn ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
333 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
334 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
335 $links[] = wfMsgHtml( 'wlhideshowown', $link );
337 $wgOut->addHTML( implode( ' | ', $links ) );
339 # Form for namespace filtering
340 $wgOut->addHTML( "\n" .
341 wfOpenElement( 'form', array(
343 'action' => $thisTitle->getLocalURL(),
345 wfMsgExt( 'namespace', array( 'parseinline') ) .
346 HTMLnamespaceselector( $nameSpace, '' ) . "\n" .
347 ( $hideOwn ?
wfHidden('hideown', 1)."\n" : '' ) .
348 ( $hideBots ?
wfHidden('hidebots', 1)."\n" : '' ) .
349 wfHidden( 'days', $days ) . "\n" .
350 wfSubmitButton( wfMsgExt( 'allpagessubmit', array( 'escape') ) ) . "\n" .
351 wfCloseElement( 'form' ) . "\n"
354 if ( $numRows == 0 ) {
355 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
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='" .$dbr->strencode($obj->page_title
). "' AND wl_namespace='{$obj->page_namespace}'" ;
379 $res3 = $dbr->query( $sql3, $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 ) );
422 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
423 $hours = array( 1, 2, 6, 12 );
424 $days = array( 1, 3, 7 );
426 foreach( $hours as $h ) {
427 $hours[$i++
] = wlHoursLink( $h, $page, $options );
430 foreach( $days as $d ) {
431 $days[$i++
] = wlDaysLink( $d, $page, $options );
433 return wfMsgExt('wlshowlast',
434 array('parseinline', 'replaceafter'),
435 implode(' | ', $hours),
436 implode(' | ', $days),
437 wlDaysLink( 0, $page, $options ) );
441 * Count the number of items on a user's watchlist
443 * @param $talk Include talk pages
446 function wlCountItems( &$user, $talk = true ) {
447 $dbr =& wfGetDB( DB_SLAVE
);
449 # Fetch the raw count
450 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
451 $row = $dbr->fetchObject( $res );
452 $count = $row->count
;
453 $dbr->freeResult( $res );
455 # Halve to remove talk pages if needed
457 $count = floor( $count / 2 );
463 * Allow the user to clear their watchlist
465 * @param $out Output object
466 * @param $request Request object
467 * @param $par Parameters passed to the watchlist page
468 * @return bool True if it's been taken care of; false indicates the watchlist
469 * code needs to do something further
471 function wlHandleClear( &$out, &$request, $par ) {
474 # Check this function has something to do
475 if( $request->getText( 'action' ) == 'clear' ||
$par == 'clear' ) {
477 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
478 $count = wlCountItems( $wgUser );
480 # See if we're clearing or confirming
481 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
482 # Clearing, so do it and report the result
483 $dbw =& wfGetDB( DB_MASTER
);
484 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId
), 'wlHandleClear' );
485 $out->addWikiText( wfMsgExt( 'watchlistcleardone', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
486 $out->returnToMain();
488 # Confirming, so show a form
489 $wlTitle = SpecialPage
::getTitleFor( 'Watchlist' );
490 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
491 $out->addWikiText( wfMsgExt( 'watchlistcount', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
492 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
494 wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
495 wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
496 wfCloseElement( 'form' )
501 # Nothing on the watchlist; nothing to do here
502 $out->addWikiText( wfMsg( 'nowatchlist' ) );
503 $out->returnToMain();