Note that jquery.json will be removed in 1.25
[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 protected $title;
41
42 protected $description;
43
44 protected $url;
45
46 protected $date;
47
48 protected $author;
49
50 protected $uniqueId;
51
52 protected $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( $this->uniqueId );
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 $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 * @todo document (needs one-sentence top-level class description).
188 * @ingroup Feed
189 */
190 abstract class ChannelFeed extends FeedItem {
191 /**
192 * Generate Header of the feed
193 * @par Example:
194 * @code
195 * print "<feed>";
196 * @endcode
197 */
198 abstract public function outHeader();
199
200 /**
201 * Generate an item
202 * @par Example:
203 * @code
204 * print "<item>...</item>";
205 * @endcode
206 * @param FeedItem $item
207 */
208 abstract public function outItem( $item );
209
210 /**
211 * Generate Footer of the feed
212 * @par Example:
213 * @code
214 * print "</feed>";
215 * @endcode
216 */
217 abstract public function outFooter();
218
219 /**
220 * Setup and send HTTP headers. Don't send any content;
221 * content might end up being cached and re-sent with
222 * these same headers later.
223 *
224 * This should be called from the outHeader() method,
225 * but can also be called separately.
226 */
227 public function httpHeaders() {
228 global $wgOut, $wgVaryOnXFP;
229
230 # We take over from $wgOut, excepting its cache header info
231 $wgOut->disable();
232 $mimetype = $this->contentType();
233 header( "Content-type: $mimetype; charset=UTF-8" );
234 if ( $wgVaryOnXFP ) {
235 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
236 }
237 $wgOut->sendCacheControl();
238
239 }
240
241 /**
242 * Return an internet media type to be sent in the headers.
243 *
244 * @return string
245 */
246 private function contentType() {
247 global $wgRequest;
248
249 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
250 $allowedctypes = array(
251 'application/xml',
252 'text/xml',
253 'application/rss+xml',
254 'application/atom+xml'
255 );
256
257 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
258 }
259
260 /**
261 * Output the initial XML headers with a stylesheet for legibility
262 * if someone finds it in a browser.
263 */
264 protected function outXmlHeader() {
265 global $wgStylePath, $wgStyleVersion;
266
267 $this->httpHeaders();
268 echo '<?xml version="1.0"?>' . "\n";
269 echo '<?xml-stylesheet type="text/css" href="' .
270 htmlspecialchars( wfExpandUrl(
271 "$wgStylePath/common/feed.css?$wgStyleVersion",
272 PROTO_CURRENT
273 ) ) .
274 '"?' . ">\n";
275 }
276 }
277
278 /**
279 * Generate a RSS feed
280 *
281 * @ingroup Feed
282 */
283 class RSSFeed extends ChannelFeed {
284
285 /**
286 * Format a date given a timestamp
287 *
288 * @param int $ts Timestamp
289 * @return string Date string
290 */
291 function formatTime( $ts ) {
292 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
293 }
294
295 /**
296 * Output an RSS 2.0 header
297 */
298 function outHeader() {
299 global $wgVersion;
300
301 $this->outXmlHeader();
302 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
303 <channel>
304 <title><?php print $this->getTitle() ?></title>
305 <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
306 <description><?php print $this->getDescription() ?></description>
307 <language><?php print $this->getLanguage() ?></language>
308 <generator>MediaWiki <?php print $wgVersion ?></generator>
309 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
310 <?php
311 }
312
313 /**
314 * Output an RSS 2.0 item
315 * @param FeedItem $item Item to be output
316 */
317 function outItem( $item ) {
318 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
319 ?>
320 <item>
321 <title><?php print $item->getTitle(); ?></title>
322 <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
323 <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
324 <description><?php print $item->getDescription() ?></description>
325 <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
326 <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
327 <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
328 </item>
329 <?php
330 // @codingStandardsIgnoreEnd
331 }
332
333 /**
334 * Output an RSS 2.0 footer
335 */
336 function outFooter() {
337 ?>
338 </channel>
339 </rss><?php
340 }
341 }
342
343 /**
344 * Generate an Atom feed
345 *
346 * @ingroup Feed
347 */
348 class AtomFeed extends ChannelFeed {
349 /**
350 * @todo document
351 * @return string
352 */
353 function formatTime( $ts ) {
354 // need to use RFC 822 time format at least for rss2.0
355 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
356 }
357
358 /**
359 * Outputs a basic header for Atom 1.0 feeds.
360 */
361 function outHeader() {
362 global $wgVersion;
363
364 $this->outXmlHeader();
365 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
366 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
367 <id><?php print $this->getFeedId() ?></id>
368 <title><?php print $this->getTitle() ?></title>
369 <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
370 <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
371 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
372 <subtitle><?php print $this->getDescription() ?></subtitle>
373 <generator>MediaWiki <?php print $wgVersion ?></generator>
374
375 <?php
376 // @codingStandardsIgnoreEnd
377 }
378
379 /**
380 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
381 * for every feed we create. For now just use the URL, but who
382 * can tell if that's right? If we put options on the feed, do we
383 * have to change the id? Maybe? Maybe not.
384 *
385 * @return string
386 */
387 private function getFeedId() {
388 return $this->getSelfUrl();
389 }
390
391 /**
392 * Atom 1.0 requests a self-reference to the feed.
393 * @return string
394 */
395 private function getSelfUrl() {
396 global $wgRequest;
397 return htmlspecialchars( $wgRequest->getFullRequestURL() );
398 }
399
400 /**
401 * Output a given item.
402 * @param FeedItem $item
403 */
404 function outItem( $item ) {
405 global $wgMimeType;
406 // @codingStandardsIgnoreStart Ignore long lines and formatting issues.
407 ?>
408 <entry>
409 <id><?php print $item->getUniqueId(); ?></id>
410 <title><?php print $item->getTitle(); ?></title>
411 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
412 <?php if ( $item->getDate() ) { ?>
413 <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
414 <?php } ?>
415
416 <summary type="html"><?php print $item->getDescription() ?></summary>
417 <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
418 </entry>
419
420 <?php /* @todo FIXME: Need to add comments
421 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
422 */
423 }
424
425 /**
426 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
427 */
428 function outFooter() {?>
429 </feed><?php
430 // @codingStandardsIgnoreEnd
431 }
432 }