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