* Removed messages in English, they'll be inherited from the parent.
[lhc/web/wiklou.git] / includes / SpecialRecentchanges.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once( 'Feed.php' );
12 require_once( 'ChangesList.php' );
13 require_once( 'Revision.php' );
14
15 /**
16 * Constructor
17 */
18 function wfSpecialRecentchanges( $par ) {
19 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgTitle, $wgMemc, $wgDBname;
20 global $wgRequest, $wgSitename, $wgLanguageCode, $wgContLanguageCode;
21 global $wgFeedClasses, $wgUseRCPatrol;
22 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
23 global $wgLinkCache;
24 $fname = 'wfSpecialRecentchanges';
25
26 # Get query parameters
27 $feedFormat = $wgRequest->getVal( 'feed' );
28
29 $defaultDays = $wgUser->getOption( 'rcdays' );
30 if ( !$defaultDays ) { $defaultDays = 3; }
31
32 $days = $wgRequest->getInt( 'days', $defaultDays );
33 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor' ) ) ? 1 : 0;
34 list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
35
36 # As a feed, use limited settings only
37 if( $feedFormat ) {
38 $from = null;
39 $hidebots = 1;
40 $hideliu = 0;
41 $hidepatrolled = 0;
42 global $wgFeedLimit;
43 if( $limit > $wgFeedLimit ) {
44 $limit = $wgFeedLimit;
45 }
46 } else {
47 $from = $wgRequest->getText( 'from' );
48 $hidebots = $wgRequest->getBool( 'hidebots', true ) ? 1 : 0;
49 $hideliu = $wgRequest->getBool( 'hideliu', false ) ? 1 : 0;
50 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', false ) ? 1 : 0;
51
52 # Get query parameters from path
53 if( $par ) {
54 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
55 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
56 if( in_array( 'bots', $bits ) ) $hidebots = 0;
57 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
58 if( in_array( 'minor', $bits ) ) $hideminor = 0;
59 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
60 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
61 }
62 }
63
64
65 # Database connection and caching
66 $dbr =& wfGetDB( DB_SLAVE );
67 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
68
69
70 # 10 seconds server-side caching max
71 $wgOut->setSquidMaxage( 10 );
72
73 # Get last modified date, for client caching
74 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
75 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
76 if ( $feedFormat || !$wgUseRCPatrol ) {
77 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
78 # Client cache fresh and headers sent, nothing more to do.
79 return;
80 }
81 }
82
83 # Output header
84 $rctext = wfMsgForContent( "recentchangestext" );
85 $wgOut->addWikiText( $rctext );
86
87
88 $now = wfTimestampNow();
89 $cutoff_unixtime = time() - ( $days * 86400 );
90 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
91 $cutoff = $dbr->timestamp( $cutoff_unixtime );
92 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW,$cutoff)) {
93 $cutoff = $dbr->timestamp($from);
94 } else {
95 unset($from);
96 }
97
98 $sk = $wgUser->getSkin();
99
100 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
101
102 $hidem = ( $hideminor ) ? 'AND rc_minor=0' : '';
103 $hidem .= ( $hidebots ) ? ' AND rc_bot=0' : '';
104 $hidem .= ( $hideliu ) ? ' AND rc_user=0' : '';
105 $hidem .= ( $hidepatrolled )? ' AND rc_patrolled=0' : '';
106
107 $urlparams = array( 'hideminor' => $hideminor, 'hideliu' => $hideliu,
108 'hidebots' => $hidebots, 'hidepatrolled' => $hidepatrolled );
109 $hideparams = wfArrayToCGI( $urlparams );
110
111 $minorLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
112 $showhide[1-$hideminor], wfArrayToCGI( array( 'hideminor' => 1-$hideminor ), $urlparams ) );
113 $botLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
114 $showhide[1-$hidebots], wfArrayToCGI( array( 'hidebots' => 1-$hidebots ), $urlparams ) );
115 $liuLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
116 $showhide[1-$hideliu], wfArrayToCGI( array( 'hideliu' => 1-$hideliu ), $urlparams ) );
117 $patrLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
118 $showhide[1-$hidepatrolled], wfArrayToCGI( array( 'hidepatrolled' => 1-$hidepatrolled ), $urlparams ) );
119
120 $uid = $wgUser->getID();
121
122 // Perform query
123 $sql2 = "SELECT $recentchanges.*" . ($uid ? ",wl_user,wl_notificationtimestamp" : "") . " FROM $recentchanges " .
124 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
125 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
126 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
127 $res = $dbr->query( $sql2, $fname );
128
129 // Fetch results, prepare a batch link existence check query
130 $rows = array();
131 $batch = new LinkBatch;
132 while( $row = $dbr->fetchObject( $res ) ){
133 $rows[] = $row;
134 // User page link
135 $title = Title::makeTitleSafe( NS_USER, $row->rc_user_text );
136 $batch->addObj( $title );
137
138 // User talk
139 $title = Title::makeTitleSafe( NS_USER_TALK, $row->rc_user_text );
140 $batch->addObj( $title );
141
142 }
143 $dbr->freeResult( $res );
144
145 // Run existence checks
146 $batch->execute( $wgLinkCache );
147
148 if(isset($from)) {
149 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
150 $wgLang->timeanddate( $from, true ) );
151 } else {
152 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
153 }
154 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
155
156 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
157
158 $note .= "<br />\n" . wfMsg( 'rclistfrom',
159 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
160 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
161
162 $wgOut->addHTML( $note."\n" );
163
164 if( $feedFormat ) {
165 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
166 } else {
167 # Web output...
168 $wgOut->setSyndicated( true );
169 $list =& new ChangesList( $sk );
170 $s = $list->beginRecentChangesList();
171 $counter = 1;
172 foreach( $rows as $obj ){
173 if( $limit == 0) {
174 break;
175 }
176
177 if ( ! ( $hideminor && $obj->rc_minor ) &&
178 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
179 $rc = RecentChange::newFromRow( $obj );
180 $rc->counter = $counter++;
181
182 if ($wgShowUpdatedMarker
183 && $wgUser->getOption( 'showupdated' )
184 && !empty( $obj->wl_notificationtimestamp )
185 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
186 $rc->notificationtimestamp = true;
187 } else {
188 $rc->notificationtimestamp = false;
189 }
190
191 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
192 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title) ."' AND wl_namespace=$obj->rc_namespace" ;
193 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
194 $x = $dbr->fetchObject( $res3 );
195 $rc->numberofWatchingusers = $x->n;
196 } else {
197 $rc->numberofWatchingusers = 0;
198 }
199 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
200 --$limit;
201 }
202 }
203 $s .= $list->endRecentChangesList();
204 $wgOut->addHTML( $s );
205 }
206 }
207
208 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
209 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
210 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
211
212 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
213 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
214 return false;
215 }
216
217 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
218 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
219
220 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
221 ' [' . $wgContLanguageCode . ']';
222 $feed = new $wgFeedClasses[$feedFormat](
223 $feedTitle,
224 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
225 $wgTitle->getFullUrl() );
226
227 /**
228 * Bumping around loading up diffs can be pretty slow, so where
229 * possible we want to cache the feed output so the next visitor
230 * gets it quick too.
231 */
232 $cachedFeed = false;
233 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
234 /**
235 * If the cached feed was rendered very recently, we may
236 * go ahead and use it even if there have been edits made
237 * since it was rendered. This keeps a swarm of requests
238 * from being too bad on a super-frequently edited wiki.
239 */
240 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
241 < $wgFeedCacheTimeout
242 || wfTimestamp( TS_UNIX, $feedLastmod )
243 > wfTimestamp( TS_UNIX, $lastmod ) ) {
244 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
245 $cachedFeed = $messageMemc->get( $key );
246 } else {
247 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
248 }
249 }
250 if( is_string( $cachedFeed ) ) {
251 wfDebug( "RC: Outputting cached feed\n" );
252 $feed->httpHeaders();
253 echo $cachedFeed;
254 } else {
255 wfDebug( "RC: rendering new feed and caching it\n" );
256 ob_start();
257 rcDoOutputFeed( $rows, $feed );
258 $cachedFeed = ob_get_contents();
259 ob_end_flush();
260
261 $expire = 3600 * 24; # One day
262 $messageMemc->set( $key, $cachedFeed );
263 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
264 }
265 return true;
266 }
267
268 function rcDoOutputFeed( $rows, &$feed ) {
269 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
270
271 $feed->outHeader();
272
273 # Merge adjacent edits by one user
274 $sorted = array();
275 $n = 0;
276 foreach( $rows as $obj ) {
277 if( $n > 0 &&
278 $obj->rc_namespace >= 0 &&
279 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
280 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
281 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
282 } else {
283 $sorted[$n] = $obj;
284 $n++;
285 }
286 $first = false;
287 }
288
289 foreach( $sorted as $obj ) {
290 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
291 $talkpage = $title->getTalkPage();
292 $item = new FeedItem(
293 $title->getPrefixedText(),
294 rcFormatDiff( $obj ),
295 $title->getFullURL(),
296 $obj->rc_timestamp,
297 $obj->rc_user_text,
298 $talkpage->getFullURL()
299 );
300 $feed->outItem( $item );
301 }
302 $feed->outFooter();
303 }
304
305 /**
306 *
307 */
308 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
309 global $wgUser, $wgLang, $wgContLang;
310 $sk = $wgUser->getSkin();
311 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
312 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
313 ($d ? "days={$d}&" : '') . 'limit='.$lim );
314 return $s;
315 }
316
317 /**
318 *
319 */
320 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
321 global $wgUser, $wgLang, $wgContLang;
322 $sk = $wgUser->getSkin();
323 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
324 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
325 ($lim ? '&limit='.$lim : '') );
326 return $s;
327 }
328
329 /**
330 * Used also by Recentchangeslinked
331 */
332 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
333 $botLink = '', $liuLink = '', $patrLink = '' ) {
334 if ($more != '') $more .= '&';
335 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
336 rcCountLink( 100, $days, $page, $more ) . ' | ' .
337 rcCountLink( 250, $days, $page, $more ) . ' | ' .
338 rcCountLink( 500, $days, $page, $more ) .
339 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
340 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
341 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
342 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
343 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
344 rcDaysLink( $limit, 30, $page, $more ) .
345 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
346 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
347 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
348 return $note;
349 }
350
351 /**
352 * Format a diff for the newsfeed
353 */
354 function rcFormatDiff( $row ) {
355 $fname = 'rcFormatDiff';
356 wfProfileIn( $fname );
357
358 require_once( 'DifferenceEngine.php' );
359 $comment = "<p>" . htmlspecialchars( $row->rc_comment ) . "</p>\n";
360
361 if( $row->rc_namespace >= 0 ) {
362 global $wgContLang;
363
364 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
365 #$diff->showDiffPage();
366
367 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
368 $dbr =& wfGetDB( DB_SLAVE );
369 $newrev =& Revision::newFromTitle( $titleObj, $row->rc_this_oldid );
370 if( $newrev ) {
371 $newtext = $newrev->getText();
372 } else {
373 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
374 wfProfileOut( $fname );
375 return $comment . $diffText;
376 }
377
378 if( $row->rc_last_oldid ) {
379 wfProfileIn( "$fname-dodiff" );
380 $oldrev =& Revision::newFromId( $row->rc_last_oldid );
381 if( !$oldrev ) {
382 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
383 wfProfileOut( $fname );
384 return $comment . $diffText;
385 }
386 $oldtext = $oldrev->getText();
387
388 # Old entries may contain illegal characters
389 # which will damage output
390 $oldtext = UtfNormal::cleanUp( $oldtext );
391
392 global $wgFeedDiffCutoff;
393 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
394 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
395 $diffLink = $titleObj->escapeFullUrl(
396 'diff=' . $row->rc_this_oldid .
397 '&oldid=' . $row->rc_last_oldid );
398 $diffText = '<a href="' .
399 $diffLink .
400 '">' .
401 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
402 '</a>';
403 } else {
404 $diffText = DifferenceEngine::getDiff( $oldtext, $newtext,
405 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
406 wfMsg( 'currentrev' ) );
407 }
408 wfProfileOut( "$fname-dodiff" );
409 } else {
410 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
411 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
412 }
413
414 wfProfileOut( $fname );
415 return $comment . $diffText;
416 }
417
418 wfProfileOut( $fname );
419 return $comment;
420 }
421
422 ?>