6a917a41b3bc28ac7cec57803f59ae7126588966
[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;
21 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
22 global $wgEnotifWatchlist;
23 $fname = 'wfSpecialWatchlist';
24
25 $skin =& $wgUser->getSkin();
26 $specialTitle = SpecialPage::getTitleFor( '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( SpecialPage::getTitleFor( '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( wfMsg( 'couldntremove', htmlspecialchars($one) ) . "<br />\n" );
94 } else {
95 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
96 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
97 }
98 } else {
99 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
100 }
101 }
102 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
103 }
104
105 if ( $wgUseWatchlistCache ) {
106 $memckey = wfMemcKey( '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 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', '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 $cutoff = false;
154 $npages = wfMsg( 'watchlistall1' );
155 } else {
156 $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) );
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 /* Edit watchlist form */
167 if($wgRequest->getBool('edit') || $par == 'edit' ) {
168 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
169 "\n\n" . wfMsg( 'watcheditlist' ) );
170
171 $wgOut->addHTML( '<form action=\'' .
172 $specialTitle->escapeLocalUrl( 'action=submit' ) .
173 "' method='post'>\n" );
174
175 # Patch A2
176 # The following was proposed by KTurner 07.11.2004 to T.Gries
177 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
178 $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";
179
180 $res = $dbr->query( $sql, $fname );
181
182 # Batch existence check
183 $linkBatch = new LinkBatch();
184 while( $row = $dbr->fetchObject( $res ) )
185 $linkBatch->addObj( Title::makeTitleSafe( $row->wl_namespace, $row->wl_title ) );
186 $linkBatch->execute();
187
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 $pageLink = $sk->makeLinkObj( $titleObj );
216 $toolLinks[] = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK ) );
217 if( $titleObj->exists() )
218 $toolLinks[] = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'history_short' ), 'action=history' );
219 $toolLinks = '(' . implode( ' | ', $toolLinks ) . ')';
220 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ? '&rlm;' : '&lrm;' );
221 if( $redir ) {
222 $spanopen = '<span class="watchlistredir">';
223 $spanclosed = '</span>';
224 } else {
225 $spanopen = $spanclosed = '';
226 }
227
228 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
229 }
230 }
231 $wgOut->addHTML( '</ul>' );
232 }
233 $wgOut->addHTML(
234 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
235 "\n</form>\n"
236 );
237
238 return;
239 }
240
241 # If the watchlist is relatively short, it's simplest to zip
242 # down its entirety and then sort the results.
243
244 # If it's relatively long, it may be worth our while to zip
245 # through the time-sorted page list checking for watched items.
246
247 # Up estimate of watched items by 15% to compensate for talk pages...
248
249 # Toggles
250 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
251 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
252
253 # Show watchlist header
254 $header = '';
255 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
256 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
257 }
258 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
259 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
260 }
261
262 # Toggle watchlist content (all recent edits or just the latest)
263 if( $wgUser->getOption( 'extendwatchlist' )) {
264 $andLatest='';
265 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
266 } else {
267 $andLatest= 'AND rc_this_oldid=page_latest';
268 $limitWatchlist = '';
269 }
270
271 # TODO: Consider removing the third parameter
272 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
273 $wgLang->formatNum( $npages ), '',
274 $specialTitle->getFullUrl( 'edit=yes' ) );
275 $wgOut->addWikiText( $header );
276
277 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
278 $wgOut->addHTML( '<form action="' .
279 $specialTitle->escapeLocalUrl() .
280 '" method="post"><input type="submit" name="dummy" value="' .
281 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
282 '" /><input type="hidden" name="reset" value="all" /></form>' .
283 "\n\n" );
284 }
285
286 $sql = "SELECT
287 rc_namespace AS page_namespace, rc_title AS page_title,
288 rc_comment AS rev_comment, rc_cur_id AS page_id,
289 rc_user AS rev_user, rc_user_text AS rev_user_text,
290 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
291 rc_this_oldid AS rev_id,
292 rc_last_oldid, rc_id, rc_patrolled,
293 rc_new AS page_is_new,wl_notificationtimestamp
294 FROM $watchlist,$recentchanges,$page
295 WHERE wl_user=$uid
296 AND wl_namespace=rc_namespace
297 AND wl_title=rc_title
298 AND rc_timestamp > '$cutoff'
299 AND rc_cur_id=page_id
300 $andLatest
301 $andHideOwn
302 $andHideBots
303 $nameSpaceClause
304 ORDER BY rc_timestamp DESC
305 $limitWatchlist";
306
307 $res = $dbr->query( $sql, $fname );
308 $numRows = $dbr->numRows( $res );
309
310 /* Start bottom header */
311 $wgOut->addHTML( "<hr />\n" );
312
313 if($days >= 1) {
314 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
315 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
316 } elseif($days > 0) {
317 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
318 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
319 }
320
321 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
322
323 # Spit out some control panel links
324 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
325 $skin = $wgUser->getSkin();
326
327 # Problems encountered using the fancier method
328 $label = $hideBots ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
329 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
330 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
331 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
332
333 $label = $hideOwn ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
334 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
335 $link = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
336 $links[] = wfMsgHtml( 'wlhideshowown', $link );
337
338 $wgOut->addHTML( implode( ' | ', $links ) );
339
340 # Form for namespace filtering
341 $wgOut->addHTML( "\n" .
342 wfOpenElement( 'form', array(
343 'method' => 'post',
344 'action' => $thisTitle->getLocalURL(),
345 ) ) .
346 wfMsgExt( 'namespace', array( 'parseinline') ) .
347 HTMLnamespaceselector( $nameSpace, '' ) . "\n" .
348 ( $hideOwn ? wfHidden('hideown', 1)."\n" : '' ) .
349 ( $hideBots ? wfHidden('hidebots', 1)."\n" : '' ) .
350 wfHidden( 'days', $days ) . "\n" .
351 wfSubmitButton( wfMsgExt( 'allpagessubmit', array( 'escape') ) ) . "\n" .
352 wfCloseElement( 'form' ) . "\n"
353 );
354
355 if ( $numRows == 0 ) {
356 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
357 return;
358 }
359
360 /* End bottom header */
361
362 $list = ChangesList::newFromUser( $wgUser );
363
364 $s = $list->beginRecentChangesList();
365 $counter = 1;
366 while ( $obj = $dbr->fetchObject( $res ) ) {
367 # Make fake RC entry
368 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
369 $rc->counter = $counter++;
370
371 if ( $wgShowUpdatedMarker ) {
372 $updated = $obj->wl_notificationtimestamp;
373 } else {
374 // Same visual appearance as MW 1.4
375 $updated = true;
376 }
377
378 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
379 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .$dbr->strencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
380 $res3 = $dbr->query( $sql3, $fname );
381 $x = $dbr->fetchObject( $res3 );
382 $rc->numberofWatchingusers = $x->n;
383 } else {
384 $rc->numberofWatchingusers = 0;
385 }
386
387 $s .= $list->recentChangesLine( $rc, $updated );
388 }
389 $s .= $list->endRecentChangesList();
390
391 $dbr->freeResult( $res );
392 $wgOut->addHTML( $s );
393
394 if ( $wgUseWatchlistCache ) {
395 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
396 }
397
398 }
399
400 function wlHoursLink( $h, $page, $options = array() ) {
401 global $wgUser, $wgLang, $wgContLang;
402 $sk = $wgUser->getSkin();
403 $s = $sk->makeKnownLink(
404 $wgContLang->specialPage( $page ),
405 $wgLang->formatNum( $h ),
406 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
407 return $s;
408 }
409
410 function wlDaysLink( $d, $page, $options = array() ) {
411 global $wgUser, $wgLang, $wgContLang;
412 $sk = $wgUser->getSkin();
413 $s = $sk->makeKnownLink(
414 $wgContLang->specialPage( $page ),
415 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
416 wfArrayToCGI( array('days' => $d), $options ) );
417 return $s;
418 }
419
420 /**
421 * Returns html
422 */
423 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
424 $hours = array( 1, 2, 6, 12 );
425 $days = array( 1, 3, 7 );
426 $i = 0;
427 foreach( $hours as $h ) {
428 $hours[$i++] = wlHoursLink( $h, $page, $options );
429 }
430 $i = 0;
431 foreach( $days as $d ) {
432 $days[$i++] = wlDaysLink( $d, $page, $options );
433 }
434 return wfMsgExt('wlshowlast',
435 array('parseinline', 'replaceafter'),
436 implode(' | ', $hours),
437 implode(' | ', $days),
438 wlDaysLink( 0, $page, $options ) );
439 }
440
441 /**
442 * Count the number of items on a user's watchlist
443 *
444 * @param $talk Include talk pages
445 * @return integer
446 */
447 function wlCountItems( &$user, $talk = true ) {
448 $dbr =& wfGetDB( DB_SLAVE );
449
450 # Fetch the raw count
451 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
452 $row = $dbr->fetchObject( $res );
453 $count = $row->count;
454 $dbr->freeResult( $res );
455
456 # Halve to remove talk pages if needed
457 if( !$talk )
458 $count = floor( $count / 2 );
459
460 return( $count );
461 }
462
463 /**
464 * Allow the user to clear their watchlist
465 *
466 * @param $out Output object
467 * @param $request Request object
468 * @param $par Parameters passed to the watchlist page
469 * @return bool True if it's been taken care of; false indicates the watchlist
470 * code needs to do something further
471 */
472 function wlHandleClear( &$out, &$request, $par ) {
473 global $wgLang;
474
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( wfMsgExt( 'watchlistcleardone', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
487 $out->returnToMain();
488 } else {
489 # Confirming, so show a form
490 $wlTitle = SpecialPage::getTitleFor( 'Watchlist' );
491 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
492 $out->addWikiText( wfMsgExt( 'watchlistcount', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
493 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
494 $out->addHTML(
495 wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
496 wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
497 wfCloseElement( 'form' )
498 );
499 }
500 return( true );
501 } else {
502 # Nothing on the watchlist; nothing to do here
503 $out->addWikiText( wfMsg( 'nowatchlist' ) );
504 $out->returnToMain();
505 return( true );
506 }
507 } else {
508 return( false );
509 }
510 }
511 ?>