some comments
[mediawiki.git] / includes / Feed.php
blobf50d7ee628868dc8daa90a0a113bb7b157746b2f
1 <?php
2 # Basic support for outputting syndication feeds in RSS, other formats
3 #
4 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # http://www.gnu.org/copyleft/gpl.html
22 /**
23 * Contain a feed class as well as classes to build rss / atom ... feeds
24 * @package MediaWiki
27 /**
28 * Available feeds objects
30 $wgFeedClasses = array(
31 'rss' => 'RSSFeed',
32 'atom' => 'AtomFeed',
35 /**
36 * @todo document
37 * @package MediaWiki
39 class FeedItem {
40 /**#@+
41 * @var string
42 * @access private
44 var $Title = 'Wiki';
45 var $Description = '';
46 var $Url = '';
47 var $Date = '';
48 var $Author = '';
49 /**#@-*/
51 /**
52 * @todo document
54 function FeedItem( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
55 $this->Title = $Title;
56 $this->Description = $Description;
57 $this->Url = $Url;
58 $this->Date = $Date;
59 $this->Author = $Author;
60 $this->Comments = $Comments;
63 /**
64 * @static
65 * @todo document
67 function xmlEncode( $string ) {
68 global $wgInputEncoding, $wgLang;
69 $string = str_replace( "\r\n", "\n", $string );
70 if( strcasecmp( $wgInputEncoding, 'utf-8' ) != 0 ) {
71 $string = $wgLang->iconv( $wgInputEncoding, 'utf-8', $string );
73 return htmlspecialchars( $string );
76 /**
77 * @todo document
79 function getTitle() { return $this->xmlEncode( $this->Title ); }
80 /**
81 * @todo document
83 function getUrl() { return $this->xmlEncode( $this->Url ); }
84 /**
85 * @todo document
87 function getDescription() { return $this->xmlEncode( $this->Description ); }
88 /**
89 * @todo document
91 function getLanguage() {
92 global $wgLanguageCode;
93 return $wgLanguageCode;
95 /**
96 * @todo document
98 function getDate() { return $this->Date; }
99 /**
100 * @todo document
102 function getAuthor() { return $this->xmlEncode( $this->Author ); }
104 * @todo document
106 function getComments() { return $this->xmlEncode( $this->Comments ); }
110 * @todo document
111 * @package MediaWiki
113 class ChannelFeed extends FeedItem {
114 /**#@+
115 * Abstract function, override!
116 * @abstract
120 * Generate Header of the feed
122 function outHeader() {
123 # print "<feed>";
127 * Generate an item
128 * @param $item
130 function outItem( $item ) {
131 # print "<item>...</item>";
135 * Generate Footer of the feed
137 function outFooter() {
138 # print "</feed>";
140 /**#@-*/
143 * @todo document
144 * @param string $mimetype (optional) type of output
146 function outXmlHeader( $mimetype='application/xml' ) {
147 global $wgServer, $wgStylePath, $wgOut;
149 # We take over from $wgOut, excepting its cache header info
150 $wgOut->disable();
151 header( "Content-type: $mimetype; charset=UTF-8" );
152 $wgOut->sendCacheControl();
154 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
155 print '<' . '?xml-stylesheet type="text/css" href="' .
156 htmlspecialchars( "$wgServer$wgStylePath/feed.css" ) . '"?' . ">\n";
161 * Generate a RSS feed
162 * @todo document
163 * @package MediaWiki
165 class RSSFeed extends ChannelFeed {
168 * Format a date given a timestamp
169 * @param integer $ts Timestamp
170 * @return string Date string
172 function formatTime( $ts ) {
173 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp2Unix( $ts ) );
177 * Ouput an RSS 2.0 header
179 function outHeader() {
180 global $wgVersion;
182 $this->outXmlHeader();
183 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
184 <channel>
185 <title><?php print $this->getTitle() ?></title>
186 <link><?php print $this->getUrl() ?></link>
187 <description><?php print $this->getDescription() ?></description>
188 <language><?php print $this->getLanguage() ?></language>
189 <generator>MediaWiki <?php print $wgVersion ?></generator>
190 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
191 <?php
195 * Output an RSS 2.0 item
196 * @param FeedItem item to be output
198 function outItem( $item ) {
200 <item>
201 <title><?php print $item->getTitle() ?></title>
202 <link><?php print $item->getUrl() ?></link>
203 <description><?php print $item->getDescription() ?></description>
204 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
205 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
206 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
207 </item>
208 <?php
212 * Ouput an RSS 2.0 footer
214 function outFooter() {
216 </channel>
217 </rss><?php
222 * Generate an Atom feed
223 * @todo document
224 * @package MediaWiki
226 class AtomFeed extends ChannelFeed {
228 * @todo document
230 function formatTime( $ts ) {
231 // need to use RFC 822 time format at least for rss2.0
232 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp2Unix( $ts ) );
236 * @todo document
238 function outHeader() {
239 global $wgVersion, $wgOut;
241 $this->outXmlHeader();
242 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage() ?>">
243 <title><?php print $this->getTitle() ?></title>
244 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
245 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
246 <tagline><?php print $this->getDescription() ?></tagline>
247 <generator>MediaWiki <?php print $wgVersion ?></generator>
249 <?php
253 * @todo document
255 function outItem( $item ) {
256 global $wgMimeType;
258 <entry>
259 <title><?php print $item->getTitle() ?></title>
260 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
261 <?php if( $item->getDate() ) { ?>
262 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
263 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
264 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
266 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
267 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><!-- <url></url><email></email> --></author><?php }?>
268 <comment>foobar</comment>
269 </entry>
271 <?php /* FIXME need to add comments
272 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
277 * @todo document
279 function outFooter() {?>
280 </feed><?php