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