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