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