public function get_feed(array $args) { // Read items from Content Repository $this->get_items($args); // Set up feed midgardmvc_core::get_instance()->component->load_library('Feed'); $feed = new ezcFeed(); $feed->title = $this->data['title']; $feed->description = ''; $now = new DateTime(); $feed->published = $now->format(DateTime::RSS); $link = $feed->add('link'); $link->href = midgardmvc_core::get_instance()->dispatcher->generate_url('index', array(), $this->request); array_walk($this->data['items'], function ($item) use($feed) { $feeditem = $feed->add('item'); $feeditem->title = $item->title; $feeditem->description = $item->content; $feeditem->published = $item->published->format(DateTime::RSS); $author = $feeditem->add('author'); $author->name = $item->firstname; $link = $feeditem->add('link'); $link->href = $item->url; }); $this->data['feed'] = $feed; }
<?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; }
/** * 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); }
/** * 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 ); }
/** * Parses the provided XML element object and stores it as a feed textinput in * the provided ezcFeed object. * * @param ezcFeed $feed The feed object in which to store the parsed XML element as a feed textinput * @param DOMElement $xml The XML element object to parse */ private function parseTextInput(ezcFeed $feed, DOMElement $xml = null) { $textInput = $feed->add('textInput'); if ($xml !== null) { foreach ($xml->childNodes as $itemChild) { if ($itemChild->nodeType == XML_ELEMENT_NODE) { $tagName = $itemChild->tagName; switch ($tagName) { case 'title': case 'description': case 'name': case 'link': $textInput->{$tagName} = $itemChild->textContent; break; } } } if ($xml->hasAttributeNS(self::NAMESPACE_URI, 'about')) { $textInput->about = $xml->getAttributeNS(self::NAMESPACE_URI, 'about'); } } }
protected function createFeed($type, $data) { $feed = new ezcFeed($type); $supportedModules = ezcFeed::getSupportedModules(); if (is_array($data)) { foreach ($data as $property => $value) { if (is_array($value)) { foreach ($value as $val) { if (isset($supportedModules[$property])) { $element = $feed->addModule($property); } else { $element = $feed->add($property); } if (is_array($val)) { foreach ($val as $subKey => $subValue) { if ($subKey === '#') { $element->set($subValue); } else { if ($subKey === 'MULTI') { $values = array(); foreach ($subValue as $multi) { foreach ($multi as $subSubKey => $subSubValue) { if (isset($supportedModules[$subSubKey])) { $subElement = $element->addModule($subSubKey); } else { if ($property === 'skipDays') { $values[] = $subSubValue; $element->days = $values; } else { if ($property === 'skipHours') { $values[] = $subSubValue; $element->hours = $values; } else { $subElement = $element->add($subSubKey); } } } if ($property !== 'skipDays' && $property !== 'skipHours') { $subElement->set($subSubValue); } } } } else { if (is_array($subValue)) { if (count($subValue) === 0 || !isset($subValue[0])) { if (isset($supportedModules[$subKey])) { $subElement = $element->addModule($subKey); } else { $subElement = $element->add($subKey); } } foreach ($subValue as $subSubKey => $subSubValue) { if ($subSubKey === '#') { $subElement->set($subSubValue); } else { if (is_array($subSubValue)) { if (isset($supportedModules[$subKey])) { $subElement = $element->addModule($subKey); } else { $subElement = $element->add($subKey); } foreach ($subSubValue as $subSubSubKey => $subSubSubValue) { if ($subSubSubKey === '#') { $subElement->set($subSubSubValue); } else { if (is_array($subSubSubValue)) { foreach ($subSubSubValue as $subSubSubSubKey => $subSubSubSubValue) { $subSubElement = $subElement->add($subSubSubKey); foreach ($subSubSubSubValue as $subSubSubSubSubKey => $subSubSubSubSubValue) { if ($subSubSubSubSubKey === '#') { $subSubElement->set($subSubSubSubSubValue); } else { $subSubElement->{$subSubSubSubSubKey} = $subSubSubSubSubValue; } } } } else { $subElement->{$subSubSubKey} = $subSubSubValue; } } } } else { $subElement->{$subSubKey} = $subSubValue; } } } } else { $element->{$subKey} = $subValue; } } } } } else { $element->set($val); } } } else { $feed->{$property} = $value; } } } return $feed; }
/** * Parses the provided XML document object and returns an ezcFeed object * from it. * * @throws ezcFeedParseErrorException * If an error was encountered during parsing. * * @param DOMDocument $xml The XML document object to parse * @return ezcFeed */ public function parse(DOMDocument $xml) { $feed = new ezcFeed(self::FEED_TYPE); $rssChildren = $xml->documentElement->childNodes; $channel = null; $this->usedPrefixes = $this->fetchUsedPrefixes($xml); foreach ($rssChildren as $rssChild) { if ($rssChild->nodeType === XML_ELEMENT_NODE && $rssChild->tagName === 'channel') { $channel = $rssChild; } } if ($channel === null) { throw new ezcFeedParseErrorException(null, "No channel tag"); } foreach ($channel->childNodes as $channelChild) { if ($channelChild->nodeType == XML_ELEMENT_NODE) { $tagName = $channelChild->tagName; switch ($tagName) { case 'title': case 'description': case 'copyright': $element = $feed->add($tagName); $element->text = $channelChild->textContent; break; case 'language': case 'ttl': case 'docs': case 'rating': $element = $feed->add($tagName); $element->text = $channelChild->textContent; break; case 'generator': $element = $feed->add($tagName); $element->name = $channelChild->textContent; break; case 'managingEditor': $element = $feed->add('author'); $element->name = $channelChild->textContent; // @todo parse $name to see if it has the structure // email@address (name) to fill the ->email field from it break; case 'webMaster': $element = $feed->add('webMaster'); $element->name = $channelChild->textContent; // @todo parse $name to see if it has the structure // email@address (name) to fill the ->email field from it break; case 'category': $element = $feed->add($tagName); $element->term = $channelChild->textContent; if ($channelChild->hasAttribute('domain')) { $element->scheme = $channelChild->getAttribute('domain'); } break; case 'link': $element = $feed->add($tagName); $element->href = $channelChild->textContent; break; case 'cloud': $element = $feed->add($tagName); $attributes = array('domain' => 'domain', 'port' => 'port', 'path' => 'path', 'registerProcedure' => 'registerProcedure', 'protocol' => 'protocol'); foreach ($attributes as $name => $alias) { if ($channelChild->hasAttribute($name)) { $element->{$alias} = $channelChild->getAttribute($name); } } break; case 'pubDate': $feed->published = $channelChild->textContent; break; case 'lastBuildDate': $feed->updated = $channelChild->textContent; break; case 'item': $element = $feed->add($tagName); $this->parseItem($element, $channelChild); break; case 'image': $image = $feed->add('image'); $this->parseImage($image, $channelChild); break; case 'skipHours': $element = $feed->add($tagName); $this->parseSkipHours($element, $channelChild); break; case 'skipDays': $element = $feed->add($tagName); $this->parseSkipDays($element, $channelChild); break; case 'textInput': $element = $feed->add($tagName); $this->parseTextInput($element, $channelChild); break; default: // check if it's part of a known module/namespace $this->parseModules($feed, $channelChild, $tagName); } } } return $feed; }
public function testCreateModuleITunes() { $feed = new ezcFeed('atom'); $item = $feed->add('item'); $module = $item->addModule('iTunes'); $this->assertEquals('iTunes', $module->getModuleName()); $this->assertEquals('http://www.itunes.com/dtds/podcast-1.0.dtd', $module->getNamespace()); $this->assertEquals('itunes', $module->getNamespacePrefix()); }
/** * 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); }
/** * Parses the XML element $node and creates modules in the feed or * feed item $item. * * @param ezcFeedEntryElement|ezcFeed $item The feed or feed item which will contain the modules * @param DOMElement $node The XML element from which to get the module elements * @param string $tagName The XML tag name (if it contains ':' it will be considered part of a module) * @ignore */ protected function parseModules($item, DOMElement $node, $tagName) { $supportedModules = ezcFeed::getSupportedModules(); if (strpos($tagName, ':') !== false) { list($prefix, $key) = explode(':', $tagName); $moduleName = isset($this->usedPrefixes[$prefix]) ? $this->usedPrefixes[$prefix] : null; if (isset($supportedModules[$moduleName])) { $module = $item->hasModule($moduleName) ? $item->{$moduleName} : $item->addModule($moduleName); $module->parse($key, $node); } else { if ($moduleName === 'Atom') { $element = $item->add('id'); if ($node->hasAttribute('href')) { $item->id = $node->getAttribute('href'); } } } } }
<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'));