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