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