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