Autoload WatchedItem
[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
13 /**
14 * Constructor
15 * @todo Document $par parameter.
16 * @param $par String: FIXME
17 */
18 function wfSpecialWatchlist( $par ) {
19 global $wgUser, $wgOut, $wgLang, $wgMemc, $wgRequest, $wgContLang;
20 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname;
21 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
22 global $wgEnotifWatchlist;
23 $fname = 'wfSpecialWatchlist';
24
25 $wgOut->setPagetitle( wfMsg( 'watchlist' ) );
26 $sub = htmlspecialchars( wfMsg( 'watchlistsub', $wgUser->getName() ) );
27 $wgOut->setSubtitle( $sub );
28 $wgOut->setRobotpolicy( 'noindex,nofollow' );
29
30 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
31
32 if( $wgUser->isAnon() ) {
33 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
34 return;
35 }
36
37 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
38 return;
39 }
40
41 $defaults = array(
42 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
43 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
44 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
45 /* ? */ 'namespace' => 'all',
46 );
47
48 extract($defaults);
49
50 # Extract variables from the request, falling back to user preferences or
51 # other default values if these don't exist
52 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
53 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
54 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
55
56 # Get query variables
57 $days = $wgRequest->getVal( 'days', $prefs['days'] );
58 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
59 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
60
61 # Get namespace value, if supplied, and prepare a WHERE fragment
62 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
63 if( !is_null( $nameSpace ) ) {
64 $nameSpace = intval( $nameSpace );
65 $nameSpaceClause = " AND rc_namespace = $nameSpace";
66 } else {
67 $nameSpace = '';
68 $nameSpaceClause = '';
69 }
70
71 # Watchlist editing
72 $action = $wgRequest->getVal( 'action' );
73 $remove = $wgRequest->getVal( 'remove' );
74 $id = $wgRequest->getArray( 'id' );
75
76 $uid = $wgUser->getID();
77 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
78 $wgUser->clearAllNotifications( $uid );
79 }
80
81 # Deleting items from watchlist
82 if(($action == 'submit') && isset($remove) && is_array($id)) {
83 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
84 $wgOut->addHTML( '<p>' );
85 foreach($id as $one) {
86 $t = Title::newFromURL( $one );
87 if( !is_null( $t ) ) {
88 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
89 if( $wl->removeWatch() === false ) {
90 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
91 } else {
92 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
93 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
94 }
95 } else {
96 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
97 }
98 }
99 $wgOut->addHTML( "<br />\n" . wfMsg( 'wldone' ) . "</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', (int)$hideOwn, $defaults, $nondefaults);
146 wfAppendToArrayIfNotDefault( 'hideBots', (int)$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 # Toggle watchlist content (all recent edits or just the latest)
265 if( $wgUser->getOption( 'extendwatchlist' )) {
266 $andLatest='';
267 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
268 } else {
269 $andLatest= 'AND rc_this_oldid=page_latest';
270 $limitWatchlist = '';
271 }
272
273 # TODO: Consider removing the third parameter
274 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
275 $wgLang->formatNum( $npages ), '',
276 $specialTitle->getFullUrl( 'edit=yes' ) );
277 $wgOut->addWikiText( $header );
278
279 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
280 $wgOut->addHTML( '<form action="' .
281 $specialTitle->escapeLocalUrl() .
282 '" method="post"><input type="submit" name="dummy" value="' .
283 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
284 '" /><input type="hidden" name="reset" value="all" /></form>' .
285 "\n\n" );
286 }
287
288 $sql = "SELECT
289 rc_namespace AS page_namespace, rc_title AS page_title,
290 rc_comment AS rev_comment, rc_cur_id AS page_id,
291 rc_user AS rev_user, rc_user_text AS rev_user_text,
292 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
293 rc_this_oldid AS rev_id,
294 rc_last_oldid, rc_id, rc_patrolled,
295 rc_new AS page_is_new,wl_notificationtimestamp
296 FROM $watchlist,$recentchanges,$page
297 WHERE wl_user=$uid
298 AND wl_namespace=rc_namespace
299 AND wl_title=rc_title
300 AND rc_timestamp > '$cutoff'
301 AND rc_cur_id=page_id
302 $andLatest
303 $andHideOwn
304 $andHideBots
305 $nameSpaceClause
306 ORDER BY rc_timestamp DESC
307 $limitWatchlist";
308
309 $res = $dbr->query( $sql, $fname );
310 $numRows = $dbr->numRows( $res );
311
312 /* Start bottom header */
313 $wgOut->addHTML( "<hr />\n<p>" );
314
315 if($days >= 1)
316 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
317 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
318 elseif($days > 0)
319 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
320 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
321
322 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
323
324 # Spit out some control panel links
325 $thisTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
326 $skin = $wgUser->getSkin();
327 $linkElements = array( 'hideOwn' => 'wlhideshowown', 'hideBots' => 'wlhideshowbots' );
328
329 # Problems encountered using the fancier method
330 $label = $hideBots ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
331 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
332 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
333 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
334
335 $label = $hideOwn ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
336 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
337 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
338 $links[] = wfMsgHtml( 'wlhideshowown', $link );
339
340 $wgOut->addHTML( implode( ' | ', $links ) );
341
342 # Form for namespace filtering
343 $thisAction = $thisTitle->escapeLocalUrl();
344 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
345 $nsForm .= "<label for=\"namespace\">" . wfMsgExt( 'namespace', array( 'parseinline') ) . "</label> ";
346 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
347 $nsForm .= ( $hideOwn ? "<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
348 $nsForm .= ( $hideBots ? "<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
349 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
350 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgExt( 'allpagessubmit', array( 'escape') ) . "\" />\n";
351 $nsForm .= "</form>\n";
352 $wgOut->addHTML( $nsForm );
353
354 if ( $numRows == 0 ) {
355 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
356 $wgOut->addHTML( "</p>\n" );
357 return;
358 }
359
360 $wgOut->addHTML( "</p>\n" );
361 /* End bottom header */
362
363 $list = ChangesList::newFromUser( $wgUser );
364
365 $s = $list->beginRecentChangesList();
366 $counter = 1;
367 while ( $obj = $dbr->fetchObject( $res ) ) {
368 # Make fake RC entry
369 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
370 $rc->counter = $counter++;
371
372 if ( $wgShowUpdatedMarker ) {
373 $updated = $obj->wl_notificationtimestamp;
374 } else {
375 // Same visual appearance as MW 1.4
376 $updated = true;
377 }
378
379 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
380 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
381 $res3 = $dbr->query( $sql3, DB_READ, $fname );
382 $x = $dbr->fetchObject( $res3 );
383 $rc->numberofWatchingusers = $x->n;
384 } else {
385 $rc->numberofWatchingusers = 0;
386 }
387
388 $s .= $list->recentChangesLine( $rc, $updated );
389 }
390 $s .= $list->endRecentChangesList();
391
392 $dbr->freeResult( $res );
393 $wgOut->addHTML( $s );
394
395 if ( $wgUseWatchlistCache ) {
396 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
397 }
398
399 }
400
401 function wlHoursLink( $h, $page, $options = array() ) {
402 global $wgUser, $wgLang, $wgContLang;
403 $sk = $wgUser->getSkin();
404 $s = $sk->makeKnownLink(
405 $wgContLang->specialPage( $page ),
406 $wgLang->formatNum( $h ),
407 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
408 return $s;
409 }
410
411 function wlDaysLink( $d, $page, $options = array() ) {
412 global $wgUser, $wgLang, $wgContLang;
413 $sk = $wgUser->getSkin();
414 $s = $sk->makeKnownLink(
415 $wgContLang->specialPage( $page ),
416 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
417 wfArrayToCGI( array('days' => $d), $options ) );
418 return $s;
419 }
420
421 /**
422 * Returns html
423 */
424 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
425 $hours = array( 1, 2, 6, 12 );
426 $days = array( 1, 3, 7 );
427 $cl = '';
428 $i = 0;
429 foreach( $hours as $h ) {
430 $hours[$i++] = wlHoursLink( $h, $page, $options );
431 }
432 $i = 0;
433 foreach( $days as $d ) {
434 $days[$i++] = wlDaysLink( $d, $page, $options );
435 }
436 return wfMsgExt('wlshowlast',
437 array('parseinline', 'replaceafter'),
438 implode(' | ', $hours),
439 implode(' | ', $days),
440 wlDaysLink( 0, $page, $options ) );
441 }
442
443 /**
444 * Count the number of items on a user's watchlist
445 *
446 * @param $talk Include talk pages
447 * @return integer
448 */
449 function wlCountItems( &$user, $talk = true ) {
450 $dbr =& wfGetDB( DB_SLAVE );
451
452 # Fetch the raw count
453 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
454 $row = $dbr->fetchObject( $res );
455 $count = $row->count;
456 $dbr->freeResult( $res );
457
458 # Halve to remove talk pages if needed
459 if( !$talk )
460 $count = floor( $count / 2 );
461
462 return( $count );
463 }
464
465 /**
466 * Allow the user to clear their watchlist
467 *
468 * @param $out Output object
469 * @param $request Request object
470 * @param $par Parameters passed to the watchlist page
471 * @return bool True if it's been taken care of; false indicates the watchlist
472 * code needs to do something further
473 */
474 function wlHandleClear( &$out, &$request, $par ) {
475 # Check this function has something to do
476 if( $request->getText( 'action' ) == 'clear' || $par == 'clear' ) {
477 global $wgUser;
478 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
479 $count = wlCountItems( $wgUser );
480 if( $count > 0 ) {
481 # See if we're clearing or confirming
482 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
483 # Clearing, so do it and report the result
484 $dbw =& wfGetDB( DB_MASTER );
485 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId ), 'wlHandleClear' );
486 $out->addWikiText( wfMsg( 'watchlistcleardone', $count ) );
487 $out->returnToMain();
488 } else {
489 # Confirming, so show a form
490 $wlTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
491 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
492 $out->addWikiText( wfMsg( 'watchlistcount', $count ) );
493 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
494 $out->addHTML( wfElement( 'input', array( 'type' => 'hidden', 'name' => 'token', 'value' => $wgUser->editToken( 'clearwatchlist' ) ), '' ) );
495 $out->addHTML( wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) );
496 $out->addHTML( wfCloseElement( 'form' ) );
497 }
498 return( true );
499 } else {
500 # Nothing on the watchlist; nothing to do here
501 $out->addWikiText( wfMsg( 'nowatchlist' ) );
502 $out->returnToMain();
503 return( true );
504 }
505 } else {
506 return( false );
507 }
508 }
509
510 ?>