* Fixed E_NOTICE..
[mediawiki.git] / includes / Feed.php
blobed4343c31bfbc5d330c98706546a1aa19d028f90
1 <?php
3 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
21 /**
22 * Basic support for outputting syndication feeds in RSS, other formats.
23 * Contain a feed class as well as classes to build rss / atom ... feeds
24 * Available feeds are defined in Defines.php
27 /**
28 * A base class for basic support for outputting syndication feeds in RSS and other formats.
30 class FeedItem {
31 /**#@+
32 * @var string
33 * @private
35 var $Title = 'Wiki';
36 var $Description = '';
37 var $Url = '';
38 var $Date = '';
39 var $Author = '';
40 /**#@-*/
42 /**#@+
43 * @todo document
45 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
46 $this->Title = $Title;
47 $this->Description = $Description;
48 $this->Url = $Url;
49 $this->Date = $Date;
50 $this->Author = $Author;
51 $this->Comments = $Comments;
54 /**
55 * @static
57 function xmlEncode( $string ) {
58 $string = str_replace( "\r\n", "\n", $string );
59 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
60 return htmlspecialchars( $string );
63 function getTitle() { return $this->xmlEncode( $this->Title ); }
64 function getUrl() { return $this->xmlEncode( $this->Url ); }
65 function getDescription() { return $this->xmlEncode( $this->Description ); }
66 function getLanguage() {
67 global $wgContLanguageCode;
68 return $wgContLanguageCode;
70 function getDate() { return $this->Date; }
71 function getAuthor() { return $this->xmlEncode( $this->Author ); }
72 function getComments() { return $this->xmlEncode( $this->Comments ); }
73 /**#@-*/
76 /**
77 * @todo document (needs one-sentence top-level class description).
79 class ChannelFeed extends FeedItem {
80 /**#@+
81 * Abstract function, override!
82 * @abstract
85 /**
86 * Generate Header of the feed
88 function outHeader() {
89 # print "<feed>";
92 /**
93 * Generate an item
94 * @param $item
96 function outItem( $item ) {
97 # print "<item>...</item>";
101 * Generate Footer of the feed
103 function outFooter() {
104 # print "</feed>";
106 /**#@-*/
109 * Setup and send HTTP headers. Don't send any content;
110 * content might end up being cached and re-sent with
111 * these same headers later.
113 * This should be called from the outHeader() method,
114 * but can also be called separately.
116 * @public
118 function httpHeaders() {
119 global $wgOut;
121 # We take over from $wgOut, excepting its cache header info
122 $wgOut->disable();
123 $mimetype = $this->contentType();
124 header( "Content-type: $mimetype; charset=UTF-8" );
125 $wgOut->sendCacheControl();
130 * Return an internet media type to be sent in the headers.
132 * @return string
133 * @private
135 function contentType() {
136 global $wgRequest;
137 $ctype = $wgRequest->getVal('ctype','application/xml');
138 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
139 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
143 * Output the initial XML headers with a stylesheet for legibility
144 * if someone finds it in a browser.
145 * @private
147 function outXmlHeader() {
148 global $wgServer, $wgStylePath, $wgStyleVersion;
150 $this->httpHeaders();
151 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
152 echo '<?xml-stylesheet type="text/css" href="' .
153 htmlspecialchars( "$wgServer$wgStylePath/common/feed.css?$wgStyleVersion" ) . '"?' . ">\n";
158 * Generate a RSS feed
160 class RSSFeed extends ChannelFeed {
163 * Format a date given a timestamp
164 * @param integer $ts Timestamp
165 * @return string Date string
167 function formatTime( $ts ) {
168 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
172 * Ouput an RSS 2.0 header
174 function outHeader() {
175 global $wgVersion;
177 $this->outXmlHeader();
178 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
179 <channel>
180 <title><?php print $this->getTitle() ?></title>
181 <link><?php print $this->getUrl() ?></link>
182 <description><?php print $this->getDescription() ?></description>
183 <language><?php print $this->getLanguage() ?></language>
184 <generator>MediaWiki <?php print $wgVersion ?></generator>
185 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
186 <?php
190 * Output an RSS 2.0 item
191 * @param FeedItem item to be output
193 function outItem( $item ) {
195 <item>
196 <title><?php print $item->getTitle() ?></title>
197 <link><?php print $item->getUrl() ?></link>
198 <description><?php print $item->getDescription() ?></description>
199 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
200 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
201 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
202 </item>
203 <?php
207 * Ouput an RSS 2.0 footer
209 function outFooter() {
211 </channel>
212 </rss><?php
217 * Generate an Atom feed
219 class AtomFeed extends ChannelFeed {
221 * @todo document
223 function formatTime( $ts ) {
224 // need to use RFC 822 time format at least for rss2.0
225 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
229 * Outputs a basic header for Atom 1.0 feeds.
231 function outHeader() {
232 global $wgVersion;
234 $this->outXmlHeader();
235 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
236 <id><?php print $this->getFeedId() ?></id>
237 <title><?php print $this->getTitle() ?></title>
238 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
239 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
240 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
241 <subtitle><?php print $this->getDescription() ?></subtitle>
242 <generator>MediaWiki <?php print $wgVersion ?></generator>
244 <?php
248 * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
249 * for every feed we create. For now just use the URL, but who
250 * can tell if that's right? If we put options on the feed, do we
251 * have to change the id? Maybe? Maybe not.
253 * @return string
254 * @private
256 function getFeedId() {
257 return $this->getSelfUrl();
261 * Atom 1.0 requests a self-reference to the feed.
262 * @return string
263 * @private
265 function getSelfUrl() {
266 global $wgRequest;
267 return htmlspecialchars( $wgRequest->getFullRequestURL() );
271 * Output a given item.
272 * @param $item
274 function outItem( $item ) {
275 global $wgMimeType;
277 <entry>
278 <id><?php print $item->getUrl() ?></id>
279 <title><?php print $item->getTitle() ?></title>
280 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
281 <?php if( $item->getDate() ) { ?>
282 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
283 <?php } ?>
285 <summary type="html"><?php print $item->getDescription() ?></summary>
286 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
287 </entry>
289 <?php /* FIXME need to add comments
290 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
295 * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
297 function outFooter() {?>
298 </feed><?php