Apache Zeta Components Manual :: File Source for rss1.php
Source for file rss1.php
Documentation is available at rss1.php
* File containing the ezcFeedRss1 class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @version //autogentag//
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* Class providing parsing and generating of RSS1 feeds.
* {@link http://web.resource.org/rss/1.0/spec RSS1 Specifications}
* @version //autogentag//
* Defines the feed type of this processor.
* Defines the feed content type of this processor.
* Defines the namespace for RSS1 (RDF) feeds.
const NAMESPACE_URI =
'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
* Creates a new RSS1 processor.
* @param ezcFeed $container The feed data container used when generating
$this->feedContainer =
$container;
$this->feedType =
self::FEED_TYPE;
$this->contentType =
self::CONTENT_TYPE;
* Returns an XML string from the feed information contained in this
$this->xml =
new DOMDocument( '1.0', 'utf-8' );
$this->xml->formatOutput =
1;
$this->createRootElement( '2.0' );
$this->generateChannel();
$this->generateFeedModules( $this->channel );
$this->generateTextInput();
return $this->xml->saveXML();
* Returns true if the parser can parse the provided XML document object,
* @param DOMDocument $xml The XML document object to check for parseability
public static function canParse( DOMDocument $xml )
if ( strpos( $xml->documentElement->tagName, 'RDF' ) ===
false )
$namespaceUri =
$xml->documentElement->lookupNamespaceURI( null );
if ( $namespaceUri ===
"http://channel.netscape.com/rdf/simple/0.9/" )
* Parses the provided XML document object and returns an ezcFeed object
* @throws ezcFeedParseErrorException
* If an error was encountered during parsing.
* @param DOMDocument $xml The XML document object to parse
public function parse( DOMDocument $xml )
$feed =
new ezcFeed( self::FEED_TYPE );
$rssChildren =
$xml->documentElement->childNodes;
$this->usedPrefixes =
$this->fetchUsedPrefixes( $xml );
foreach ( $rssChildren as $rssChild )
if ( $rssChild->nodeType ===
XML_ELEMENT_NODE
&&
$rssChild->tagName ===
'channel' )
if ( $channel->hasAttributeNS( self::NAMESPACE_URI, 'about' ) )
$feed->id =
$channel->getAttributeNS( self::NAMESPACE_URI, 'about' );
foreach ( $channel->childNodes as $channelChild )
if ( $channelChild->nodeType ==
XML_ELEMENT_NODE )
$tagName =
$channelChild->tagName;
$feed->$tagName =
$channelChild->textContent;
$seq =
$channelChild->getElementsByTagNameNS( self::NAMESPACE_URI, 'Seq' );
if ( $seq->length ===
0 )
$lis =
$seq->item( 0 )->getElementsByTagNameNS( self::NAMESPACE_URI, 'li' );
$resource =
$el->getAttribute( 'resource' );
if ( empty( $resource ) )
// some RSS1 (RDF) feeds specify the "resource" attribute as "rdf:resource"
$resource =
$el->getAttributeNS( self::NAMESPACE_URI, 'resource' );
$item =
$this->getNodeByAttributeNS( $xml->documentElement, 'item', self::NAMESPACE_URI, 'about', $resource );
if ( $item instanceof
DOMElement )
$element =
$feed->add( 'item' );
$this->parseItem( $feed, $element, $item );
$resource =
$channelChild->getAttributeNS( self::NAMESPACE_URI, 'resource' );
$image =
$this->getNodeByAttributeNS( $xml->documentElement, 'image', self::NAMESPACE_URI, 'about', $resource );
$this->parseImage( $feed, $image );
$resource =
$channelChild->getAttributeNS( self::NAMESPACE_URI, 'resource' );
$textInput =
$this->getNodeByAttributeNS( $xml->documentElement, 'textinput', self::NAMESPACE_URI, 'about', $resource );
$this->parseTextInput( $feed, $textInput );
// check if it's part of a known module/namespace
$this->parseModules( $feed, $channelChild, $tagName );
if ( $channel->hasAttribute( 'xml:lang' ) )
$feed->language =
$channel->getAttribute( 'xml:lang' );
* Creates a root node for the XML document being generated.
* @param string $version The RSS version for the root node
private function createRootElement( $version )
$rss =
$this->xml->createElementNS( self::NAMESPACE_URI, 'rdf:RDF' );
$this->addAttribute( $rss, 'xmlns', 'http://purl.org/rss/1.0/' );
$this->channel =
$channelTag =
$this->xml->createElement( 'channel' );
$rss->appendChild( $channelTag );
$this->root =
$this->xml->appendChild( $rss );
* Adds the required feed elements to the XML document being generated.
private function generateChannel()
$aboutAttr =
$this->xml->createAttribute( 'rdf:about' );
$aboutVal =
$this->xml->createTextNode( $data );
$aboutAttr->appendChild( $aboutVal );
$this->channel->appendChild( $aboutAttr );
$elements =
array( 'title', 'link', 'description' );
foreach ( $elements as $element )
$this->generateMetaData( $this->channel, $element, $data );
$this->generateMetaData( $this->channel, $element, $data );
$this->addAttribute( $this->channel, 'xml:lang', $this->language );
if ( count( $items ) ===
0 )
$itemsTag =
$this->xml->createElement( 'items' );
$this->channel->appendChild( $itemsTag );
$seqTag =
$this->xml->createElement( 'rdf:Seq' );
$itemsTag->appendChild( $seqTag );
foreach ( $items as $item )
$liTag =
$this->xml->createElement( 'rdf:li' );
$resourceAttr =
$this->xml->createAttribute( 'resource' );
$resourceVal =
$this->xml->createTextNode( $about );
$resourceAttr->appendChild( $resourceVal );
$liTag->appendChild( $resourceAttr );
$seqTag->appendChild( $liTag );
$imageTag =
$this->xml->createElement( 'image' );
$resourceAttr =
$this->xml->createAttribute( 'rdf:resource' );
$resourceVal =
$this->xml->createTextNode( $about );
$resourceAttr->appendChild( $resourceVal );
$imageTag->appendChild( $resourceAttr );
$this->channel->appendChild( $imageTag );
$textInput =
$this->textInput;
if ( $textInput !==
null )
$textInputTag =
$this->xml->createElement( 'textinput' );
$about =
$textInput->about;
$resourceAttr =
$this->xml->createAttribute( 'rdf:resource' );
$resourceVal =
$this->xml->createTextNode( $about );
$resourceAttr->appendChild( $resourceVal );
$textInputTag->appendChild( $resourceAttr );
$this->channel->appendChild( $textInputTag );
* Adds the feed items to the XML document being generated.
private function generateItems()
foreach ( $this->item as $element )
$itemTag =
$this->xml->createElement( 'item' );
$this->root->appendChild( $itemTag );
$aboutAttr =
$this->xml->createAttribute( 'rdf:about' );
$aboutVal =
$this->xml->createTextNode( $data );
$aboutAttr->appendChild( $aboutVal );
$itemTag->appendChild( $aboutAttr );
$elements =
array( 'title', 'link' );
foreach ( $elements as $attribute )
$data =
$element->$attribute;
$this->generateMetaData( $itemTag, $attribute, $data );
$elements =
array( 'description', 'language' );
foreach ( $elements as $attribute )
$data =
$element->$attribute;
$this->generateMetaData( $itemTag, $attribute, $data );
$this->addAttribute( $itemTag, 'xml:lang', $data );
$this->generateItemModules( $element, $itemTag );
* Adds the feed image to the XML document being generated.
private function generateImage()
$imageTag =
$this->xml->createElement( 'image' );
$this->root->appendChild( $imageTag );
$aboutAttr =
$this->xml->createAttribute( 'rdf:about' );
$aboutVal =
$this->xml->createTextNode( $data );
$aboutAttr->appendChild( $aboutVal );
$imageTag->appendChild( $aboutAttr );
$elements =
array( 'title', 'url', 'link' );
foreach ( $elements as $attribute )
$data =
$image->$attribute;
$this->generateMetaData( $imageTag, $attribute, $data );
* Adds the feed textinput to the XML document being generated.
private function generateTextInput()
$textInput =
$this->textInput;
if ( $textInput !==
null )
$textInputTag =
$this->xml->createElement( 'textinput' );
$this->root->appendChild( $textInputTag );
$data =
$textInput->about;
$aboutAttr =
$this->xml->createAttribute( 'rdf:about' );
$aboutVal =
$this->xml->createTextNode( $data );
$aboutAttr->appendChild( $aboutVal );
$textInputTag->appendChild( $aboutAttr );
$elements =
array( 'title', 'description', 'name', 'link' );
foreach ( $elements as $attribute )
$data =
$textInput->$attribute;
$this->generateMetaData( $textInputTag, $attribute, $data );
* Parses the provided XML element object and stores it as a feed item in
* the provided ezcFeed object.
* @param ezcFeed $feed The feed object in which to store the parsed XML element as a feed item
* @param ezcFeedElement $element The feed element object that will contain the feed item
* @param DOMElement $xml The XML element object to parse
private function parseItem( ezcFeed $feed, $element, DOMElement $xml )
if ( $xml->hasAttributeNS( self::NAMESPACE_URI, 'about' ) )
$element->id =
$xml->getAttributeNS( self::NAMESPACE_URI, 'about' );
foreach ( $xml->childNodes as $itemChild )
if ( $itemChild->nodeType ==
XML_ELEMENT_NODE )
$tagName =
$itemChild->tagName;
$element->$tagName =
$itemChild->textContent;
// check if it's part of a known module/namespace
$this->parseModules( $element, $itemChild, $tagName );
if ( $xml->hasAttribute( 'xml:lang' ) )
$element->language =
$xml->getAttribute( 'xml:lang' );
* Parses the provided XML element object and stores it as a feed image in
* the provided ezcFeed object.
* @param ezcFeed $feed The feed object in which to store the parsed XML element as a feed image
* @param DOMElement $xml The XML element object to parse
private function parseImage( ezcFeed $feed, DOMElement $xml =
null )
$image =
$feed->add( 'image' );
foreach ( $xml->childNodes as $itemChild )
if ( $itemChild->nodeType ==
XML_ELEMENT_NODE )
$tagName =
$itemChild->tagName;
$image->$tagName =
$itemChild->textContent;
if ( $xml->hasAttributeNS( self::NAMESPACE_URI, 'about' ) )
$image->about =
$xml->getAttributeNS( self::NAMESPACE_URI, 'about' );
* 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' );
foreach ( $xml->childNodes as $itemChild )
if ( $itemChild->nodeType ==
XML_ELEMENT_NODE )
$tagName =
$itemChild->tagName;
$textInput->$tagName =
$itemChild->textContent;
if ( $xml->hasAttributeNS( self::NAMESPACE_URI, 'about' ) )
$textInput->about =
$xml->getAttributeNS( self::NAMESPACE_URI, 'about' );