5 * @subpackage SpecialPage
11 require_once( 'SpecialRecentchanges.php' );
16 * @param $par Parameter passed to the page
18 function wfSpecialWatchlist( $par ) {
19 global $wgUser, $wgOut, $wgLang, $wgMemc, $wgRequest, $wgContLang;
20 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
21 global $wgEnotifWatchlist;
22 $fname = 'wfSpecialWatchlist';
24 $skin =& $wgUser->getSkin();
25 $specialTitle = SpecialPage
::getTitleFor( 'Watchlist' );
26 $wgOut->setRobotPolicy( 'noindex,nofollow' );
28 # Anons don't get a watchlist
29 if( $wgUser->isAnon() ) {
30 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
31 $llink = $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Userlogin' ), wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
32 $wgOut->addHtml( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
35 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
36 $wgOut->setSubtitle( wfMsgWikiHtml( 'watchlistfor', htmlspecialchars( $wgUser->getName() ) ) );
39 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
44 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
45 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
46 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
47 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
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' );
58 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
61 $days = $wgRequest->getVal( 'days', $prefs['days'] );
62 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
63 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
64 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
66 # Get namespace value, if supplied, and prepare a WHERE fragment
67 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
68 if( !is_null( $nameSpace ) ) {
69 $nameSpace = intval( $nameSpace );
70 $nameSpaceClause = " AND rc_namespace = $nameSpace";
73 $nameSpaceClause = '';
77 $action = $wgRequest->getVal( 'action' );
78 $remove = $wgRequest->getVal( 'remove' );
79 $id = $wgRequest->getArray( 'id' );
81 $uid = $wgUser->getID();
82 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
83 $wgUser->clearAllNotifications( $uid );
86 # Deleting items from watchlist
87 if(($action == 'submit') && isset($remove) && is_array($id)) {
88 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
89 $wgOut->addHTML( '<p>' );
90 foreach($id as $one) {
91 $t = Title
::newFromURL( $one );
92 if( !is_null( $t ) ) {
93 $wl = WatchedItem
::fromUserTitle( $wgUser, $t );
94 if( $wl->removeWatch() === false ) {
95 $wgOut->addHTML( wfMsg( 'couldntremove', htmlspecialchars($one) ) . "<br />\n" );
97 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
98 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
101 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
104 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
107 $dbr =& wfGetDB( DB_SLAVE
);
108 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
110 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
111 $res = $dbr->query( $sql, $fname );
112 $s = $dbr->fetchObject( $res );
114 # Patch *** A1 *** (see A2 below)
115 # adjust for page X, talk:page X, which are both stored separately, but treated together
116 $nitems = floor($s->n
/ 2);
120 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
124 if( is_null($days) ||
!is_numeric($days) ) {
125 $big = 1000; /* The magical big */
127 # Set default cutoff shorter
128 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
130 $days = $defaults['days']; # default cutoff for shortlisters
133 $days = floatval($days);
136 // Dump everything here
137 $nondefaults = array();
139 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
140 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
141 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
142 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
143 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
147 $npages = wfMsg( 'watchlistall1' );
149 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
151 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
152 $res = $dbr->query( $sql, $fname );
153 $s = $dbr->fetchObject( $res );
156 $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();
181 if( $dbr->numRows( $res ) > 0 )
182 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
184 $sk = $wgUser->getSkin();
187 while( $s = $dbr->fetchObject( $res ) ) {
188 $list[$s->wl_namespace
][$s->wl_title
] = $s->page_is_redirect
;
191 // TODO: Display a TOC
192 foreach($list as $ns => $titles) {
193 if (Namespace::isTalk($ns))
196 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
197 $wgOut->addHTML( '<ul>' );
198 foreach( $titles as $title => $redir ) {
199 $titleObj = Title
::makeTitle( $ns, $title );
200 if( is_null( $titleObj ) ) {
203 htmlspecialchars( $s->wl_title
) . '" in namespace ' . $s->wl_namespace
. " -->\n"
207 $toolLinks = array();
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 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
234 # If the watchlist is relatively short, it's simplest to zip
235 # down its entirety and then sort the results.
237 # If it's relatively long, it may be worth our while to zip
238 # through the time-sorted page list checking for watched items.
240 # Up estimate of watched items by 15% to compensate for talk pages...
243 $andHideOwn = $hideOwn ?
"AND (rc_user <> $uid)" : '';
244 $andHideBots = $hideBots ?
"AND (rc_bot = 0)" : '';
245 $andHideMinor = $hideMinor ?
'AND rc_minor = 0' : '';
247 # Show watchlist header
249 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
250 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
252 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
253 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
256 # Toggle watchlist content (all recent edits or just the latest)
257 if( $wgUser->getOption( 'extendwatchlist' )) {
259 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
261 $andLatest= 'AND rc_this_oldid=page_latest';
262 $limitWatchlist = '';
265 # TODO: Consider removing the third parameter
266 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
267 $wgLang->formatNum( $npages ), '',
268 $specialTitle->getFullUrl( 'edit=yes' ) );
269 $wgOut->addWikiText( $header );
271 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
272 $wgOut->addHTML( '<form action="' .
273 $specialTitle->escapeLocalUrl() .
274 '" method="post"><input type="submit" name="dummy" value="' .
275 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
276 '" /><input type="hidden" name="reset" value="all" /></form>' .
281 FROM $watchlist,$recentchanges,$page
283 AND wl_namespace=rc_namespace
284 AND wl_title=rc_title
285 AND rc_cur_id=page_id
292 ORDER BY rc_timestamp DESC
295 $res = $dbr->query( $sql, $fname );
296 $numRows = $dbr->numRows( $res );
298 /* Start bottom header */
299 $wgOut->addHTML( "<hr />\n" );
302 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
303 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
304 } elseif($days > 0) {
305 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
306 $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 = SpecialPage
::getTitleFor( 'Watchlist' );
313 $skin = $wgUser->getSkin();
315 # Hide/show bot edits
316 $label = $hideBots ?
wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
317 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
318 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
320 # Hide/show own edits
321 $label = $hideOwn ?
wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
322 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
323 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
325 # Hide/show minor edits
326 $label = $hideMinor ?
wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
327 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
328 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
330 $wgOut->addHTML( implode( ' | ', $links ) );
332 # Form for namespace filtering
333 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
335 $form .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ';
336 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
337 $form .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
338 $form .= Xml
::hidden( 'days', $days );
340 $form .= Xml
::hidden( 'hideOwn', 1 );
342 $form .= Xml
::hidden( 'hideBots', 1 );
344 $form .= Xml
::hidden( 'hideMinor', 1 );
345 $form .= Xml
::closeElement( 'form' );
346 $wgOut->addHtml( $form );
348 # If there's nothing to show, stop here
349 if( $numRows == 0 ) {
350 $wgOut->addWikiText( wfMsgNoTrans( 'watchnochange' ) );
354 /* End bottom header */
356 $list = ChangesList
::newFromUser( $wgUser );
358 $s = $list->beginRecentChangesList();
360 while ( $obj = $dbr->fetchObject( $res ) ) {
362 $rc = RecentChange
::newFromRow( $obj );
363 $rc->counter
= $counter++
;
365 if ( $wgShowUpdatedMarker ) {
366 $updated = $obj->wl_notificationtimestamp
;
368 // Same visual appearance as MW 1.4
372 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
373 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .$dbr->strencode($obj->page_title
). "' AND wl_namespace='{$obj->page_namespace}'" ;
374 $res3 = $dbr->query( $sql3, $fname );
375 $x = $dbr->fetchObject( $res3 );
376 $rc->numberofWatchingusers
= $x->n
;
378 $rc->numberofWatchingusers
= 0;
381 $s .= $list->recentChangesLine( $rc, $updated );
383 $s .= $list->endRecentChangesList();
385 $dbr->freeResult( $res );
386 $wgOut->addHTML( $s );
390 function wlHoursLink( $h, $page, $options = array() ) {
391 global $wgUser, $wgLang, $wgContLang;
392 $sk = $wgUser->getSkin();
393 $s = $sk->makeKnownLink(
394 $wgContLang->specialPage( $page ),
395 $wgLang->formatNum( $h ),
396 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
400 function wlDaysLink( $d, $page, $options = array() ) {
401 global $wgUser, $wgLang, $wgContLang;
402 $sk = $wgUser->getSkin();
403 $s = $sk->makeKnownLink(
404 $wgContLang->specialPage( $page ),
405 ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
406 wfArrayToCGI( array('days' => $d), $options ) );
413 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
414 $hours = array( 1, 2, 6, 12 );
415 $days = array( 1, 3, 7 );
417 foreach( $hours as $h ) {
418 $hours[$i++
] = wlHoursLink( $h, $page, $options );
421 foreach( $days as $d ) {
422 $days[$i++
] = wlDaysLink( $d, $page, $options );
424 return wfMsgExt('wlshowlast',
425 array('parseinline', 'replaceafter'),
426 implode(' | ', $hours),
427 implode(' | ', $days),
428 wlDaysLink( 0, $page, $options ) );
432 * Count the number of items on a user's watchlist
434 * @param $talk Include talk pages
437 function wlCountItems( &$user, $talk = true ) {
438 $dbr =& wfGetDB( DB_SLAVE
);
440 # Fetch the raw count
441 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
442 $row = $dbr->fetchObject( $res );
443 $count = $row->count
;
444 $dbr->freeResult( $res );
446 # Halve to remove talk pages if needed
448 $count = floor( $count / 2 );
454 * Allow the user to clear their watchlist
456 * @param $out Output object
457 * @param $request Request object
458 * @param $par Parameters passed to the watchlist page
459 * @return bool True if it's been taken care of; false indicates the watchlist
460 * code needs to do something further
462 function wlHandleClear( &$out, &$request, $par ) {
465 # Check this function has something to do
466 if( $request->getText( 'action' ) == 'clear' ||
$par == 'clear' ) {
468 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
469 $count = wlCountItems( $wgUser );
471 # See if we're clearing or confirming
472 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
473 # Clearing, so do it and report the result
474 $dbw =& wfGetDB( DB_MASTER
);
475 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId
), 'wlHandleClear' );
476 $out->addWikiText( wfMsgExt( 'watchlistcleardone', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
477 $out->returnToMain();
479 # Confirming, so show a form
480 $wlTitle = SpecialPage
::getTitleFor( 'Watchlist' );
481 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
482 $out->addWikiText( wfMsgExt( 'watchlistcount', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
483 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
485 wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
486 wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
487 wfCloseElement( 'form' )
492 # Nothing on the watchlist; nothing to do here
493 $out->addWikiText( wfMsg( 'nowatchlist' ) );
494 $out->returnToMain();