ac4a8c5077fd42979ba8e3c9fb1c451befa81873
[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, $wgRequest, $wgUseRCPatrol;
20 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
21 global $wgLinkCache;
22 $fname = 'wfSpecialRecentchanges';
23
24 # Get query parameters
25 $feedFormat = $wgRequest->getVal( 'feed' );
26
27 $defaults = array(
28 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
29 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
30 /* bool */ 'hideminor' => false,
31 /* bool */ 'hidebots' => true,
32 /* bool */ 'hideliu' => false,
33 /* bool */ 'hidepatrolled' => false,
34 /* text */ 'from' => '',
35 /* text */ 'namespace' => null,
36 /* bool */ 'invert' => false,
37 );
38
39 extract($defaults);
40
41
42 $days = $wgUser->getOption( 'rcdays' );
43 if ( !$days ) { $days = $defaults['days']; }
44 $days = $wgRequest->getInt( 'days', $days );
45
46 $limit = $wgUser->getOption( 'rclimit' );
47 if ( !$limit ) { $limit = $defaults['limit']; }
48
49 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
50 $limit = $wgRequest->getInt( 'limit', $limit );
51
52 /* order of selection: url > preferences > default */
53 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ? true : $defaults['hideminor'] );
54
55
56 # As a feed, use limited settings only
57 if( $feedFormat ) {
58 global $wgFeedLimit;
59 if( $limit > $wgFeedLimit ) {
60 $options['limit'] = $wgFeedLimit;
61 }
62
63 } else {
64
65 $namespace = $wgRequest->getIntOrNull( 'namespace' );
66 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
67 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
68 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
69 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
70 $from = $wgRequest->getVal( 'from', $defaults['from'] );
71
72 # Get query parameters from path
73 if( $par ) {
74 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
75 foreach ( $bits as $bit ) {
76 if ( 'hidebots' == $bit ) $hidebots = 1;
77 if ( 'bots' == $bit ) $hidebots = 0;
78 if ( 'hideminor' == $bit ) $hideminor = 1;
79 if ( 'minor' == $bit ) $hideminor = 0;
80 if ( 'hideliu' == $bit ) $hideliu = 1;
81 if ( 'hidepatrolled' == $bit ) $hidepatrolled = 1;
82
83 if ( is_numeric( $bit ) ) {
84 $limit = $bit;
85 }
86
87 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
88 $limit = $m[1];
89 }
90
91 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
92 $days = $m[1];
93 }
94 }
95 }
96 }
97
98 if ( $limit < 0 || $limit > 5000 ) $limit = $defaults['limit'];
99
100
101 # Database connection and caching
102 $dbr =& wfGetDB( DB_SLAVE );
103 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
104
105
106 $cutoff_unixtime = time() - ( $days * 86400 );
107 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
108 $cutoff = $dbr->timestamp( $cutoff_unixtime );
109 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW,$cutoff)) {
110 $cutoff = $dbr->timestamp($from);
111 } else {
112 $from = $defaults['from'];
113 }
114
115 # 10 seconds server-side caching max
116 $wgOut->setSquidMaxage( 10 );
117
118 # Get last modified date, for client caching
119 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
120 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
121 if ( $feedFormat || !$wgUseRCPatrol ) {
122 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
123 # Client cache fresh and headers sent, nothing more to do.
124 return;
125 }
126 }
127
128 $hidem = $hideminor ? 'AND rc_minor=0' : '';
129 $hidem .= $hidebots ? ' AND rc_bot=0' : '';
130 $hidem .= $hideliu ? ' AND rc_user=0' : '';
131 $hidem .= $hidepatrolled ? ' AND rc_patrolled=0' : '';
132 $hidem .= is_null( $namespace ) ? '' : ' AND rc_namespace' . ($invert ? '!=' : '=') . $namespace;
133
134 // This is the big thing!
135
136 $uid = $wgUser->getID();
137
138 // Perform query
139 $sql2 = "SELECT * FROM $recentchanges FORCE INDEX (rc_timestamp) " .
140 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
141 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
142 "ORDER BY rc_timestamp DESC";
143 $sql2 = $dbr->limitResult($sql2, $limit, 0);
144 $res = $dbr->query( $sql2, $fname );
145
146 // Fetch results, prepare a batch link existence check query
147 $rows = array();
148 $batch = new LinkBatch;
149 while( $row = $dbr->fetchObject( $res ) ){
150 $rows[] = $row;
151 if ( !$feedFormat ) {
152 // User page link
153 $title = Title::makeTitleSafe( NS_USER, $row->rc_user_text );
154 $batch->addObj( $title );
155
156 // User talk
157 $title = Title::makeTitleSafe( NS_USER_TALK, $row->rc_user_text );
158 $batch->addObj( $title );
159 }
160
161 }
162 $dbr->freeResult( $res );
163
164 if( $feedFormat ) {
165 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
166 } else {
167
168 # Web output...
169
170 // Run existence checks
171 $batch->execute( $wgLinkCache );
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 $fname = 'rcDoOutputFeed';
301 wfProfileIn( $fname );
302
303 $feed->outHeader();
304
305 # Merge adjacent edits by one user
306 $sorted = array();
307 $n = 0;
308 foreach( $rows as $obj ) {
309 if( $n > 0 &&
310 $obj->rc_namespace >= 0 &&
311 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
312 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
313 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
314 } else {
315 $sorted[$n] = $obj;
316 $n++;
317 }
318 $first = false;
319 }
320
321 foreach( $sorted as $obj ) {
322 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
323 $talkpage = $title->getTalkPage();
324 $item = new FeedItem(
325 $title->getPrefixedText(),
326 rcFormatDiff( $obj ),
327 $title->getFullURL(),
328 $obj->rc_timestamp,
329 $obj->rc_user_text,
330 $talkpage->getFullURL()
331 );
332 $feed->outItem( $item );
333 }
334 $feed->outFooter();
335 wfProfileOut( $fname );
336 }
337
338 /**
339 *
340 */
341 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
342 global $wgUser, $wgLang, $wgContLang;
343 $sk = $wgUser->getSkin();
344 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
345 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
346 ($d ? "days={$d}&" : '') . 'limit='.$lim );
347 return $s;
348 }
349
350 /**
351 *
352 */
353 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
354 global $wgUser, $wgLang, $wgContLang;
355 $sk = $wgUser->getSkin();
356 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
357 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
358 ($lim ? '&limit='.$lim : '') );
359 return $s;
360 }
361
362 /**
363 * Used by Recentchangeslinked
364 */
365 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
366 $botLink = '', $liuLink = '', $patrLink = '' ) {
367 if ($more != '') $more .= '&';
368 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
369 rcCountLink( 100, $days, $page, $more ) . ' | ' .
370 rcCountLink( 250, $days, $page, $more ) . ' | ' .
371 rcCountLink( 500, $days, $page, $more ) .
372 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
373 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
374 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
375 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
376 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
377 rcDaysLink( $limit, 30, $page, $more ) .
378 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
379 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
380 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
381 return $note;
382 }
383
384
385 /**
386 * Makes change an option link which carries all the other options
387 */
388 function makeOptionsLink( $title, $override, $options ) {
389 global $wgUser, $wgLang, $wgContLang;
390 $sk = $wgUser->getSkin();
391 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
392 $title, wfArrayToCGI( $override, $options ) );
393 }
394
395 /**
396 * Creates the options panel
397 */
398 function rcOptionsPanel( $defaults, $nondefaults ) {
399 global $wgLang;
400
401 $options = $nondefaults + $defaults;
402
403 if( $options['from'] )
404 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $options['limit'] ), $wgLang->timeanddate( $options['from'], true ) );
405 else
406 $note = wfMsg( 'rcnote', $wgLang->formatNum( $options['limit'] ), $wgLang->formatNum( $options['days'] ) );
407
408 // limit links
409 $cl = '';
410 $options_limit = array(50, 100, 250, 500);
411 $i = 0;
412 while ( $i+1 < count($options_limit) ) {
413 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) . ' | ' ;
414 $i++;
415 }
416 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) ;
417
418 // day links, reset 'from' to none
419 $dl = '';
420 $options_days = array(1, 3, 7, 14, 30);
421 $i = 0;
422 while ( $i+1 < count($options_days) ) {
423 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) . ' | ' ;
424 $i++;
425 }
426 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) ;
427
428 // show/hide links
429 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
430 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
431 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
432 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
433 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
434 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
435 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
436 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
437 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
438
439 $hl = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
440
441 // show from this onward link
442 $now = $wgLang->timeanddate( wfTimestampNow(), true );
443 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
444
445 $rclinks = wfMsg( 'rclinks', $cl, $dl, $hl );
446 $rclistfrom = wfMsg( 'rclistfrom', $tl );
447 return "$note<br />$rclinks<br />$rclistfrom";
448
449 }
450
451 /**<F2>
452 * Creates the choose namespace selection
453 *
454 * @access private
455 *
456 * @param mixed $namespace The key of the currently selected namespace, empty string
457 * if there is none
458 * @param bool $invert Whether to invert the namespace selection
459 * @param array $nondefaults An array of non default options to be remembered
460 *
461 * @return string
462 */
463 function rcNamespaceForm ( $namespace, $invert, $nondefaults ) {
464 global $wgContLang, $wgScript;
465 $t = Title::makeTitle( NS_SPECIAL, 'Recentchanges' );
466
467 $namespaceselect = HTMLnamespaceselector($namespace, '');
468 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
469 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
470
471 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
472
473 foreach ( $nondefaults as $key => $value ) {
474 if ($key != 'namespace' && $key != 'invert')
475 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
476 }
477
478 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
479 $out .= "
480 <div id='nsselect' class='recentchanges'>
481 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
482 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>\n</div>";
483 $out .= '</form></div>';
484 return $out;
485 }
486
487
488 /**
489 * Format a diff for the newsfeed
490 */
491 function rcFormatDiff( $row ) {
492 global $wgFeedDiffCutoff, $wgContLang;
493 $fname = 'rcFormatDiff';
494 wfProfileIn( $fname );
495
496 require_once( 'DifferenceEngine.php' );
497 $completeText = '<p>' . htmlspecialchars( $row->rc_comment ) . "</p>\n";
498
499 if( $row->rc_namespace >= 0 ) {
500 if( $row->rc_last_oldid ) {
501 wfProfileIn( "$fname-dodiff" );
502
503 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
504 $de = new DifferenceEngine( $titleObj, $row->rc_last_oldid, $row->rc_this_oldid );
505 $diffText = $de->getDiff( wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
506 wfMsg( 'currentrev' ) );
507
508 if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
509 // Omit large diffs
510 $diffLink = $titleObj->escapeFullUrl(
511 'diff=' . $row->rc_this_oldid .
512 '&oldid=' . $row->rc_last_oldid );
513 $diffText = '<a href="' .
514 $diffLink .
515 '">' .
516 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
517 '</a>';
518 } elseif ( $diffText === false ) {
519 // Error in diff engine, probably a missing revision
520 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
521 } else {
522 // Diff output fine, clean up any illegal UTF-8
523 $diffText = UtfNormal::cleanUp( $diffText );
524 }
525 wfProfileOut( "$fname-dodiff" );
526 } else {
527 $rev = Revision::newFromId( $row->rc_this_oldid );
528 if( is_null( $rev ) ) {
529 $newtext = '';
530 } else {
531 $newtext = $rev->getText();
532 }
533 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
534 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
535 }
536 $completeText .= $diffText;
537 }
538
539 wfProfileOut( $fname );
540 return $completeText;
541 }
542
543 ?>