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