(bug 4610) Indicate patrolled status on watchlists and allow users to mark changes...
[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 require_once( 'WatchedItem.php' );
13
14 /**
15 * Constructor
16 * @todo Document $par parameter.
17 * @param $par String: FIXME
18 */
19 function wfSpecialWatchlist( $par ) {
20 global $wgUser, $wgOut, $wgLang, $wgMemc, $wgRequest, $wgContLang;
21 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname;
22 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
23 global $wgEnotifWatchlist;
24 $fname = 'wfSpecialWatchlist';
25
26 $wgOut->setPagetitle( wfMsg( 'watchlist' ) );
27 $sub = htmlspecialchars( wfMsg( 'watchlistsub', $wgUser->getName() ) );
28 $wgOut->setSubtitle( $sub );
29 $wgOut->setRobotpolicy( 'noindex,nofollow' );
30
31 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
32
33 if( $wgUser->isAnon() ) {
34 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
35 return;
36 }
37
38 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
39 return;
40 }
41
42 $defaults = array(
43 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
44 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
45 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
46 /* ? */ 'namespace' => 'all',
47 );
48
49 extract($defaults);
50
51 # Extract variables from the request, falling back to user preferences or
52 # other default values if these don't exist
53 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
54 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
55 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
56
57 # Get query variables
58 $days = $wgRequest->getVal( 'days', $prefs['days'] );
59 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
60 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
61
62 # Get namespace value, if supplied, and prepare a WHERE fragment
63 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
64 if( !is_null( $nameSpace ) ) {
65 $nameSpace = intval( $nameSpace );
66 $nameSpaceClause = " AND rc_namespace = $nameSpace";
67 } else {
68 $nameSpace = '';
69 $nameSpaceClause = '';
70 }
71
72 # Watchlist editing
73 $action = $wgRequest->getVal( 'action' );
74 $remove = $wgRequest->getVal( 'remove' );
75 $id = $wgRequest->getArray( 'id' );
76
77 $uid = $wgUser->getID();
78 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
79 $wgUser->clearAllNotifications( $uid );
80 }
81
82 # Deleting items from watchlist
83 if(($action == 'submit') && isset($remove) && is_array($id)) {
84 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
85 $wgOut->addHTML( '<p>' );
86 foreach($id as $one) {
87 $t = Title::newFromURL( $one );
88 if( !is_null( $t ) ) {
89 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
90 if( $wl->removeWatch() === false ) {
91 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
92 } else {
93 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
94 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
95 }
96 } else {
97 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
98 }
99 }
100 $wgOut->addHTML( "done.</p>\n" );
101 }
102
103 if ( $wgUseWatchlistCache ) {
104 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
105 $cache_s = @$wgMemc->get( $memckey );
106 if( $cache_s ){
107 $wgOut->addWikiText( wfMsg('wlsaved') );
108 $wgOut->addHTML( $cache_s );
109 return;
110 }
111 }
112
113 $dbr =& wfGetDB( DB_SLAVE );
114 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
115
116 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
117 $res = $dbr->query( $sql, $fname );
118 $s = $dbr->fetchObject( $res );
119
120 # Patch *** A1 *** (see A2 below)
121 # adjust for page X, talk:page X, which are both stored separately, but treated together
122 $nitems = floor($s->n / 2);
123 # $nitems = $s->n;
124
125 if($nitems == 0) {
126 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
127 return;
128 }
129
130 if( is_null($days) || !is_numeric($days) ) {
131 $big = 1000; /* The magical big */
132 if($nitems > $big) {
133 # Set default cutoff shorter
134 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
135 } else {
136 $days = $defaults['days']; # default cutoff for shortlisters
137 }
138 } else {
139 $days = floatval($days);
140 }
141
142 // Dump everything here
143 $nondefaults = array();
144
145 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
146 wfAppendToArrayIfNotDefault( 'hideOwn', (int)$hideOwn, $defaults, $nondefaults);
147 wfAppendToArrayIfNotDefault( 'hideBots', (int)$hideBots, $defaults, $nondefaults);
148 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
149
150 if ( $days <= 0 ) {
151 $docutoff = '';
152 $cutoff = false;
153 $npages = wfMsg( 'watchlistall1' );
154 } else {
155 $docutoff = "AND rev_timestamp > '" .
156 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
157 . "'";
158 /*
159 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
160 $res = $dbr->query( $sql, $fname );
161 $s = $dbr->fetchObject( $res );
162 $npages = $s->n;
163 */
164 $npages = 40000 * $days;
165
166 }
167
168 /* Edit watchlist form */
169 if($wgRequest->getBool('edit') || $par == 'edit' ) {
170 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
171 "\n\n" . wfMsg( 'watcheditlist' ) );
172
173 $wgOut->addHTML( '<form action=\'' .
174 $specialTitle->escapeLocalUrl( 'action=submit' ) .
175 "' method='post'>\n" );
176
177 # Patch A2
178 # The following was proposed by KTurner 07.11.2004 to T.Gries
179 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
180 $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
182 $res = $dbr->query( $sql, $fname );
183
184 # Batch existence check
185 $linkBatch = new LinkBatch();
186 while( $row = $dbr->fetchObject( $res ) )
187 $linkBatch->addObj( Title::makeTitleSafe( $row->wl_namespace, $row->wl_title ) );
188 $linkBatch->execute();
189 if( $dbr->numRows( $res ) > 0 )
190 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
191
192 $sk = $wgUser->getSkin();
193
194 $list = array();
195 while( $s = $dbr->fetchObject( $res ) ) {
196 $list[$s->wl_namespace][$s->wl_title] = $s->page_is_redirect;
197 }
198
199 // TODO: Display a TOC
200 foreach($list as $ns => $titles) {
201 if (Namespace::isTalk($ns))
202 continue;
203 if ($ns != NS_MAIN)
204 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
205 $wgOut->addHTML( '<ul>' );
206 foreach( $titles as $title => $redir ) {
207 $titleObj = Title::makeTitle( $ns, $title );
208 if( is_null( $titleObj ) ) {
209 $wgOut->addHTML(
210 '<!-- bad title "' .
211 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
212 );
213 } else {
214 global $wgContLang;
215 $toolLinks = array();
216 $titleText = $titleObj->getPrefixedText();
217 $pageLink = $sk->makeLinkObj( $titleObj );
218 $toolLinks[] = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK ) );
219 if( $titleObj->exists() )
220 $toolLinks[] = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'history_short' ), 'action=history' );
221 $toolLinks = '(' . implode( ' | ', $toolLinks ) . ')';
222 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ? '&rlm;' : '&lrm;' );
223 if( $redir ) {
224 $spanopen = '<span class="watchlistredir">';
225 $spanclosed = '</span>';
226 } else {
227 $spanopen = $spanclosed = '';
228 }
229
230 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
231 }
232 }
233 $wgOut->addHTML( '</ul>' );
234 }
235 $wgOut->addHTML(
236 "<input type='submit' name='remove' value=\"" .
237 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
238 "</form>\n"
239 );
240
241 return;
242 }
243
244 # If the watchlist is relatively short, it's simplest to zip
245 # down its entirety and then sort the results.
246
247 # If it's relatively long, it may be worth our while to zip
248 # through the time-sorted page list checking for watched items.
249
250 # Up estimate of watched items by 15% to compensate for talk pages...
251
252 # Toggles
253 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
254 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
255
256 # Show watchlist header
257 $header = '';
258 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
259 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
260 }
261 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
262 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
263 }
264
265 # Toggle watchlist content (all recent edits or just the latest)
266 if( $wgUser->getOption( 'extendwatchlist' )) {
267 $andLatest='';
268 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
269 } else {
270 $andLatest= 'AND rc_this_oldid=page_latest';
271 $limitWatchlist = '';
272 }
273
274 # TODO: Consider removing the third parameter
275 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
276 $wgLang->formatNum( $npages ), '',
277 $specialTitle->getFullUrl( 'edit=yes' ) );
278 $wgOut->addWikiText( $header );
279
280 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
281 $wgOut->addHTML( '<form action="' .
282 $specialTitle->escapeLocalUrl() .
283 '" method="post"><input type="submit" name="dummy" value="' .
284 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
285 '" /><input type="hidden" name="reset" value="all" /></form>' .
286 "\n\n" );
287 }
288
289 $sql = "SELECT
290 rc_namespace AS page_namespace, rc_title AS page_title,
291 rc_comment AS rev_comment, rc_cur_id AS page_id,
292 rc_user AS rev_user, rc_user_text AS rev_user_text,
293 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
294 rc_this_oldid AS rev_id,
295 rc_last_oldid, rc_id, rc_patrolled,
296 rc_new AS page_is_new,wl_notificationtimestamp
297 FROM $watchlist,$recentchanges,$page
298 WHERE wl_user=$uid
299 AND wl_namespace=rc_namespace
300 AND wl_title=rc_title
301 AND rc_timestamp > '$cutoff'
302 AND rc_cur_id=page_id
303 $andLatest
304 $andHideOwn
305 $andHideBots
306 $nameSpaceClause
307 ORDER BY rc_timestamp DESC
308 $limitWatchlist";
309
310 $res = $dbr->query( $sql, $fname );
311 $numRows = $dbr->numRows( $res );
312
313 /* Start bottom header */
314 $wgOut->addHTML( "<hr />\n<p>" );
315
316 if($days >= 1)
317 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
318 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
319 elseif($days > 0)
320 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
321 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
322
323 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
324
325 # Spit out some control panel links
326 $thisTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
327 $skin = $wgUser->getSkin();
328 $linkElements = array( 'hideOwn' => 'wlhideshowown', 'hideBots' => 'wlhideshowbots' );
329
330 # Problems encountered using the fancier method
331 $label = $hideBots ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
332 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
333 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
334 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
335
336 $label = $hideOwn ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
337 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
338 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
339 $links[] = wfMsgHtml( 'wlhideshowown', $link );
340
341 $wgOut->addHTML( implode( ' | ', $links ) );
342
343 # Form for namespace filtering
344 $thisAction = $thisTitle->escapeLocalUrl();
345 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
346 $nsForm .= "<label for=\"namespace\">" . wfMsgExt( 'namespace', array( 'parseinline') ) . "</label> ";
347 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
348 $nsForm .= ( $hideOwn ? "<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
349 $nsForm .= ( $hideBots ? "<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
350 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
351 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgExt( 'allpagessubmit', array( 'escape') ) . "\" />\n";
352 $nsForm .= "</form>\n";
353 $wgOut->addHTML( $nsForm );
354
355 if ( $numRows == 0 ) {
356 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
357 $wgOut->addHTML( "</p>\n" );
358 return;
359 }
360
361 $wgOut->addHTML( "</p>\n" );
362 /* End bottom header */
363
364 $list = ChangesList::newFromUser( $wgUser );
365
366 $s = $list->beginRecentChangesList();
367 $counter = 1;
368 while ( $obj = $dbr->fetchObject( $res ) ) {
369 # Make fake RC entry
370 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
371 $rc->counter = $counter++;
372
373 if ( $wgShowUpdatedMarker ) {
374 $updated = $obj->wl_notificationtimestamp;
375 } else {
376 // Same visual appearance as MW 1.4
377 $updated = true;
378 }
379
380 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
381 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
382 $res3 = $dbr->query( $sql3, DB_READ, $fname );
383 $x = $dbr->fetchObject( $res3 );
384 $rc->numberofWatchingusers = $x->n;
385 } else {
386 $rc->numberofWatchingusers = 0;
387 }
388
389 $s .= $list->recentChangesLine( $rc, $updated );
390 }
391 $s .= $list->endRecentChangesList();
392
393 $dbr->freeResult( $res );
394 $wgOut->addHTML( $s );
395
396 if ( $wgUseWatchlistCache ) {
397 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
398 }
399
400 }
401
402 function wlHoursLink( $h, $page, $options = array() ) {
403 global $wgUser, $wgLang, $wgContLang;
404 $sk = $wgUser->getSkin();
405 $s = $sk->makeKnownLink(
406 $wgContLang->specialPage( $page ),
407 $wgLang->formatNum( $h ),
408 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
409 return $s;
410 }
411
412 function wlDaysLink( $d, $page, $options = array() ) {
413 global $wgUser, $wgLang, $wgContLang;
414 $sk = $wgUser->getSkin();
415 $s = $sk->makeKnownLink(
416 $wgContLang->specialPage( $page ),
417 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
418 wfArrayToCGI( array('days' => $d), $options ) );
419 return $s;
420 }
421
422 /**
423 * Returns html
424 */
425 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
426 $hours = array( 1, 2, 6, 12 );
427 $days = array( 1, 3, 7 );
428 $cl = '';
429 $i = 0;
430 foreach( $hours as $h ) {
431 $hours[$i++] = wlHoursLink( $h, $page, $options );
432 }
433 $i = 0;
434 foreach( $days as $d ) {
435 $days[$i++] = wlDaysLink( $d, $page, $options );
436 }
437 return wfMsgExt('wlshowlast',
438 array('parseinline', 'replaceafter'),
439 implode(' | ', $hours),
440 implode(' | ', $days),
441 wlDaysLink( 0, $page, $options ) );
442 }
443
444 /**
445 * Count the number of items on a user's watchlist
446 *
447 * @param $talk Include talk pages
448 * @return integer
449 */
450 function wlCountItems( &$user, $talk = true ) {
451 $dbr =& wfGetDB( DB_SLAVE );
452
453 # Fetch the raw count
454 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
455 $row = $dbr->fetchObject( $res );
456 $count = $row->count;
457 $dbr->freeResult( $res );
458
459 # Halve to remove talk pages if needed
460 if( !$talk )
461 $count = floor( $count / 2 );
462
463 return( $count );
464 }
465
466 /**
467 * Allow the user to clear their watchlist
468 *
469 * @param $out Output object
470 * @param $request Request object
471 * @param $par Parameters passed to the watchlist page
472 * @return bool True if it's been taken care of; false indicates the watchlist
473 * code needs to do something further
474 */
475 function wlHandleClear( &$out, &$request, $par ) {
476 # Check this function has something to do
477 if( $request->getText( 'action' ) == 'clear' || $par == 'clear' ) {
478 global $wgUser;
479 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
480 $count = wlCountItems( $wgUser );
481 if( $count > 0 ) {
482 # See if we're clearing or confirming
483 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
484 # Clearing, so do it and report the result
485 $dbw =& wfGetDB( DB_MASTER );
486 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId ), 'wlHandleClear' );
487 $out->addWikiText( wfMsg( 'watchlistcleardone', $count ) );
488 $out->returnToMain();
489 } else {
490 # Confirming, so show a form
491 $wlTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
492 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
493 $out->addWikiText( wfMsg( 'watchlistcount', $count ) );
494 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
495 $out->addHTML( wfElement( 'input', array( 'type' => 'hidden', 'name' => 'token', 'value' => $wgUser->editToken( 'clearwatchlist' ) ), '' ) );
496 $out->addHTML( wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) );
497 $out->addHTML( wfCloseElement( 'form' ) );
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
511 ?>