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