00001 <?php
00002
00003 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
00004 # http://www.mediawiki.org/
00005 #
00006 # This program is free software; you can redistribute it and/or modify
00007 # it under the terms of the GNU General Public License as published by
00008 # the Free Software Foundation; either version 2 of the License, or
00009 # (at your option) any later version.
00010 #
00011 # This program is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014 # GNU General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU General Public License along
00017 # with this program; if not, write to the Free Software Foundation, Inc.,
00018 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 # http://www.gnu.org/copyleft/gpl.html
00020
00030 class FeedItem {
00035 var $Title = 'Wiki';
00036 var $Description = '';
00037 var $Url = '';
00038 var $Date = '';
00039 var $Author = '';
00046 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
00047 $this->Title = $Title;
00048 $this->Description = $Description;
00049 $this->Url = $Url;
00050 $this->Date = $Date;
00051 $this->Author = $Author;
00052 $this->Comments = $Comments;
00053 }
00054
00055 public function xmlEncode( $string ) {
00056 $string = str_replace( "\r\n", "\n", $string );
00057 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
00058 return htmlspecialchars( $string );
00059 }
00060
00061 public function getTitle() {
00062 return $this->xmlEncode( $this->Title );
00063 }
00064
00065 public function getUrl() {
00066 return $this->xmlEncode( $this->Url );
00067 }
00068
00069 public function getDescription() {
00070 return $this->xmlEncode( $this->Description );
00071 }
00072
00073 public function getLanguage() {
00074 global $wgContLanguageCode;
00075 return $wgContLanguageCode;
00076 }
00077
00078 public function getDate() {
00079 return $this->Date;
00080 }
00081 public function getAuthor() {
00082 return $this->xmlEncode( $this->Author );
00083 }
00084 public function getComments() {
00085 return $this->xmlEncode( $this->Comments );
00086 }
00087
00091 public static function stripComment( $text ) {
00092 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
00093 }
00095 }
00096
00100 class ChannelFeed extends FeedItem {
00109 function outHeader() {
00110 # print "<feed>";
00111 }
00112
00117 function outItem( $item ) {
00118 # print "<item>...</item>";
00119 }
00120
00124 function outFooter() {
00125 # print "</feed>";
00126 }
00139 function httpHeaders() {
00140 global $wgOut;
00141
00142 # We take over from $wgOut, excepting its cache header info
00143 $wgOut->disable();
00144 $mimetype = $this->contentType();
00145 header( "Content-type: $mimetype; charset=UTF-8" );
00146 $wgOut->sendCacheControl();
00147
00148 }
00149
00156 function contentType() {
00157 global $wgRequest;
00158 $ctype = $wgRequest->getVal('ctype','application/xml');
00159 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
00160 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
00161 }
00162
00168 function outXmlHeader() {
00169 global $wgStylePath, $wgStyleVersion;
00170
00171 $this->httpHeaders();
00172 echo '<?xml version="1.0"?>' . "\n";
00173 echo '<?xml-stylesheet type="text/css" href="' .
00174 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
00175 '"?' . ">\n";
00176 }
00177 }
00178
00182 class RSSFeed extends ChannelFeed {
00183
00189 function formatTime( $ts ) {
00190 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
00191 }
00192
00196 function outHeader() {
00197 global $wgVersion;
00198
00199 $this->outXmlHeader();
00200 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
00201 <channel>
00202 <title><?php print $this->getTitle() ?></title>
00203 <link><?php print $this->getUrl() ?></link>
00204 <description><?php print $this->getDescription() ?></description>
00205 <language><?php print $this->getLanguage() ?></language>
00206 <generator>MediaWiki <?php print $wgVersion ?></generator>
00207 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
00208 <?php
00209 }
00210
00215 function outItem( $item ) {
00216 ?>
00217 <item>
00218 <title><?php print $item->getTitle() ?></title>
00219 <link><?php print $item->getUrl() ?></link>
00220 <description><?php print $item->getDescription() ?></description>
00221 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
00222 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
00223 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
00224 </item>
00225 <?php
00226 }
00227
00231 function outFooter() {
00232 ?>
00233 </channel>
00234 </rss><?php
00235 }
00236 }
00237
00241 class AtomFeed extends ChannelFeed {
00245 function formatTime( $ts ) {
00246
00247 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
00248 }
00249
00253 function outHeader() {
00254 global $wgVersion;
00255
00256 $this->outXmlHeader();
00257 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
00258 <id><?php print $this->getFeedId() ?></id>
00259 <title><?php print $this->getTitle() ?></title>
00260 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
00261 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
00262 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
00263 <subtitle><?php print $this->getDescription() ?></subtitle>
00264 <generator>MediaWiki <?php print $wgVersion ?></generator>
00265
00266 <?php
00267 }
00268
00278 function getFeedId() {
00279 return $this->getSelfUrl();
00280 }
00281
00287 function getSelfUrl() {
00288 global $wgRequest;
00289 return htmlspecialchars( $wgRequest->getFullRequestURL() );
00290 }
00291
00296 function outItem( $item ) {
00297 global $wgMimeType;
00298 ?>
00299 <entry>
00300 <id><?php print $item->getUrl() ?></id>
00301 <title><?php print $item->getTitle() ?></title>
00302 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
00303 <?php if( $item->getDate() ) { ?>
00304 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
00305 <?php } ?>
00306
00307 <summary type="html"><?php print $item->getDescription() ?></summary>
00308 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
00309 </entry>
00310
00311 <?php
00312
00313
00314 }
00315
00319 function outFooter() {?>
00320 </feed><?php
00321 }
00322 }