Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / Feed.php
1 <?php
2 /**
3 * Basic support for outputting syndication feeds in RSS, other formats.
4 *
5 * Contain a feed class as well as classes to build rss / atom ... feeds
6 * Available feeds are defined in Defines.php
7 *
8 * Copyright © 2004 Brion Vibber <brion@pobox.com>
9 * https://www.mediawiki.org/
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 /**
30 * @defgroup Feed Feed
31 */
32
33 /**
34 * A base class for basic support for outputting syndication feeds in RSS and other formats.
35 *
36 * @ingroup Feed
37 */
38 class FeedItem {
39 /** @var Title */
40 public $title;
41
42 public $description;
43
44 public $url;
45
46 public $date;
47
48 public $author;
49
50 public $uniqueId;
51
52 public $comments;
53
54 public $rssIsPermalink = false;
55
56 /**
57 * Constructor
58 *
59 * @param string|Title $title Item's title
60 * @param string $description
61 * @param string $url URL uniquely designating the item.
62 * @param string $date Item's date
63 * @param string $author Author's user name
64 * @param string $comments
65 */
66 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
67 $this->title = $title;
68 $this->description = $description;
69 $this->url = $url;
70 $this->uniqueId = $url;
71 $this->date = $date;
72 $this->author = $author;
73 $this->comments = $comments;
74 }
75
76 /**
77 * Encode $string so that it can be safely embedded in a XML document
78 *
79 * @param string $string String to encode
80 * @return string
81 */
82 public function xmlEncode( $string ) {
83 $string = str_replace( "\r\n", "\n", $string );
84 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
85 return htmlspecialchars( $string );
86 }
87
88 /**
89 * Get the unique id of this item
90 *
91 * @return string
92 */
93 public function getUniqueId() {
94 if ( $this->uniqueId ) {
95 return $this->xmlEncode( wfExpandUrl( $this->uniqueId, PROTO_CURRENT ) );
96 }
97 }
98
99 /**
100 * Set the unique id of an item
101 *
102 * @param string $uniqueId Unique id for the item
103 * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
104 */
105 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
106 $this->uniqueId = $uniqueId;
107 $this->rssIsPermalink = $rssIsPermalink;
108 }
109
110 /**
111 * Get the title of this item; already xml-encoded
112 *
113 * @return string
114 */
115 public function getTitle() {
116 return $this->xmlEncode( $this->title );
117 }
118
119 /**
120 * Get the URL of this item; already xml-encoded
121 *
122 * @return string
123 */
124 public function getUrl() {
125 return $this->xmlEncode( $this->url );
126 }
127
128 /**
129 * Get the description of this item; already xml-encoded
130 *
131 * @return string
132 */
133 public function getDescription() {
134 return $this->xmlEncode( $this->description );
135 }
136
137 /**
138 * Get the language of this item
139 *
140 * @return string
141 */
142 public function getLanguage() {
143 global $wgLanguageCode;
144 return wfBCP47( $wgLanguageCode );
145 }
146
147 /**
148 * Get the date of this item
149 *
150 * @return string
151 */
152 public function getDate() {
153 return $this->date;
154 }
155
156 /**
157 * Get the author of this item; already xml-encoded
158 *
159 * @return string
160 */
161 public function getAuthor() {
162 return $this->xmlEncode( $this->author );
163 }
164
165 /**
166 * Get the comment of this item; already xml-encoded
167 *
168 * @return string
169 */
170 public function getComments() {
171 return $this->xmlEncode( $this->comments );
172 }
173
174 /**
175 * Quickie hack... strip out wikilinks to more legible form from the comment.
176 *
177 * @param string $text Wikitext
178 * @return string
179 */
180 public static function stripComment( $text ) {
181 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
182 }
183 /**#@-*/
184 }
185
186 /**
187 * Class to support the outputting of syndication feeds in Atom and RSS format.
188 *
189 * @ingroup Feed
190 */
191 abstract class ChannelFeed extends FeedItem {
192 /**
193 * Generate Header of the feed
194 * @par Example:
195 * @code
196 * print "<feed>";
197 * @endcode
198 */
199 abstract public function outHeader();
200
201 /**
202 * Generate an item
203 * @par Example:
204 * @code
205 * print "<item>...</item>";
206 * @endcode
207 * @param FeedItem $item
208 */
209 abstract public function outItem( $item );
210
211 /**
212 * Generate Footer of the feed
213 * @par Example:
214 * @code
215 * print "</feed>";
216 * @endcode
217 */
218 abstract public function outFooter();
219
220 /**
221 * Setup and send HTTP headers. Don't send any content;
222 * content might end up being cached and re-sent with
223 * these same headers later.
224 *
225 * This should be called from the outHeader() method,
226 * but can also be called separately.
227 */
228 public function httpHeaders() {
229 global $wgOut, $wgVaryOnXFP;
230
231 # We take over from $wgOut, excepting its cache header info
232 $wgOut->disable();
233 $mimetype = $this->contentType();
234 header( "Content-type: $mimetype; charset=UTF-8" );
235 if ( $wgVaryOnXFP ) {
236 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
237 }
238 $wgOut->sendCacheControl();
239
240 }
241
242 /**
243 * Return an internet media type to be sent in the headers.
244 *
245 * @return string
246 */
247 private function contentType() {
248 global $wgRequest;
249
250 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
251 $allowedctypes = [
252 'application/xml',
253 'text/xml',
254 'application/rss+xml',
255 'application/atom+xml'
256 ];
257
258 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
259 }
260
261 /**
262 * Output the initial XML headers.
263 */
264 protected function outXmlHeader() {
265 $this->httpHeaders();
266 echo '<?xml version="1.0"?>' . "\n";
267 }
268 }
269
270 /**
271 * Generate a RSS feed
272 *
273 * @ingroup Feed
274 */
275 class RSSFeed extends ChannelFeed {
276
277 /**
278 * Format a date given a timestamp
279 *
280 * @param int $ts Timestamp
281 * @return string Date string
282 */
283 function formatTime( $ts ) {
284 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
285 }
286
287 /**
288 * Output an RSS 2.0 header
289 */
290 function outHeader() {
291 global $wgVersion;
292
293 $this->outXmlHeader();
294 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
295 <channel>
296 <title><?php print $this->getTitle() ?></title>
297 <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
298 <description><?php print $this->getDescription() ?></description>
299 <language><?php print $this->getLanguage() ?></language>
300 <generator>MediaWiki <?php print $wgVersion ?></generator>
301 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
302 <?php
303 }
304
305 /**
306 * Output an RSS 2.0 item
307 * @param FeedItem $item Item to be output
308 */
309 function outItem( $item ) {
310 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
311 ?>
312 <item>
313 <title><?php print $item->getTitle(); ?></title>
314 <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
315 <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
316 <description><?php print $item->getDescription() ?></description>
317 <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
318 <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
319 <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
320 </item>
321 <?php
322 // @codingStandardsIgnoreEnd
323 }
324
325 /**
326 * Output an RSS 2.0 footer
327 */
328 function outFooter() {
329 ?>
330 </channel>
331 </rss><?php
332 }
333 }
334
335 /**
336 * Generate an Atom feed
337 *
338 * @ingroup Feed
339 */
340 class AtomFeed extends ChannelFeed {
341 /**
342 * Format a date given timestamp.
343 *
344 * @param string|int $timestamp
345 * @return string
346 */
347 function formatTime( $timestamp ) {
348 // need to use RFC 822 time format at least for rss2.0
349 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $timestamp ) );
350 }
351
352 /**
353 * Outputs a basic header for Atom 1.0 feeds.
354 */
355 function outHeader() {
356 global $wgVersion;
357
358 $this->outXmlHeader();
359 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
360 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
361 <id><?php print $this->getFeedId() ?></id>
362 <title><?php print $this->getTitle() ?></title>
363 <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
364 <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
365 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
366 <subtitle><?php print $this->getDescription() ?></subtitle>
367 <generator>MediaWiki <?php print $wgVersion ?></generator>
368
369 <?php
370 // @codingStandardsIgnoreEnd
371 }
372
373 /**
374 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
375 * for every feed we create. For now just use the URL, but who
376 * can tell if that's right? If we put options on the feed, do we
377 * have to change the id? Maybe? Maybe not.
378 *
379 * @return string
380 */
381 private function getFeedId() {
382 return $this->getSelfUrl();
383 }
384
385 /**
386 * Atom 1.0 requests a self-reference to the feed.
387 * @return string
388 */
389 private function getSelfUrl() {
390 global $wgRequest;
391 return htmlspecialchars( $wgRequest->getFullRequestURL() );
392 }
393
394 /**
395 * Output a given item.
396 * @param FeedItem $item
397 */
398 function outItem( $item ) {
399 global $wgMimeType;
400 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
401 ?>
402 <entry>
403 <id><?php print $item->getUniqueId(); ?></id>
404 <title><?php print $item->getTitle(); ?></title>
405 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
406 <?php if ( $item->getDate() ) { ?>
407 <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
408 <?php } ?>
409
410 <summary type="html"><?php print $item->getDescription() ?></summary>
411 <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
412 </entry>
413
414 <?php /* @todo FIXME: Need to add comments
415 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
416 */
417 }
418
419 /**
420 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
421 */
422 function outFooter() {?>
423 </feed><?php
424 // @codingStandardsIgnoreEnd
425 }
426 }