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