Posted By

iloveitaly on 01/16/10


Tagged

rss feed xml attribute namespace kohana


Versions (?)

Kohana Feed With Attributes & Namespace Support


 / Published in: PHP
 

  1. <?
  2. class Feed2 extends feed {
  3. /**
  4. * Creates a feed from the given parameters.
  5. *
  6. * @param array feed information
  7. * @param array items to add to the feed
  8. * @param string define which format to use
  9. * @param string define which encoding to use
  10. * @return string
  11. */
  12. public static function create($info, $items, $format = 'rss2', $encoding = 'UTF-8')
  13. {
  14. $info += array('title' => 'Generated Feed', 'link' => '', 'generator' => 'KohanaPHP');
  15.  
  16. // handle namespaces
  17. $namespaceString = '';
  18.  
  19. if(isset($info['namespaces'])) {
  20. foreach($info['namespaces'] as $prefix => $ns) {
  21. // can't add namespaces using the api bc it adds in xmlns:xmlns="..." which is explicitly declared as invalid xml: http://www.w3.org/TR/REC-xml-names/#xmlReserved
  22. // $feed->addAttribute('xmlns:'.$prefix, $ns, 'http://www.w3.org/2000/xmlns/');
  23.  
  24. $namespaceString .= " xmlns:".$prefix.'="'.$ns.'"';
  25. }
  26.  
  27. $namespaces = $info['namespaces'];
  28.  
  29. // this is so it is not included in the rest of the $info generation
  30. unset($info['namespaces']);
  31. }
  32.  
  33. $feed = '<?xml version="1.0" encoding="'.$encoding.'"?><rss version="2.0"'.$namespaceString.'><channel></channel></rss>';
  34. $feed = simplexml_load_string($feed);
  35.  
  36. foreach ($info as $name => $value)
  37. {
  38. if (($name === 'pubDate' OR $name === 'lastBuildDate') AND (is_int($value) OR ctype_digit($value)))
  39. {
  40. // Convert timestamps to RFC 822 formatted dates
  41. $value = date(DATE_RFC822, $value);
  42. }
  43. elseif (($name === 'link' OR $name === 'docs') AND strpos($value, '://') === FALSE)
  44. {
  45. // Convert URIs to URLs
  46. $value = url::site($value, 'http');
  47. }
  48.  
  49. // Add the info to the channel
  50. $feed->channel->addChild($name, $value);
  51. }
  52.  
  53. foreach ($items as $item)
  54. {
  55. // Add the item to the channel
  56. $row = $feed->channel->addChild('item');
  57.  
  58. foreach ($item as $name => $value)
  59. {
  60. if(!is_array($value)) {
  61. if ($name === 'pubDate' AND (is_int($value) OR ctype_digit($value)))
  62. {
  63. // Convert timestamps to RFC 822 formatted dates
  64. $value = date(DATE_RFC822, $value);
  65. }
  66. elseif (($name === 'link' OR $name === 'guid') AND strpos($value, '://') === FALSE)
  67. {
  68. // Convert URIs to URLs
  69. $value = url::site($value, 'http');
  70. }
  71.  
  72. // Add the info to the row
  73. $row->addChild($name, $value);
  74. } else {
  75. $child = $row->addChild($name);
  76.  
  77. // If the value is an array we are specifying attributes
  78. foreach($value as $attribute => $attributeValue) {
  79. if(strchr($attribute, ':') !== FALSE) {
  80. list($ns, $attribute2) = explode(':', $attribute);
  81. } else {
  82. $ns = "";
  83. }
  84.  
  85. //!empty($ns) ? $namespaces[$ns] : NULL
  86. $child->addAttribute($attribute, $attributeValue, !empty($ns) ? $namespaces[$ns] : NULL);
  87. }
  88. }
  89. }
  90. }
  91.  
  92. return $feed->asXML();
  93. }
  94. }
  95.  
  96. // Example Feed Generation (For Sparkle Update System):
  97.  
  98. $feedItems = array();
  99.  
  100. foreach(ORM::factory('updates')->find_all() as $clientUpdate) {
  101. $feedItems[] = array(
  102. 'title' => 'v'.$clientUpdate->version,
  103. 'pubDate' => $clientUpdate->date,
  104. 'enclosure' => array(
  105. 'url' => 'http://domain.com/',
  106. 'type' => 'application/octet-stream',
  107. 'length' => filesize('/path/to/file.zip'),
  108. 'sparkle:dsaSignature' => 'dsasig',
  109. 'sparkle:version' => '200',
  110. 'sparkle:shortVersionString' => '1.0',
  111. 'sparkle:minimumSystemVersion' => '10.6'
  112. )
  113. );
  114. }
  115.  
  116. $feedData = feed2::create(
  117. array(
  118. 'title' => 'Update Feed',
  119. 'author' => 'Michael Bianco',
  120. 'pubDate' => date('r'),
  121. 'link' => 'http://domain.com/',
  122. 'language' => 'en',
  123. 'namespaces' => array(
  124. 'sparkle' => 'http://www.andymatuschak.org/xml-namespaces/sparkle',
  125. 'dc' => 'http://purl.org/dc/elements/1.1/'
  126. )
  127. ),
  128. $feedItems
  129. );
  130.  
  131. ?>

Report this snippet  

You need to login to post a comment.