]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/Feed.php
MediaWiki 1.11.0-scripts
[autoinstalls/mediawiki.git] / includes / Feed.php
1 <?php
2
3 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
5 #
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.
10 #
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.
15 #
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
20
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
25  */
26
27 /**
28  * A base class for basic support for outputting syndication feeds in RSS and other formats.
29  */
30 class FeedItem {
31         /**#@+
32          * @var string
33          * @private
34          */
35         var $Title = 'Wiki';
36         var $Description = '';
37         var $Url = '';
38         var $Date = '';
39         var $Author = '';
40         /**#@-*/
41
42         /**#@+
43          * @todo document
44          */
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;
52         }
53
54         /**
55          * @static
56          */
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 );
61         }
62
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;
69         }
70         function getDate() { return $this->Date; }
71         function getAuthor() { return $this->xmlEncode( $this->Author ); }
72         function getComments() { return $this->xmlEncode( $this->Comments ); }
73         /**#@-*/
74 }
75
76 /**
77  * @todo document (needs one-sentence top-level class description).
78  */
79 class ChannelFeed extends FeedItem {
80         /**#@+
81          * Abstract function, override!
82          * @abstract
83          */
84
85         /**
86          * Generate Header of the feed
87          */
88         function outHeader() {
89                 # print "<feed>";
90         }
91
92         /**
93          * Generate an item
94          * @param $item
95          */
96         function outItem( $item ) {
97                 # print "<item>...</item>";
98         }
99
100         /**
101          * Generate Footer of the feed
102          */
103         function outFooter() {
104                 # print "</feed>";
105         }
106         /**#@-*/
107
108         /**
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.
112          *
113          * This should be called from the outHeader() method,
114          * but can also be called separately.
115          *
116          * @public
117          */
118         function httpHeaders() {
119                 global $wgOut;
120
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();
126
127         }
128
129         /**
130          * Return an internet media type to be sent in the headers.
131          *
132          * @return string
133          * @private
134          */
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');
140         }
141
142         /**
143          * Output the initial XML headers with a stylesheet for legibility
144          * if someone finds it in a browser.
145          * @private
146          */
147         function outXmlHeader() {
148                 global $wgServer, $wgStylePath, $wgStyleVersion;
149
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";
154         }
155 }
156
157 /**
158  * Generate a RSS feed
159  */
160 class RSSFeed extends ChannelFeed {
161
162         /**
163          * Format a date given a timestamp
164          * @param integer $ts Timestamp
165          * @return string Date string
166          */
167         function formatTime( $ts ) {
168                 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
169         }
170
171         /**
172          * Ouput an RSS 2.0 header
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          * Output an RSS 2.0 item
191          * @param FeedItem item to be output
192          */
193         function outItem( $item ) {
194         ?>
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
204         }
205
206         /**
207          * Ouput an RSS 2.0 footer
208          */
209         function outFooter() {
210         ?>
211         </channel>
212 </rss><?php
213         }
214 }
215
216 /**
217  * Generate an Atom feed
218  */
219 class AtomFeed extends ChannelFeed {
220         /**
221          * @todo document
222          */
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 ) );
226         }
227
228         /**
229          * Outputs a basic header for Atom 1.0 feeds.
230          */
231         function outHeader() {
232                 global $wgVersion;
233
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>
243
244 <?php
245         }
246
247         /**
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.
252          *
253          * @return string
254          * @private
255          */
256         function getFeedId() {
257                 return $this->getSelfUrl();
258         }
259
260         /**
261          * Atom 1.0 requests a self-reference to the feed.
262          * @return string
263          * @private
264          */
265         function getSelfUrl() {
266                 global $wgRequest;
267                 return htmlspecialchars( $wgRequest->getFullRequestURL() );
268         }
269
270         /**
271          * Output a given item.
272          * @param $item
273          */
274         function outItem( $item ) {
275                 global $wgMimeType;
276         ?>
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 } ?>
284
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>
288
289 <?php /* FIXME need to add comments
290         <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
291       */
292         }
293
294         /**
295          * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
296          */
297         function outFooter() {?>
298         </feed><?php
299         }
300 }
301
302 ?>