Merge "Consistently follow conventions for documenting parameters"
[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 * http://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 /**
40 * @var Title
41 */
42 var $title;
43
44 var $description;
45 var $url;
46 var $date;
47 var $author;
48 var $uniqueId;
49 var $comments;
50 var $rssIsPermalink = false;
51
52 /**
53 * Constructor
54 *
55 * @param string|Title $title Item's title
56 * @param $description String
57 * @param string $url URL uniquely designating the item.
58 * @param string $date Item's date
59 * @param string $author Author's user name
60 * @param $comments String
61 */
62 function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
63 $this->title = $title;
64 $this->description = $description;
65 $this->url = $url;
66 $this->uniqueId = $url;
67 $this->date = $date;
68 $this->author = $author;
69 $this->comments = $comments;
70 }
71
72 /**
73 * Encode $string so that it can be safely embedded in a XML document
74 *
75 * @param string $string string to encode
76 * @return String
77 */
78 public function xmlEncode( $string ) {
79 $string = str_replace( "\r\n", "\n", $string );
80 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
81 return htmlspecialchars( $string );
82 }
83
84 /**
85 * Get the unique id of this item
86 *
87 * @return String
88 */
89 public function getUniqueId() {
90 if ( $this->uniqueId ) {
91 return $this->xmlEncode( $this->uniqueId );
92 }
93 }
94
95 /**
96 * set the unique id of an item
97 *
98 * @param string $uniqueId unique id for the item
99 * @param $rssIsPermalink Boolean: set to true if the guid (unique id) is a permalink (RSS feeds only)
100 */
101 public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
102 $this->uniqueId = $uniqueId;
103 $this->rssIsPermalink = $rssIsPermalink;
104 }
105
106 /**
107 * Get the title of this item; already xml-encoded
108 *
109 * @return String
110 */
111 public function getTitle() {
112 return $this->xmlEncode( $this->title );
113 }
114
115 /**
116 * Get the URL of this item; already xml-encoded
117 *
118 * @return String
119 */
120 public function getUrl() {
121 return $this->xmlEncode( $this->url );
122 }
123
124 /**
125 * Get the description of this item; already xml-encoded
126 *
127 * @return String
128 */
129 public function getDescription() {
130 return $this->xmlEncode( $this->description );
131 }
132
133 /**
134 * Get the language of this item
135 *
136 * @return String
137 */
138 public function getLanguage() {
139 global $wgLanguageCode;
140 return $wgLanguageCode;
141 }
142
143 /**
144 * Get the title of this item
145 *
146 * @return String
147 */
148 public function getDate() {
149 return $this->date;
150 }
151
152 /**
153 * Get the author of this item; already xml-encoded
154 *
155 * @return String
156 */
157 public function getAuthor() {
158 return $this->xmlEncode( $this->author );
159 }
160
161 /**
162 * Get the comment of this item; already xml-encoded
163 *
164 * @return String
165 */
166 public function getComments() {
167 return $this->xmlEncode( $this->comments );
168 }
169
170 /**
171 * Quickie hack... strip out wikilinks to more legible form from the comment.
172 *
173 * @param string $text wikitext
174 * @return String
175 */
176 public static function stripComment( $text ) {
177 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
178 }
179 /**#@-*/
180 }
181
182 /**
183 * @todo document (needs one-sentence top-level class description).
184 * @ingroup Feed
185 */
186 abstract class ChannelFeed extends FeedItem {
187 /**
188 * Generate Header of the feed
189 * @par Example:
190 * @code
191 * print "<feed>";
192 * @endcode
193 * @param $item
194 */
195 abstract public function outHeader();
196
197 /**
198 * Generate an item
199 * @par Example:
200 * @code
201 * print "<item>...</item>";
202 * @endcode
203 * @param $item
204 */
205 abstract public function outItem( $item );
206
207 /**
208 * Generate Footer of the feed
209 * @par Example:
210 * @code
211 * print "</feed>";
212 * @endcode
213 */
214 abstract public function outFooter();
215
216 /**
217 * Setup and send HTTP headers. Don't send any content;
218 * content might end up being cached and re-sent with
219 * these same headers later.
220 *
221 * This should be called from the outHeader() method,
222 * but can also be called separately.
223 */
224 public function httpHeaders() {
225 global $wgOut, $wgVaryOnXFP;
226
227 # We take over from $wgOut, excepting its cache header info
228 $wgOut->disable();
229 $mimetype = $this->contentType();
230 header( "Content-type: $mimetype; charset=UTF-8" );
231 if ( $wgVaryOnXFP ) {
232 $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
233 }
234 $wgOut->sendCacheControl();
235
236 }
237
238 /**
239 * Return an internet media type to be sent in the headers.
240 *
241 * @return string
242 * @private
243 */
244 function contentType() {
245 global $wgRequest;
246 $ctype = $wgRequest->getVal( 'ctype', 'application/xml' );
247 $allowedctypes = array( 'application/xml', 'text/xml', 'application/rss+xml', 'application/atom+xml' );
248 return ( in_array( $ctype, $allowedctypes ) ? $ctype : 'application/xml' );
249 }
250
251 /**
252 * Output the initial XML headers with a stylesheet for legibility
253 * if someone finds it in a browser.
254 * @private
255 */
256 function outXmlHeader() {
257 global $wgStylePath, $wgStyleVersion;
258
259 $this->httpHeaders();
260 echo '<?xml version="1.0"?>' . "\n";
261 echo '<?xml-stylesheet type="text/css" href="' .
262 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion", PROTO_CURRENT ) ) .
263 '"?' . ">\n";
264 }
265 }
266
267 /**
268 * Generate a RSS feed
269 *
270 * @ingroup Feed
271 */
272 class RSSFeed extends ChannelFeed {
273
274 /**
275 * Format a date given a timestamp
276 *
277 * @param $ts Integer: timestamp
278 * @return String: date string
279 */
280 function formatTime( $ts ) {
281 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
282 }
283
284 /**
285 * Output an RSS 2.0 header
286 */
287 function outHeader() {
288 global $wgVersion;
289
290 $this->outXmlHeader();
291 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
292 <channel>
293 <title><?php print $this->getTitle() ?></title>
294 <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
295 <description><?php print $this->getDescription() ?></description>
296 <language><?php print $this->getLanguage() ?></language>
297 <generator>MediaWiki <?php print $wgVersion ?></generator>
298 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
299 <?php
300 }
301
302 /**
303 * Output an RSS 2.0 item
304 * @param $item FeedItem: item to be output
305 */
306 function outItem( $item ) {
307 ?>
308 <item>
309 <title><?php print $item->getTitle(); ?></title>
310 <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?></link>
311 <guid<?php if ( !$item->rssIsPermalink ) { print ' isPermaLink="false"'; } ?>><?php print $item->getUniqueId(); ?></guid>
312 <description><?php print $item->getDescription() ?></description>
313 <?php if ( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ); ?></pubDate><?php } ?>
314 <?php if ( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor(); ?></dc:creator><?php }?>
315 <?php if ( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ); ?></comments><?php }?>
316 </item>
317 <?php
318 }
319
320 /**
321 * Output an RSS 2.0 footer
322 */
323 function outFooter() {
324 ?>
325 </channel>
326 </rss><?php
327 }
328 }
329
330 /**
331 * Generate an Atom feed
332 *
333 * @ingroup Feed
334 */
335 class AtomFeed extends ChannelFeed {
336 /**
337 * @todo document
338 * @return string
339 */
340 function formatTime( $ts ) {
341 // need to use RFC 822 time format at least for rss2.0
342 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
343 }
344
345 /**
346 * Outputs a basic header for Atom 1.0 feeds.
347 */
348 function outHeader() {
349 global $wgVersion;
350
351 $this->outXmlHeader();
352 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
353 <id><?php print $this->getFeedId() ?></id>
354 <title><?php print $this->getTitle() ?></title>
355 <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
356 <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
357 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
358 <subtitle><?php print $this->getDescription() ?></subtitle>
359 <generator>MediaWiki <?php print $wgVersion ?></generator>
360
361 <?php
362 }
363
364 /**
365 * Atom 1.0 requires a unique, opaque IRI as a unique identifier
366 * for every feed we create. For now just use the URL, but who
367 * can tell if that's right? If we put options on the feed, do we
368 * have to change the id? Maybe? Maybe not.
369 *
370 * @return string
371 * @private
372 */
373 function getFeedId() {
374 return $this->getSelfUrl();
375 }
376
377 /**
378 * Atom 1.0 requests a self-reference to the feed.
379 * @return string
380 * @private
381 */
382 function getSelfUrl() {
383 global $wgRequest;
384 return htmlspecialchars( $wgRequest->getFullRequestURL() );
385 }
386
387 /**
388 * Output a given item.
389 * @param $item
390 */
391 function outItem( $item ) {
392 global $wgMimeType;
393 ?>
394 <entry>
395 <id><?php print $item->getUniqueId(); ?></id>
396 <title><?php print $item->getTitle(); ?></title>
397 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ); ?>"/>
398 <?php if ( $item->getDate() ) { ?>
399 <updated><?php print $this->formatTime( $item->getDate() ); ?>Z</updated>
400 <?php } ?>
401
402 <summary type="html"><?php print $item->getDescription() ?></summary>
403 <?php if ( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor(); ?></name></author><?php }?>
404 </entry>
405
406 <?php /* @todo FIXME: Need to add comments
407 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
408 */
409 }
410
411 /**
412 * Outputs the footer for Atom 1.0 feed (basically '\</feed\>').
413 */
414 function outFooter() {?>
415 </feed><?php
416 }
417 }