Created
January 16, 2010 18:38
-
-
Save iloveitaly/278943 to your computer and use it in GitHub Desktop.
Kohana 2 feed class with better namespace support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
class Feed2 extends feed { | |
/** | |
* Creates a feed from the given parameters. | |
* | |
* @param array feed information | |
* @param array items to add to the feed | |
* @param string define which format to use | |
* @param string define which encoding to use | |
* @return string | |
*/ | |
public static function create($info, $items, $format = 'rss2', $encoding = 'UTF-8') | |
{ | |
$info += array('title' => 'Generated Feed', 'link' => '', 'generator' => 'KohanaPHP'); | |
// handle namespaces | |
$namespaceString = ''; | |
if(isset($info['namespaces'])) { | |
foreach($info['namespaces'] as $prefix => $ns) { | |
// 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 | |
// $feed->addAttribute('xmlns:'.$prefix, $ns, 'http://www.w3.org/2000/xmlns/'); | |
$namespaceString .= " xmlns:".$prefix.'="'.$ns.'"'; | |
} | |
$namespaces = $info['namespaces']; | |
// this is so it is not included in the rest of the $info generation | |
unset($info['namespaces']); | |
} | |
$feed = '<?xml version="1.0" encoding="'.$encoding.'"?><rss version="2.0"'.$namespaceString.'><channel></channel></rss>'; | |
$feed = simplexml_load_string($feed); | |
foreach ($info as $name => $value) | |
{ | |
if (($name === 'pubDate' OR $name === 'lastBuildDate') AND (is_int($value) OR ctype_digit($value))) | |
{ | |
// Convert timestamps to RFC 822 formatted dates | |
$value = date(DATE_RFC822, $value); | |
} | |
elseif (($name === 'link' OR $name === 'docs') AND strpos($value, '://') === FALSE) | |
{ | |
// Convert URIs to URLs | |
$value = url::site($value, 'http'); | |
} | |
// Add the info to the channel | |
$feed->channel->addChild($name, $value); | |
} | |
foreach ($items as $item) | |
{ | |
// Add the item to the channel | |
$row = $feed->channel->addChild('item'); | |
foreach ($item as $name => $value) | |
{ | |
if(!is_array($value)) { | |
if ($name === 'pubDate' AND (is_int($value) OR ctype_digit($value))) { | |
// Convert timestamps to RFC 822 formatted dates | |
$value = date(DATE_RFC822, $value); | |
} elseif (($name === 'link' OR $name === 'guid') AND strpos($value, '://') === FALSE) { | |
// Convert URIs to URLs | |
$value = url::site($value, 'http'); | |
} | |
if(strstr($name, ":") !== FALSE) { | |
list($namespace, $tagName) = explode(":", $name); | |
$row->addChild($name, $value, $namespace); | |
} else { | |
$row->addChild($name, $value); | |
} | |
} else { | |
$child = $row->addChild($name); | |
// If the value is an array we are specifying attributes | |
foreach($value as $attribute => $attributeValue) { | |
if(strchr($attribute, ':') !== FALSE) { | |
list($ns, $attribute2) = explode(':', $attribute); | |
} else { | |
$ns = ""; | |
} | |
//!empty($ns) ? $namespaces[$ns] : NULL | |
$child->addAttribute($attribute, $attributeValue, !empty($ns) ? $namespaces[$ns] : NULL); | |
} | |
} | |
} | |
} | |
return $feed->asXML(); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// snippet from a larger piece of code for generating a sparkle feed | |
foreach($updatesList as $clientUpdate) { | |
$clientUpdatePath = join_paths(DOCROOT, '/install/', $clientUpdate->file); | |
if(!file_exists($clientUpdatePath)) continue; | |
$feedItems[] = array( | |
'title' => 'App v'.$clientUpdate->version, | |
'pubDate' => $clientUpdate->date, | |
'sparkle:minimumSystemVersion' => $clientUpdate->version > 572 ? '10.7.0' : '10.6.0', | |
'enclosure' => array( | |
'url' => url::base(TRUE).'install/'.$clientUpdate->file, | |
'type' => 'application/octet-stream', | |
'length' => filesize($clientUpdatePath), | |
'sparkle:dsaSignature' => $clientUpdate->signature, | |
'sparkle:version' => $clientUpdate->version, | |
'sparkle:shortVersionString' => '1.0', | |
) | |
); | |
} | |
$feedData = feed2::create( | |
array( | |
'title' => 'App Update Feed', | |
'author' => 'Michael Bianco', | |
'pubDate' => date('r'), | |
'link' => 'http://mabblog.com/', | |
'language' => 'en', | |
'namespaces' => array( | |
'sparkle' => 'http://www.andymatuschak.org/xml-namespaces/sparkle', | |
'dc' => 'http://purl.org/dc/elements/1.1/' | |
) | |
), | |
$feedItems | |
); | |
echo $feedData; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment