<?php date_default_timezone_set('UTC'); require '/home/derick/dev/zetacomponents/trunk/Base/src/ezc_bootstrap.php'; /* Find files */ $d = glob("news/*txt"); sort($d); $d = array_reverse($d); $latest = preg_replace('@^news/(.*).txt$@', '\\1', $d[0]); $feed = new ezcFeed(); $feed->title = 'Xdebug.org announcements'; $feed->description = 'This is a feed showing the latest announcements from xdebug.org.'; $feed->published = new DateTime("{$latest} 09:00"); $author = $feed->add('author'); $author->name = 'Derick Rethans'; $author->email = '*****@*****.**'; $link = $feed->add('link'); $link->href = 'http://xdebug.org'; foreach ($d as $item) { $date = preg_replace('@^news/(.*).txt$@', '\\1', $item); $file = file($item); $title = array_shift($file); $item = $feed->add('item'); $item->title = trim($title); $item->description = join('', $file); $item->published = new DateTime("{$date} 09:00"); } $xml = $feed->generate('rss2'); header('Content-Type: ' . $feed->getContentType() . '; charset=utf-8'); echo $xml;
function node2rss($nodeList, $meta) { $feed = new ezcFeed(); $feed->title = $meta['title']; $feed->description = $meta['description']; $feed->language = $meta['language']; $link = $feed->add('link'); $link->href = sprintf('http://%s/', $meta['siteURL']); // to add the <atom:link> element needed for RSS2 $feed->id = sprintf('http://%s%s/', $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI']); // required for ATOM $feed->updated = time(); $author = $feed->add('author'); $author->email = $meta['authorMail']; $author->name = $meta['authorName']; $descriptionFields = $this->feedIni->variable('FeedSettings', 'description'); $contentFields = $this->feedIni->variable('FeedSettings', 'content'); $catFields = $this->feedIni->variable('FeedSettings', 'category'); // convert objects to nodes, filter class types, and limit number foreach ($nodeList as $node) { $item = $feed->add('item'); $item->title = $node->attribute('name'); $item->published = date('r', $node->ContentObject->Published); $link = $item->add('link'); $link->href = sprintf('http://%s/%s', $meta['siteURL'], $node->attribute('url_alias')); $item->id = sprintf('http://%s/content/view/full/%d', $meta['siteURL'], $node->attribute('node_id')); $map = $node->attribute('data_map'); $description = $this->preferedField($descriptionFields, $map); $item->description = $description; $content = $this->preferedField($contentFields, $map); if (!empty($content)) { $module = $item->addModule('Content'); $module->encoded = $content; } $tagString = $this->preferedField($catFields, $map); $tags = explode('|#', $tagString); foreach ($tags as $tag) { $cat = $item->add('category'); $cat->term = $tag; } } $rss = $feed->generate('rss2'); // add host to local links $rss = preg_replace('#(src|href)=([\'"])/#i', sprintf("\$1=\$2http:/%s/", $meta['siteURL']), $rss); return $rss; }
/** * Generates an RSS feed document with type $type and returns it as a string. * * It uses the Feed component from eZ Components. * * Supported types: 'rss1', 'rss2', 'atom'. * * @since 4.2 * @param string $type One of 'rss1', 'rss2' and 'atom' * @return string XML document as a string */ function generateFeed( $type ) { $locale = eZLocale::instance(); // Get URL Translation settings. $config = eZINI::instance(); if ( $config->variable( 'URLTranslator', 'Translation' ) == 'enabled' ) { $useURLAlias = true; } else { $useURLAlias = false; } if ( $this->attribute( 'url' ) == '' ) { $baseItemURL = ''; eZURI::transformURI( $baseItemURL, false, 'full' ); $baseItemURL .= '/'; } else { $baseItemURL = $this->attribute( 'url' ) . '/'; //.$this->attribute( 'site_access' ).'/'; } $feed = new ezcFeed(); $feed->title = $this->attribute( 'title' ); $link = $feed->add( 'link' ); $link->href = $baseItemURL; $feed->description = $this->attribute( 'description' ); $feed->language = $locale->httpLocaleCode(); // to add the <atom:link> element needed for RSS2 $feed->id = $baseItemURL . 'rss/feed/' . $this->attribute( 'access_url' ); // required for ATOM $feed->updated = time(); $author = $feed->add( 'author' ); $author->email = $config->variable( 'MailSettings', 'AdminEmail' ); $creatorObject = eZContentObject::fetch( $this->attribute( 'creator_id' ) ); if ( $creatorObject instanceof eZContentObject ) { $author->name = $creatorObject->attribute('name'); } $imageURL = $this->fetchImageURL(); if ( $imageURL !== false ) { $image = $feed->add( 'image' ); // Required for RSS1 $image->about = $imageURL; $image->url = $imageURL; $image->title = $this->attribute( 'title' ); $image->link = $baseItemURL; } $cond = array( 'rssexport_id' => $this->ID, 'status' => $this->Status ); $rssSources = eZRSSExportItem::fetchFilteredList( $cond ); $nodeArray = eZRSSExportItem::fetchNodeList( $rssSources, $this->getObjectListFilter() ); if ( is_array( $nodeArray ) && count( $nodeArray ) ) { $attributeMappings = eZRSSExportItem::getAttributeMappings( $rssSources ); foreach ( $nodeArray as $node ) { if ( $node->attribute('is_hidden') && !eZContentObjectTreeNode::showInvisibleNodes() ) { // if the node is hidden skip past it and don't add it to the RSS export continue; } $object = $node->attribute( 'object' ); $dataMap = $object->dataMap(); if ( $useURLAlias === true ) { $nodeURL = $this->urlEncodePath( $baseItemURL . $node->urlAlias() ); } else { $nodeURL = $baseItemURL . 'content/view/full/' . $node->attribute( 'node_id' ); } // keep track if there's any match $doesMatch = false; // start mapping the class attribute to the respective RSS field foreach ( $attributeMappings as $attributeMapping ) { // search for correct mapping by path if ( $attributeMapping[0]->attribute( 'class_id' ) == $object->attribute( 'contentclass_id' ) and in_array( $attributeMapping[0]->attribute( 'source_node_id' ), $node->attribute( 'path_array' ) ) ) { // found it $doesMatch = true; // now fetch the attributes $title = $dataMap[$attributeMapping[0]->attribute( 'title' )]; // description is optional $descAttributeIdentifier = $attributeMapping[0]->attribute( 'description' ); $description = $descAttributeIdentifier ? $dataMap[$descAttributeIdentifier] : false; // category is optional $catAttributeIdentifier = $attributeMapping[0]->attribute( 'category' ); $category = $catAttributeIdentifier ? $dataMap[$catAttributeIdentifier] : false; // enclosure is optional $enclosureAttributeIdentifier = $attributeMapping[0]->attribute( 'enclosure' ); $enclosure = $enclosureAttributeIdentifier ? $dataMap[$enclosureAttributeIdentifier] : false; break; } } if( !$doesMatch ) { // no match eZDebug::writeError( 'Cannot find matching RSS attributes for datamap on node: ' . $node->attribute( 'node_id' ), __METHOD__ ); return null; } // title RSS element with respective class attribute content $titleContent = $title->attribute( 'content' ); if ( $titleContent instanceof eZXMLText ) { $outputHandler = $titleContent->attribute( 'output' ); $itemTitleText = $outputHandler->attribute( 'output_text' ); } else { $itemTitleText = $titleContent; } $item = $feed->add( 'item' ); $item->title = $itemTitleText; $link = $item->add( 'link' ); $link->href = $nodeURL; switch ( $type ) { case 'rss2': $item->id = $object->attribute( 'remote_id' ); $item->id->isPermaLink = false; break; default: $item->id = $nodeURL; } $itemCreatorObject = $node->attribute('creator'); if ( $itemCreatorObject instanceof eZContentObject ) { $author = $item->add( 'author' ); $author->name = $itemCreatorObject->attribute('name'); $author->email = $config->variable( 'MailSettings', 'AdminEmail' ); } // description RSS element with respective class attribute content if ( $description ) { $descContent = $description->attribute( 'content' ); if ( $descContent instanceof eZXMLText ) { $outputHandler = $descContent->attribute( 'output' ); $itemDescriptionText = str_replace( ' ', '&nbsp;', $outputHandler->attribute( 'output_text' ) ); } else if ( $descContent instanceof eZImageAliasHandler ) { $itemImage = $descContent->hasAttribute( 'rssitem' ) ? $descContent->attribute( 'rssitem' ) : $descContent->attribute( 'rss' ); $origImage = $descContent->attribute( 'original' ); eZURI::transformURI( $itemImage['full_path'], true, 'full' ); eZURI::transformURI( $origImage['full_path'], true, 'full' ); $itemDescriptionText = '<a href="' . htmlspecialchars( $origImage['full_path'] ) . '"><img alt="' . htmlspecialchars( $descContent->attribute( 'alternative_text' ) ) . '" src="' . htmlspecialchars( $itemImage['full_path'] ) . '" width="' . $itemImage['width'] . '" height="' . $itemImage['height'] . '" /></a>'; } else { $itemDescriptionText = $descContent; } $item->description = $itemDescriptionText; } // category RSS element with respective class attribute content if ( $category ) { $categoryContent = $category->attribute( 'content' ); if ( $categoryContent instanceof eZXMLText ) { $outputHandler = $categoryContent->attribute( 'output' ); $itemCategoryText = $outputHandler->attribute( 'output_text' ); } elseif ( $categoryContent instanceof eZKeyword ) { $itemCategoryText = $categoryContent->keywordString(); } else { $itemCategoryText = $categoryContent; } if ( $itemCategoryText ) { $cat = $item->add( 'category' ); $cat->term = $itemCategoryText; } } // enclosure RSS element with respective class attribute content if ( $enclosure ) { $encItemURL = false; $enclosureContent = $enclosure->attribute( 'content' ); if ( $enclosureContent instanceof eZMedia ) { $enc = $item->add( 'enclosure' ); $enc->length = $enclosureContent->attribute('filesize'); $enc->type = $enclosureContent->attribute('mime_type'); $encItemURL = 'content/download/' . $enclosure->attribute('contentobject_id') . '/' . $enclosureContent->attribute( 'contentobject_attribute_id' ) . '/' . urlencode( $enclosureContent->attribute( 'original_filename' ) ); eZURI::transformURI( $encItemURL, false, 'full' ); } else if ( $enclosureContent instanceof eZBinaryFile ) { $enc = $item->add( 'enclosure' ); $enc->length = $enclosureContent->attribute('filesize'); $enc->type = $enclosureContent->attribute('mime_type'); $encItemURL = 'content/download/' . $enclosure->attribute('contentobject_id') . '/' . $enclosureContent->attribute( 'contentobject_attribute_id' ) . '/version/' . $enclosureContent->attribute( 'version' ) . '/file/' . urlencode( $enclosureContent->attribute( 'original_filename' ) ); eZURI::transformURI( $encItemURL, false, 'full' ); } else if ( $enclosureContent instanceof eZImageAliasHandler ) { $enc = $item->add( 'enclosure' ); $origImage = $enclosureContent->attribute( 'original' ); $enc->length = $origImage['filesize']; $enc->type = $origImage['mime_type']; $encItemURL = $origImage['full_path']; eZURI::transformURI( $encItemURL, true, 'full' ); } if ( $encItemURL ) { $enc->url = $encItemURL; } } $item->published = $object->attribute( 'published' ); $item->updated = $object->attribute( 'published' ); } } return $feed->generate( $type ); }
/** * Uses the array $data to create a feed of type $feedType ('rss1', 'rss2' or * 'atom') and returns it as a string. * * The format of the $data array is: * <code> * array( 'title' => 'Feed title', * 'link' => 'Feed link', * 'published' => 'Feed published date', * 'authorName' => 'Feed author name', * 'authorEmail' => 'Feed author email', * 'description' => 'Feed description', * 'items' => array( * 0 => array( 'title' => 'Item 0 title', * 'link' => 'Item 0 link', * 'published' => 'Item 0 published date', * 'authorName' => 'Item 0 author name', * 'authorEmail' => 'Item 0 author email', * 'description' => 'Item 0 description', * ), * 1 => array( 'title' => 'Item 1 title', * 'link' => 'Item 1 link', * 'published' => 'Item 1 published date', * 'authorName' => 'Item 1 author name', * 'authorEmail' => 'Item 1 author email', * 'description' => 'Item 1 description', * ), * ) * ); * </code> * * @param string $feedType The type of the feed to create ('rss1', 'rss2' or 'atom') * @param array(mixed) $data Data for the elements of the feed * @return string */ function createFeed($feedType, $data) { $feed = new ezcFeed(); $feed->title = $data['title']; $feed->description = $data['description']; $feed->id = $data['link']; $link = $feed->add('link'); $link->href = $data['link']; $feed->updated = time(); $feed->published = $data['published']; $author = $feed->add('author'); $author->name = $data['authorName']; $author->email = $data['authorEmail']; foreach ($data['item'] as $dataItem) { $item = $feed->add('item'); $item->title = $dataItem['title']; $item->description = $dataItem['description']; $item->id = $dataItem['link']; $item->id->isPermaLink = true; // RSS2 only $link = $item->add('link'); $link->href = $dataItem['link']; $link->rel = 'alternate'; $item->updated = time(); $item->published = $dataItem['published']; $author = $item->add('author'); $author->name = $dataItem['authorName']; $author->email = $dataItem['authorEmail']; } return $feed->generate($feedType); }
public function testGenerateRss2() { $feed = new ezcFeed(); $feed->title = 'xxx'; $feed->link = 'xxx'; $feed->description = 'xxx'; $expected = <<<EOL <?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>xxx</title> <link>xxx</link> <description>xxx</description> <pubDate>XXX</pubDate> <generator>XXX</generator> <docs>http://www.rssboard.org/rss-specification</docs> </channel> </rss> EOL; $generated = $feed->generate('rss2'); $generated = preg_replace('@<pubDate>.*?</pubDate>@', '<pubDate>XXX</pubDate>', $generated); $generated = preg_replace('@<lastBuildDate>.*?</lastBuildDate>@', '<lastBuildDate>XXX</lastBuildDate>', $generated); $generated = preg_replace('@<generator.*?>.*?</generator>@', '<generator>XXX</generator>', $generated); $generated = str_replace("\n", PHP_EOL, $generated); $this->assertEquals(trim($expected), trim($generated)); }
/** * Test for issue #13963: ezcFeedEnclosureElement obsolete? */ public function testAssignEnclosure() { $feed = new ezcFeed('rss2'); $feed->title = 'Feed title'; $feed->description = 'Feed description'; $link = $feed->add('link'); $link->href = 'http://example.com/'; $item = $feed->add('item'); $item->title = 'Item title'; $item->description = 'Item description'; $link = $item->add('link'); $link->href = 'http://example.com/item/'; // assign the enclosure directly. Before the fix it would fail with error // as it tried to assign the property 'link' instead of 'url' $item->enclosure = 'http://example.com/enclosure.mp3'; $xml = $feed->generate('rss2'); // assert that the enclosure element is inside the generated XML feed $this->assertEquals(true, strpos($xml, '<enclosure url="http://example.com/enclosure.mp3"/>') !== false); }
<pre style="font-size: 11px;"> <?php require 'ezc-setup.php'; // setup feed and content module $feed = new ezcFeed(); $feed->title = 'eZ components release feed'; $feed->link = 'http://components.ez.no/'; $feed->description = <<<ENDL This feed shows all the latest components releases. ENDL; $feed->copyright = "eZ systems A.S."; $feed->language = 'en-us'; // load data $stmt = (require 'feed-data.php'); foreach ($stmt as $release) { $item = $feed->add('item'); $item->title = "{$release['package']} {$release['version']}"; $item->link = "http://ez.no/doc/components/view/latest/(file)/changelog_{$release['package']}.html"; $item->description = $release['releasenotes']; $item->published = $release['releasedate']; $item->guid = md5($item->title); } echo htmlspecialchars($feed->generate('rss2'));