## Apache Zeta Components Manual :: File Source for rss1.php

## Source for file rss1.php

Documentation is available at rss1.php

1. <?php
2. /**
3. * File containing the ezcFeedRss1 class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package Feed
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * Class providing parsing and generating of RSS1 feeds.
30. *
31. * Specifications:
32. * {@link http://web.resource.org/rss/1.0/spec RSS1 Specifications}
33. *
34. * @package Feed
35. * @version //autogentag//
36. */
37. class ezcFeedRss1 extends ezcFeedProcessor implements ezcFeedParser
38. {
39. /**
40. * Defines the feed type of this processor.
41. */
42. const FEED_TYPE = 'rss1';
44. /**
45. * Defines the feed content type of this processor.
46. */
47. const CONTENT_TYPE = 'application/rss+xml';
49. /**
50. * Defines the namespace for RSS1 (RDF) feeds.
51. */
52. const NAMESPACE_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
54. /**
55. * Creates a new RSS1 processor.
56. *
57. * @param ezcFeed $container The feed data container used when generating
58. */
59. public function __construct ( ezcFeed $container )
60. {
61. $this->feedContainer = $container;
62. $this->feedType = self::FEED_TYPE;
63. $this->contentType = self::CONTENT_TYPE;
64. }
66. /**
67. * Returns an XML string from the feed information contained in this
68. * processor.
69. *
70. * @return string
71. */
72. public function generate ()
73. {
74. $this->xml = new DOMDocument( '1.0', 'utf-8' );
75. $this->xml->formatOutput = 1;
76. $this->createRootElement( '2.0' );
78. $this->generateChannel();
79. $this->generateFeedModules( $this->channel );
80. $this->generateItems();
81. $this->generateImage();
82. $this->generateTextInput();
84. return $this->xml->saveXML();
85. }
87. /**
88. * Returns true if the parser can parse the provided XML document object,
89. * false otherwise.
90. *
91. * @param DOMDocument $xml The XML document object to check for parseability
92. * @return bool
93. */
94. public static function canParse ( DOMDocument $xml )
95. {
96. if ( strpos ( $xml->documentElement->tagName, 'RDF' ) === false )
97. {
98. return false;
99. }
101. $namespaceUri = $xml->documentElement->lookupNamespaceURI( null );
103. // RSS 0.90
104. if ( $namespaceUri === "http://channel.netscape.com/rdf/simple/0.9/" )
105. {
106. return false;
107. }
109. return true;
110. }
112. /**
113. * Parses the provided XML document object and returns an ezcFeed object
114. * from it.
115. *
116. * @throws ezcFeedParseErrorException
117. * If an error was encountered during parsing.
118. *
119. * @param DOMDocument $xml The XML document object to parse
120. * @return ezcFeed
121. */
122. public function parse ( DOMDocument $xml )
123. {
124. $feed = new ezcFeed ( self::FEED_TYPE );
125. $rssChildren = $xml->documentElement->childNodes;
126. $channel = null;
128. $this->usedPrefixes = $this->fetchUsedPrefixes( $xml );
130. foreach ( $rssChildren as $rssChild )
131. {
132. if ( $rssChild->nodeType === XML_ELEMENT_NODE
133. && $rssChild->tagName === 'channel' )
134. {
135. $channel = $rssChild;
136. }
137. }
139. if ( $channel === null )
140. {
141. throw new ezcFeedParseErrorException ( null, "No channel tag" );
142. }
144. if ( $channel->hasAttributeNS( self::NAMESPACE_URI, 'about' ) )
145. {
146. $feed->id = $channel->getAttributeNS( self::NAMESPACE_URI, 'about' );
147. }
149. foreach ( $channel->childNodes as $channelChild )
150. {
151. if ( $channelChild->nodeType == XML_ELEMENT_NODE )
152. {
153. $tagName = $channelChild->tagName;
155. switch ( $tagName )
156. {
157. case 'title':
158. case 'link':
159. case 'description':
160. $feed->$tagName = $channelChild->textContent;
161. break;
163. case 'items':
164. $seq = $channelChild->getElementsByTagNameNS( self::NAMESPACE_URI, 'Seq' );
165. if ( $seq->length === 0 )
166. {
167. break;
168. }
170. $lis = $seq->item( 0 )->getElementsByTagNameNS( self::NAMESPACE_URI, 'li' );
172. foreach ( $lis as $el )
173. {
174. $resource = $el->getAttribute( 'resource' );
175. if ( empty( $resource ) )
176. {
177. // some RSS1 (RDF) feeds specify the "resource" attribute as "rdf:resource"
178. // see issue #13109
179. $resource = $el->getAttributeNS( self::NAMESPACE_URI, 'resource' );
180. }
182. $item = $this->getNodeByAttributeNS( $xml->documentElement, 'item', self::NAMESPACE_URI, 'about', $resource );
183. if ( $item instanceof DOMElement )
184. {
185. $element = $feed->add( 'item' );
186. $this->parseItem( $feed, $element, $item );
187. }
188. }
189. break;
191. case 'image':
192. $resource = $channelChild->getAttributeNS( self::NAMESPACE_URI, 'resource' );
194. $image = $this->getNodeByAttributeNS( $xml->documentElement, 'image', self::NAMESPACE_URI, 'about', $resource );
195. $this->parseImage( $feed, $image );
196. break;
198. case 'textinput':
199. $resource = $channelChild->getAttributeNS( self::NAMESPACE_URI, 'resource' );
201. $textInput = $this->getNodeByAttributeNS( $xml->documentElement, 'textinput', self::NAMESPACE_URI, 'about', $resource );
202. $this->parseTextInput( $feed, $textInput );
203. break;
205. default:
206. // check if it's part of a known module/namespace
207. $this->parseModules( $feed, $channelChild, $tagName );
208. break;
209. }
210. }
211. }
213. if ( $channel->hasAttribute( 'xml:lang' ) )
214. {
215. $feed->language = $channel->getAttribute( 'xml:lang' );
216. }
218. return $feed;
219. }
221. /**
222. * Creates a root node for the XML document being generated.
223. *
224. * @param string $version The RSS version for the root node
225. */
226. private function createRootElement( $version )
227. {
228. $rss = $this->xml->createElementNS( self::NAMESPACE_URI, 'rdf:RDF' );
229. $this->addAttribute( $rss, 'xmlns', 'http://purl.org/rss/1.0/' );
231. $this->channel = $channelTag = $this->xml->createElement( 'channel' );
232. $rss->appendChild( $channelTag );
233. $this->root = $this->xml->appendChild( $rss );
234. }
236. /**
237. * Adds the required feed elements to the XML document being generated.
238. */
239. private function generateChannel()
240. {
241. $data = $this->id;
243. if ( is_null ( $data ) )
244. {
245. throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/@about

" );
- }
- $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 )
- {
- $data = $this->$element;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/{$element}" );
- }
- switch ( $element )
- {
- case 'link':
- $this->generateMetaData( $this->channel, $element, $data );
- break;
- case 'title':
- case 'description':
- $this->generateMetaData( $this->channel, $element, $data );
- break;
- }
- }
- if ( ! is_null ( $this->language ) )
- {
- $this->addAttribute( $this->channel, 'xml:lang', $this->language );
- }
- $items = $this->item;
- if ( count ( $items ) === 0 )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/item" );
- }
- $itemsTag = $this->xml->createElement( 'items' );
- $this->channel->appendChild( $itemsTag );
- $seqTag = $this->xml->createElement( 'rdf:Seq' );
- $itemsTag->appendChild( $seqTag );
- foreach ( $items as $item )
- {
- $about = $item->id;
- $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 );
- }
- $image = $this->image;
- if ( $image !== null )
- {
- $imageTag = $this->xml->createElement( 'image' );
- $about = $image->about;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/image/@about" );
- }
- $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;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/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 );
- $data = $element->id;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/item/@about" );
- }
- $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;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/item/{$attribute}" );
- }
- $this->generateMetaData( $itemTag, $attribute, $data );
- }
- $elements = array( 'description', 'language' );
- foreach ( $elements as $attribute )
- {
- $data = $element->$attribute;
- if ( ! is_null ( $data ) )
- {
- switch ( $attribute )
- {
- case 'description':
- $this->generateMetaData( $itemTag, $attribute, $data );
- break;
- case 'language':
- $this->addAttribute( $itemTag, 'xml:lang', $data );
- break;
- }
- }
- }
- $this->generateItemModules( $element, $itemTag );
- }
- }
- /**
- * Adds the feed image to the XML document being generated.
- */
- private function generateImage()
- {
- $image = $this->image;
- if ( $image !== null )
- {
- $imageTag = $this->xml->createElement( 'image' );
- $this->root->appendChild( $imageTag );
- $data = $image->about;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/image/@about" );
- }
- $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;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/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;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/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;
- if ( is_null ( $data ) )
- {
- throw new ezcFeedRequiredMetaDataMissingException ( "/{$this->root->nodeName}/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;
- switch ( $tagName )
- {
- case 'title':
- case 'link':
- case 'description':
- $element->$tagName = $itemChild->textContent;
- break;
- default:
- // check if it's part of a known module/namespace
- $this->parseModules( $element, $itemChild, $tagName );
- break;
- }
- }
- }
- 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' );
- if ( $xml !== null )
- {
- foreach ( $xml->childNodes as $itemChild )
- {
- if ( $itemChild->nodeType == XML_ELEMENT_NODE )
- {
- $tagName = $itemChild->tagName;
- switch ( $tagName )
- {
- case 'title':
- case 'link':
- case 'url':
- $image->$tagName = $itemChild->textContent;
- break;
- }
- }
- }
- 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' );
- 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' );
- }
- }
- }
- }
- ?>
Documentation generated by phpDocumentor 1.4.3