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
00036 class FeedItem {
00041 var $Title = 'Wiki';
00042 var $Description = '';
00043 var $Url = '';
00044 var $Date = '';
00045 var $Author = '';
00046 var $UniqueId = '';
00047 var $RSSIsPermalink;
00060 function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
00061 $this->Title = $Title;
00062 $this->Description = $Description;
00063 $this->Url = $Url;
00064 $this->UniqueId = $Url;
00065 $this->RSSIsPermalink = false;
00066 $this->Date = $Date;
00067 $this->Author = $Author;
00068 $this->Comments = $Comments;
00069 }
00070
00077 public function xmlEncode( $string ) {
00078 $string = str_replace( "\r\n", "\n", $string );
00079 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
00080 return htmlspecialchars( $string );
00081 }
00082
00088 public function getUniqueId() {
00089 if ( $this->UniqueId ) {
00090 return $this->xmlEncode( $this->UniqueId );
00091 }
00092 }
00093
00100 public function setUniqueId($uniqueId, $RSSisPermalink = False) {
00101 $this->UniqueId = $uniqueId;
00102 $this->RSSIsPermalink = $isPermalink;
00103 }
00104
00110 public function getTitle() {
00111 return $this->xmlEncode( $this->Title );
00112 }
00113
00119 public function getUrl() {
00120 return $this->xmlEncode( $this->Url );
00121 }
00122
00128 public function getDescription() {
00129 return $this->xmlEncode( $this->Description );
00130 }
00131
00137 public function getLanguage() {
00138 global $wgContLanguageCode;
00139 return $wgContLanguageCode;
00140 }
00141
00147 public function getDate() {
00148 return $this->Date;
00149 }
00150
00156 public function getAuthor() {
00157 return $this->xmlEncode( $this->Author );
00158 }
00159
00165 public function getComments() {
00166 return $this->xmlEncode( $this->Comments );
00167 }
00168
00175 public static function stripComment( $text ) {
00176 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
00177 }
00179 }
00180
00185 class ChannelFeed extends FeedItem {
00194 function outHeader() {
00195 # print "<feed>";
00196 }
00197
00202 function outItem( $item ) {
00203 # print "<item>...</item>";
00204 }
00205
00209 function outFooter() {
00210 # print "</feed>";
00211 }
00222 public function httpHeaders() {
00223 global $wgOut;
00224
00225 # We take over from $wgOut, excepting its cache header info
00226 $wgOut->disable();
00227 $mimetype = $this->contentType();
00228 header( "Content-type: $mimetype; charset=UTF-8" );
00229 $wgOut->sendCacheControl();
00230
00231 }
00232
00239 function contentType() {
00240 global $wgRequest;
00241 $ctype = $wgRequest->getVal('ctype','application/xml');
00242 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
00243 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
00244 }
00245
00251 function outXmlHeader() {
00252 global $wgStylePath, $wgStyleVersion;
00253
00254 $this->httpHeaders();
00255 echo '<?xml version="1.0"?>' . "\n";
00256 echo '<?xml-stylesheet type="text/css" href="' .
00257 htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
00258 '"?' . ">\n";
00259 }
00260 }
00261
00267 class RSSFeed extends ChannelFeed {
00268
00275 function formatTime( $ts ) {
00276 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
00277 }
00278
00282 function outHeader() {
00283 global $wgVersion;
00284
00285 $this->outXmlHeader();
00286 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
00287 <channel>
00288 <title><?php print $this->getTitle() ?></title>
00289 <link><?php print $this->getUrl() ?></link>
00290 <description><?php print $this->getDescription() ?></description>
00291 <language><?php print $this->getLanguage() ?></language>
00292 <generator>MediaWiki <?php print $wgVersion ?></generator>
00293 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
00294 <?php
00295 }
00296
00301 function outItem( $item ) {
00302 ?>
00303 <item>
00304 <title><?php print $item->getTitle() ?></title>
00305 <link><?php print $item->getUrl() ?></link>
00306 <guid<?php if( $item->RSSIsPermalink ) print ' isPermaLink="true"' ?>><?php print $item->getUniqueId() ?></guid>
00307 <description><?php print $item->getDescription() ?></description>
00308 <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
00309 <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
00310 <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
00311 </item>
00312 <?php
00313 }
00314
00318 function outFooter() {
00319 ?>
00320 </channel>
00321 </rss><?php
00322 }
00323 }
00324
00330 class AtomFeed extends ChannelFeed {
00334 function formatTime( $ts ) {
00335
00336 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
00337 }
00338
00342 function outHeader() {
00343 global $wgVersion;
00344
00345 $this->outXmlHeader();
00346 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
00347 <id><?php print $this->getFeedId() ?></id>
00348 <title><?php print $this->getTitle() ?></title>
00349 <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
00350 <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
00351 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
00352 <subtitle><?php print $this->getDescription() ?></subtitle>
00353 <generator>MediaWiki <?php print $wgVersion ?></generator>
00354
00355 <?php
00356 }
00357
00367 function getFeedId() {
00368 return $this->getSelfUrl();
00369 }
00370
00376 function getSelfUrl() {
00377 global $wgRequest;
00378 return htmlspecialchars( $wgRequest->getFullRequestURL() );
00379 }
00380
00385 function outItem( $item ) {
00386 global $wgMimeType;
00387 ?>
00388 <entry>
00389 <id><?php print $item->getUniqueId() ?></id>
00390 <title><?php print $item->getTitle() ?></title>
00391 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
00392 <?php if( $item->getDate() ) { ?>
00393 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
00394 <?php } ?>
00395
00396 <summary type="html"><?php print $item->getDescription() ?></summary>
00397 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
00398 </entry>
00399
00400 <?php
00401
00402
00403 }
00404
00408 function outFooter() {?>
00409 </feed><?php
00410 }
00411 }