4ed12304fab14a2bd89d03eb58292c98fe30a657
[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 $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
26 $wgOut->setRobotPolicy( 'noindex,nofollow' );
27
28 # Anons don't get a watchlist
29 if( $wgUser->isAnon() ) {
30 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
31 $llink = Linker::makeKnownLinkObj( SpecialPage::getTitleFor( 'Userlogin' ), wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
32 $wgOut->addHtml( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
33 return;
34 } else {
35 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
36 $wgOut->setSubtitle( wfMsgWikiHtml( 'watchlistfor', htmlspecialchars( $wgUser->getName() ) ) );
37 }
38
39 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
40 return;
41 }
42
43 $defaults = array(
44 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
45 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
46 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
47 /* ? */ 'namespace' => 'all',
48 );
49
50 extract($defaults);
51
52 # Extract variables from the request, falling back to user preferences or
53 # other default values if these don't exist
54 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
55 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
56 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
57
58 # Get query variables
59 $days = $wgRequest->getVal( 'days', $prefs['days'] );
60 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
61 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
62
63 # Get namespace value, if supplied, and prepare a WHERE fragment
64 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
65 if( !is_null( $nameSpace ) ) {
66 $nameSpace = intval( $nameSpace );
67 $nameSpaceClause = " AND rc_namespace = $nameSpace";
68 } else {
69 $nameSpace = '';
70 $nameSpaceClause = '';
71 }
72
73 # Watchlist editing
74 $action = $wgRequest->getVal( 'action' );
75 $remove = $wgRequest->getVal( 'remove' );
76 $id = $wgRequest->getArray( 'id' );
77
78 $uid = $wgUser->getID();
79 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
80 $wgUser->clearAllNotifications( $uid );
81 }
82
83 # Deleting items from watchlist
84 if(($action == 'submit') && isset($remove) && is_array($id)) {
85 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
86 $wgOut->addHTML( '<p>' );
87 foreach($id as $one) {
88 $t = Title::newFromURL( $one );
89 if( !is_null( $t ) ) {
90 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
91 if( $wl->removeWatch() === false ) {
92 $wgOut->addHTML( wfMsg( 'couldntremove', htmlspecialchars($one) ) . "<br />\n" );
93 } else {
94 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
95 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
96 }
97 } else {
98 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
99 }
100 }
101 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
102 }
103
104 if ( $wgUseWatchlistCache ) {
105 $memckey = wfMemcKey( 'watchlist', 'id', $wgUser->getId() );
106 $cache_s = @$wgMemc->get( $memckey );
107 if( $cache_s ){
108 $wgOut->addWikiText( wfMsg('wlsaved') );
109 $wgOut->addHTML( $cache_s );
110 return;
111 }
112 }
113
114 $dbr =& wfGetDB( DB_SLAVE );
115 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
116
117 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
118 $res = $dbr->query( $sql, $fname );
119 $s = $dbr->fetchObject( $res );
120
121 # Patch *** A1 *** (see A2 below)
122 # adjust for page X, talk:page X, which are both stored separately, but treated together
123 $nitems = floor($s->n / 2);
124 # $nitems = $s->n;
125
126 if($nitems == 0) {
127 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
128 return;
129 }
130
131 if( is_null($days) || !is_numeric($days) ) {
132 $big = 1000; /* The magical big */
133 if($nitems > $big) {
134 # Set default cutoff shorter
135 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
136 } else {
137 $days = $defaults['days']; # default cutoff for shortlisters
138 }
139 } else {
140 $days = floatval($days);
141 }
142
143 // Dump everything here
144 $nondefaults = array();
145
146 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
147 wfAppendToArrayIfNotDefault( 'hideOwn', (int)$hideOwn, $defaults, $nondefaults);
148 wfAppendToArrayIfNotDefault( 'hideBots', (int)$hideBots, $defaults, $nondefaults);
149 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
150
151 if ( $days <= 0 ) {
152 $docutoff = '';
153 $cutoff = false;
154 $npages = wfMsg( 'watchlistall1' );
155 } else {
156 $docutoff = "AND rev_timestamp > '" .
157 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
158 . "'";
159 /*
160 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
161 $res = $dbr->query( $sql, $fname );
162 $s = $dbr->fetchObject( $res );
163 $npages = $s->n;
164 */
165 $npages = 40000 * $days;
166
167 }
168
169 /* Edit watchlist form */
170 if($wgRequest->getBool('edit') || $par == 'edit' ) {
171 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
172 "\n\n" . wfMsg( 'watcheditlist' ) );
173
174 $wgOut->addHTML( '<form action=\'' .
175 $specialTitle->escapeLocalUrl( 'action=submit' ) .
176 "' method='post'>\n" );
177
178 # Patch A2
179 # The following was proposed by KTurner 07.11.2004 to T.Gries
180 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
181 $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";
182
183 $res = $dbr->query( $sql, $fname );
184
185 # Batch existence check
186 $linkBatch = new LinkBatch();
187 while( $row = $dbr->fetchObject( $res ) )
188 $linkBatch->addObj( Title::makeTitleSafe( $row->wl_namespace, $row->wl_title ) );
189 $linkBatch->execute();
190 if( $dbr->numRows( $res ) > 0 )
191 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
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 = Linker::makeLinkObj( $titleObj );
217 $toolLinks[] = Linker::makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK ) );
218 if( $titleObj->exists() )
219 $toolLinks[] = Linker::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 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
236 "\n</form>\n"
237 );
238
239 return;
240 }
241
242 # If the watchlist is relatively short, it's simplest to zip
243 # down its entirety and then sort the results.
244
245 # If it's relatively long, it may be worth our while to zip
246 # through the time-sorted page list checking for watched items.
247
248 # Up estimate of watched items by 15% to compensate for talk pages...
249
250 # Toggles
251 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
252 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
253
254 # Show watchlist header
255 $header = '';
256 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
257 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
258 }
259 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
260 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
261 }
262
263 # Toggle watchlist content (all recent edits or just the latest)
264 if( $wgUser->getOption( 'extendwatchlist' )) {
265 $andLatest='';
266 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
267 } else {
268 $andLatest= 'AND rc_this_oldid=page_latest';
269 $limitWatchlist = '';
270 }
271
272 # TODO: Consider removing the third parameter
273 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
274 $wgLang->formatNum( $npages ), '',
275 $specialTitle->getFullUrl( 'edit=yes' ) );
276 $wgOut->addWikiText( $header );
277
278 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
279 $wgOut->addHTML( '<form action="' .
280 $specialTitle->escapeLocalUrl() .
281 '" method="post"><input type="submit" name="dummy" value="' .
282 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
283 '" /><input type="hidden" name="reset" value="all" /></form>' .
284 "\n\n" );
285 }
286
287 $sql = "SELECT
288 rc_namespace AS page_namespace, rc_title AS page_title,
289 rc_comment AS rev_comment, rc_cur_id AS page_id,
290 rc_user AS rev_user, rc_user_text AS rev_user_text,
291 rc_timestamp AS rev_timestamp, rc_minor AS rev_minor_edit,
292 rc_this_oldid AS rev_id,
293 rc_last_oldid, rc_id, rc_patrolled,
294 rc_new AS page_is_new,wl_notificationtimestamp
295 FROM $watchlist,$recentchanges,$page
296 WHERE wl_user=$uid
297 AND wl_namespace=rc_namespace
298 AND wl_title=rc_title
299 AND rc_timestamp > '$cutoff'
300 AND rc_cur_id=page_id
301 $andLatest
302 $andHideOwn
303 $andHideBots
304 $nameSpaceClause
305 ORDER BY rc_timestamp DESC
306 $limitWatchlist";
307
308 $res = $dbr->query( $sql, $fname );
309 $numRows = $dbr->numRows( $res );
310
311 /* Start bottom header */
312 $wgOut->addHTML( "<hr />\n" );
313
314 if($days >= 1) {
315 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
316 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
317 } elseif($days > 0) {
318 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
319 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
320 }
321
322 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
323
324 # Spit out some control panel links
325 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
326 $linkElements = array( 'hideOwn' => 'wlhideshowown', 'hideBots' => 'wlhideshowbots' );
327
328 # Problems encountered using the fancier method
329 $label = $hideBots ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
330 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
331 $link = Linker::makeKnownLinkObj( $thisTitle, $label, $linkBits );
332 $links[] = wfMsgHtml( 'wlhideshowbots', $link );
333
334 $label = $hideOwn ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' );
335 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
336 $link = Linker::makeKnownLinkObj( $thisTitle, $label, $linkBits );
337 $links[] = wfMsgHtml( 'wlhideshowown', $link );
338
339 $wgOut->addHTML( implode( ' | ', $links ) );
340
341 # Form for namespace filtering
342 $wgOut->addHTML( "\n" .
343 wfOpenElement( 'form', array(
344 'method' => 'post',
345 'action' => $thisTitle->getLocalURL(),
346 ) ) .
347 wfMsgExt( 'namespace', array( 'parseinline') ) .
348 HTMLnamespaceselector( $nameSpace, '' ) . "\n" .
349 ( $hideOwn ? wfHidden('hideown', 1)."\n" : '' ) .
350 ( $hideBots ? wfHidden('hidebots', 1)."\n" : '' ) .
351 wfHidden( 'days', $days ) . "\n" .
352 wfSubmitButton( wfMsgExt( 'allpagessubmit', array( 'escape') ) ) . "\n" .
353 wfCloseElement( 'form' ) . "\n"
354 );
355
356 if ( $numRows == 0 ) {
357 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
358 return;
359 }
360
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 $wgLang, $wgContLang;
403 $s = Linker::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 $wgLang, $wgContLang;
412 $s = Linker::makeKnownLink(
413 $wgContLang->specialPage( $page ),
414 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
415 wfArrayToCGI( array('days' => $d), $options ) );
416 return $s;
417 }
418
419 /**
420 * Returns html
421 */
422 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
423 $hours = array( 1, 2, 6, 12 );
424 $days = array( 1, 3, 7 );
425 $cl = '';
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 ?>