Merge "Use local context to get messages"
[lhc/web/wiklou.git] / includes / FeedUtils.php
1 <?php
2
3 /**
4 * Helper functions for feeds
5 *
6 * @ingroup Feed
7 */
8 class FeedUtils {
9
10 /**
11 * Check whether feed's cache should be cleared; for changes feeds
12 * If the feed should be purged; $timekey and $key will be removed from
13 * $messageMemc
14 *
15 * @param $timekey String: cache key of the timestamp of the last item
16 * @param $key String: cache key of feed's content
17 */
18 public static function checkPurge( $timekey, $key ) {
19 global $wgRequest, $wgUser, $messageMemc;
20 $purge = $wgRequest->getVal( 'action' ) === 'purge';
21 if ( $purge && $wgUser->isAllowed('purge') ) {
22 $messageMemc->delete( $timekey );
23 $messageMemc->delete( $key );
24 }
25 }
26
27 /**
28 * Check whether feeds can be used and that $type is a valid feed type
29 *
30 * @param $type String: feed type, as requested by the user
31 * @return Boolean
32 */
33 public static function checkFeedOutput( $type ) {
34 global $wgOut, $wgFeed, $wgFeedClasses;
35
36 if ( !$wgFeed ) {
37 $wgOut->addWikiMsg( 'feed-unavailable' );
38 return false;
39 }
40
41 if( !isset( $wgFeedClasses[$type] ) ) {
42 $wgOut->addWikiMsg( 'feed-invalid' );
43 return false;
44 }
45
46 return true;
47 }
48
49 /**
50 * Format a diff for the newsfeed
51 *
52 * @param $row Object: row from the recentchanges table
53 * @return String
54 */
55 public static function formatDiff( $row ) {
56 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
57 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
58 $actiontext = '';
59 if( $row->rc_type == RC_LOG ) {
60 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
61 $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
62 }
63 return self::formatDiffRow( $titleObj,
64 $row->rc_last_oldid, $row->rc_this_oldid,
65 $timestamp,
66 ($row->rc_deleted & Revision::DELETED_COMMENT)
67 ? wfMsgHtml('rev-deleted-comment')
68 : $row->rc_comment,
69 $actiontext
70 );
71 }
72
73 /**
74 * Really format a diff for the newsfeed
75 *
76 * @param $title Title object
77 * @param $oldid Integer: old revision's id
78 * @param $newid Integer: new revision's id
79 * @param $timestamp Integer: new revision's timestamp
80 * @param $comment String: new revision's comment
81 * @param $actiontext String: text of the action; in case of log event
82 * @return String
83 */
84 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
85 global $wgFeedDiffCutoff, $wgLang;
86 wfProfileIn( __METHOD__ );
87
88 # log enties
89 $completeText = '<p>' . implode( ' ',
90 array_filter(
91 array(
92 $actiontext,
93 Linker::formatComment( $comment ) ) ) ) . "</p>\n";
94
95 // NOTE: Check permissions for anonymous users, not current user.
96 // No "privileged" version should end up in the cache.
97 // Most feed readers will not log in anway.
98 $anon = new User();
99 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
100
101 // Can't diff special pages, unreadable pages or pages with no new revision
102 // to compare against: just return the text.
103 if( $title->getNamespace() < 0 || $accErrors || !$newid ) {
104 wfProfileOut( __METHOD__ );
105 return $completeText;
106 }
107
108 if( $oldid ) {
109 wfProfileIn( __METHOD__."-dodiff" );
110
111 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
112 # $wgLang->timeanddate( $timestamp ),
113 # $wgLang->date( $timestamp ),
114 # $wgLang->time( $timestamp ) ),
115 # wfMsg( 'currentrev' ) );
116
117 $diffText = '';
118 // Don't bother generating the diff if we won't be able to show it
119 if ( $wgFeedDiffCutoff > 0 ) {
120 $de = new DifferenceEngine( $title, $oldid, $newid );
121 $diffText = $de->getDiff(
122 wfMsg( 'previousrevision' ), // hack
123 wfMsg( 'revisionasof',
124 $wgLang->timeanddate( $timestamp ),
125 $wgLang->date( $timestamp ),
126 $wgLang->time( $timestamp ) ) );
127 }
128
129 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
130 // Omit large diffs
131 $diffText = self::getDiffLink( $title, $newid, $oldid );
132 } elseif ( $diffText === false ) {
133 // Error in diff engine, probably a missing revision
134 $diffText = "<p>Can't load revision $newid</p>";
135 } else {
136 // Diff output fine, clean up any illegal UTF-8
137 $diffText = UtfNormal::cleanUp( $diffText );
138 $diffText = self::applyDiffStyle( $diffText );
139 }
140 wfProfileOut( __METHOD__."-dodiff" );
141 } else {
142 $rev = Revision::newFromId( $newid );
143 if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
144 $newtext = '';
145 } else {
146 $newtext = $rev->getText();
147 }
148 if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
149 // Omit large new page diffs, bug 29110
150 $diffText = self::getDiffLink( $title, $newid );
151 } else {
152 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
153 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
154 }
155 }
156 $completeText .= $diffText;
157
158 wfProfileOut( __METHOD__ );
159 return $completeText;
160 }
161
162 /**
163 * Generates a diff link. Used when the full diff is not wanted for example
164 * when $wgFeedDiffCutoff is 0.
165 *
166 * @param $title Title object: used to generate the diff URL
167 * @param $newid Integer newid for this diff
168 * @param $oldid Integer|null oldid for the diff. Null means it is a new article
169 * @return string
170 */
171 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
172 $queryParameters = ($oldid == null)
173 ? "diff={$newid}"
174 : "diff={$newid}&oldid={$oldid}" ;
175 $diffUrl = $title->getFullUrl( $queryParameters );
176
177 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
178 wfMsgForContent( 'showdiff' ) );
179
180 return $diffLink;
181 }
182
183 /**
184 * Hacky application of diff styles for the feeds.
185 * Might be 'cleaner' to use DOM or XSLT or something,
186 * but *gack* it's a pain in the ass.
187 *
188 * @param $text String: diff's HTML output
189 * @return String: modified HTML
190 */
191 public static function applyDiffStyle( $text ) {
192 $styles = array(
193 'diff' => 'background-color: white; color:black;',
194 'diff-otitle' => 'background-color: white; color:black;',
195 'diff-ntitle' => 'background-color: white; color:black;',
196 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
197 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
198 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
199 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
200 );
201
202 foreach( $styles as $class => $style ) {
203 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
204 "\\1style=\"$style\"\\3", $text );
205 }
206
207 return $text;
208 }
209
210 }