Fix silly little ballsed-up wrong-way-round join causing Village Pumpers to whine...
[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 require_once( 'WatchedItem.php' );
13
14 /**
15 * constructor
16 */
17 function wfSpecialWatchlist( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgMemc, $wgRequest, $wgContLang;
19 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname;
20 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
21 global $wgEnotifWatchlist, $wgFilterRobotsWL;
22 $fname = 'wfSpecialWatchlist';
23
24 $wgOut->setPagetitle( wfMsg( 'watchlist' ) );
25 $sub = htmlspecialchars( wfMsg( 'watchlistsub', $wgUser->getName() ) );
26 $wgOut->setSubtitle( $sub );
27 $wgOut->setRobotpolicy( 'noindex,nofollow' );
28
29 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
30
31 if( $wgUser->isAnon() ) {
32 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
33 return;
34 }
35
36 $defaults = array(
37 /* float */ 'days' => 3.0, /* or 0.5, watch further below */
38 /* bool */ 'hideOwn' => false,
39 /* bool */ 'hideBots' => false,
40 'namespace' => 'all',
41 );
42
43 extract($defaults);
44
45 # Get query variables
46 $days = $wgRequest->getVal( 'days' );
47 $hideOwn = $wgRequest->getBool( 'hideOwn' );
48 $hideBots = $wgRequest->getBool( 'hideBots' );
49
50 # Get namespace value, if supplied, and prepare a WHERE fragment
51 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
52 if( !is_null( $nameSpace ) ) {
53 $nameSpace = intval( $nameSpace );
54 $nameSpaceClause = " AND rc_namespace = $nameSpace";
55 } else {
56 $nameSpace = '';
57 $nameSpaceClause = '';
58 }
59
60 # Watchlist editing
61 $action = $wgRequest->getVal( 'action' );
62 $remove = $wgRequest->getVal( 'remove' );
63 $id = $wgRequest->getArray( 'id' );
64
65 $uid = $wgUser->getID();
66 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
67 $wgUser->clearAllNotifications( $uid );
68 }
69
70 # Deleting items from watchlist
71 if(($action == 'submit') && isset($remove) && is_array($id)) {
72 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
73 $wgOut->addHTML( '<p>' );
74 foreach($id as $one) {
75 $t = Title::newFromURL( $one );
76 if( !is_null( $t ) ) {
77 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
78 if( $wl->removeWatch() === false ) {
79 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
80 } else {
81 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
82 }
83 } else {
84 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
85 }
86 }
87 $wgOut->addHTML( "done.</p>\n" );
88 }
89
90 if ( $wgUseWatchlistCache ) {
91 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
92 $cache_s = @$wgMemc->get( $memckey );
93 if( $cache_s ){
94 $wgOut->addWikiText( wfMsg('wlsaved') );
95 $wgOut->addHTML( $cache_s );
96 return;
97 }
98 }
99
100 $dbr =& wfGetDB( DB_SLAVE );
101 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
102
103 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
104 $res = $dbr->query( $sql, $fname );
105 $s = $dbr->fetchObject( $res );
106
107 # Patch *** A1 *** (see A2 below)
108 # adjust for page X, talk:page X, which are both stored separately, but treated together
109 $nitems = floor($s->n / 2);
110 # $nitems = $s->n;
111
112 if($nitems == 0) {
113 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
114 return;
115 }
116
117 if( is_null($days) || !is_numeric($days) ) {
118 $big = 1000; /* The magical big */
119 if($nitems > $big) {
120 # Set default cutoff shorter
121 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
122 } else {
123 $days = $defaults['days']; # default cutoff for shortlisters
124 }
125 } else {
126 $days = floatval($days);
127 }
128
129 // Dump everything here
130 $nondefaults = array();
131
132 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
133 wfAppendToArrayIfNotDefault( 'hideOwn', $hideOwn, $defaults, $nondefaults);
134 wfAppendToArrayIfNotDefault( 'hideBots', $hideBots, $defaults, $nondefaults);
135 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
136
137 if ( $days <= 0 ) {
138 $docutoff = '';
139 $cutoff = false;
140 $npages = wfMsg( 'watchlistall1' );
141 } else {
142 $docutoff = "AND rev_timestamp > '" .
143 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
144 . "'";
145 /*
146 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
147 $res = $dbr->query( $sql, $fname );
148 $s = $dbr->fetchObject( $res );
149 $npages = $s->n;
150 */
151 $npages = 40000 * $days;
152
153 }
154
155 /* Edit watchlist form */
156 if($wgRequest->getBool('edit') || $par == 'edit' ) {
157 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
158 "\n\n" . wfMsg( 'watcheditlist' ) );
159
160 $wgOut->addHTML( '<form action=\'' .
161 $specialTitle->escapeLocalUrl( 'action=submit' ) .
162 "' method='post'>\n" );
163
164 # Patch A2
165 # The following was proposed by KTurner 07.11.2004 to T.Gries
166 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
167 $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";
168
169 $res = $dbr->query( $sql, $fname );
170
171 # Batch existence check
172 $linkBatch = new LinkBatch();
173 while( $row = $dbr->fetchObject( $res ) )
174 $linkBatch->addObj( Title::makeTitleSafe( $row->wl_namespace, $row->wl_title ) );
175 $linkBatch->execute();
176 if( $dbr->numRows( $res ) > 0 )
177 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
178
179 $sk = $wgUser->getSkin();
180
181 $list = array();
182 while( $s = $dbr->fetchObject( $res ) ) {
183 $list[$s->wl_namespace][$s->wl_title] = $s->page_is_redirect;
184 }
185
186 // TODO: Display a TOC
187 foreach($list as $ns => $titles) {
188 if (Namespace::isTalk($ns))
189 continue;
190 if ($ns != NS_MAIN)
191 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
192 $wgOut->addHTML( '<ul>' );
193 foreach( $titles as $title => $redir ) {
194 $titleObj = Title::makeTitle( $ns, $title );
195 if( is_null( $titleObj ) ) {
196 $wgOut->addHTML(
197 '<!-- bad title "' .
198 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
199 );
200 } else {
201 global $wgContLang;
202 $toolLinks = array();
203 $titleText = $titleObj->getPrefixedText();
204 $pageLink = $sk->makeLinkObj( $titleObj );
205 $toolLinks[] = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK ) );
206 if( $titleObj->exists() )
207 $toolLinks[] = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'history_short' ), 'action=history' );
208 $toolLinks = '(' . implode( ' | ', $toolLinks ) . ')';
209 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ? '&rlm;' : '&lrm;' );
210 if( $redir ) {
211 $spanopen = '<span class="watchlistredir">';
212 $spanclosed = '</span>';
213 } else {
214 $spanopen = $spanclosed = '';
215 }
216
217 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
218 }
219 }
220 $wgOut->addHTML( '</ul>' );
221 }
222 $wgOut->addHTML(
223 "<input type='submit' name='remove' value=\"" .
224 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
225 "</form>\n"
226 );
227
228 return;
229 }
230
231 # If the watchlist is relatively short, it's simplest to zip
232 # down its entirety and then sort the results.
233
234 # If it's relatively long, it may be worth our while to zip
235 # through the time-sorted page list checking for watched items.
236
237 # Up estimate of watched items by 15% to compensate for talk pages...
238
239 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
240 if( $wgFilterRobotsWL ) {
241 $andHideBotsOptional = $hideBots ? "AND (rc_bot = 0)" : '';
242 } else {
243 $andHideBotsOptional = "AND rc_this_oldid=page_latest";
244 }
245
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 # TODO: Consider removing the third parameter
257 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
258 $wgLang->formatNum( $npages ), '',
259 $specialTitle->getFullUrl( 'edit=yes' ) );
260 $wgOut->addWikiText( $header );
261
262 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
263 $wgOut->addHTML( '<form action="' .
264 $specialTitle->escapeLocalUrl() .
265 '" method="post"><input type="submit" name="dummy" value="' .
266 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
267 '" /><input type="hidden" name="reset" value="all" /></form>' .
268 "\n\n" );
269 }
270
271 $sql = "SELECT
272 rc_namespace page_namespace,rc_title page_title,
273 rc_comment rev_comment, rc_cur_id page_id,
274 rc_user rev_user,rc_user_text rev_user_text,
275 rc_timestamp rev_timestamp,rc_minor rev_minor_edit,
276 rc_this_oldid rev_id,
277 rc_last_oldid,
278 rc_new page_is_new,wl_notificationtimestamp
279 FROM $watchlist,$recentchanges,$page
280 WHERE wl_user=$uid
281 AND wl_namespace=rc_namespace
282 AND wl_title=rc_title
283 AND rc_timestamp > '$cutoff'
284 AND rc_cur_id=page_id
285 $andHideOwn
286 $andHideBotsOptional
287 $nameSpaceClause
288 ORDER BY rc_timestamp DESC";
289
290 $res = $dbr->query( $sql, $fname );
291 $numRows = $dbr->numRows( $res );
292
293 /* Start bottom header */
294 $wgOut->addHTML( "<hr />\n<p>" );
295
296 if($days >= 1)
297 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
298 $wgLang->formatNum( $days ) ) . '<br />' , false );
299 elseif($days > 0)
300 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
301 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
302
303 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
304
305 $sk = $wgUser->getSkin();
306 $s = $sk->makeKnownLink(
307 $wgContLang->specialPage( 'Watchlist' ),
308 (0 == $hideOwn) ? wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
309 wfArrayToCGI( array('hideOwn' => 1-$hideOwn ), $nondefaults ) );
310 $wgOut->addHTML( wfMsgHtml( "wlhideshowown", $s ) );
311
312 if( $wgFilterRobotsWL ) {
313 $s = $sk->makeKnownLink(
314 $wgContLang->specialPage( 'Watchlist' ),
315 (0 == $hideBots) ? wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
316 wfArrayToCGI( array('hideBots' => 1-$hideBots ), $nondefaults ) );
317 $wgOut->addHTML( wfMsgHtml( "wlhideshowbots", " $s" ) );
318 }
319
320 # Form for namespace filtering
321 $thisTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
322 $thisAction = $thisTitle->escapeLocalUrl();
323 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
324 $nsForm .= "<label for=\"namespace\">" . wfMsg( 'namespace' ) . "</label> ";
325 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
326 $nsForm .= ( $hideOwn ? "<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
327 $nsForm .= ( $hideBots ? "<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
328 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
329 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
330 $nsForm .= "</form>\n";
331 $wgOut->addHTML( $nsForm );
332
333 if ( $numRows == 0 ) {
334 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
335 $wgOut->addHTML( "</p>\n" );
336 return;
337 }
338
339 $wgOut->addHTML( "</p>\n" );
340 /* End bottom header */
341
342 $list = ChangesList::newFromUser( $wgUser );
343
344 $s = $list->beginRecentChangesList();
345 $counter = 1;
346 while ( $obj = $dbr->fetchObject( $res ) ) {
347 # Make fake RC entry
348 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
349 $rc->counter = $counter++;
350
351 if ( $wgShowUpdatedMarker ) {
352 $updated = $obj->wl_notificationtimestamp;
353 } else {
354 // Same visual appearance as MW 1.4
355 $updated = true;
356 }
357
358 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
359 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
360 $res3 = $dbr->query( $sql3, DB_READ, $fname );
361 $x = $dbr->fetchObject( $res3 );
362 $rc->numberofWatchingusers = $x->n;
363 } else {
364 $rc->numberofWatchingusers = 0;
365 }
366
367 $s .= $list->recentChangesLine( $rc, $updated );
368 }
369 $s .= $list->endRecentChangesList();
370
371 $dbr->freeResult( $res );
372 $wgOut->addHTML( $s );
373
374 if ( $wgUseWatchlistCache ) {
375 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
376 }
377
378 }
379
380 function wlHoursLink( $h, $page, $options = array() ) {
381 global $wgUser, $wgLang, $wgContLang;
382 $sk = $wgUser->getSkin();
383 $s = $sk->makeKnownLink(
384 $wgContLang->specialPage( $page ),
385 $wgLang->formatNum( $h ),
386 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
387 return $s;
388 }
389
390 function wlDaysLink( $d, $page, $options = array() ) {
391 global $wgUser, $wgLang, $wgContLang;
392 $sk = $wgUser->getSkin();
393 $s = $sk->makeKnownLink(
394 $wgContLang->specialPage( $page ),
395 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
396 wfArrayToCGI( array('days' => $d), $options ) );
397 return $s;
398 }
399
400 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
401 $hours = array( 1, 2, 6, 12 );
402 $days = array( 1, 3, 7 );
403 $cl = '';
404 $i = 0;
405 foreach( $hours as $h ) {
406 $hours[$i++] = wlHoursLink( $h, $page, $options );
407 }
408 $i = 0;
409 foreach( $days as $d ) {
410 $days[$i++] = wlDaysLink( $d, $page, $options );
411 }
412 return wfMsg ('wlshowlast',
413 implode(' | ', $hours),
414 implode(' | ', $days),
415 wlDaysLink( 0, $page, $options ) );
416 }
417
418 ?>