Split files and classes in different packages for phpdocumentor. I probably changed...
[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 */
117
118 /**
119 * @todo document
120 */
121 function outHeader() {
122 # print "<feed>";
123 }
124
125 /**
126 * @todo document
127 */
128 function outItem( $item ) {
129 # print "<item>...</item>";
130 }
131
132 /**
133 * @todo document
134 */
135 function outFooter() {
136 # print "</feed>";
137 }
138
139 /**
140 * @todo document
141 */
142 function outXmlHeader( $mimetype='application/xml' ) {
143 global $wgServer, $wgStylePath, $wgOut;
144
145 # We take over from $wgOut, excepting its cache header info
146 $wgOut->disable();
147 header( "Content-type: $mimetype; charset=UTF-8" );
148 $wgOut->sendCacheControl();
149
150 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
151 print '<' . '?xml-stylesheet type="text/css" href="' .
152 htmlspecialchars( "$wgServer$wgStylePath/feed.css" ) . '"?' . ">\n";
153 }
154 /**#@-*/
155 }
156
157 /**
158 * Generate a RSS feed
159 * @todo document
160 * @package MediaWiki
161 */
162 class RSSFeed extends ChannelFeed {
163
164 /**
165 * @todo document
166 */
167 function formatTime( $ts ) {
168 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp2Unix( $ts ) );
169 }
170
171 /**
172 * @todo document
173 */
174 function outHeader() {
175 global $wgVersion;
176
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
187 }
188
189 /**
190 * @todo document
191 */
192 function outItem( $item ) {
193 ?>
194 <item>
195 <title><?php print $item->getTitle() ?></title>
196 <link><?php print $item->getUrl() ?></link>
197 <description><?php print $item->getDescription() ?></description>
198 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
199 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
200 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
201 </item>
202 <?php
203 }
204
205 /**
206 * @todo document
207 */
208 function outFooter() {
209 ?>
210 </channel>
211 </rss><?php
212 }
213 }
214
215 /**
216 * Generate an Atom feed
217 * @todo document
218 * @package MediaWiki
219 */
220 class AtomFeed extends ChannelFeed {
221 /**
222 * @todo document
223 */
224 function formatTime( $ts ) {
225 // need to use RFC 822 time format at least for rss2.0
226 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp2Unix( $ts ) );
227 }
228
229 /**
230 * @todo document
231 */
232 function outHeader() {
233 global $wgVersion, $wgOut;
234
235 $this->outXmlHeader();
236 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage() ?>">
237 <title><?php print $this->getTitle() ?></title>
238 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
239 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
240 <tagline><?php print $this->getDescription() ?></tagline>
241 <generator>MediaWiki <?php print $wgVersion ?></generator>
242
243 <?php
244 }
245
246 /**
247 * @todo document
248 */
249 function outItem( $item ) {
250 global $wgMimeType;
251 ?>
252 <entry>
253 <title><?php print $item->getTitle() ?></title>
254 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
255 <?php if( $item->getDate() ) { ?>
256 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
257 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
258 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
259
260 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
261 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><!-- <url></url><email></email> --></author><?php }?>
262 <comment>foobar</comment>
263 </entry>
264
265 <?php /* FIXME need to add comments
266 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
267 */
268 }
269
270 /**
271 * @todo document
272 */
273 function outFooter() {?>
274 </feed><?php
275 }
276 }
277
278 ?>