Man, it's not a fake anymore. Damn, that's important!
[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 *
16 * @param $par Parameter passed to the page
17 */
18 function wfSpecialWatchlist( $par ) {
19 global $wgUser, $wgOut, $wgLang, $wgMemc, $wgRequest, $wgContLang;
20 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
21 global $wgEnotifWatchlist;
22 $fname = 'wfSpecialWatchlist';
23
24 $skin =& $wgUser->getSkin();
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 = $skin->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 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
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 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
59
60 # Get query variables
61 $days = $wgRequest->getVal( 'days', $prefs['days'] );
62 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
63 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
64 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
65
66 # Get namespace value, if supplied, and prepare a WHERE fragment
67 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
68 if( !is_null( $nameSpace ) ) {
69 $nameSpace = intval( $nameSpace );
70 $nameSpaceClause = " AND rc_namespace = $nameSpace";
71 } else {
72 $nameSpace = '';
73 $nameSpaceClause = '';
74 }
75
76 # Watchlist editing
77 $action = $wgRequest->getVal( 'action' );
78 $remove = $wgRequest->getVal( 'remove' );
79 $id = $wgRequest->getArray( 'id' );
80
81 $uid = $wgUser->getID();
82 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
83 $wgUser->clearAllNotifications( $uid );
84 }
85
86 # Deleting items from watchlist
87 if(($action == 'submit') && isset($remove) && is_array($id)) {
88 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
89 $wgOut->addHTML( '<p>' );
90 foreach($id as $one) {
91 $t = Title::newFromURL( $one );
92 if( !is_null( $t ) ) {
93 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
94 if( $wl->removeWatch() === false ) {
95 $wgOut->addHTML( wfMsg( 'couldntremove', htmlspecialchars($one) ) . "<br />\n" );
96 } else {
97 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
98 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
99 }
100 } else {
101 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
102 }
103 }
104 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
105 }
106
107 $dbr =& wfGetDB( DB_SLAVE );
108 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
109
110 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
111 $res = $dbr->query( $sql, $fname );
112 $s = $dbr->fetchObject( $res );
113
114 # Patch *** A1 *** (see A2 below)
115 # adjust for page X, talk:page X, which are both stored separately, but treated together
116 $nitems = floor($s->n / 2);
117 # $nitems = $s->n;
118
119 if($nitems == 0) {
120 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
121 return;
122 }
123
124 if( is_null($days) || !is_numeric($days) ) {
125 $big = 1000; /* The magical big */
126 if($nitems > $big) {
127 # Set default cutoff shorter
128 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
129 } else {
130 $days = $defaults['days']; # default cutoff for shortlisters
131 }
132 } else {
133 $days = floatval($days);
134 }
135
136 // Dump everything here
137 $nondefaults = array();
138
139 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
140 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
141 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
142 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
143 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
144
145 if ( $days <= 0 ) {
146 $andcutoff = '';
147 $npages = wfMsg( 'watchlistall1' );
148 } else {
149 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
150 /*
151 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
152 $res = $dbr->query( $sql, $fname );
153 $s = $dbr->fetchObject( $res );
154 $npages = $s->n;
155 */
156 $npages = 40000 * $days;
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
181 if( $dbr->numRows( $res ) > 0 )
182 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
183
184 $sk = $wgUser->getSkin();
185
186 $list = array();
187 while( $s = $dbr->fetchObject( $res ) ) {
188 $list[$s->wl_namespace][$s->wl_title] = $s->page_is_redirect;
189 }
190
191 // TODO: Display a TOC
192 foreach($list as $ns => $titles) {
193 if (Namespace::isTalk($ns))
194 continue;
195 if ($ns != NS_MAIN)
196 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
197 $wgOut->addHTML( '<ul>' );
198 foreach( $titles as $title => $redir ) {
199 $titleObj = Title::makeTitle( $ns, $title );
200 if( is_null( $titleObj ) ) {
201 $wgOut->addHTML(
202 '<!-- bad title "' .
203 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
204 );
205 } else {
206 global $wgContLang;
207 $toolLinks = array();
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 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
228 "\n</form>\n"
229 );
230
231 return;
232 }
233
234 # If the watchlist is relatively short, it's simplest to zip
235 # down its entirety and then sort the results.
236
237 # If it's relatively long, it may be worth our while to zip
238 # through the time-sorted page list checking for watched items.
239
240 # Up estimate of watched items by 15% to compensate for talk pages...
241
242 # Toggles
243 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
244 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
245 $andHideMinor = $hideMinor ? 'AND rc_minor = 0' : '';
246
247 # Show watchlist header
248 $header = '';
249 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
250 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
251 }
252 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
253 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
254 }
255
256 # Toggle watchlist content (all recent edits or just the latest)
257 if( $wgUser->getOption( 'extendwatchlist' )) {
258 $andLatest='';
259 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
260 } else {
261 $andLatest= 'AND rc_this_oldid=page_latest';
262 $limitWatchlist = '';
263 }
264
265 # TODO: Consider removing the third parameter
266 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
267 $wgLang->formatNum( $npages ), '',
268 $specialTitle->getFullUrl( 'edit=yes' ) );
269 $wgOut->addWikiText( $header );
270
271 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
272 $wgOut->addHTML( '<form action="' .
273 $specialTitle->escapeLocalUrl() .
274 '" method="post"><input type="submit" name="dummy" value="' .
275 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
276 '" /><input type="hidden" name="reset" value="all" /></form>' .
277 "\n\n" );
278 }
279
280 $sql = "SELECT *
281 FROM $watchlist,$recentchanges,$page
282 WHERE wl_user=$uid
283 AND wl_namespace=rc_namespace
284 AND wl_title=rc_title
285 AND rc_cur_id=page_id
286 $andcutoff
287 $andLatest
288 $andHideOwn
289 $andHideBots
290 $andHideMinor
291 $nameSpaceClause
292 ORDER BY rc_timestamp DESC
293 $limitWatchlist";
294
295 $res = $dbr->query( $sql, $fname );
296 $numRows = $dbr->numRows( $res );
297
298 /* Start bottom header */
299 $wgOut->addHTML( "<hr />\n" );
300
301 if($days >= 1) {
302 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
303 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
304 } elseif($days > 0) {
305 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
306 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
307 }
308
309 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
310
311 # Spit out some control panel links
312 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
313 $skin = $wgUser->getSkin();
314
315 # Hide/show bot edits
316 $label = $hideBots ? wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
317 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
318 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
319
320 # Hide/show own edits
321 $label = $hideOwn ? wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
322 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
323 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
324
325 # Hide/show minor edits
326 $label = $hideMinor ? wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
327 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
328 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
329
330 $wgOut->addHTML( implode( ' | ', $links ) );
331
332 # Form for namespace filtering
333 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
334 $form .= '<p>';
335 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
336 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
337 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
338 $form .= Xml::hidden( 'days', $days );
339 if( $hideOwn )
340 $form .= Xml::hidden( 'hideOwn', 1 );
341 if( $hideBots )
342 $form .= Xml::hidden( 'hideBots', 1 );
343 if( $hideMinor )
344 $form .= Xml::hidden( 'hideMinor', 1 );
345 $form .= Xml::closeElement( 'form' );
346 $wgOut->addHtml( $form );
347
348 # If there's nothing to show, stop here
349 if( $numRows == 0 ) {
350 $wgOut->addWikiText( wfMsgNoTrans( 'watchnochange' ) );
351 return;
352 }
353
354 /* End bottom header */
355
356 $list = ChangesList::newFromUser( $wgUser );
357
358 $s = $list->beginRecentChangesList();
359 $counter = 1;
360 while ( $obj = $dbr->fetchObject( $res ) ) {
361 # Make RC entry
362 $rc = RecentChange::newFromRow( $obj );
363 $rc->counter = $counter++;
364
365 if ( $wgShowUpdatedMarker ) {
366 $updated = $obj->wl_notificationtimestamp;
367 } else {
368 // Same visual appearance as MW 1.4
369 $updated = true;
370 }
371
372 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
373 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .$dbr->strencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
374 $res3 = $dbr->query( $sql3, $fname );
375 $x = $dbr->fetchObject( $res3 );
376 $rc->numberofWatchingusers = $x->n;
377 } else {
378 $rc->numberofWatchingusers = 0;
379 }
380
381 $s .= $list->recentChangesLine( $rc, $updated );
382 }
383 $s .= $list->endRecentChangesList();
384
385 $dbr->freeResult( $res );
386 $wgOut->addHTML( $s );
387
388 }
389
390 function wlHoursLink( $h, $page, $options = array() ) {
391 global $wgUser, $wgLang, $wgContLang;
392 $sk = $wgUser->getSkin();
393 $s = $sk->makeKnownLink(
394 $wgContLang->specialPage( $page ),
395 $wgLang->formatNum( $h ),
396 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
397 return $s;
398 }
399
400 function wlDaysLink( $d, $page, $options = array() ) {
401 global $wgUser, $wgLang, $wgContLang;
402 $sk = $wgUser->getSkin();
403 $s = $sk->makeKnownLink(
404 $wgContLang->specialPage( $page ),
405 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
406 wfArrayToCGI( array('days' => $d), $options ) );
407 return $s;
408 }
409
410 /**
411 * Returns html
412 */
413 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
414 $hours = array( 1, 2, 6, 12 );
415 $days = array( 1, 3, 7 );
416 $i = 0;
417 foreach( $hours as $h ) {
418 $hours[$i++] = wlHoursLink( $h, $page, $options );
419 }
420 $i = 0;
421 foreach( $days as $d ) {
422 $days[$i++] = wlDaysLink( $d, $page, $options );
423 }
424 return wfMsgExt('wlshowlast',
425 array('parseinline', 'replaceafter'),
426 implode(' | ', $hours),
427 implode(' | ', $days),
428 wlDaysLink( 0, $page, $options ) );
429 }
430
431 /**
432 * Count the number of items on a user's watchlist
433 *
434 * @param $talk Include talk pages
435 * @return integer
436 */
437 function wlCountItems( &$user, $talk = true ) {
438 $dbr =& wfGetDB( DB_SLAVE );
439
440 # Fetch the raw count
441 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
442 $row = $dbr->fetchObject( $res );
443 $count = $row->count;
444 $dbr->freeResult( $res );
445
446 # Halve to remove talk pages if needed
447 if( !$talk )
448 $count = floor( $count / 2 );
449
450 return( $count );
451 }
452
453 /**
454 * Allow the user to clear their watchlist
455 *
456 * @param $out Output object
457 * @param $request Request object
458 * @param $par Parameters passed to the watchlist page
459 * @return bool True if it's been taken care of; false indicates the watchlist
460 * code needs to do something further
461 */
462 function wlHandleClear( &$out, &$request, $par ) {
463 global $wgLang;
464
465 # Check this function has something to do
466 if( $request->getText( 'action' ) == 'clear' || $par == 'clear' ) {
467 global $wgUser;
468 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
469 $count = wlCountItems( $wgUser );
470 if( $count > 0 ) {
471 # See if we're clearing or confirming
472 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
473 # Clearing, so do it and report the result
474 $dbw =& wfGetDB( DB_MASTER );
475 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId ), 'wlHandleClear' );
476 $out->addWikiText( wfMsgExt( 'watchlistcleardone', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
477 $out->returnToMain();
478 } else {
479 # Confirming, so show a form
480 $wlTitle = SpecialPage::getTitleFor( 'Watchlist' );
481 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
482 $out->addWikiText( wfMsgExt( 'watchlistcount', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
483 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
484 $out->addHTML(
485 wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
486 wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
487 wfCloseElement( 'form' )
488 );
489 }
490 return( true );
491 } else {
492 # Nothing on the watchlist; nothing to do here
493 $out->addWikiText( wfMsg( 'nowatchlist' ) );
494 $out->returnToMain();
495 return( true );
496 }
497 } else {
498 return( false );
499 }
500 }
501 ?>