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