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