rss.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2002-2004 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This script defines "?action=rss". It will read the current page
  8. for a WikiTrail, and then output an RSS 2.0 document with the current
  9. page as the channel and the pages in the WikiTrail as the items.
  10. To add RSS capabilities to your site, simply add to config.php:
  11. include_once("scripts/rss.php");
  12. */
  13. SDV($HandleActions['rss'],'HandleRss');
  14. SDV($HandleActions['rdf'],'HandleRss');
  15. SDV($RssMaxItems,20); # maximum items to display
  16. SDV($RssSourceSize,256); # max size to build desc from
  17. SDV($RssDescSize,80); # max desc size
  18. SDV($RssItems,array()); # RSS item elements
  19. SDV($RssItemsRDFList,array()); # RDF <items> elements
  20. if ($action=='rdf') {
  21. ### RSS 1.0 (RDF) definitions
  22. SDV($RssTimeFmt,'%Y-%m-%dT%H:%M+00:00'); # time format
  23. SDV($RssItemsRDFListFmt,"<rdf:li rdf:resource=\"\$PageUrl\" />\n");
  24. SDV($RssChannelFmt,array('<?xml version="1.0"?'.'>
  25. <!DOCTYPE rdf:RDF [
  26. <!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN"
  27. "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent"> %HTMLlat1;
  28. <!ENTITY % HTMLspecial PUBLIC "-//W3C//ENTITIES Special for XHTML//EN"
  29. "http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent"> %HTMLspecial;
  30. ]>
  31. <rdf:RDF xmlns="http://purl.org/rss/1.0/"
  32. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  33. xmlns:dc="http://purl.org/dc/elements/1.1/">
  34. <channel rdf:about="$PageUrl">
  35. <title>$WikiTitle - $Group.$Title</title>
  36. <link>$PageUrl</link>
  37. <description>$RssChannelDesc</description>
  38. <dc:date>$RssChannelBuildDate</dc:date>
  39. <items>
  40. <rdf:Seq>',&$RssItemsRDFList,'
  41. </rdf:Seq>
  42. </items>
  43. </channel>'));
  44. SDV($RssItemFmt,'
  45. <item rdf:about="$PageUrl">
  46. <title>$WikiTitle - $Group.$Title</title>
  47. <link>$PageUrl</link>
  48. <description>$RssItemDesc</description>
  49. <dc:date>$RssItemPubDate</dc:date>
  50. </item>');
  51. SDV($HandleRssFmt,array(&$RssChannelFmt,&$RssItems,'</rdf:RDF>'));
  52. }
  53. ### RSS 2.0 definitions
  54. SDV($RssTimeFmt,'%a, %d %b %Y %H:%M:%S GMT');
  55. SDV($RssChannelFmt,'<?xml version="1.0"?'.'>
  56. <!DOCTYPE rdf:RDF [
  57. <!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN"
  58. "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent"> %HTMLlat1;
  59. <!ENTITY % HTMLspecial PUBLIC "-//W3C//ENTITIES Special for XHTML//EN"
  60. "http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent"> %HTMLspecial;
  61. ]>
  62. <rss version="2.0">
  63. <channel>
  64. <title>$WikiTitle - $Group.$Title</title>
  65. <link>$PageUrl</link>
  66. <description>$RssChannelDesc</description>
  67. <lastBuildDate>$RssChannelBuildDate</lastBuildDate>
  68. <generator>$Version</generator>');
  69. SDV($RssItemFmt,'
  70. <item>
  71. <title>$PageName</title>
  72. <link>$PageUrl</link>
  73. <description>$RssItemDesc</description>
  74. <pubDate>$RssItemPubDate</pubDate>
  75. </item>');
  76. SDV($HandleRssFmt,array(&$RssChannelFmt,&$RssItems,'</channel></rss>'));
  77. function rssencode($s)
  78. { return preg_replace('/([\\x80-\\xff])/e',"'&#'.ord('\$1').';'",$s); }
  79. function HandleRss($pagename) {
  80. global $RssMaxItems,$RssSourceSize,$RssDescSize,
  81. $RssChannelFmt,$RssChannelDesc,$RssTimeFmt,$RssChannelBuildDate,
  82. $RssItemsRDFList,$RssItemsRDFListFmt,$RssItems,$RssItemFmt,$RssItemDesc,
  83. $RssItemPubDate,$GCount,$HandleRssFmt;
  84. $t = ReadTrail($pagename);
  85. $page = RetrieveAuthPage($pagename,false);
  86. $cbgmt = $page['time'];
  87. $r = array();
  88. for($i=0;$i<count($t) && count($r)<$RssMaxItems;$i++) {
  89. if (!PageExists($t[$i]['name'])) continue;
  90. $page = RetrieveAuthPage($t[$i]['name'],false);
  91. ob_start();
  92. PrintText($t[$i]['name'],substr($page['text'],0,$RssSourceSize));
  93. $text = ob_get_contents(); ob_end_clean();
  94. $text = rssencode(preg_replace("/<.*?>/s","",$text));
  95. preg_match("/^(.{0,$RssDescSize}\\s)/s",$text,$match);
  96. $r[] = array('name' => $t[$i]['name'],'time' => $page['time'],
  97. 'desc' => $match[1]." ...");
  98. if ($page['time']>$cbgmt) $cbgmt=$page['time'];
  99. }
  100. SDV($RssChannelBuildDate,rssencode(gmstrftime($RssTimeFmt,$cbgmt)));
  101. SDV($RssChannelDesc,rssencode(FmtPageName('$Group.$Title',$pagename)));
  102. foreach($r as $page) {
  103. $RssItemPubDate = gmstrftime($RssTimeFmt,$page['time']);
  104. $RssItemDesc = $page['desc']; $GCount = 0;
  105. $RssItemsRDFList[] =
  106. rssencode(FmtPageName($RssItemsRDFListFmt,$page['name']));
  107. $RssItems[] =
  108. rssencode(FmtPageName($RssItemFmt,$page['name']));
  109. }
  110. header("Content-type: text/xml");
  111. PrintFmt($pagename,$HandleRssFmt);
  112. exit();
  113. }