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