4ed12304fab14a2bd89d03eb58292c98fe30a657
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 $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 = Linker
::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 /* ? */ 'namespace' => 'all',
52 # Extract variables from the request, falling back to user preferences or
53 # other default values if these don't exist
54 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
55 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
56 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
59 $days = $wgRequest->getVal( 'days', $prefs['days'] );
60 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
61 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
63 # Get namespace value, if supplied, and prepare a WHERE fragment
64 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
65 if( !is_null( $nameSpace ) ) {
66 $nameSpace = intval( $nameSpace );
67 $nameSpaceClause = " AND rc_namespace = $nameSpace";
70 $nameSpaceClause = '';
74 $action = $wgRequest->getVal( 'action' );
75 $remove = $wgRequest->getVal( 'remove' );
76 $id = $wgRequest->getArray( 'id' );
78 $uid = $wgUser->getID();
79 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
80 $wgUser->clearAllNotifications( $uid );
83 # Deleting items from watchlist
84 if(($action == 'submit') && isset($remove) && is_array($id)) {
85 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
86 $wgOut->addHTML( '<p>' );
87 foreach($id as $one) {
88 $t = Title
::newFromURL( $one );
89 if( !is_null( $t ) ) {
90 $wl = WatchedItem
::fromUserTitle( $wgUser, $t );
91 if( $wl->removeWatch() === false ) {
92 $wgOut->addHTML( wfMsg( 'couldntremove', htmlspecialchars($one) ) . "<br />\n" );
94 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
95 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
98 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
101 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
104 if ( $wgUseWatchlistCache ) {
105 $memckey = wfMemcKey( 'watchlist', 'id', $wgUser->getId() );
106 $cache_s = @$wgMemc->get( $memckey );
108 $wgOut->addWikiText( wfMsg('wlsaved') );
109 $wgOut->addHTML( $cache_s );
114 $dbr =& wfGetDB( DB_SLAVE
);
115 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
117 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
118 $res = $dbr->query( $sql, $fname );
119 $s = $dbr->fetchObject( $res );
121 # Patch *** A1 *** (see A2 below)
122 # adjust for page X, talk:page X, which are both stored separately, but treated together
123 $nitems = floor($s->n
/ 2);
127 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
131 if( is_null($days) ||
!is_numeric($days) ) {
132 $big = 1000; /* The magical big */
134 # Set default cutoff shorter
135 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
137 $days = $defaults['days']; # default cutoff for shortlisters
140 $days = floatval($days);
143 // Dump everything here
144 $nondefaults = array();
146 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
147 wfAppendToArrayIfNotDefault( 'hideOwn', (int)$hideOwn, $defaults, $nondefaults);
148 wfAppendToArrayIfNotDefault( 'hideBots', (int)$hideBots, $defaults, $nondefaults);
149 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
154 $npages = wfMsg( 'watchlistall1' );
156 $docutoff = "AND rev_timestamp > '" .
157 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
160 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
161 $res = $dbr->query( $sql, $fname );
162 $s = $dbr->fetchObject( $res );
165 $npages = 40000 * $days;
169 /* Edit watchlist form */
170 if($wgRequest->getBool('edit') ||
$par == 'edit' ) {
171 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
172 "\n\n" . wfMsg( 'watcheditlist' ) );
174 $wgOut->addHTML( '<form action=\'' .
175 $specialTitle->escapeLocalUrl( 'action=submit' ) .
176 "' method='post'>\n" );
179 # The following was proposed by KTurner 07.11.2004 to T.Gries
180 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
181 $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";
183 $res = $dbr->query( $sql, $fname );
185 # Batch existence check
186 $linkBatch = new LinkBatch();
187 while( $row = $dbr->fetchObject( $res ) )
188 $linkBatch->addObj( Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
) );
189 $linkBatch->execute();
190 if( $dbr->numRows( $res ) > 0 )
191 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
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 = Linker
::makeLinkObj( $titleObj );
217 $toolLinks[] = Linker
::makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK
) );
218 if( $titleObj->exists() )
219 $toolLinks[] = Linker
::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 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
242 # If the watchlist is relatively short, it's simplest to zip
243 # down its entirety and then sort the results.
245 # If it's relatively long, it may be worth our while to zip
246 # through the time-sorted page list checking for watched items.
248 # Up estimate of watched items by 15% to compensate for talk pages...
251 $andHideOwn = $hideOwn ?
"AND (rc_user <> $uid)" : '';
252 $andHideBots = $hideBots ?
"AND (rc_bot = 0)" : '';
254 # Show watchlist header
256 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
257 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
259 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
260 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
263 # Toggle watchlist content (all recent edits or just the latest)
264 if( $wgUser->getOption( 'extendwatchlist' )) {
266 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
268 $andLatest= 'AND rc_this_oldid=page_latest';
269 $limitWatchlist = '';
272 # TODO: Consider removing the third parameter
273 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
274 $wgLang->formatNum( $npages ), '',
275 $specialTitle->getFullUrl( 'edit=yes' ) );
276 $wgOut->addWikiText( $header );
278 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
279 $wgOut->addHTML( '<form action="' .
280 $specialTitle->escapeLocalUrl() .
281 '" method="post"><input type="submit" name="dummy" value="' .
282 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
283 '" /><input type="hidden" name="reset" value="all" /></form>' .
288 rc_namespace AS page_namespace, rc_title AS page_title,
289 rc_comment AS rev_comment, rc_cur_id AS page_id,
290 rc_user AS rev_user, rc_user_text AS rev_user_text,
291 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
292 rc_this_oldid AS rev_id,
293 rc_last_oldid, rc_id, rc_patrolled,
294 rc_new AS page_is_new,wl_notificationtimestamp
295 FROM $watchlist,$recentchanges,$page
297 AND wl_namespace=rc_namespace
298 AND wl_title=rc_title
299 AND rc_timestamp > '$cutoff'
300 AND rc_cur_id=page_id
305 ORDER BY rc_timestamp DESC
308 $res = $dbr->query( $sql, $fname );
309 $numRows = $dbr->numRows( $res );
311 /* Start bottom header */
312 $wgOut->addHTML( "<hr />\n" );
315 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
316 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
317 } elseif($days > 0) {
318 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
319 $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 = SpecialPage
::getTitleFor( 'Watchlist' );
326 $linkElements = array( 'hideOwn' => 'wlhideshowown', 'hideBots' => 'wlhideshowbots' );
328 # Problems encountered using the fancier method
329 $label = $hideBots ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
330 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
331 $link = Linker
::makeKnownLinkObj( $thisTitle, $label, $linkBits );
332 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
334 $label = $hideOwn ?
wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
335 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
336 $link = Linker
::makeKnownLinkObj( $thisTitle, $label, $linkBits );
337 $links[] = wfMsgHtml( 'wlhideshowown', $link );
339 $wgOut->addHTML( implode( ' | ', $links ) );
341 # Form for namespace filtering
342 $wgOut->addHTML( "\n" .
343 wfOpenElement( 'form', array(
345 'action' => $thisTitle->getLocalURL(),
347 wfMsgExt( 'namespace', array( 'parseinline') ) .
348 HTMLnamespaceselector( $nameSpace, '' ) . "\n" .
349 ( $hideOwn ?
wfHidden('hideown', 1)."\n" : '' ) .
350 ( $hideBots ?
wfHidden('hidebots', 1)."\n" : '' ) .
351 wfHidden( 'days', $days ) . "\n" .
352 wfSubmitButton( wfMsgExt( 'allpagessubmit', array( 'escape') ) ) . "\n" .
353 wfCloseElement( 'form' ) . "\n"
356 if ( $numRows == 0 ) {
357 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
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 $wgLang, $wgContLang;
403 $s = Linker
::makeKnownLink(
404 $wgContLang->specialPage( $page ),
405 $wgLang->formatNum( $h ),
406 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
410 function wlDaysLink( $d, $page, $options = array() ) {
411 global $wgLang, $wgContLang;
412 $s = Linker
::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 );
427 foreach( $hours as $h ) {
428 $hours[$i++
] = wlHoursLink( $h, $page, $options );
431 foreach( $days as $d ) {
432 $days[$i++
] = wlDaysLink( $d, $page, $options );
434 return wfMsgExt('wlshowlast',
435 array('parseinline', 'replaceafter'),
436 implode(' | ', $hours),
437 implode(' | ', $days),
438 wlDaysLink( 0, $page, $options ) );
442 * Count the number of items on a user's watchlist
444 * @param $talk Include talk pages
447 function wlCountItems( &$user, $talk = true ) {
448 $dbr =& wfGetDB( DB_SLAVE
);
450 # Fetch the raw count
451 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId
), 'wlCountItems' );
452 $row = $dbr->fetchObject( $res );
453 $count = $row->count
;
454 $dbr->freeResult( $res );
456 # Halve to remove talk pages if needed
458 $count = floor( $count / 2 );
464 * Allow the user to clear their watchlist
466 * @param $out Output object
467 * @param $request Request object
468 * @param $par Parameters passed to the watchlist page
469 * @return bool True if it's been taken care of; false indicates the watchlist
470 * code needs to do something further
472 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( wfMsgExt( 'watchlistcleardone', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
487 $out->returnToMain();
489 # Confirming, so show a form
490 $wlTitle = SpecialPage
::getTitleFor( 'Watchlist' );
491 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
492 $out->addWikiText( wfMsgExt( 'watchlistcount', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
493 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
495 wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
496 wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
497 wfCloseElement( 'form' )
502 # Nothing on the watchlist; nothing to do here
503 $out->addWikiText( wfMsg( 'nowatchlist' ) );
504 $out->returnToMain();