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