2ad6c9f42684597a744a5ddf56fda797797890c3
[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 AND rc_this_oldid=page_latest
292 $andHideOwn
293 $andHideBots
294 $nameSpaceClause
295 ORDER BY rc_timestamp DESC";
296
297 $res = $dbr->query( $sql, $fname );
298 $numRows = $dbr->numRows( $res );
299
300 /* Start bottom header */
301 $wgOut->addHTML( "<hr />\n<p>" );
302
303 if($days >= 1)
304 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
305 $wgLang->formatNum( $days ) ) . '<br />' , false );
306 elseif($days > 0)
307 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
308 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
309
310 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
311
312 # Spit out some control panel links
313 $thisTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
314 $skin = $wgUser->getSkin();
315 $linkElements = array( 'hideOwn' => 'wlhideshowown', 'hideBots' => 'wlhideshowbots' );
316
317 # Problems encountered using the fancier method
318 $label = $hideBots ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
319 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
320 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
321 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
322
323 $label = $hideOwn ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
324 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
325 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
326 $links[] = wfMsgHtml( 'wlhideshowown', $link );
327
328 $wgOut->addHTML( implode( ' | ', $links ) );
329
330 # Form for namespace filtering
331 $thisAction = $thisTitle->escapeLocalUrl();
332 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
333 $nsForm .= "<label for=\"namespace\">" . wfMsg( 'namespace' ) . "</label> ";
334 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
335 $nsForm .= ( $hideOwn ? "<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
336 $nsForm .= ( $hideBots ? "<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
337 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
338 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
339 $nsForm .= "</form>\n";
340 $wgOut->addHTML( $nsForm );
341
342 if ( $numRows == 0 ) {
343 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
344 $wgOut->addHTML( "</p>\n" );
345 return;
346 }
347
348 $wgOut->addHTML( "</p>\n" );
349 /* End bottom header */
350
351 $list = ChangesList::newFromUser( $wgUser );
352
353 $s = $list->beginRecentChangesList();
354 $counter = 1;
355 while ( $obj = $dbr->fetchObject( $res ) ) {
356 # Make fake RC entry
357 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
358 $rc->counter = $counter++;
359
360 if ( $wgShowUpdatedMarker ) {
361 $updated = $obj->wl_notificationtimestamp;
362 } else {
363 // Same visual appearance as MW 1.4
364 $updated = true;
365 }
366
367 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
368 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
369 $res3 = $dbr->query( $sql3, DB_READ, $fname );
370 $x = $dbr->fetchObject( $res3 );
371 $rc->numberofWatchingusers = $x->n;
372 } else {
373 $rc->numberofWatchingusers = 0;
374 }
375
376 $s .= $list->recentChangesLine( $rc, $updated );
377 }
378 $s .= $list->endRecentChangesList();
379
380 $dbr->freeResult( $res );
381 $wgOut->addHTML( $s );
382
383 if ( $wgUseWatchlistCache ) {
384 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
385 }
386
387 }
388
389 function wlHoursLink( $h, $page, $options = array() ) {
390 global $wgUser, $wgLang, $wgContLang;
391 $sk = $wgUser->getSkin();
392 $s = $sk->makeKnownLink(
393 $wgContLang->specialPage( $page ),
394 $wgLang->formatNum( $h ),
395 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
396 return $s;
397 }
398
399 function wlDaysLink( $d, $page, $options = array() ) {
400 global $wgUser, $wgLang, $wgContLang;
401 $sk = $wgUser->getSkin();
402 $s = $sk->makeKnownLink(
403 $wgContLang->specialPage( $page ),
404 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
405 wfArrayToCGI( array('days' => $d), $options ) );
406 return $s;
407 }
408
409 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
410 $hours = array( 1, 2, 6, 12 );
411 $days = array( 1, 3, 7 );
412 $cl = '';
413 $i = 0;
414 foreach( $hours as $h ) {
415 $hours[$i++] = wlHoursLink( $h, $page, $options );
416 }
417 $i = 0;
418 foreach( $days as $d ) {
419 $days[$i++] = wlDaysLink( $d, $page, $options );
420 }
421 return wfMsg ('wlshowlast',
422 implode(' | ', $hours),
423 implode(' | ', $days),
424 wlDaysLink( 0, $page, $options ) );
425 }
426
427 /**
428 * Count the number of items on a user's watchlist
429 *
430 * @param $talk Include talk pages
431 * @return integer
432 */
433 function wlCountItems( &$user, $talk = true ) {
434 $dbr =& wfGetDB( DB_SLAVE );
435
436 # Fetch the raw count
437 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
438 $row = $dbr->fetchObject( $res );
439 $count = $row->count;
440 $dbr->freeResult( $res );
441
442 # Halve to remove talk pages if needed
443 if( !$talk )
444 $count = floor( $count / 2 );
445
446 return( $count );
447 }
448
449 /**
450 * Allow the user to clear their watchlist
451 *
452 * @param $out Output object
453 * @param $request Request object
454 * @param $par Parameters passed to the watchlist page
455 * @return bool True if it's been taken care of; false indicates the watchlist
456 * code needs to do something further
457 */
458 function wlHandleClear( &$out, &$request, $par ) {
459 # Check this function has something to do
460 if( $request->getText( 'action' ) == 'clear' || $par == 'clear' ) {
461 global $wgUser;
462 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
463 $count = wlCountItems( $wgUser );
464 if( $count > 0 ) {
465 # See if we're clearing or confirming
466 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
467 # Clearing, so do it and report the result
468 $dbw =& wfGetDB( DB_MASTER );
469 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId ), 'wlHandleClear' );
470 $out->addWikiText( wfMsg( 'watchlistcleardone', $count ) );
471 $out->returnToMain();
472 } else {
473 # Confirming, so show a form
474 $wlTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
475 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
476 $out->addWikiText( wfMsg( 'watchlistcount', $count ) );
477 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
478 $out->addHTML( wfElement( 'input', array( 'type' => 'hidden', 'name' => 'token', 'value' => $wgUser->editToken( 'clearwatchlist' ) ), '' ) );
479 $out->addHTML( wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) );
480 $out->addHTML( wfCloseElement( 'form' ) );
481 }
482 return( true );
483 } else {
484 # Nothing on the watchlist; nothing to do here
485 $out->addWikiText( wfMsg( 'nowatchlist' ) );
486 $out->returnToMain();
487 return( true );
488 }
489 } else {
490 return( false );
491 }
492 }
493
494 ?>