Workaround for APC bug. Not fully isolated, but AutoLoader::$localClasses is implicat...
[lhc/web/wiklou.git] / includes / FeedUtils.php
1 <?php
2
3 // TODO: document
4 class FeedUtils {
5
6 public static function checkPurge( $timekey, $key ) {
7 global $wgRequest, $wgUser, $messageMemc;
8 $purge = $wgRequest->getVal( 'action' ) === 'purge';
9 if ( $purge && $wgUser->isAllowed('purge') ) {
10 $messageMemc->delete( $timekey );
11 $messageMemc->delete( $key );
12 }
13 }
14
15 public static function checkFeedOutput( $type ) {
16 global $wgFeed, $wgOut, $wgFeedClasses;
17
18 if ( !$wgFeed ) {
19 global $wgOut;
20 $wgOut->addWikiMsg( 'feed-unavailable' );
21 return false;
22 }
23
24 if( !isset( $wgFeedClasses[$type] ) ) {
25 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
26 return false;
27 }
28
29 return true;
30 }
31
32 /**
33 * Format a diff for the newsfeed
34 */
35 public static function formatDiff( $row ) {
36 global $wgUser;
37
38 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
39 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
40 $actiontext = '';
41 if( $row->rc_type == RC_LOG ) {
42 if( $row->rc_deleted & LogPage::DELETED_ACTION ) {
43 $actiontext = wfMsgHtml('rev-deleted-event');
44 } else {
45 $actiontext = LogPage::actionText( $row->rc_log_type, $row->rc_log_action,
46 $titleObj, $wgUser->getSkin(), LogPage::extractParams($row->rc_params,true,true) );
47 }
48 }
49 return self::formatDiffRow( $titleObj,
50 $row->rc_last_oldid, $row->rc_this_oldid,
51 $timestamp,
52 ($row->rc_deleted & Revision::DELETED_COMMENT) ? wfMsgHtml('rev-deleted-comment') : $row->rc_comment,
53 $actiontext );
54 }
55
56 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
57 global $wgFeedDiffCutoff, $wgContLang, $wgUser;
58 wfProfileIn( __FUNCTION__ );
59
60 $skin = $wgUser->getSkin();
61 # log enties
62 $completeText = '<p>' . implode( ' ',
63 array_filter(
64 array(
65 $actiontext,
66 $skin->formatComment( $comment ) ) ) ) . "</p>\n";
67
68 //NOTE: Check permissions for anonymous users, not current user.
69 // No "privileged" version should end up in the cache.
70 // Most feed readers will not log in anway.
71 $anon = new User();
72 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
73
74 if( $title->getNamespace() >= 0 && !$accErrors ) {
75 if( $oldid ) {
76 wfProfileIn( __FUNCTION__."-dodiff" );
77
78 $de = new DifferenceEngine( $title, $oldid, $newid );
79 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
80 # $wgContLang->timeanddate( $timestamp ) ),
81 # wfMsg( 'currentrev' ) );
82 $diffText = $de->getDiff(
83 wfMsg( 'previousrevision' ), // hack
84 wfMsg( 'revisionasof',
85 $wgContLang->timeanddate( $timestamp ) ) );
86
87
88 if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
89 // Omit large diffs
90 $diffLink = $title->escapeFullUrl(
91 'diff=' . $newid .
92 '&oldid=' . $oldid );
93 $diffText = '<a href="' .
94 $diffLink .
95 '">' .
96 htmlspecialchars( wfMsgForContent( 'showdiff' ) ) .
97 '</a>';
98 } elseif ( $diffText === false ) {
99 // Error in diff engine, probably a missing revision
100 $diffText = "<p>Can't load revision $newid</p>";
101 } else {
102 // Diff output fine, clean up any illegal UTF-8
103 $diffText = UtfNormal::cleanUp( $diffText );
104 $diffText = self::applyDiffStyle( $diffText );
105 }
106 wfProfileOut( __FUNCTION__."-dodiff" );
107 } else {
108 $rev = Revision::newFromId( $newid );
109 if( is_null( $rev ) ) {
110 $newtext = '';
111 } else {
112 $newtext = $rev->getText();
113 }
114 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
115 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
116 }
117 $completeText .= $diffText;
118 }
119
120 wfProfileOut( __FUNCTION__ );
121 return $completeText;
122 }
123
124 /**
125 * Hacky application of diff styles for the feeds.
126 * Might be 'cleaner' to use DOM or XSLT or something,
127 * but *gack* it's a pain in the ass.
128 *
129 * @param $text String:
130 * @return string
131 * @private
132 */
133 public static function applyDiffStyle( $text ) {
134 $styles = array(
135 'diff' => 'background-color: white; color:black;',
136 'diff-otitle' => 'background-color: white; color:black;',
137 'diff-ntitle' => 'background-color: white; color:black;',
138 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
139 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
140 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
141 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
142 );
143
144 foreach( $styles as $class => $style ) {
145 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
146 "\\1style=\"$style\"\\3", $text );
147 }
148
149 return $text;
150 }
151
152 }