b0d208b251e105a2e6db50f010abc79bad832b7d
[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 $wgFeedClasses = array(
23 'rss' => 'RSSFeed',
24 'atom' => 'AtomFeed',
25 );
26
27 class FeedItem {
28 var $Title = 'Wiki';
29 var $Description = '';
30 var $Url = '';
31 var $Date = '';
32 var $Author = '';
33
34 function FeedItem( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
35 $this->Title = $Title;
36 $this->Description = $Description;
37 $this->Url = $Url;
38 $this->Date = $Date;
39 $this->Author = $Author;
40 $this->Comments = $Comments;
41 }
42
43 /* Static... */
44 function xmlEncode( $string ) {
45 global $wgInputEncoding, $wgLang;
46 $string = str_replace( "\r\n", "\n", $string );
47 if( strcasecmp( $wgInputEncoding, 'utf-8' ) != 0 ) {
48 $string = $wgLang->iconv( $wgInputEncoding, 'utf-8', $string );
49 }
50 return htmlspecialchars( $string );
51 }
52 function getTitle() {
53 return $this->xmlEncode( $this->Title );
54 }
55 function getUrl() {
56 return $this->xmlEncode( $this->Url );
57 }
58 function getDescription() {
59 return $this->xmlEncode( $this->Description );
60 }
61 function getLanguage() {
62 global $wgLanguageCode;
63 return $wgLanguageCode;
64 }
65 function getDate() {
66 return $this->Date;
67 }
68 function getAuthor() {
69 return $this->xmlEncode( $this->Author );
70 }
71 function getComments() {
72 return $this->xmlEncode( $this->Comments );
73 }
74 }
75
76 class ChannelFeed extends FeedItem {
77 /* Abstract functions, override! */
78 function outHeader() {
79 # print "<feed>";
80 }
81 function outItem( $item ) {
82 # print "<item>...</item>";
83 }
84 function outFooter() {
85 # print "</feed>";
86 }
87
88 function outXmlHeader( $mimetype='application/xml' ) {
89 global $wgServer, $wgStylePath, $wgOut;
90
91 # We take over from $wgOut, excepting its cache header info
92 $wgOut->disable();
93 header( "Content-type: $mimetype; charset=UTF-8" );
94 $wgOut->sendCacheControl();
95
96 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
97 print '<' . '?xml-stylesheet type="text/css" href="' .
98 htmlspecialchars( "$wgServer$wgStylePath/feed.css" ) . '"?' . ">\n";
99 }
100 }
101
102 class RSSFeed extends ChannelFeed {
103
104 function formatTime( $ts ) {
105 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp2Unix( $ts ) );
106 }
107
108 function outHeader() {
109 global $wgVersion;
110
111 $this->outXmlHeader();
112 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
113 <channel>
114 <title><?php print $this->getTitle() ?></title>
115 <link><?php print $this->getUrl() ?></link>
116 <description><?php print $this->getDescription() ?></description>
117 <language><?php print $this->getLanguage() ?></language>
118 <generator>MediaWiki <?php print $wgVersion ?></generator>
119 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
120 <?php
121 }
122
123 function outItem( $item ) {
124 ?>
125 <item>
126 <title><?php print $item->getTitle() ?></title>
127 <link><?php print $item->getUrl() ?></link>
128 <description><?php print $item->getDescription() ?></description>
129 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
130 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
131 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
132 </item>
133 <?php
134 }
135
136 function outFooter() {
137 ?>
138 </channel>
139 </rss><?php
140 }
141 }
142
143 class AtomFeed extends ChannelFeed {
144 function formatTime( $ts ) {
145 // need to use RFC 822 time format at least for rss2.0
146 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp2Unix( $ts ) );
147 }
148
149 function outHeader() {
150 global $wgVersion, $wgOut;
151
152 $this->outXmlHeader();
153 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage() ?>">
154 <title><?php print $this->getTitle() ?></title>
155 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
156 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
157 <tagline><?php print $this->getDescription() ?></tagline>
158 <generator>MediaWiki <?php print $wgVersion ?></generator>
159
160 <?php
161 }
162
163 function outItem( $item ) {
164 global $wgMimeType;
165 ?>
166 <entry>
167 <title><?php print $item->getTitle() ?></title>
168 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
169 <?php if( $item->getDate() ) { ?>
170 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
171 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
172 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
173
174 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
175 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><!-- <url></url><email></email> --></author><?php }?>
176 <comment>foobar</comment>
177 </entry>
178
179 <?php /* FIXME need to add comments
180 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
181 */
182 }
183
184 function outFooter() {?>
185 </feed><?php
186 }
187 }
188
189 ?>