Feed.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
  3. # http://www.mediawiki.org/
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # http://www.gnu.org/copyleft/gpl.html
  19. /**
  20. * Basic support for outputting syndication feeds in RSS, other formats.
  21. * Contain a feed class as well as classes to build rss / atom ... feeds
  22. * Available feeds are defined in Defines.php
  23. */
  24. /**
  25. * A base class for basic support for outputting syndication feeds in RSS and other formats.
  26. */
  27. class FeedItem {
  28. /**#@+
  29. * @var string
  30. * @private
  31. */
  32. var $Title = 'Wiki';
  33. var $Description = '';
  34. var $Url = '';
  35. var $Date = '';
  36. var $Author = '';
  37. /**#@-*/
  38. /**#@+
  39. * @todo document
  40. * @param $Url URL uniquely designating the item.
  41. */
  42. function __construct( $Title, $Description, $Url, $Date = '', $Author = '', $Comments = '' ) {
  43. $this->Title = $Title;
  44. $this->Description = $Description;
  45. $this->Url = $Url;
  46. $this->Date = $Date;
  47. $this->Author = $Author;
  48. $this->Comments = $Comments;
  49. }
  50. public function xmlEncode( $string ) {
  51. $string = str_replace( "\r\n", "\n", $string );
  52. $string = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string );
  53. return htmlspecialchars( $string );
  54. }
  55. public function getTitle() {
  56. return $this->xmlEncode( $this->Title );
  57. }
  58. public function getUrl() {
  59. return $this->xmlEncode( $this->Url );
  60. }
  61. public function getDescription() {
  62. return $this->xmlEncode( $this->Description );
  63. }
  64. public function getLanguage() {
  65. global $wgContLanguageCode;
  66. return $wgContLanguageCode;
  67. }
  68. public function getDate() {
  69. return $this->Date;
  70. }
  71. public function getAuthor() {
  72. return $this->xmlEncode( $this->Author );
  73. }
  74. public function getComments() {
  75. return $this->xmlEncode( $this->Comments );
  76. }
  77. /**
  78. * Quickie hack... strip out wikilinks to more legible form from the comment.
  79. */
  80. public static function stripComment( $text ) {
  81. return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
  82. }
  83. /**#@-*/
  84. }
  85. /**
  86. * @todo document (needs one-sentence top-level class description).
  87. */
  88. class ChannelFeed extends FeedItem {
  89. /**#@+
  90. * Abstract function, override!
  91. * @abstract
  92. */
  93. /**
  94. * Generate Header of the feed
  95. */
  96. function outHeader() {
  97. # print "<feed>";
  98. }
  99. /**
  100. * Generate an item
  101. * @param $item
  102. */
  103. function outItem( $item ) {
  104. # print "<item>...</item>";
  105. }
  106. /**
  107. * Generate Footer of the feed
  108. */
  109. function outFooter() {
  110. # print "</feed>";
  111. }
  112. /**#@-*/
  113. /**
  114. * Setup and send HTTP headers. Don't send any content;
  115. * content might end up being cached and re-sent with
  116. * these same headers later.
  117. *
  118. * This should be called from the outHeader() method,
  119. * but can also be called separately.
  120. *
  121. * @public
  122. */
  123. function httpHeaders() {
  124. global $wgOut;
  125. # We take over from $wgOut, excepting its cache header info
  126. $wgOut->disable();
  127. $mimetype = $this->contentType();
  128. header( "Content-type: $mimetype; charset=UTF-8" );
  129. $wgOut->sendCacheControl();
  130. }
  131. /**
  132. * Return an internet media type to be sent in the headers.
  133. *
  134. * @return string
  135. * @private
  136. */
  137. function contentType() {
  138. global $wgRequest;
  139. $ctype = $wgRequest->getVal('ctype','application/xml');
  140. $allowedctypes = array('application/xml','text/xml','application/rss+xml','application/atom+xml');
  141. return (in_array($ctype, $allowedctypes) ? $ctype : 'application/xml');
  142. }
  143. /**
  144. * Output the initial XML headers with a stylesheet for legibility
  145. * if someone finds it in a browser.
  146. * @private
  147. */
  148. function outXmlHeader() {
  149. global $wgStylePath, $wgStyleVersion;
  150. $this->httpHeaders();
  151. echo '<?xml version="1.0"?>' . "\n";
  152. echo '<?xml-stylesheet type="text/css" href="' .
  153. htmlspecialchars( wfExpandUrl( "$wgStylePath/common/feed.css?$wgStyleVersion" ) ) .
  154. '"?' . ">\n";
  155. }
  156. }
  157. /**
  158. * Generate a RSS feed
  159. */
  160. class RSSFeed extends ChannelFeed {
  161. /**
  162. * Format a date given a timestamp
  163. * @param integer $ts Timestamp
  164. * @return string Date string
  165. */
  166. function formatTime( $ts ) {
  167. return gmdate( 'D, d M Y H:i:s \G\M\T', wfTimestamp( TS_UNIX, $ts ) );
  168. }
  169. /**
  170. * Ouput an RSS 2.0 header
  171. */
  172. function outHeader() {
  173. global $wgVersion;
  174. $this->outXmlHeader();
  175. ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  176. <channel>
  177. <title><?php print $this->getTitle() ?></title>
  178. <link><?php print $this->getUrl() ?></link>
  179. <description><?php print $this->getDescription() ?></description>
  180. <language><?php print $this->getLanguage() ?></language>
  181. <generator>MediaWiki <?php print $wgVersion ?></generator>
  182. <lastBuildDate><?php print $this->formatTime( wfTimestampNow() ) ?></lastBuildDate>
  183. <?php
  184. }
  185. /**
  186. * Output an RSS 2.0 item
  187. * @param FeedItem item to be output
  188. */
  189. function outItem( $item ) {
  190. ?>
  191. <item>
  192. <title><?php print $item->getTitle() ?></title>
  193. <link><?php print $item->getUrl() ?></link>
  194. <description><?php print $item->getDescription() ?></description>
  195. <?php if( $item->getDate() ) { ?><pubDate><?php print $this->formatTime( $item->getDate() ) ?></pubDate><?php } ?>
  196. <?php if( $item->getAuthor() ) { ?><dc:creator><?php print $item->getAuthor() ?></dc:creator><?php }?>
  197. <?php if( $item->getComments() ) { ?><comments><?php print $item->getComments() ?></comments><?php }?>
  198. </item>
  199. <?php
  200. }
  201. /**
  202. * Ouput an RSS 2.0 footer
  203. */
  204. function outFooter() {
  205. ?>
  206. </channel>
  207. </rss><?php
  208. }
  209. }
  210. /**
  211. * Generate an Atom feed
  212. */
  213. class AtomFeed extends ChannelFeed {
  214. /**
  215. * @todo document
  216. */
  217. function formatTime( $ts ) {
  218. // need to use RFC 822 time format at least for rss2.0
  219. return gmdate( 'Y-m-d\TH:i:s', wfTimestamp( TS_UNIX, $ts ) );
  220. }
  221. /**
  222. * Outputs a basic header for Atom 1.0 feeds.
  223. */
  224. function outHeader() {
  225. global $wgVersion;
  226. $this->outXmlHeader();
  227. ?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="<?php print $this->getLanguage() ?>">
  228. <id><?php print $this->getFeedId() ?></id>
  229. <title><?php print $this->getTitle() ?></title>
  230. <link rel="self" type="application/atom+xml" href="<?php print $this->getSelfUrl() ?>"/>
  231. <link rel="alternate" type="text/html" href="<?php print $this->getUrl() ?>"/>
  232. <updated><?php print $this->formatTime( wfTimestampNow() ) ?>Z</updated>
  233. <subtitle><?php print $this->getDescription() ?></subtitle>
  234. <generator>MediaWiki <?php print $wgVersion ?></generator>
  235. <?php
  236. }
  237. /**
  238. * Atom 1.0 requires a unique, opaque IRI as a unique indentifier
  239. * for every feed we create. For now just use the URL, but who
  240. * can tell if that's right? If we put options on the feed, do we
  241. * have to change the id? Maybe? Maybe not.
  242. *
  243. * @return string
  244. * @private
  245. */
  246. function getFeedId() {
  247. return $this->getSelfUrl();
  248. }
  249. /**
  250. * Atom 1.0 requests a self-reference to the feed.
  251. * @return string
  252. * @private
  253. */
  254. function getSelfUrl() {
  255. global $wgRequest;
  256. return htmlspecialchars( $wgRequest->getFullRequestURL() );
  257. }
  258. /**
  259. * Output a given item.
  260. * @param $item
  261. */
  262. function outItem( $item ) {
  263. global $wgMimeType;
  264. ?>
  265. <entry>
  266. <id><?php print $item->getUrl() ?></id>
  267. <title><?php print $item->getTitle() ?></title>
  268. <link rel="alternate" type="<?php print $wgMimeType ?>" href="<?php print $item->getUrl() ?>"/>
  269. <?php if( $item->getDate() ) { ?>
  270. <updated><?php print $this->formatTime( $item->getDate() ) ?>Z</updated>
  271. <?php } ?>
  272. <summary type="html"><?php print $item->getDescription() ?></summary>
  273. <?php if( $item->getAuthor() ) { ?><author><name><?php print $item->getAuthor() ?></name></author><?php }?>
  274. </entry>
  275. <?php /* FIXME need to add comments
  276. <?php if( $item->getComments() ) { ?><dc:comment><?php print $item->getComments() ?></dc:comment><?php }?>
  277. */
  278. }
  279. /**
  280. * Outputs the footer for Atom 1.0 feed (basicly '\</feed\>').
  281. */
  282. function outFooter() {?>
  283. </feed><?php
  284. }
  285. }