Changing lines like this: "extract( $dbw->tableNames( 'page', 'archive' ) );" to...
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once( 'SpecialRecentchanges.php' );
12
13 /**
14 * Constructor
15 * @todo Document $par parameter.
16 * @param $par String: FIXME
17 */
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';
24
25 $skin =& $wgUser->getSkin();
26 $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
27 $wgOut->setRobotPolicy( 'noindex,nofollow' );
28
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 ) );
34 return;
35 } else {
36 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
37 $wgOut->setSubtitle( wfMsgWikiHtml( 'watchlistfor', htmlspecialchars( $wgUser->getName() ) ) );
38 }
39
40 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
41 return;
42 }
43
44 $defaults = array(
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',
49 );
50
51 extract($defaults);
52
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
59 # Get query variables
60 $days = $wgRequest->getVal( 'days', $prefs['days'] );
61 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
62 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
63
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";
69 } else {
70 $nameSpace = '';
71 $nameSpaceClause = '';
72 }
73
74 # Watchlist editing
75 $action = $wgRequest->getVal( 'action' );
76 $remove = $wgRequest->getVal( 'remove' );
77 $id = $wgRequest->getArray( 'id' );
78
79 $uid = $wgUser->getID();
80 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
81 $wgUser->clearAllNotifications( $uid );
82 }
83
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" );
94 } else {
95 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
96 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
97 }
98 } else {
99 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
100 }
101 }
102 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
103 }
104
105 if ( $wgUseWatchlistCache ) {
106 $memckey = wfMemcKey( 'watchlist', 'id', $wgUser->getId() );
107 $cache_s = @$wgMemc->get( $memckey );
108 if( $cache_s ){
109 $wgOut->addWikiText( wfMsg('wlsaved') );
110 $wgOut->addHTML( $cache_s );
111 return;
112 }
113 }
114
115 $dbr =& wfGetDB( DB_SLAVE );
116 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
117
118 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
119 $res = $dbr->query( $sql, $fname );
120 $s = $dbr->fetchObject( $res );
121
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);
125 # $nitems = $s->n;
126
127 if($nitems == 0) {
128 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
129 return;
130 }
131
132 if( is_null($days) || !is_numeric($days) ) {
133 $big = 1000; /* The magical big */
134 if($nitems > $big) {
135 # Set default cutoff shorter
136 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
137 } else {
138 $days = $defaults['days']; # default cutoff for shortlisters
139 }
140 } else {
141 $days = floatval($days);
142 }
143
144 // Dump everything here
145 $nondefaults = array();
146
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 );
151
152 if ( $days <= 0 ) {
153 $cutoff = false;
154 $npages = wfMsg( 'watchlistall1' );
155 } else {
156 $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) );
157 /*
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 );
161 $npages = $s->n;
162 */
163 $npages = 40000 * $days;
164 }
165
166 /* Edit watchlist form */
167 if($wgRequest->getBool('edit') || $par == 'edit' ) {
168 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
169 "\n\n" . wfMsg( 'watcheditlist' ) );
170
171 $wgOut->addHTML( '<form action=\'' .
172 $specialTitle->escapeLocalUrl( 'action=submit' ) .
173 "' method='post'>\n" );
174
175 # Patch A2
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";
179
180 $res = $dbr->query( $sql, $fname );
181
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!
189
190 $sk = $wgUser->getSkin();
191
192 $list = array();
193 while( $s = $dbr->fetchObject( $res ) ) {
194 $list[$s->wl_namespace][$s->wl_title] = $s->page_is_redirect;
195 }
196
197 // TODO: Display a TOC
198 foreach($list as $ns => $titles) {
199 if (Namespace::isTalk($ns))
200 continue;
201 if ($ns != NS_MAIN)
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 ) ) {
207 $wgOut->addHTML(
208 '<!-- bad title "' .
209 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
210 );
211 } else {
212 global $wgContLang;
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() ? '&rlm;' : '&lrm;' );
220 if( $redir ) {
221 $spanopen = '<span class="watchlistredir">';
222 $spanclosed = '</span>';
223 } else {
224 $spanopen = $spanclosed = '';
225 }
226
227 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
228 }
229 }
230 $wgOut->addHTML( '</ul>' );
231 }
232 $wgOut->addHTML(
233 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
234 "\n</form>\n"
235 );
236
237 return;
238 }
239
240 # If the watchlist is relatively short, it's simplest to zip
241 # down its entirety and then sort the results.
242
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.
245
246 # Up estimate of watched items by 15% to compensate for talk pages...
247
248 # Toggles
249 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
250 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
251
252 # Show watchlist header
253 $header = '';
254 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
255 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
256 }
257 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
258 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
259 }
260
261 # Toggle watchlist content (all recent edits or just the latest)
262 if( $wgUser->getOption( 'extendwatchlist' )) {
263 $andLatest='';
264 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
265 } else {
266 $andLatest= 'AND rc_this_oldid=page_latest';
267 $limitWatchlist = '';
268 }
269
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 );
275
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>' .
282 "\n\n" );
283 }
284
285 $sql = "SELECT
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
294 WHERE wl_user=$uid
295 AND wl_namespace=rc_namespace
296 AND wl_title=rc_title
297 AND rc_timestamp > '$cutoff'
298 AND rc_cur_id=page_id
299 $andLatest
300 $andHideOwn
301 $andHideBots
302 $nameSpaceClause
303 ORDER BY rc_timestamp DESC
304 $limitWatchlist";
305
306 $res = $dbr->query( $sql, $fname );
307 $numRows = $dbr->numRows( $res );
308
309 /* Start bottom header */
310 $wgOut->addHTML( "<hr />\n" );
311
312 if($days >= 1) {
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 );
318 }
319
320 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
321
322 # Spit out some control panel links
323 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
324 $skin = $wgUser->getSkin();
325
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 );
331
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 );
336
337 $wgOut->addHTML( implode( ' | ', $links ) );
338
339 # Form for namespace filtering
340 $wgOut->addHTML( "\n" .
341 wfOpenElement( 'form', array(
342 'method' => 'post',
343 'action' => $thisTitle->getLocalURL(),
344 ) ) .
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"
352 );
353
354 if ( $numRows == 0 ) {
355 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
356 return;
357 }
358
359 /* End bottom header */
360
361 $list = ChangesList::newFromUser( $wgUser );
362
363 $s = $list->beginRecentChangesList();
364 $counter = 1;
365 while ( $obj = $dbr->fetchObject( $res ) ) {
366 # Make fake RC entry
367 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
368 $rc->counter = $counter++;
369
370 if ( $wgShowUpdatedMarker ) {
371 $updated = $obj->wl_notificationtimestamp;
372 } else {
373 // Same visual appearance as MW 1.4
374 $updated = true;
375 }
376
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;
382 } else {
383 $rc->numberofWatchingusers = 0;
384 }
385
386 $s .= $list->recentChangesLine( $rc, $updated );
387 }
388 $s .= $list->endRecentChangesList();
389
390 $dbr->freeResult( $res );
391 $wgOut->addHTML( $s );
392
393 if ( $wgUseWatchlistCache ) {
394 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
395 }
396
397 }
398
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 ) );
406 return $s;
407 }
408
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 ) );
416 return $s;
417 }
418
419 /**
420 * Returns html
421 */
422 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
423 $hours = array( 1, 2, 6, 12 );
424 $days = array( 1, 3, 7 );
425 $i = 0;
426 foreach( $hours as $h ) {
427 $hours[$i++] = wlHoursLink( $h, $page, $options );
428 }
429 $i = 0;
430 foreach( $days as $d ) {
431 $days[$i++] = wlDaysLink( $d, $page, $options );
432 }
433 return wfMsgExt('wlshowlast',
434 array('parseinline', 'replaceafter'),
435 implode(' | ', $hours),
436 implode(' | ', $days),
437 wlDaysLink( 0, $page, $options ) );
438 }
439
440 /**
441 * Count the number of items on a user's watchlist
442 *
443 * @param $talk Include talk pages
444 * @return integer
445 */
446 function wlCountItems( &$user, $talk = true ) {
447 $dbr =& wfGetDB( DB_SLAVE );
448
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 );
454
455 # Halve to remove talk pages if needed
456 if( !$talk )
457 $count = floor( $count / 2 );
458
459 return( $count );
460 }
461
462 /**
463 * Allow the user to clear their watchlist
464 *
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
470 */
471 function wlHandleClear( &$out, &$request, $par ) {
472 global $wgLang;
473
474 # Check this function has something to do
475 if( $request->getText( 'action' ) == 'clear' || $par == 'clear' ) {
476 global $wgUser;
477 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
478 $count = wlCountItems( $wgUser );
479 if( $count > 0 ) {
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();
487 } else {
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' ) );
493 $out->addHTML(
494 wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
495 wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
496 wfCloseElement( 'form' )
497 );
498 }
499 return( true );
500 } else {
501 # Nothing on the watchlist; nothing to do here
502 $out->addWikiText( wfMsg( 'nowatchlist' ) );
503 $out->returnToMain();
504 return( true );
505 }
506 } else {
507 return( false );
508 }
509 }
510 ?>