Update IPSet use statements
[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 * @param string|Title $title Item's title
58 * @param string $description
59 * @param string $url URL uniquely designating the item.
60 * @param string $date Item's date
61 * @param string $author Author's user name
62 * @param string $comments
63 */
64 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
65 $this->title = $title;
66 $this->description = $description;
67 $this->url = $url;
68 $this->uniqueId = $url;
69 $this->date = $date;
70 $this->author = $author;
71 $this->comments = $comments;
72 }
73
74 /**
75 * Encode $string so that it can be safely embedded in a XML document
76 *
77 * @param string $string String to encode
78 * @return string
79 */
80 public function xmlEncode( $string ) {
81 $string = str_replace( "\r\n", "\n", $string );
82 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
83 return htmlspecialchars( $string );
84 }
85
86 /**
87 * Get the unique id of this item; already xml-encoded
88 * @return string
89 */
90 public function getUniqueID() {
91 $id = $this->getUniqueIDUnescaped();
92 if ( $id ) {
93 return $this->xmlEncode( $id );
94 }
95 }
96
97 /**
98 * Get the unique id of this item, without any escaping
99 * @return string
100 */
101 public function getUniqueIdUnescaped() {
102 if ( $this->uniqueId ) {
103 return wfExpandUrl( $this->uniqueId, PROTO_CURRENT );
104 }
105 }
106
107 /**
108 * Set the unique id of an item
109 *
110 * @param string $uniqueId Unique id for the item
111 * @param bool $rssIsPermalink Set to true if the guid (unique id) is a permalink (RSS feeds only)
112 */
113 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
114 $this->uniqueId = $uniqueId;
115 $this->rssIsPermalink = $rssIsPermalink;
116 }
117
118 /**
119 * Get the title of this item; already xml-encoded
120 *
121 * @return string
122 */
123 public function getTitle() {
124 return $this->xmlEncode( $this->title );
125 }
126
127 /**
128 * Get the URL of this item; already xml-encoded
129 *
130 * @return string
131 */
132 public function getUrl() {
133 return $this->xmlEncode( $this->url );
134 }
135
136 /** Get the URL of this item without any escaping
137 *
138 * @return string
139 */
140 public function getUrlUnescaped() {
141 return $this->url;
142 }
143
144 /**
145 * Get the description of this item; already xml-encoded
146 *
147 * @return string
148 */
149 public function getDescription() {
150 return $this->xmlEncode( $this->description );
151 }
152
153 /**
154 * Get the description of this item without any escaping
155 *
156 * @return string
157 */
158 public function getDescriptionUnescaped() {
159 return $this->description;
160 }
161
162 /**
163 * Get the language of this item
164 *
165 * @return string
166 */
167 public function getLanguage() {
168 global $wgLanguageCode;
169 return LanguageCode::bcp47( $wgLanguageCode );
170 }
171
172 /**
173 * Get the date of this item
174 *
175 * @return string
176 */
177 public function getDate() {
178 return $this->date;
179 }
180
181 /**
182 * Get the author of this item; already xml-encoded
183 *
184 * @return string
185 */
186 public function getAuthor() {
187 return $this->xmlEncode( $this->author );
188 }
189
190 /**
191 * Get the author of this item without any escaping
192 *
193 * @return string
194 */
195 public function getAuthorUnescaped() {
196 return $this->author;
197 }
198
199 /**
200 * Get the comment of this item; already xml-encoded
201 *
202 * @return string
203 */
204 public function getComments() {
205 return $this->xmlEncode( $this->comments );
206 }
207
208 /**
209 * Get the comment of this item without any escaping
210 *
211 * @return string
212 */
213 public function getCommentsUnescaped() {
214 return $this->comments;
215 }
216
217 /**
218 * Quickie hack... strip out wikilinks to more legible form from the comment.
219 *
220 * @param string $text Wikitext
221 * @return string
222 */
223 public static function stripComment( $text ) {
224 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
225 }
226 /**#@-*/
227 }
228
229 /**
230 * Class to support the outputting of syndication feeds in Atom and RSS format.
231 *
232 * @ingroup Feed
233 */
234 abstract class ChannelFeed extends FeedItem {
235
236 /** @var TemplateParser */
237 protected $templateParser;
238
239 /**
240 * @param string|Title $title Feed's title
241 * @param string $description
242 * @param string $url URL uniquely designating the feed.
243 * @param string $date Feed's date
244 * @param string $author Author's user name
245 * @param string $comments
246 */
247 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
248 parent::__construct( $title, $description, $url, $date, $author, $comments );
249 $this->templateParser = new TemplateParser();
250 }
251
252 /**
253 * Generate Header of the feed
254 * @par Example:
255 * @code
256 * print "<feed>";
257 * @endcode
258 */
259 abstract public function outHeader();
260
261 /**
262 * Generate an item
263 * @par Example:
264 * @code
265 * print "<item>...</item>";
266 * @endcode
267 * @param FeedItem $item
268 */
269 abstract public function outItem( $item );
270
271 /**
272 * Generate Footer of the feed
273 * @par Example:
274 * @code
275 * print "</feed>";
276 * @endcode
277 */
278 abstract public function outFooter();
279
280 /**
281 * Setup and send HTTP headers. Don't send any content;
282 * content might end up being cached and re-sent with
283 * these same headers later.
284 *
285 * This should be called from the outHeader() method,
286 * but can also be called separately.
287 */
288 public function httpHeaders() {
289 global $wgOut, $wgVaryOnXFP;
290
291 # We take over from $wgOut, excepting its cache header info
292 $wgOut->disable();
293 $mimetype = $this->contentType();
294 header( "Content-type: $mimetype; charset=UTF-8" );
295
296 // Set a sane filename
297 $exts = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer()
298 ->getExtensionsForType( $mimetype );
299 $ext = $exts ? strtok( $exts, ' ' ) : 'xml';
300 header( "Content-Disposition: inline; filename=\"feed.{$ext}\"" );
301
302 if ( $wgVaryOnXFP ) {
303 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
304 }
305 $wgOut->sendCacheControl();
306 }
307
308 /**
309 * Return an internet media type to be sent in the headers.
310 *
311 * @return string
312 */
313 private function contentType() {
314 global $wgRequest;
315
316 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
317 $allowedctypes = [
318 'application/xml',
319 'text/xml',
320 'application/rss+xml',
321 'application/atom+xml'
322 ];
323
324 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
325 }
326
327 /**
328 * Output the initial XML headers.
329 */
330 protected function outXmlHeader() {
331 $this->httpHeaders();
332 echo '<?xml version="1.0"?>' . "\n";
333 }
334 }
335
336 /**
337 * Generate a RSS feed
338 *
339 * @ingroup Feed
340 */
341 class RSSFeed extends ChannelFeed {
342
343 /**
344 * Format a date given a timestamp. If a timestamp is not given, nothing is returned
345 *
346 * @param int|null $ts Timestamp
347 * @return string|null Date string
348 */
349 function formatTime( $ts ) {
350 if ( $ts ) {
351 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
352 }
353 }
354
355 /**
356 * Output an RSS 2.0 header
357 */
358 function outHeader() {
359 global $wgVersion;
360
361 $this->outXmlHeader();
362 // Manually escaping rather than letting Mustache do it because Mustache
363 // uses htmlentities, which does not work with XML
364 $templateParams = [
365 'title' => $this->getTitle(),
366 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT ) ),
367 'description' => $this->getDescription(),
368 'language' => $this->xmlEncode( $this->getLanguage() ),
369 'version' => $this->xmlEncode( $wgVersion ),
370 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) )
371 ];
372 print $this->templateParser->processTemplate( 'RSSHeader', $templateParams );
373 }
374
375 /**
376 * Output an RSS 2.0 item
377 * @param FeedItem $item Item to be output
378 */
379 function outItem( $item ) {
380 // Manually escaping rather than letting Mustache do it because Mustache
381 // uses htmlentities, which does not work with XML
382 $templateParams = [
383 "title" => $item->getTitle(),
384 "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT ) ),
385 "permalink" => $item->rssIsPermalink,
386 "uniqueID" => $item->getUniqueId(),
387 "description" => $item->getDescription(),
388 "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
389 "author" => $item->getAuthor()
390 ];
391 $comments = $item->getCommentsUnescaped();
392 if ( $comments ) {
393 $commentsEscaped = $this->xmlEncode( wfExpandUrl( $comments, PROTO_CURRENT ) );
394 $templateParams["comments"] = $commentsEscaped;
395 }
396 print $this->templateParser->processTemplate( 'RSSItem', $templateParams );
397 }
398
399 /**
400 * Output an RSS 2.0 footer
401 */
402 function outFooter() {
403 print "</channel></rss>";
404 }
405 }
406
407 /**
408 * Generate an Atom feed
409 *
410 * @ingroup Feed
411 */
412 class AtomFeed extends ChannelFeed {
413 /**
414 * Format a date given timestamp, if one is given.
415 *
416 * @param string|int|null $timestamp
417 * @return string|null
418 */
419 function formatTime( $timestamp ) {
420 if ( $timestamp ) {
421 // need to use RFC 822 time format at least for rss2.0
422 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $timestamp ) );
423 }
424 }
425
426 /**
427 * Outputs a basic header for Atom 1.0 feeds.
428 */
429 function outHeader() {
430 global $wgVersion;
431 $this->outXmlHeader();
432 // Manually escaping rather than letting Mustache do it because Mustache
433 // uses htmlentities, which does not work with XML
434 $templateParams = [
435 'language' => $this->xmlEncode( $this->getLanguage() ),
436 'feedID' => $this->getFeedID(),
437 'title' => $this->getTitle(),
438 'url' => $this->xmlEncode( wfExpandUrl( $this->getUrlUnescaped(), PROTO_CURRENT ) ),
439 'selfUrl' => $this->getSelfUrl(),
440 'timestamp' => $this->xmlEncode( $this->formatTime( wfTimestampNow() ) ),
441 'description' => $this->getDescription(),
442 'version' => $this->xmlEncode( $wgVersion ),
443 ];
444 print $this->templateParser->processTemplate( 'AtomHeader', $templateParams );
445 }
446
447 /**
448 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
449 * for every feed we create. For now just use the URL, but who
450 * can tell if that's right? If we put options on the feed, do we
451 * have to change the id? Maybe? Maybe not.
452 *
453 * @return string
454 */
455 private function getFeedId() {
456 return $this->getSelfUrl();
457 }
458
459 /**
460 * Atom 1.0 requests a self-reference to the feed.
461 * @return string
462 */
463 private function getSelfUrl() {
464 global $wgRequest;
465 return htmlspecialchars( $wgRequest->getFullRequestURL() );
466 }
467
468 /**
469 * Output a given item.
470 * @param FeedItem $item
471 */
472 function outItem( $item ) {
473 global $wgMimeType;
474 // Manually escaping rather than letting Mustache do it because Mustache
475 // uses htmlentities, which does not work with XML
476 $templateParams = [
477 "uniqueID" => $item->getUniqueId(),
478 "title" => $item->getTitle(),
479 "mimeType" => $this->xmlEncode( $wgMimeType ),
480 "url" => $this->xmlEncode( wfExpandUrl( $item->getUrlUnescaped(), PROTO_CURRENT ) ),
481 "date" => $this->xmlEncode( $this->formatTime( $item->getDate() ) ),
482 "description" => $item->getDescription(),
483 "author" => $item->getAuthor()
484 ];
485 print $this->templateParser->processTemplate( 'AtomItem', $templateParams );
486 }
487
488 /**
489 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
490 */
491 function outFooter() {
492 print "</feed>";
493 }
494 }