f50d7ee628868dc8daa90a0a113bb7b157746b2f
[lhc/web/wiklou.git] / includes / Feed.php
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.
11 #
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.
16 #
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
21
22 /**
23 * Contain a feed class as well as classes to build rss / atom ... feeds
24 * @package MediaWiki
25 */
26
27 /**
28 * Available feeds objects
29 */
30 $wgFeedClasses = array(
31 'rss' => 'RSSFeed',
32 'atom' => 'AtomFeed',
33 );
34
35 /**
36 * @todo document
37 * @package MediaWiki
38 */
39 class FeedItem {
40 /**#@+
41 * @var string
42 * @access private
43 */
44 var $Title = 'Wiki';
45 var $Description = '';
46 var $Url = '';
47 var $Date = '';
48 var $Author = '';
49 /**#@-*/
50
51 /**
52 * @todo document
53 */
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;
61 }
62
63 /**
64 * @static
65 * @todo document
66 */
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 );
72 }
73 return htmlspecialchars( $string );
74 }
75
76 /**
77 * @todo document
78 */
79 function getTitle() { return $this->xmlEncode( $this->Title ); }
80 /**
81 * @todo document
82 */
83 function getUrl() { return $this->xmlEncode( $this->Url ); }
84 /**
85 * @todo document
86 */
87 function getDescription() { return $this->xmlEncode( $this->Description ); }
88 /**
89 * @todo document
90 */
91 function getLanguage() {
92 global $wgLanguageCode;
93 return $wgLanguageCode;
94 }
95 /**
96 * @todo document
97 */
98 function getDate() { return $this->Date; }
99 /**
100 * @todo document
101 */
102 function getAuthor() { return $this->xmlEncode( $this->Author ); }
103 /**
104 * @todo document
105 */
106 function getComments() { return $this->xmlEncode( $this->Comments ); }
107 }
108
109 /**
110 * @todo document
111 * @package MediaWiki
112 */
113 class ChannelFeed extends FeedItem {
114 /**#@+
115 * Abstract function, override!
116 * @abstract
117 */
118
119 /**
120 * Generate Header of the feed
121 */
122 function outHeader() {
123 # print "<feed>";
124 }
125
126 /**
127 * Generate an item
128 * @param $item
129 */
130 function outItem( $item ) {
131 # print "<item>...</item>";
132 }
133
134 /**
135 * Generate Footer of the feed
136 */
137 function outFooter() {
138 # print "</feed>";
139 }
140 /**#@-*/
141
142 /**
143 * @todo document
144 * @param string $mimetype (optional) type of output
145 */
146 function outXmlHeader( $mimetype='application/xml' ) {
147 global $wgServer, $wgStylePath, $wgOut;
148
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();
153
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";
157 }
158 }
159
160 /**
161 * Generate a RSS feed
162 * @todo document
163 * @package MediaWiki
164 */
165 class RSSFeed extends ChannelFeed {
166
167 /**
168 * Format a date given a timestamp
169 * @param integer $ts Timestamp
170 * @return string Date string
171 */
172 function formatTime( $ts ) {
173 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp2Unix( $ts ) );
174 }
175
176 /**
177 * Ouput an RSS 2.0 header
178 */
179 function outHeader() {
180 global $wgVersion;
181
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
192 }
193
194 /**
195 * Output an RSS 2.0 item
196 * @param FeedItem item to be output
197 */
198 function outItem( $item ) {
199 ?>
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
209 }
210
211 /**
212 * Ouput an RSS 2.0 footer
213 */
214 function outFooter() {
215 ?>
216 </channel>
217 </rss><?php
218 }
219 }
220
221 /**
222 * Generate an Atom feed
223 * @todo document
224 * @package MediaWiki
225 */
226 class AtomFeed extends ChannelFeed {
227 /**
228 * @todo document
229 */
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 ) );
233 }
234
235 /**
236 * @todo document
237 */
238 function outHeader() {
239 global $wgVersion, $wgOut;
240
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>
248
249 <?php
250 }
251
252 /**
253 * @todo document
254 */
255 function outItem( $item ) {
256 global $wgMimeType;
257 ?>
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 } ?>
265
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>
270
271 <?php /* FIXME need to add comments
272 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
273 */
274 }
275
276 /**
277 * @todo document
278 */
279 function outFooter() {?>
280 </feed><?php
281 }
282 }
283
284 ?>