+add the "comments" field to rss feeds
[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
89 class RSSFeed extends ChannelFeed {
90
91 function formatTime( $ts ) {
92 return gmdate( "D, d M Y H:i:s \G\M\T", wfTimestamp2Unix( $ts ) );
93 }
94
95 function outHeader() {
96 global $wgVersion, $wgOut;
97
98 # We take over from $wgOut, excepting its cache header info
99 $wgOut->disable();
100 header( "Content-type: application/xml; charset=UTF-8" );
101 $wgOut->sendCacheControl();
102
103 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
104 ?><rss version="2.0">
105 <channel>
106 <title><?php print $this->getTitle() ?></title>
107 <link><?php print $this->getUrl() ?></link>
108 <description><?php print $this->getDescription() ?></description>
109 <language><?php print $this->getLanguage() ?></language>
110 <generator>MediaWiki <?php print $wgVersion ?></generator>
111 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
112 <?php
113 }
114
115 function outItem( $item ) {
116 ?>
117 <item>
118 <title><?php print $item->getTitle() ?></title>
119 <link><?php print $item->getUrl() ?></link>
120 <description><?php print $item->getDescription() ?></description>
121 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
122 <?php if( $item->getAuthor() ) { ?><author><?php print $item->getAuthor() ?></author><?php }?>
123 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
124 </item>
125 <?php
126 }
127
128 function outFooter() {
129 ?>
130 </channel>
131 </rss><?php
132 }
133 }
134
135 class AtomFeed extends ChannelFeed {
136 function formatTime( $ts ) {
137 // need to use RFC 822 time format at least for rss2.0
138 return gmdate( "Y-m-d\TH:i:s", wfTimestamp2Unix( $ts ) );
139 }
140
141 function outHeader() {
142 global $wgVersion, $wgOut;
143
144 # We take over from $wgOut, excepting its cache header info
145 $wgOut->disable();
146 header( "Content-type: application/xml; charset=UTF-8" );
147 $wgOut->sendCacheControl();
148
149 print '<' . '?xml version="1.0" encoding="utf-8"?' . ">\n";
150 ?><feed version="0.3" xml:lang="<?php print $this->getLanguage()."-".$this->getLanguage() ?>">
151 <title><?php print $this->getTitle() ?></title>
152 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
153 <modified><?php print $this->formatTime( wfTimestampNow() ) ?>Z</modified>
154 <tagline><?php print $this->getDescription() ?></tagline>
155 <generator>MediaWiki <?php print $wgVersion ?></generator>
156
157 <?php
158 }
159
160 function outItem( $item ) {
161 ?>
162 <entry>
163 <title><?php print $item->getTitle() ?></title>
164 <link rel="alternate" type="text/html" href="<?php print $item->getUrl() ?>"/>
165 <?php if( $item->getDate() ) { ?>
166 <modified><?php print $this->formatTime( $item->getDate() ) ?>Z</modified>
167 <issued><?php print $this->formatTime( $item->getDate() ) ?></issued>
168 <created><?php print $this->formatTime( $item->getDate() ) ?>Z</created><?php } ?>
169
170 <summary type="text/plain"><?php print $item->getDescription() ?></summary>
171 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name><url></url><email></email></author><?php }?>
172 <comment>foobar</comment>
173 </entry>
174
175 <?php /* FIXME need to add comments
176 <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
177 */
178 }
179
180 function outFooter() {?>
181 </feed><?php
182 }
183 }
184
185 ?>