]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/Feed.php
MediaWiki 1.14.0
[autoinstallsdev/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          * @param $Url URL uniquely designating the item.
45          */
46         function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
47                 $this->Title = $Title;
48                 $this->Description = $Description;
49                 $this->Url = $Url;
50                 $this->Date = $Date;
51                 $this->Author = $Author;
52                 $this->Comments = $Comments;
53         }
54
55         public function xmlEncode( $string ) {
56                 $string = str_replace( "\r\n", "\n", $string );
57                 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
58                 return htmlspecialchars( $string );
59         }
60
61         public function getTitle() {
62                 return $this->xmlEncode( $this->Title );
63         }
64
65         public function getUrl() {
66                 return $this->xmlEncode( $this->Url );
67         }
68
69         public function getDescription() {
70                 return $this->xmlEncode( $this->Description );
71         }
72
73         public function getLanguage() {
74                 global $wgContLanguageCode;
75                 return $wgContLanguageCode;
76         }
77
78         public function getDate() {
79                 return $this->Date;
80         }
81         public function getAuthor() {
82                 return $this->xmlEncode( $this->Author );
83         }
84         public function getComments() {
85                 return $this->xmlEncode( $this->Comments );
86         }
87         
88         /**
89          * Quickie hack... strip out wikilinks to more legible form from the comment.
90          */
91         public static function stripComment( $text ) {
92                 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
93         }
94         /**#@-*/
95 }
96
97 /**
98  * @todo document (needs one-sentence top-level class description).
99  */
100 class ChannelFeed extends FeedItem {
101         /**#@+
102          * Abstract function, override!
103          * @abstract
104          */
105
106         /**
107          * Generate Header of the feed
108          */
109         function outHeader() {
110                 # print "<feed>";
111         }
112
113         /**
114          * Generate an item
115          * @param $item
116          */
117         function outItem( $item ) {
118                 # print "<item>...</item>";
119         }
120
121         /**
122          * Generate Footer of the feed
123          */
124         function outFooter() {
125                 # print "</feed>";
126         }
127         /**#@-*/
128
129         /**
130          * Setup and send HTTP headers. Don't send any content;
131          * content might end up being cached and re-sent with
132          * these same headers later.
133          *
134          * This should be called from the outHeader() method,
135          * but can also be called separately.
136          *
137          * @public
138          */
139         function httpHeaders() {
140                 global $wgOut;
141
142                 # We take over from $wgOut, excepting its cache header info
143                 $wgOut->disable();
144                 $mimetype = $this->contentType();
145                 header( "Content-type: $mimetype; charset=UTF-8" );
146                 $wgOut->sendCacheControl();
147
148         }
149
150         /**
151          * Return an internet media type to be sent in the headers.
152          *
153          * @return string
154          * @private
155          */
156         function contentType() {
157                 global $wgRequest;
158                 $ctype = $wgRequest->getVal('ctype','application/xml');
159                 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
160                 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
161         }
162
163         /**
164          * Output the initial XML headers with a stylesheet for legibility
165          * if someone finds it in a browser.
166          * @private
167          */
168         function outXmlHeader() {
169                 global $wgStylePath, $wgStyleVersion;
170
171                 $this->httpHeaders();
172                 echo '<?xml version="1.0"?>' . "\n";
173                 echo '<?xml-stylesheet type="text/css" href="' .
174                         htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
175                         '"?' . ">\n";
176         }
177 }
178
179 /**
180  * Generate a RSS feed
181  */
182 class RSSFeed extends ChannelFeed {
183
184         /**
185          * Format a date given a timestamp
186          * @param integer $ts Timestamp
187          * @return string Date string
188          */
189         function formatTime( $ts ) {
190                 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
191         }
192
193         /**
194          * Ouput an RSS 2.0 header
195          */
196         function outHeader() {
197                 global $wgVersion;
198
199                 $this->outXmlHeader();
200                 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
201         <channel>
202                 <title><?php print $this->getTitle() ?></title>
203                 <link><?php print $this->getUrl() ?></link>
204                 <description><?php print $this->getDescription() ?></description>
205                 <language><?php print $this->getLanguage() ?></language>
206                 <generator>MediaWiki <?php print $wgVersion ?></generator>
207                 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
208 <?php
209         }
210
211         /**
212          * Output an RSS 2.0 item
213          * @param FeedItem item to be output
214          */
215         function outItem( $item ) {
216         ?>
217                 <item>
218                         <title><?php print $item->getTitle() ?></title>
219                         <link><?php print $item->getUrl() ?></link>
220                         <description><?php print $item->getDescription() ?></description>
221                         <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
222                         <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
223                         <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
224                 </item>
225 <?php
226         }
227
228         /**
229          * Ouput an RSS 2.0 footer
230          */
231         function outFooter() {
232         ?>
233         </channel>
234 </rss><?php
235         }
236 }
237
238 /**
239  * Generate an Atom feed
240  */
241 class AtomFeed extends ChannelFeed {
242         /**
243          * @todo document
244          */
245         function formatTime( $ts ) {
246                 // need to use RFC 822 time format at least for rss2.0
247                 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
248         }
249
250         /**
251          * Outputs a basic header for Atom 1.0 feeds.
252          */
253         function outHeader() {
254                 global $wgVersion;
255
256                 $this->outXmlHeader();
257                 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
258                 <id><?php print $this->getFeedId() ?></id>
259                 <title><?php print $this->getTitle() ?></title>
260                 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
261                 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
262                 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
263                 <subtitle><?php print $this->getDescription() ?></subtitle>
264                 <generator>MediaWiki <?php print $wgVersion ?></generator>
265
266 <?php
267         }
268
269         /**
270          * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
271          * for every feed we create. For now just use the URL, but who
272          * can tell if that's right? If we put options on the feed, do we
273          * have to change the id? Maybe? Maybe not.
274          *
275          * @return string
276          * @private
277          */
278         function getFeedId() {
279                 return $this->getSelfUrl();
280         }
281
282         /**
283          * Atom 1.0 requests a self-reference to the feed.
284          * @return string
285          * @private
286          */
287         function getSelfUrl() {
288                 global $wgRequest;
289                 return htmlspecialchars( $wgRequest->getFullRequestURL() );
290         }
291
292         /**
293          * Output a given item.
294          * @param $item
295          */
296         function outItem( $item ) {
297                 global $wgMimeType;
298         ?>
299         <entry>
300                 <id><?php print $item->getUrl() ?></id>
301                 <title><?php print $item->getTitle() ?></title>
302                 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
303                 <?php if( $item->getDate() ) { ?>
304                 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
305                 <?php } ?>
306
307                 <summary type="html"><?php print $item->getDescription() ?></summary>
308                 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
309         </entry>
310
311 <?php /* FIXME need to add comments
312         <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
313       */
314         }
315
316         /**
317          * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
318          */
319         function outFooter() {?>
320         </feed><?php
321         }
322 }