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