* (bug 2392) Fix Atom items content type, upgrade to Atom 1.0
[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, $specialPage ) {
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 $defaults = array(
30 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
31 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
32 /* bool */ 'hideminor' => false,
33 /* bool */ 'hidebots' => true,
34 /* bool */ 'hideliu' => false,
35 /* bool */ 'hidepatrolled' => false,
36 /* text */ 'from' => '',
37 /* text */ 'namespace' => null,
38 /* bool */ 'invert' => false,
39 );
40
41 extract($defaults);
42
43
44 $days = $wgUser->getOption( 'rcdays' );
45 if ( !$days ) { $days = $defaults['days']; }
46 $days = $wgRequest->getInt( 'days', $days );
47
48 $limit = $wgUser->getOption( 'rclimit' );
49 if ( !$limit ) { $limit = $defaults['limit']; }
50
51 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
52 $limit = $wgRequest->getInt( 'limit', $limit );
53
54 /* order of selection: url > preferences > default */
55 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ? true : $defaults['hideminor'] );
56
57
58 # As a feed, use limited settings only
59 if( $feedFormat ) {
60 global $wgFeedLimit;
61 if( $limit > $wgFeedLimit ) {
62 $options['limit'] = $wgFeedLimit;
63 }
64
65 } else {
66
67 $namespace = $wgRequest->getIntOrNull( 'namespace' );
68 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
69 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
70 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
71 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
72 $from = $wgRequest->getVal( 'from', $defaults['from'] );
73
74 # Get query parameters from path
75 if( $par ) {
76 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
77 foreach ( $bits as $bit ) {
78 if ( 'hidebots' == $bit ) $hidebots = 1;
79 if ( 'bots' == $bit ) $hidebots = 0;
80 if ( 'hideminor' == $bit ) $hideminor = 1;
81 if ( 'minor' == $bit ) $hideminor = 0;
82 if ( 'hideliu' == $bit ) $hideliu = 1;
83 if ( 'hidepatrolled' == $bit ) $hidepatrolled = 1;
84
85 if ( is_numeric( $bit ) ) {
86 $limit = $bit;
87 }
88
89 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
90 $limit = $m[1];
91 }
92
93 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
94 $days = $m[1];
95 }
96 }
97 }
98 }
99
100 if ( $limit < 0 || $limit > 5000 ) $limit = $defaults['limit'];
101
102
103 # Database connection and caching
104 $dbr =& wfGetDB( DB_SLAVE );
105 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
106
107
108 $cutoff_unixtime = time() - ( $days * 86400 );
109 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
110 $cutoff = $dbr->timestamp( $cutoff_unixtime );
111 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW,$cutoff)) {
112 $cutoff = $dbr->timestamp($from);
113 } else {
114 $from = $defaults['from'];
115 }
116
117 # 10 seconds server-side caching max
118 $wgOut->setSquidMaxage( 10 );
119
120 # Get last modified date, for client caching
121 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
122 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
123 if ( $feedFormat || !$wgUseRCPatrol ) {
124 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
125 # Client cache fresh and headers sent, nothing more to do.
126 return;
127 }
128 }
129
130 $hidem = $hideminor ? 'AND rc_minor=0' : '';
131 $hidem .= $hidebots ? ' AND rc_bot=0' : '';
132 $hidem .= $hideliu ? ' AND rc_user=0' : '';
133 $hidem .= $hidepatrolled ? ' AND rc_patrolled=0' : '';
134 $hidem .= is_null( $namespace ) ? '' : ' AND rc_namespace' . ($invert ? '!=' : '=') . $namespace;
135
136 // This is the big thing!
137
138 $uid = $wgUser->getID();
139
140 // Perform query
141 $sql2 = "SELECT * FROM $recentchanges " .
142 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
143 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
144 "ORDER BY rc_timestamp DESC";
145 $sql2 = $dbr->limitResult($sql2, $limit, 0);
146 $res = $dbr->query( $sql2, $fname );
147
148 // Fetch results, prepare a batch link existence check query
149 $rows = array();
150 $batch = new LinkBatch;
151 while( $row = $dbr->fetchObject( $res ) ){
152 $rows[] = $row;
153 // User page link
154 $title = Title::makeTitleSafe( NS_USER, $row->rc_user_text );
155 $batch->addObj( $title );
156
157 // User talk
158 $title = Title::makeTitleSafe( NS_USER_TALK, $row->rc_user_text );
159 $batch->addObj( $title );
160
161 }
162 $dbr->freeResult( $res );
163
164 // Run existence checks
165 $batch->execute( $wgLinkCache );
166
167 if( $feedFormat ) {
168 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
169 } else {
170
171 # Web output...
172
173 // Output header
174 if ( !$specialPage->including() ) {
175 $wgOut->addWikiText( wfMsgForContent( "recentchangestext" ) );
176
177 // Dump everything here
178 $nondefaults = array();
179
180 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
181 wfAppendToArrayIfNotDefault( 'limit', $limit , $defaults, $nondefaults);
182 wfAppendToArrayIfNotDefault( 'hideminor', $hideminor, $defaults, $nondefaults);
183 wfAppendToArrayIfNotDefault( 'hidebots', $hidebots, $defaults, $nondefaults);
184 wfAppendToArrayIfNotDefault( 'hideliu', $hideliu, $defaults, $nondefaults);
185 wfAppendToArrayIfNotDefault( 'hidepatrolled', $hidepatrolled, $defaults, $nondefaults);
186 wfAppendToArrayIfNotDefault( 'from', $from, $defaults, $nondefaults);
187 wfAppendToArrayIfNotDefault( 'namespace', $namespace, $defaults, $nondefaults);
188 wfAppendToArrayIfNotDefault( 'invert', $invert, $defaults, $nondefaults);
189
190 // Add end of the texts
191 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $defaults, $nondefaults ) . "\n" );
192 $wgOut->addHTML( rcNamespaceForm( $namespace, $invert, $nondefaults) . '</div>'."\n");
193 }
194
195 // And now for the content
196 $sk = $wgUser->getSkin();
197 $wgOut->setSyndicated( true );
198
199 $list = ChangesList::newFromUser( $wgUser );
200
201 $s = $list->beginRecentChangesList();
202 $counter = 1;
203 foreach( $rows as $obj ){
204 if( $limit == 0) {
205 break;
206 }
207
208 if ( ! ( $hideminor && $obj->rc_minor ) &&
209 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
210 $rc = RecentChange::newFromRow( $obj );
211 $rc->counter = $counter++;
212
213 if ($wgShowUpdatedMarker
214 && !empty( $obj->wl_notificationtimestamp )
215 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
216 $rc->notificationtimestamp = true;
217 } else {
218 $rc->notificationtimestamp = false;
219 }
220
221 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
222 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title) ."' AND wl_namespace=$obj->rc_namespace" ;
223 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
224 $x = $dbr->fetchObject( $res3 );
225 $rc->numberofWatchingusers = $x->n;
226 } else {
227 $rc->numberofWatchingusers = 0;
228 }
229 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
230 --$limit;
231 }
232 }
233 $s .= $list->endRecentChangesList();
234 $wgOut->addHTML( $s );
235 }
236 }
237
238 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
239 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
240 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
241
242 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
243 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
244 return false;
245 }
246
247 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
248 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
249
250 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
251 ' [' . $wgContLanguageCode . ']';
252 $feed = new $wgFeedClasses[$feedFormat](
253 $feedTitle,
254 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
255 $wgTitle->getFullUrl() );
256
257 /**
258 * Bumping around loading up diffs can be pretty slow, so where
259 * possible we want to cache the feed output so the next visitor
260 * gets it quick too.
261 */
262 $cachedFeed = false;
263 if( ( $wgFeedCacheTimeout > 0 ) && ( $feedLastmod = $messageMemc->get( $timekey ) ) ) {
264 /**
265 * If the cached feed was rendered very recently, we may
266 * go ahead and use it even if there have been edits made
267 * since it was rendered. This keeps a swarm of requests
268 * from being too bad on a super-frequently edited wiki.
269 */
270 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
271 < $wgFeedCacheTimeout
272 || wfTimestamp( TS_UNIX, $feedLastmod )
273 > wfTimestamp( TS_UNIX, $lastmod ) ) {
274 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
275 $cachedFeed = $messageMemc->get( $key );
276 } else {
277 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
278 }
279 }
280 if( is_string( $cachedFeed ) ) {
281 wfDebug( "RC: Outputting cached feed\n" );
282 $feed->httpHeaders();
283 echo $cachedFeed;
284 } else {
285 wfDebug( "RC: rendering new feed and caching it\n" );
286 ob_start();
287 rcDoOutputFeed( $rows, $feed );
288 $cachedFeed = ob_get_contents();
289 ob_end_flush();
290
291 $expire = 3600 * 24; # One day
292 $messageMemc->set( $key, $cachedFeed );
293 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
294 }
295 return true;
296 }
297
298 function rcDoOutputFeed( $rows, &$feed ) {
299 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
300
301 $feed->outHeader();
302
303 # Merge adjacent edits by one user
304 $sorted = array();
305 $n = 0;
306 foreach( $rows as $obj ) {
307 if( $n > 0 &&
308 $obj->rc_namespace >= 0 &&
309 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
310 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
311 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
312 } else {
313 $sorted[$n] = $obj;
314 $n++;
315 }
316 $first = false;
317 }
318
319 foreach( $sorted as $obj ) {
320 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
321 $talkpage = $title->getTalkPage();
322 $item = new FeedItem(
323 $title->getPrefixedText(),
324 rcFormatDiff( $obj ),
325 $title->getFullURL(),
326 $obj->rc_timestamp,
327 $obj->rc_user_text,
328 $talkpage->getFullURL()
329 );
330 $feed->outItem( $item );
331 }
332 $feed->outFooter();
333 }
334
335 /**
336 *
337 */
338 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
339 global $wgUser, $wgLang, $wgContLang;
340 $sk = $wgUser->getSkin();
341 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
342 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
343 ($d ? "days={$d}&" : '') . 'limit='.$lim );
344 return $s;
345 }
346
347 /**
348 *
349 */
350 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
351 global $wgUser, $wgLang, $wgContLang;
352 $sk = $wgUser->getSkin();
353 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
354 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
355 ($lim ? '&limit='.$lim : '') );
356 return $s;
357 }
358
359 /**
360 * Used by Recentchangeslinked
361 */
362 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
363 $botLink = '', $liuLink = '', $patrLink = '' ) {
364 if ($more != '') $more .= '&';
365 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
366 rcCountLink( 100, $days, $page, $more ) . ' | ' .
367 rcCountLink( 250, $days, $page, $more ) . ' | ' .
368 rcCountLink( 500, $days, $page, $more ) .
369 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
370 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
371 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
372 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
373 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
374 rcDaysLink( $limit, 30, $page, $more ) .
375 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
376 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
377 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
378 return $note;
379 }
380
381
382 /**
383 * Makes change an option link which carries all the other options
384 */
385 function makeOptionsLink( $title, $override, $options ) {
386 global $wgUser, $wgLang, $wgContLang;
387 $sk = $wgUser->getSkin();
388 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
389 $title, wfArrayToCGI( $override, $options ) );
390 }
391
392 /**
393 * Creates the options panel
394 */
395 function rcOptionsPanel( $defaults, $nondefaults ) {
396 global $wgLang;
397
398 $options = $nondefaults + $defaults;
399
400 if( $options['from'] )
401 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $options['limit'] ), $wgLang->timeanddate( $options['from'], true ) );
402 else
403 $note = wfMsg( 'rcnote', $wgLang->formatNum( $options['limit'] ), $wgLang->formatNum( $options['days'] ) );
404
405 // limit links
406 $cl = '';
407 $options_limit = array(50, 100, 250, 500);
408 $i = 0;
409 while ( $i+1 < count($options_limit) ) {
410 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) . ' | ' ;
411 $i++;
412 }
413 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) ;
414
415 // day links, reset 'from' to none
416 $dl = '';
417 $options_days = array(1, 3, 7, 14, 30);
418 $i = 0;
419 while ( $i+1 < count($options_days) ) {
420 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) . ' | ' ;
421 $i++;
422 }
423 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) ;
424
425 // show/hide links
426 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
427 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
428 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
429 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
430 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
431 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
432 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
433 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
434 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
435
436 $hl = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
437
438 // show from this onward link
439 $now = $wgLang->timeanddate( wfTimestampNow(), true );
440 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
441
442 $rclinks = wfMsg( 'rclinks', $cl, $dl, $hl );
443 $rclistfrom = wfMsg( 'rclistfrom', $tl );
444 return "$note<br />$rclinks<br />$rclistfrom";
445
446 }
447
448 /**<F2>
449 * Creates the choose namespace selection
450 *
451 * @access private
452 *
453 * @param mixed $namespace The key of the currently selected namespace, empty string
454 * if there is none
455 * @param bool $invert Whether to invert the namespace selection
456 * @param array $nondefaults An array of non default options to be remembered
457 *
458 * @return string
459 */
460 function rcNamespaceForm ( $namespace, $invert, $nondefaults ) {
461 global $wgContLang, $wgScript;
462 $t = Title::makeTitle( NS_SPECIAL, 'Recentchanges' );
463
464 $namespaceselect = HTMLnamespaceselector($namespace, '');
465 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
466 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
467
468 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
469
470 foreach ( $nondefaults as $key => $value ) {
471 if ($key != 'namespace' && $key != 'invert')
472 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
473 }
474
475 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
476 $out .= "
477 <div id='nsselect' class='recentchanges'>
478 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
479 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>\n</div>";
480 $out .= '</form></div>';
481 return $out;
482 }
483
484
485 /**
486 * Format a diff for the newsfeed
487 */
488 function rcFormatDiff( $row ) {
489 $fname = 'rcFormatDiff';
490 wfProfileIn( $fname );
491
492 require_once( 'DifferenceEngine.php' );
493 $comment = '<p>' . htmlspecialchars( $row->rc_comment ) . "</p>\n";
494
495 if( $row->rc_namespace >= 0 ) {
496 global $wgContLang;
497
498 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
499 #$diff->showDiffPage();
500
501 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
502 $dbr =& wfGetDB( DB_SLAVE );
503 $newrev = Revision::newFromTitle( $titleObj, $row->rc_this_oldid );
504 if( $newrev ) {
505 $newtext = $newrev->getText();
506 } else {
507 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
508 wfProfileOut( $fname );
509 return $comment . $diffText;
510 }
511
512 if( $row->rc_last_oldid ) {
513 wfProfileIn( "$fname-dodiff" );
514 $oldrev =& Revision::newFromId( $row->rc_last_oldid );
515 if( !$oldrev ) {
516 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
517 wfProfileOut( $fname );
518 return $comment . $diffText;
519 }
520 $oldtext = $oldrev->getText();
521
522 # Old entries may contain illegal characters
523 # which will damage output
524 $oldtext = UtfNormal::cleanUp( $oldtext );
525
526 global $wgFeedDiffCutoff;
527 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
528 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
529 $diffLink = $titleObj->escapeFullUrl(
530 'diff=' . $row->rc_this_oldid .
531 '&oldid=' . $row->rc_last_oldid );
532 $diffText = '<a href="' .
533 $diffLink .
534 '">' .
535 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
536 '</a>';
537 } else {
538 $diffText = DifferenceEngine::getDiff( $oldtext, $newtext,
539 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
540 wfMsg( 'currentrev' ) );
541 }
542 wfProfileOut( "$fname-dodiff" );
543 } else {
544 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
545 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
546 }
547
548 wfProfileOut( $fname );
549 return $comment . $diffText;
550 }
551
552 wfProfileOut( $fname );
553 return $comment;
554 }
555
556 ?>