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