Http::getProxy() method to get proxy configuration
[lhc/web/wiklou.git] / includes / FeedUtils.php
1 <?php
2 /**
3 * Helper functions for feeds.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Feed
22 */
23
24 /**
25 * Helper functions for feeds
26 *
27 * @ingroup Feed
28 */
29 class FeedUtils {
30
31 /**
32 * Check whether feed's cache should be cleared; for changes feeds
33 * If the feed should be purged; $timekey and $key will be removed from
34 * $messageMemc
35 *
36 * @param string $timekey Cache key of the timestamp of the last item
37 * @param string $key Cache key of feed's content
38 */
39 public static function checkPurge( $timekey, $key ) {
40 global $wgRequest, $wgUser, $messageMemc;
41 $purge = $wgRequest->getVal( 'action' ) === 'purge';
42 if ( $purge && $wgUser->isAllowed( 'purge' ) ) {
43 $messageMemc->delete( $timekey );
44 $messageMemc->delete( $key );
45 }
46 }
47
48 /**
49 * Check whether feeds can be used and that $type is a valid feed type
50 *
51 * @param string $type Feed type, as requested by the user
52 * @return bool
53 */
54 public static function checkFeedOutput( $type ) {
55 global $wgOut, $wgFeed, $wgFeedClasses;
56
57 if ( !$wgFeed ) {
58 $wgOut->addWikiMsg( 'feed-unavailable' );
59 return false;
60 }
61
62 if ( !isset( $wgFeedClasses[$type] ) ) {
63 $wgOut->addWikiMsg( 'feed-invalid' );
64 return false;
65 }
66
67 return true;
68 }
69
70 /**
71 * Format a diff for the newsfeed
72 *
73 * @param object $row Row from the recentchanges table
74 * @return string
75 */
76 public static function formatDiff( $row ) {
77 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
78 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
79 $actiontext = '';
80 if ( $row->rc_type == RC_LOG ) {
81 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
82 $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
83 }
84 return self::formatDiffRow( $titleObj,
85 $row->rc_last_oldid, $row->rc_this_oldid,
86 $timestamp,
87 $row->rc_deleted & Revision::DELETED_COMMENT
88 ? wfMessage( 'rev-deleted-comment' )->escaped()
89 : $row->rc_comment,
90 $actiontext
91 );
92 }
93
94 /**
95 * Really format a diff for the newsfeed
96 *
97 * @param Title $title Title object
98 * @param int $oldid Old revision's id
99 * @param int $newid New revision's id
100 * @param int $timestamp New revision's timestamp
101 * @param string $comment New revision's comment
102 * @param string $actiontext Text of the action; in case of log event
103 * @return string
104 */
105 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
106 $comment, $actiontext = ''
107 ) {
108 global $wgFeedDiffCutoff, $wgLang;
109
110 // log entries
111 $completeText = '<p>' . implode( ' ',
112 array_filter(
113 array(
114 $actiontext,
115 Linker::formatComment( $comment ) ) ) ) . "</p>\n";
116
117 // NOTE: Check permissions for anonymous users, not current user.
118 // No "privileged" version should end up in the cache.
119 // Most feed readers will not log in anyway.
120 $anon = new User();
121 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
122
123 // Can't diff special pages, unreadable pages or pages with no new revision
124 // to compare against: just return the text.
125 if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
126 return $completeText;
127 }
128
129 if ( $oldid ) {
130
131 # $diffText = $de->getDiff( wfMessage( 'revisionasof',
132 # $wgLang->timeanddate( $timestamp ),
133 # $wgLang->date( $timestamp ),
134 # $wgLang->time( $timestamp ) )->text(),
135 # wfMessage( 'currentrev' )->text() );
136
137 $diffText = '';
138 // Don't bother generating the diff if we won't be able to show it
139 if ( $wgFeedDiffCutoff > 0 ) {
140 $rev = Revision::newFromId( $oldid );
141
142 if ( !$rev ) {
143 $diffText = false;
144 } else {
145 $context = clone RequestContext::getMain();
146 $context->setTitle( $title );
147
148 $contentHandler = $rev->getContentHandler();
149 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
150 $diffText = $de->getDiff(
151 wfMessage( 'previousrevision' )->text(), // hack
152 wfMessage( 'revisionasof',
153 $wgLang->timeanddate( $timestamp ),
154 $wgLang->date( $timestamp ),
155 $wgLang->time( $timestamp ) )->text() );
156 }
157 }
158
159 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
160 // Omit large diffs
161 $diffText = self::getDiffLink( $title, $newid, $oldid );
162 } elseif ( $diffText === false ) {
163 // Error in diff engine, probably a missing revision
164 $diffText = "<p>Can't load revision $newid</p>";
165 } else {
166 // Diff output fine, clean up any illegal UTF-8
167 $diffText = UtfNormal\Validator::cleanUp( $diffText );
168 $diffText = self::applyDiffStyle( $diffText );
169 }
170 } else {
171 $rev = Revision::newFromId( $newid );
172 if ( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
173 $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
174 } else {
175 $newContent = $rev->getContent();
176 }
177
178 if ( $newContent instanceof TextContent ) {
179 // only textual content has a "source view".
180 $text = $newContent->getNativeData();
181
182 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
183 $html = null;
184 } else {
185 $html = nl2br( htmlspecialchars( $text ) );
186 }
187 } else {
188 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
189 // contain JS magic and generally may not be suitable for inclusion in a feed.
190 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
191 // Compare also ApiFeedContributions::feedItemDesc
192 $html = null;
193 }
194
195 if ( $html === null ) {
196
197 // Omit large new page diffs, bug 29110
198 // Also use diff link for non-textual content
199 $diffText = self::getDiffLink( $title, $newid );
200 } else {
201 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
202 '<div>' . $html . '</div>';
203 }
204 }
205 $completeText .= $diffText;
206
207 return $completeText;
208 }
209
210 /**
211 * Generates a diff link. Used when the full diff is not wanted for example
212 * when $wgFeedDiffCutoff is 0.
213 *
214 * @param Title $title Title object: used to generate the diff URL
215 * @param int $newid Newid for this diff
216 * @param int|null $oldid Oldid for the diff. Null means it is a new article
217 * @return string
218 */
219 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
220 $queryParameters = array( 'diff' => $newid );
221 if ( $oldid != null ) {
222 $queryParameters['oldid'] = $oldid;
223 }
224 $diffUrl = $title->getFullURL( $queryParameters );
225
226 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
227 wfMessage( 'showdiff' )->inContentLanguage()->text() );
228
229 return $diffLink;
230 }
231
232 /**
233 * Hacky application of diff styles for the feeds.
234 * Might be 'cleaner' to use DOM or XSLT or something,
235 * but *gack* it's a pain in the ass.
236 *
237 * @param string $text Diff's HTML output
238 * @return string Modified HTML
239 */
240 public static function applyDiffStyle( $text ) {
241 $styles = array(
242 'diff' => 'background-color: white; color:black;',
243 'diff-otitle' => 'background-color: white; color:black; text-align: center;',
244 'diff-ntitle' => 'background-color: white; color:black; text-align: center;',
245 'diff-addedline' => 'color:black; font-size: 88%; border-style: solid; '
246 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
247 . 'vertical-align: top; white-space: pre-wrap;',
248 'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; '
249 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
250 . 'vertical-align: top; white-space: pre-wrap;',
251 'diff-context' => 'background-color: #f9f9f9; color: #333333; font-size: 88%; '
252 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
253 . 'border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;',
254 'diffchange' => 'font-weight: bold; text-decoration: none;',
255 );
256
257 foreach ( $styles as $class => $style ) {
258 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
259 "\\1style=\"$style\"\\3", $text );
260 }
261
262 return $text;
263 }
264
265 }