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.  */
  27.  
  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';
  43.  
  44.     /**
  45.      * Defines the feed content type of this processor.
  46.      */
  47.     const CONTENT_TYPE = 'application/rss+xml';
  48.  
  49.     /**
  50.      * Defines the namespace for RSS1 (RDF) feeds.
  51.      */
  52.     const NAMESPACE_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  53.  
  54.     /**
  55.      * Creates a new RSS1 processor.
  56.      *
  57.      * @param ezcFeed $container The feed data container used when generating
  58.      */
  59.     public function __constructezcFeed $container )
  60.     {
  61.         $this->feedContainer $container;
  62.         $this->feedType self::FEED_TYPE;
  63.         $this->contentType self::CONTENT_TYPE;
  64.     }
  65.  
  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' );
  77.  
  78.         $this->generateChannel();
  79.         $this->generateFeedModules$this->channel );
  80.         $this->generateItems();
  81.         $this->generateImage();
  82.         $this->generateTextInput();
  83.  
  84.         return $this->xml->saveXML();
  85.     }
  86.  
  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 canParseDOMDocument $xml )
  95.     {
  96.         if strpos$xml->documentElement->tagName'RDF' === false )
  97.         {
  98.             return false;
  99.         }
  100.  
  101.         $namespaceUri $xml->documentElement->lookupNamespaceURInull );
  102.  
  103.         // RSS 0.90
  104.         if $namespaceUri === "http://channel.netscape.com/rdf/simple/0.9/" )
  105.         {
  106.             return false;
  107.         }
  108.  
  109.         return true;
  110.     }
  111.  
  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 parseDOMDocument $xml )
  123.     {
  124.         $feed new ezcFeedself::FEED_TYPE );
  125.         $rssChildren $xml->documentElement->childNodes;
  126.         $channel null;
  127.  
  128.         $this->usedPrefixes $this->fetchUsedPrefixes$xml );
  129.  
  130.         foreach $rssChildren as $rssChild )
  131.         {
  132.             if $rssChild->nodeType === XML_ELEMENT_NODE
  133.                  && $rssChild->tagName === 'channel' )
  134.             {
  135.                 $channel $rssChild;
  136.             }
  137.         }
  138.  
  139.         if $channel === null )
  140.         {
  141.             throw new ezcFeedParseErrorExceptionnull"No channel tag" );
  142.         }
  143.  
  144.         if $channel->hasAttributeNSself::NAMESPACE_URI'about' ) )
  145.         {
  146.             $feed->id $channel->getAttributeNSself::NAMESPACE_URI'about' );
  147.         }
  148.  
  149.         foreach $channel->childNodes as $channelChild )
  150.         {
  151.             if $channelChild->nodeType == XML_ELEMENT_NODE )
  152.             {
  153.                 $tagName $channelChild->tagName;
  154.  
  155.                 switch $tagName )
  156.                 {
  157.                     case 'title':
  158.                     case 'link':
  159.                     case 'description':
  160.                         $feed->$tagName $channelChild->textContent;
  161.                         break;
  162.  
  163.                     case 'items':
  164.                         $seq $channelChild->getElementsByTagNameNSself::NAMESPACE_URI'Seq' );
  165.                         if $seq->length === )
  166.                         {
  167.                             break;
  168.                         }
  169.  
  170.                         $lis $seq->item)->getElementsByTagNameNSself::NAMESPACE_URI'li' );
  171.  
  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->getAttributeNSself::NAMESPACE_URI'resource' );
  180.                             }
  181.  
  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;
  190.  
  191.                     case 'image':
  192.                         $resource $channelChild->getAttributeNSself::NAMESPACE_URI'resource' );
  193.  
  194.                         $image $this->getNodeByAttributeNS$xml->documentElement'image'self::NAMESPACE_URI'about'$resource );
  195.                         $this->parseImage$feed$image );
  196.                         break;
  197.  
  198.                     case 'textinput':
  199.                         $resource $channelChild->getAttributeNSself::NAMESPACE_URI'resource' );
  200.  
  201.                         $textInput $this->getNodeByAttributeNS$xml->documentElement'textinput'self::NAMESPACE_URI'about'$resource );
  202.                         $this->parseTextInput$feed$textInput );
  203.                         break;
  204.  
  205.                     default:
  206.                         // check if it's part of a known module/namespace
  207.                         $this->parseModules$feed$channelChild$tagName );
  208.                         break;
  209.                 }
  210.             }
  211.         }
  212.  
  213.         if $channel->hasAttribute'xml:lang' ) )
  214.         {
  215.             $feed->language $channel->getAttribute'xml:lang' );
  216.         }
  217.  
  218.         return $feed;
  219.     }
  220.  
  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->createElementNSself::NAMESPACE_URI'rdf:RDF' );
  229.         $this->addAttribute$rss'xmlns''http://purl.org/rss/1.0/' );
  230.  
  231.         $this->channel $channelTag $this->xml->createElement'channel' );
  232.         $rss->appendChild$channelTag );
  233.         $this->root $this->xml->appendChild$rss );
  234.     }
  235.  
  236.     /**
  237.      * Adds the required feed elements to the XML document being generated.
  238.      */
  239.     private function generateChannel()
  240.     {
  241.         $data $this->id;
  242.  
  243.         if is_null$data ) )
  244.         {
  245.             throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/@about);
  246.         }
  247.  
  248.         $aboutAttr $this->xml->createAttribute'rdf:about' );
  249.         $aboutVal $this->xml->createTextNode$data );
  250.         $aboutAttr->appendChild$aboutVal );
  251.         $this->channel->appendChild$aboutAttr );
  252.  
  253.         $elements array'title''link''description' );
  254.         foreach $elements as $element )
  255.         {
  256.             $data $this->$element;
  257.             if is_null$data ) )
  258.             {
  259.                 throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/{$element});
  260.             }
  261.  
  262.             switch $element )
  263.             {
  264.                 case 'link':
  265.                     $this->generateMetaData$this->channel$element$data );
  266.                     break;
  267.  
  268.                 case 'title':
  269.                 case 'description':
  270.                     $this->generateMetaData$this->channel$element$data );
  271.                     break;
  272.             }
  273.         }
  274.  
  275.         if !is_null$this->language ) )
  276.         {
  277.             $this->addAttribute$this->channel'xml:lang'$this->language );
  278.         }
  279.  
  280.         $items $this->item;
  281.         if count$items === )
  282.         {
  283.             throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/item);
  284.         }
  285.  
  286.         $itemsTag $this->xml->createElement'items' );
  287.         $this->channel->appendChild$itemsTag );
  288.         $seqTag $this->xml->createElement'rdf:Seq' );
  289.         $itemsTag->appendChild$seqTag );
  290.  
  291.         foreach $items as $item )
  292.         {
  293.             $about $item->id;
  294.             $liTag $this->xml->createElement'rdf:li' );
  295.             $resourceAttr $this->xml->createAttribute'resource' );
  296.             $resourceVal $this->xml->createTextNode$about );
  297.             $resourceAttr->appendChild$resourceVal );
  298.             $liTag->appendChild$resourceAttr );
  299.             $seqTag->appendChild$liTag );
  300.         }
  301.  
  302.         $image $this->image;
  303.         if $image !== null )
  304.         {
  305.             $imageTag $this->xml->createElement'image' );
  306.  
  307.             $about $image->about;
  308.             if is_null$data ) )
  309.             {
  310.                 throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/image/@about);
  311.             }
  312.  
  313.             $resourceAttr $this->xml->createAttribute'rdf:resource' );
  314.             $resourceVal $this->xml->createTextNode$about );
  315.             $resourceAttr->appendChild$resourceVal );
  316.             $imageTag->appendChild$resourceAttr );
  317.  
  318.             $this->channel->appendChild$imageTag );
  319.         }
  320.  
  321.         $textInput $this->textInput;
  322.         if $textInput !== null )
  323.         {
  324.             $textInputTag $this->xml->createElement'textinput' );
  325.  
  326.             $about $textInput->about;
  327.             if is_null$data ) )
  328.             {
  329.                 throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/textinput/@about);
  330.             }
  331.  
  332.             $resourceAttr $this->xml->createAttribute'rdf:resource' );
  333.             $resourceVal $this->xml->createTextNode$about );
  334.             $resourceAttr->appendChild$resourceVal );
  335.             $textInputTag->appendChild$resourceAttr );
  336.  
  337.             $this->channel->appendChild$textInputTag );
  338.         }
  339.     }
  340.  
  341.     /**
  342.      * Adds the feed items to the XML document being generated.
  343.      */
  344.     private function generateItems()
  345.     {
  346.         foreach $this->item as $element )
  347.         {
  348.             $itemTag $this->xml->createElement'item' );
  349.             $this->root->appendChild$itemTag );
  350.  
  351.             $data $element->id;
  352.             if is_null$data ) )
  353.             {
  354.                 throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/item/@about);
  355.             }
  356.  
  357.             $aboutAttr $this->xml->createAttribute'rdf:about' );
  358.             $aboutVal $this->xml->createTextNode$data );
  359.             $aboutAttr->appendChild$aboutVal );
  360.             $itemTag->appendChild$aboutAttr );
  361.  
  362.             $elements array'title''link' );
  363.             foreach $elements as $attribute )
  364.             {
  365.                 $data $element->$attribute;
  366.  
  367.                 if is_null$data ) )
  368.                 {
  369.                     throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/item/{$attribute});
  370.                 }
  371.  
  372.                 $this->generateMetaData$itemTag$attribute$data );
  373.             }
  374.  
  375.             $elements array'description''language' );
  376.             foreach $elements as $attribute )
  377.             {
  378.                 $data $element->$attribute;
  379.                 if !is_null$data ) )
  380.                 {
  381.                     switch $attribute )
  382.                     {
  383.                         case 'description':
  384.                             $this->generateMetaData$itemTag$attribute$data );
  385.                             break;
  386.  
  387.                         case 'language':
  388.                            $this->addAttribute$itemTag'xml:lang'$data );
  389.                            break;
  390.                     }
  391.                 }
  392.             }
  393.  
  394.             $this->generateItemModules$element$itemTag );
  395.         }
  396.     }
  397.  
  398.     /**
  399.      * Adds the feed image to the XML document being generated.
  400.      */
  401.     private function generateImage()
  402.     {
  403.         $image $this->image;
  404.         if $image !== null )
  405.         {
  406.             $imageTag $this->xml->createElement'image' );
  407.             $this->root->appendChild$imageTag );
  408.  
  409.             $data $image->about;
  410.             if is_null$data ) )
  411.             {
  412.                 throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/image/@about);
  413.             }
  414.  
  415.             $aboutAttr $this->xml->createAttribute'rdf:about' );
  416.             $aboutVal $this->xml->createTextNode$data );
  417.             $aboutAttr->appendChild$aboutVal );
  418.             $imageTag->appendChild$aboutAttr );
  419.  
  420.             $elements array'title''url''link' );
  421.             foreach $elements as $attribute )
  422.             {
  423.                 $data $image->$attribute;
  424.                 if is_null$data ) )
  425.                 {
  426.                     throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/image/{$attribute});
  427.                 }
  428.  
  429.                 $this->generateMetaData$imageTag$attribute$data );
  430.             }
  431.         }
  432.     }
  433.  
  434.     /**
  435.      * Adds the feed textinput to the XML document being generated.
  436.      */
  437.     private function generateTextInput()
  438.     {
  439.         $textInput $this->textInput;
  440.         if $textInput !== null )
  441.         {
  442.             $textInputTag $this->xml->createElement'textinput' );
  443.             $this->root->appendChild$textInputTag );
  444.  
  445.             $data $textInput->about;
  446.             if is_null$data ) )
  447.             {
  448.                 throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/textinput/@about);
  449.             }
  450.  
  451.             $aboutAttr $this->xml->createAttribute'rdf:about' );
  452.             $aboutVal $this->xml->createTextNode$data );
  453.             $aboutAttr->appendChild$aboutVal );
  454.             $textInputTag->appendChild$aboutAttr );
  455.  
  456.             $elements array'title''description''name''link' );
  457.             foreach $elements as $attribute )
  458.             {
  459.                 $data $textInput->$attribute;
  460.                 if is_null$data ) )
  461.                 {
  462.                     throw new ezcFeedRequiredMetaDataMissingException"/{$this->root->nodeName}/textinput/{$attribute});
  463.                 }
  464.  
  465.                 $this->generateMetaData$textInputTag$attribute$data );
  466.             }
  467.         }
  468.     }
  469.  
  470.     /**
  471.      * Parses the provided XML element object and stores it as a feed item in
  472.      * the provided ezcFeed object.
  473.      *
  474.      * @param ezcFeed $feed The feed object in which to store the parsed XML element as a feed item
  475.      * @param ezcFeedElement $element The feed element object that will contain the feed item
  476.      * @param DOMElement $xml The XML element object to parse
  477.      */
  478.     private function parseItemezcFeed $feed$elementDOMElement $xml )
  479.     {
  480.         if $xml->hasAttributeNSself::NAMESPACE_URI'about' ) )
  481.         {
  482.             $element->id $xml->getAttributeNSself::NAMESPACE_URI'about' );
  483.         }
  484.  
  485.         foreach $xml->childNodes as $itemChild )
  486.         {
  487.             if $itemChild->nodeType == XML_ELEMENT_NODE )
  488.             {
  489.                 $tagName $itemChild->tagName;
  490.  
  491.                 switch $tagName )
  492.                 {
  493.                     case 'title':
  494.                     case 'link':
  495.                     case 'description':
  496.                         $element->$tagName $itemChild->textContent;
  497.                         break;
  498.  
  499.                     default:
  500.                         // check if it's part of a known module/namespace
  501.                         $this->parseModules$element$itemChild$tagName );
  502.                         break;
  503.                 }
  504.             }
  505.         }
  506.  
  507.         if $xml->hasAttribute'xml:lang' ) )
  508.         {
  509.             $element->language $xml->getAttribute'xml:lang' );
  510.         }
  511.     }
  512.  
  513.     /**
  514.      * Parses the provided XML element object and stores it as a feed image in
  515.      * the provided ezcFeed object.
  516.      *
  517.      * @param ezcFeed $feed The feed object in which to store the parsed XML element as a feed image
  518.      * @param DOMElement $xml The XML element object to parse
  519.      */
  520.     private function parseImageezcFeed $feedDOMElement $xml null )
  521.     {
  522.         $image $feed->add'image' );
  523.         if $xml !== null )
  524.         {
  525.             foreach $xml->childNodes as $itemChild )
  526.             {
  527.                 if $itemChild->nodeType == XML_ELEMENT_NODE )
  528.                 {
  529.                     $tagName $itemChild->tagName;
  530.                     switch $tagName )
  531.                     {
  532.                         case 'title':
  533.                         case 'link':
  534.                         case 'url':
  535.                             $image->$tagName $itemChild->textContent;
  536.                             break;
  537.                     }
  538.                 }
  539.             }
  540.  
  541.             if $xml->hasAttributeNSself::NAMESPACE_URI'about' ) )
  542.             {
  543.                 $image->about $xml->getAttributeNSself::NAMESPACE_URI'about' );
  544.             }
  545.         }
  546.     }
  547.  
  548.     /**
  549.      * Parses the provided XML element object and stores it as a feed textinput in
  550.      * the provided ezcFeed object.
  551.      *
  552.      * @param ezcFeed $feed The feed object in which to store the parsed XML element as a feed textinput
  553.      * @param DOMElement $xml The XML element object to parse
  554.      */
  555.     private function parseTextInputezcFeed $feedDOMElement $xml null )
  556.     {
  557.         $textInput $feed->add'textInput' );
  558.         if $xml !== null )
  559.         {
  560.             foreach $xml->childNodes as $itemChild )
  561.             {
  562.                 if $itemChild->nodeType == XML_ELEMENT_NODE )
  563.                 {
  564.                     $tagName $itemChild->tagName;
  565.                     switch $tagName )
  566.                     {
  567.                         case 'title':
  568.                         case 'description':
  569.                         case 'name':
  570.                         case 'link':
  571.                             $textInput->$tagName $itemChild->textContent;
  572.                             break;
  573.                     }
  574.                 }
  575.             }
  576.  
  577.             if $xml->hasAttributeNSself::NAMESPACE_URI'about' ) )
  578.             {
  579.                 $textInput->about $xml->getAttributeNSself::NAMESPACE_URI'about' );
  580.             }
  581.         }
  582.     }
  583. }
  584. ?>
Documentation generated by phpDocumentor 1.4.3