MediaWiki  REL1_19
Feed.php
Go to the documentation of this file.
00001 <?php
00038 class FeedItem {
00042         var $title;
00043 
00044         var $description;
00045         var $url;
00046         var $date;
00047         var $author;
00048         var $uniqueId;
00049         var $comments;
00050         var $rssIsPermalink = false;
00051 
00062         function __construct( $title, $description, $url, $date = '', $author = '', $comments = '' ) {
00063                 $this->title = $title;
00064                 $this->description = $description;
00065                 $this->url = $url;
00066                 $this->uniqueId = $url;
00067                 $this->date = $date;
00068                 $this->author = $author;
00069                 $this->comments = $comments;
00070         }
00071 
00078         public function xmlEncode( $string ) {
00079                 $string = str_replace( "\r\n", "\n", $string );
00080                 $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
00081                 return htmlspecialchars( $string );
00082         }
00083 
00089         public function getUniqueId() {
00090                 if ( $this->uniqueId ) {
00091                         return $this->xmlEncode( $this->uniqueId );
00092                 }
00093         }
00094 
00101         public function setUniqueId( $uniqueId, $rssIsPermalink = false ) {
00102                 $this->uniqueId = $uniqueId;
00103                 $this->rssIsPermalink = $rssIsPermalink;
00104         }
00105 
00111         public function getTitle() {
00112                 return $this->xmlEncode( $this->title );
00113         }
00114 
00120         public function getUrl() {
00121                 return $this->xmlEncode( $this->url );
00122         }
00123 
00129         public function getDescription() {
00130                 return $this->xmlEncode( $this->description );
00131         }
00132 
00138         public function getLanguage() {
00139                 global $wgLanguageCode;
00140                 return $wgLanguageCode;
00141         }
00142 
00148         public function getDate() {
00149                 return $this->date;
00150         }
00151 
00157         public function getAuthor() {
00158                 return $this->xmlEncode( $this->author );
00159         }
00160 
00166         public function getComments() {
00167                 return $this->xmlEncode( $this->comments );
00168         }
00169 
00176         public static function stripComment( $text ) {
00177                 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
00178         }
00180 }
00181 
00186 class ChannelFeed extends FeedItem {
00195         function outHeader() {
00196                 # print "<feed>";
00197         }
00198 
00203         function outItem( $item ) {
00204                 # print "<item>...</item>";
00205         }
00206 
00210         function outFooter() {
00211                 # print "</feed>";
00212         }
00223         public function httpHeaders() {
00224                 global $wgOut, $wgVaryOnXFP;
00225 
00226                 # We take over from $wgOut, excepting its cache header info
00227                 $wgOut->disable();
00228                 $mimetype = $this->contentType();
00229                 header( "Content-type: $mimetype; charset=UTF-8" );
00230                 if ( $wgVaryOnXFP ) {
00231                         $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
00232                 }
00233                 $wgOut->sendCacheControl();
00234 
00235         }
00236 
00243         function contentType() {
00244                 global $wgRequest;
00245                 $ctype = $wgRequest->getVal('ctype','application/xml');
00246                 $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
00247                 return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
00248         }
00249 
00255         function outXmlHeader() {
00256                 global $wgStylePath, $wgStyleVersion;
00257 
00258                 $this->httpHeaders();
00259                 echo '<?xml version="1.0"?>' . "\n";
00260                 echo '<?xml-stylesheet type="text/css" href="' .
00261                         htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion", PROTO_CURRENT ) ) .
00262                         '"?' . ">\n";
00263         }
00264 }
00265 
00271 class RSSFeed extends ChannelFeed {
00272 
00279         function formatTime( $ts ) {
00280                 return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
00281         }
00282 
00286         function outHeader() {
00287                 global $wgVersion;
00288 
00289                 $this->outXmlHeader();
00290                 ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
00291         <channel>
00292                 <title><?php print $this->getTitle() ?></title>
00293                 <link><?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?></link>
00294                 <description><?php print $this->getDescription() ?></description>
00295                 <language><?php print $this->getLanguage() ?></language>
00296                 <generator>MediaWiki <?php print $wgVersion ?></generator>
00297                 <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
00298 <?php
00299         }
00300 
00305         function outItem( $item ) {
00306         ?>
00307                 <item>
00308                         <title><?php print $item->getTitle() ?></title>
00309                         <link><?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ) ?></link>
00310                         <guid<?php if( !$item->rssIsPermalink ) print ' isPermaLink="false"' ?>><?php print $item->getUniqueId() ?></guid>
00311                         <description><?php print $item->getDescription() ?></description>
00312                         <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
00313                         <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
00314                         <?php if( $item->getComments() ) { ?><comments><?php print wfExpandUrl( $item->getComments(), PROTO_CURRENT ) ?></comments><?php }?>
00315                 </item>
00316 <?php
00317         }
00318 
00322         function outFooter() {
00323         ?>
00324         </channel>
00325 </rss><?php
00326         }
00327 }
00328 
00334 class AtomFeed extends ChannelFeed {
00338         function formatTime( $ts ) {
00339                 // need to use RFC 822 time format at least for rss2.0
00340                 return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
00341         }
00342 
00346         function outHeader() {
00347                 global $wgVersion;
00348 
00349                 $this->outXmlHeader();
00350                 ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
00351                 <id><?php print $this->getFeedId() ?></id>
00352                 <title><?php print $this->getTitle() ?></title>
00353                 <link rel="self" type="application/atom+xml" href="<?php print wfExpandUrl( $this->getSelfUrl(), PROTO_CURRENT ) ?>"/>
00354                 <link rel="alternate" type="text/html" href="<?php print wfExpandUrl( $this->getUrl(), PROTO_CURRENT ) ?>"/>
00355                 <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
00356                 <subtitle><?php print $this->getDescription() ?></subtitle>
00357                 <generator>MediaWiki <?php print $wgVersion ?></generator>
00358 
00359 <?php
00360         }
00361 
00371         function getFeedId() {
00372                 return $this->getSelfUrl();
00373         }
00374 
00380         function getSelfUrl() {
00381                 global $wgRequest;
00382                 return htmlspecialchars( $wgRequest->getFullRequestURL() );
00383         }
00384 
00389         function outItem( $item ) {
00390                 global $wgMimeType;
00391         ?>
00392         <entry>
00393                 <id><?php print $item->getUniqueId() ?></id>
00394                 <title><?php print $item->getTitle() ?></title>
00395                 <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print wfExpandUrl( $item->getUrl(), PROTO_CURRENT ) ?>"/>
00396                 <?php if( $item->getDate() ) { ?>
00397                 <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
00398                 <?php } ?>
00399 
00400                 <summary type="html"><?php print $item->getDescription() ?></summary>
00401                 <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
00402         </entry>
00403 
00404 <?php /* @todo FIXME: Need to add comments
00405         <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
00406           */
00407         }
00408 
00412         function outFooter() {?>
00413         </feed><?php
00414         }
00415 }