* Removed the troublesome regular expression in isValidEmailAddr which returns
[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 'limit' => $limit );
110 $hideparams = wfArrayToCGI( $urlparams );
111
112 $minorLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
113 $showhide[1-$hideminor], wfArrayToCGI( array( 'hideminor' => 1-$hideminor ), $urlparams ) );
114 $botLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
115 $showhide[1-$hidebots], wfArrayToCGI( array( 'hidebots' => 1-$hidebots ), $urlparams ) );
116 $liuLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
117 $showhide[1-$hideliu], wfArrayToCGI( array( 'hideliu' => 1-$hideliu ), $urlparams ) );
118 $patrLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
119 $showhide[1-$hidepatrolled], wfArrayToCGI( array( 'hidepatrolled' => 1-$hidepatrolled ), $urlparams ) );
120
121 $uid = $wgUser->getID();
122
123 // Perform query
124 $sql2 = "SELECT $recentchanges.*" . ($uid ? ",wl_user,wl_notificationtimestamp" : "") . " FROM $recentchanges " .
125 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
126 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
127 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
128 $res = $dbr->query( $sql2, $fname );
129
130 // Fetch results, prepare a batch link existence check query
131 $rows = array();
132 $batch = new LinkBatch;
133 while( $row = $dbr->fetchObject( $res ) ){
134 $rows[] = $row;
135 // User page link
136 $title = Title::makeTitleSafe( NS_USER, $row->rc_user_text );
137 $batch->addObj( $title );
138
139 // User talk
140 $title = Title::makeTitleSafe( NS_USER_TALK, $row->rc_user_text );
141 $batch->addObj( $title );
142
143 }
144 $dbr->freeResult( $res );
145
146 // Run existence checks
147 $batch->execute( $wgLinkCache );
148
149 if(isset($from)) {
150 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
151 $wgLang->timeanddate( $from, true ) );
152 } else {
153 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
154 }
155 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
156
157 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
158
159 $note .= "<br />\n" . wfMsg( 'rclistfrom',
160 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
161 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
162
163 $wgOut->addHTML( $note."\n" );
164
165 if( $feedFormat ) {
166 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
167 } else {
168 # Web output...
169 $wgOut->setSyndicated( true );
170 $list =& new ChangesList( $sk );
171 $s = $list->beginRecentChangesList();
172 $counter = 1;
173 foreach( $rows as $obj ){
174 if( $limit == 0) {
175 break;
176 }
177
178 if ( ! ( $hideminor && $obj->rc_minor ) &&
179 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
180 $rc = RecentChange::newFromRow( $obj );
181 $rc->counter = $counter++;
182
183 if ($wgShowUpdatedMarker
184 && $wgUser->getOption( 'showupdated' )
185 && !empty( $obj->wl_notificationtimestamp )
186 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
187 $rc->notificationtimestamp = true;
188 } else {
189 $rc->notificationtimestamp = false;
190 }
191
192 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
193 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title) ."' AND wl_namespace=$obj->rc_namespace" ;
194 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
195 $x = $dbr->fetchObject( $res3 );
196 $rc->numberofWatchingusers = $x->n;
197 } else {
198 $rc->numberofWatchingusers = 0;
199 }
200 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
201 --$limit;
202 }
203 }
204 $s .= $list->endRecentChangesList();
205 $wgOut->addHTML( $s );
206 }
207 }
208
209 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
210 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
211 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
212
213 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
214 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
215 return false;
216 }
217
218 $timekey = "$wgDBname:rcfeed:timestamp";
219 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
220
221 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
222 ' [' . $wgContLanguageCode . ']';
223 $feed = new $wgFeedClasses[$feedFormat](
224 $feedTitle,
225 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
226 $wgTitle->getFullUrl() );
227
228 /**
229 * Bumping around loading up diffs can be pretty slow, so where
230 * possible we want to cache the feed output so the next visitor
231 * gets it quick too.
232 */
233 $cachedFeed = false;
234 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
235 /**
236 * If the cached feed was rendered very recently, we may
237 * go ahead and use it even if there have been edits made
238 * since it was rendered. This keeps a swarm of requests
239 * from being too bad on a super-frequently edited wiki.
240 */
241 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
242 < $wgFeedCacheTimeout
243 || wfTimestamp( TS_UNIX, $feedLastmod )
244 > wfTimestamp( TS_UNIX, $lastmod ) ) {
245 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
246 $cachedFeed = $messageMemc->get( $key );
247 } else {
248 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
249 }
250 }
251 if( is_string( $cachedFeed ) ) {
252 wfDebug( "RC: Outputting cached feed\n" );
253 $feed->httpHeaders();
254 echo $cachedFeed;
255 } else {
256 wfDebug( "RC: rendering new feed and caching it\n" );
257 ob_start();
258 rcDoOutputFeed( $rows, $feed );
259 $cachedFeed = ob_get_contents();
260 ob_end_flush();
261
262 $expire = 3600 * 24; # One day
263 $messageMemc->set( $key, $cachedFeed );
264 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
265 }
266 return true;
267 }
268
269 function rcDoOutputFeed( $rows, &$feed ) {
270 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
271
272 $feed->outHeader();
273
274 # Merge adjacent edits by one user
275 $sorted = array();
276 $n = 0;
277 foreach( $rows as $obj ) {
278 if( $n > 0 &&
279 $obj->rc_namespace >= 0 &&
280 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
281 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
282 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
283 } else {
284 $sorted[$n] = $obj;
285 $n++;
286 }
287 $first = false;
288 }
289
290 foreach( $sorted as $obj ) {
291 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
292 $talkpage = $title->getTalkPage();
293 $item = new FeedItem(
294 $title->getPrefixedText(),
295 rcFormatDiff( $obj ),
296 $title->getFullURL(),
297 $obj->rc_timestamp,
298 $obj->rc_user_text,
299 $talkpage->getFullURL()
300 );
301 $feed->outItem( $item );
302 }
303 $feed->outFooter();
304 }
305
306 /**
307 *
308 */
309 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
310 global $wgUser, $wgLang, $wgContLang;
311 $sk = $wgUser->getSkin();
312 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
313 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
314 ($d ? "days={$d}&" : '') . 'limit='.$lim );
315 return $s;
316 }
317
318 /**
319 *
320 */
321 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
322 global $wgUser, $wgLang, $wgContLang;
323 $sk = $wgUser->getSkin();
324 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
325 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
326 ($lim ? '&limit='.$lim : '') );
327 return $s;
328 }
329
330 /**
331 * Used also by Recentchangeslinked
332 */
333 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
334 $botLink = '', $liuLink = '', $patrLink = '' ) {
335 if ($more != '') $more .= '&';
336 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
337 rcCountLink( 100, $days, $page, $more ) . ' | ' .
338 rcCountLink( 250, $days, $page, $more ) . ' | ' .
339 rcCountLink( 500, $days, $page, $more ) .
340 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
341 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
342 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
343 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
344 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
345 rcDaysLink( $limit, 30, $page, $more ) .
346 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
347 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
348 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
349 return $note;
350 }
351
352 /**
353 * Format a diff for the newsfeed
354 */
355 function rcFormatDiff( $row ) {
356 $fname = 'rcFormatDiff';
357 wfProfileIn( $fname );
358
359 require_once( 'DifferenceEngine.php' );
360 $comment = "<p>" . htmlspecialchars( $row->rc_comment ) . "</p>\n";
361
362 if( $row->rc_namespace >= 0 ) {
363 global $wgContLang;
364
365 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
366 #$diff->showDiffPage();
367
368 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
369 $dbr =& wfGetDB( DB_SLAVE );
370 $newrev =& Revision::newFromTitle( $titleObj, $row->rc_this_oldid );
371 if( $newrev ) {
372 $newtext = $newrev->getText();
373 } else {
374 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
375 wfProfileOut( $fname );
376 return $comment . $diffText;
377 }
378
379 if( $row->rc_last_oldid ) {
380 wfProfileIn( "$fname-dodiff" );
381 $oldrev =& Revision::newFromId( $row->rc_last_oldid );
382 if( !$oldrev ) {
383 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
384 wfProfileOut( $fname );
385 return $comment . $diffText;
386 }
387 $oldtext = $oldrev->getText();
388
389 # Old entries may contain illegal characters
390 # which will damage output
391 $oldtext = UtfNormal::cleanUp( $oldtext );
392
393 global $wgFeedDiffCutoff;
394 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
395 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
396 $diffLink = $titleObj->escapeFullUrl(
397 'diff=' . $row->rc_this_oldid .
398 '&oldid=' . $row->rc_last_oldid );
399 $diffText = '<a href="' .
400 $diffLink .
401 '">' .
402 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
403 '</a>';
404 } else {
405 $diffText = DifferenceEngine::getDiff( $oldtext, $newtext,
406 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
407 wfMsg( 'currentrev' ) );
408 }
409 wfProfileOut( "$fname-dodiff" );
410 } else {
411 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
412 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
413 }
414
415 wfProfileOut( $fname );
416 return $comment . $diffText;
417 }
418
419 wfProfileOut( $fname );
420 return $comment;
421 }
422
423 ?>