web/lib/Zend/Feed/Reader/Extension/Atom/Feed.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Feed_Reader
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Feed.php 23170 2010-10-19 18:29:24Z mabe $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Feed_Reader_Extension_FeedAbstract
       
    24  */
       
    25 require_once 'Zend/Feed/Reader/Extension/FeedAbstract.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Date
       
    29  */
       
    30 require_once 'Zend/Date.php';
       
    31 
       
    32 /**
       
    33  * @see Zend_Uri
       
    34  */
       
    35 require_once 'Zend/Uri.php';
       
    36 
       
    37 /**
       
    38  * @see Zend_Feed_Reader_Collection_Author
       
    39  */
       
    40 require_once 'Zend/Feed/Reader/Collection/Author.php';
       
    41 
       
    42 /**
       
    43  * @category   Zend
       
    44  * @package    Zend_Feed_Reader
       
    45  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    46  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    47  */
       
    48 class Zend_Feed_Reader_Extension_Atom_Feed
       
    49     extends Zend_Feed_Reader_Extension_FeedAbstract
       
    50 {
       
    51     /**
       
    52      * Get a single author
       
    53      *
       
    54      * @param  int $index
       
    55      * @return string|null
       
    56      */
       
    57     public function getAuthor($index = 0)
       
    58     {
       
    59         $authors = $this->getAuthors();
       
    60 
       
    61         if (isset($authors[$index])) {
       
    62             return $authors[$index];
       
    63         }
       
    64 
       
    65         return null;
       
    66     }
       
    67 
       
    68     /**
       
    69      * Get an array with feed authors
       
    70      *
       
    71      * @return array
       
    72      */
       
    73     public function getAuthors()
       
    74     {
       
    75         if (array_key_exists('authors', $this->_data)) {
       
    76             return $this->_data['authors'];
       
    77         }
       
    78 
       
    79         $list = $this->_xpath->query('//atom:author');
       
    80 
       
    81         $authors = array();
       
    82 
       
    83         if ($list->length) {
       
    84             foreach ($list as $author) {
       
    85                 $author = $this->_getAuthor($author);
       
    86                 if (!empty($author)) {
       
    87                     $authors[] = $author;
       
    88                 }
       
    89             }
       
    90         }
       
    91 
       
    92         if (count($authors) == 0) {
       
    93             $authors = null;
       
    94         } else {
       
    95             $authors = new Zend_Feed_Reader_Collection_Author(
       
    96                 Zend_Feed_Reader::arrayUnique($authors)
       
    97             );
       
    98         }
       
    99 
       
   100         $this->_data['authors'] = $authors;
       
   101 
       
   102         return $this->_data['authors'];
       
   103     }
       
   104 
       
   105     /**
       
   106      * Get the copyright entry
       
   107      *
       
   108      * @return string|null
       
   109      */
       
   110     public function getCopyright()
       
   111     {
       
   112         if (array_key_exists('copyright', $this->_data)) {
       
   113             return $this->_data['copyright'];
       
   114         }
       
   115 
       
   116         $copyright = null;
       
   117 
       
   118         if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
       
   119             $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:copyright)');
       
   120         } else {
       
   121             $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:rights)');
       
   122         }
       
   123 
       
   124         if (!$copyright) {
       
   125             $copyright = null;
       
   126         }
       
   127 
       
   128         $this->_data['copyright'] = $copyright;
       
   129 
       
   130         return $this->_data['copyright'];
       
   131     }
       
   132 
       
   133     /**
       
   134      * Get the feed creation date
       
   135      *
       
   136      * @return Zend_Date|null
       
   137      */
       
   138     public function getDateCreated()
       
   139     {
       
   140         if (array_key_exists('datecreated', $this->_data)) {
       
   141             return $this->_data['datecreated'];
       
   142         }
       
   143 
       
   144         $date = null;
       
   145 
       
   146         if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
       
   147             $dateCreated = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
       
   148         } else {
       
   149             $dateCreated = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
       
   150         }
       
   151 
       
   152         if ($dateCreated) {
       
   153             $date = new Zend_Date;
       
   154             $date->set($dateCreated, Zend_Date::ISO_8601);
       
   155         }
       
   156 
       
   157         $this->_data['datecreated'] = $date;
       
   158 
       
   159         return $this->_data['datecreated'];
       
   160     }
       
   161 
       
   162     /**
       
   163      * Get the feed modification date
       
   164      *
       
   165      * @return Zend_Date|null
       
   166      */
       
   167     public function getDateModified()
       
   168     {
       
   169         if (array_key_exists('datemodified', $this->_data)) {
       
   170             return $this->_data['datemodified'];
       
   171         }
       
   172 
       
   173         $date = null;
       
   174 
       
   175         if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
       
   176             $dateModified = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
       
   177         } else {
       
   178             $dateModified = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
       
   179         }
       
   180 
       
   181         if ($dateModified) {
       
   182             $date = new Zend_Date;
       
   183             $date->set($dateModified, Zend_Date::ISO_8601);
       
   184         }
       
   185 
       
   186         $this->_data['datemodified'] = $date;
       
   187 
       
   188         return $this->_data['datemodified'];
       
   189     }
       
   190 
       
   191     /**
       
   192      * Get the feed description
       
   193      *
       
   194      * @return string|null
       
   195      */
       
   196     public function getDescription()
       
   197     {
       
   198         if (array_key_exists('description', $this->_data)) {
       
   199             return $this->_data['description'];
       
   200         }
       
   201 
       
   202         $description = null;
       
   203 
       
   204         if ($this->getType() === Zend_Feed_Reader::TYPE_ATOM_03) {
       
   205             $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:tagline)'); // TODO: Is this the same as subtitle?
       
   206         } else {
       
   207             $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:subtitle)');
       
   208         }
       
   209 
       
   210         if (!$description) {
       
   211             $description = null;
       
   212         }
       
   213 
       
   214         $this->_data['description'] = $description;
       
   215 
       
   216         return $this->_data['description'];
       
   217     }
       
   218 
       
   219     /**
       
   220      * Get the feed generator entry
       
   221      *
       
   222      * @return string|null
       
   223      */
       
   224     public function getGenerator()
       
   225     {
       
   226         if (array_key_exists('generator', $this->_data)) {
       
   227             return $this->_data['generator'];
       
   228         }
       
   229         // TODO: Add uri support
       
   230         $generator = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:generator)');
       
   231 
       
   232         if (!$generator) {
       
   233             $generator = null;
       
   234         }
       
   235 
       
   236         $this->_data['generator'] = $generator;
       
   237 
       
   238         return $this->_data['generator'];
       
   239     }
       
   240 
       
   241     /**
       
   242      * Get the feed ID
       
   243      *
       
   244      * @return string|null
       
   245      */
       
   246     public function getId()
       
   247     {
       
   248         if (array_key_exists('id', $this->_data)) {
       
   249             return $this->_data['id'];
       
   250         }
       
   251 
       
   252         $id = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
       
   253 
       
   254         if (!$id) {
       
   255             if ($this->getLink()) {
       
   256                 $id = $this->getLink();
       
   257             } elseif ($this->getTitle()) {
       
   258                 $id = $this->getTitle();
       
   259             } else {
       
   260                 $id = null;
       
   261             }
       
   262         }
       
   263 
       
   264         $this->_data['id'] = $id;
       
   265 
       
   266         return $this->_data['id'];
       
   267     }
       
   268 
       
   269     /**
       
   270      * Get the feed language
       
   271      *
       
   272      * @return string|null
       
   273      */
       
   274     public function getLanguage()
       
   275     {
       
   276         if (array_key_exists('language', $this->_data)) {
       
   277             return $this->_data['language'];
       
   278         }
       
   279 
       
   280         $language = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:lang)');
       
   281 
       
   282         if (!$language) {
       
   283             $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
       
   284         }
       
   285 
       
   286         if (!$language) {
       
   287             $language = null;
       
   288         }
       
   289 
       
   290         $this->_data['language'] = $language;
       
   291 
       
   292         return $this->_data['language'];
       
   293     }
       
   294 
       
   295     /**
       
   296      * Get the feed image
       
   297      *
       
   298      * @return array|null
       
   299      */
       
   300     public function getImage()
       
   301     {
       
   302         if (array_key_exists('image', $this->_data)) {
       
   303             return $this->_data['image'];
       
   304         }
       
   305 
       
   306         $imageUrl = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:logo)');
       
   307 
       
   308         if (!$imageUrl) {
       
   309             $image = null;
       
   310         } else {
       
   311             $image = array('uri'=>$imageUrl);
       
   312         }
       
   313 
       
   314         $this->_data['image'] = $image;
       
   315 
       
   316         return $this->_data['image'];
       
   317     }
       
   318     
       
   319     /**
       
   320      * Get the feed image
       
   321      *
       
   322      * @return array|null
       
   323      */
       
   324     public function getIcon()
       
   325     {
       
   326         if (array_key_exists('icon', $this->_data)) {
       
   327             return $this->_data['icon'];
       
   328         }
       
   329 
       
   330         $imageUrl = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:icon)');
       
   331 
       
   332         if (!$imageUrl) {
       
   333             $image = null;
       
   334         } else {
       
   335             $image = array('uri'=>$imageUrl);
       
   336         }
       
   337 
       
   338         $this->_data['icon'] = $image;
       
   339 
       
   340         return $this->_data['icon'];
       
   341     }
       
   342 
       
   343     /**
       
   344      * Get the base URI of the feed (if set).
       
   345      *
       
   346      * @return string|null
       
   347      */
       
   348     public function getBaseUrl()
       
   349     {
       
   350         if (array_key_exists('baseUrl', $this->_data)) {
       
   351             return $this->_data['baseUrl'];
       
   352         }
       
   353 
       
   354         $baseUrl = $this->_xpath->evaluate('string(//@xml:base[1])');
       
   355 
       
   356         if (!$baseUrl) {
       
   357             $baseUrl = null;
       
   358         }
       
   359         $this->_data['baseUrl'] = $baseUrl;
       
   360 
       
   361         return $this->_data['baseUrl'];
       
   362     }
       
   363 
       
   364     /**
       
   365      * Get a link to the source website
       
   366      *
       
   367      * @return string|null
       
   368      */
       
   369     public function getLink()
       
   370     {
       
   371         if (array_key_exists('link', $this->_data)) {
       
   372             return $this->_data['link'];
       
   373         }
       
   374 
       
   375         $link = null;
       
   376 
       
   377         $list = $this->_xpath->query(
       
   378             $this->getXpathPrefix() . '/atom:link[@rel="alternate"]/@href' . '|' .
       
   379             $this->getXpathPrefix() . '/atom:link[not(@rel)]/@href'
       
   380         );
       
   381 
       
   382         if ($list->length) {
       
   383             $link = $list->item(0)->nodeValue;
       
   384             $link = $this->_absolutiseUri($link);
       
   385         }
       
   386 
       
   387         $this->_data['link'] = $link;
       
   388 
       
   389         return $this->_data['link'];
       
   390     }
       
   391 
       
   392     /**
       
   393      * Get a link to the feed's XML Url
       
   394      *
       
   395      * @return string|null
       
   396      */
       
   397     public function getFeedLink()
       
   398     {
       
   399         if (array_key_exists('feedlink', $this->_data)) {
       
   400             return $this->_data['feedlink'];
       
   401         }
       
   402 
       
   403         $link = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:link[@rel="self"]/@href)');
       
   404 
       
   405         $link = $this->_absolutiseUri($link);
       
   406 
       
   407         $this->_data['feedlink'] = $link;
       
   408 
       
   409         return $this->_data['feedlink'];
       
   410     }
       
   411 
       
   412     /**
       
   413      * Get an array of any supported Pusubhubbub endpoints
       
   414      *
       
   415      * @return array|null
       
   416      */
       
   417     public function getHubs()
       
   418     {
       
   419         if (array_key_exists('hubs', $this->_data)) {
       
   420             return $this->_data['hubs'];
       
   421         }
       
   422         $hubs = array();
       
   423         
       
   424         $list = $this->_xpath->query($this->getXpathPrefix()
       
   425             . '//atom:link[@rel="hub"]/@href');
       
   426 
       
   427         if ($list->length) {
       
   428             foreach ($list as $uri) {
       
   429                 $hubs[] = $this->_absolutiseUri($uri->nodeValue);
       
   430             }
       
   431         } else {
       
   432             $hubs = null;
       
   433         }
       
   434 
       
   435         $this->_data['hubs'] = $hubs;
       
   436 
       
   437         return $this->_data['hubs'];
       
   438     }
       
   439 
       
   440     /**
       
   441      * Get the feed title
       
   442      *
       
   443      * @return string|null
       
   444      */
       
   445     public function getTitle()
       
   446     {
       
   447         if (array_key_exists('title', $this->_data)) {
       
   448             return $this->_data['title'];
       
   449         }
       
   450 
       
   451         $title = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
       
   452 
       
   453         if (!$title) {
       
   454             $title = null;
       
   455         }
       
   456 
       
   457         $this->_data['title'] = $title;
       
   458 
       
   459         return $this->_data['title'];
       
   460     }
       
   461     
       
   462     /**
       
   463      * Get all categories
       
   464      *
       
   465      * @return Zend_Feed_Reader_Collection_Category
       
   466      */
       
   467     public function getCategories()
       
   468     {
       
   469         if (array_key_exists('categories', $this->_data)) {
       
   470             return $this->_data['categories'];
       
   471         }
       
   472 
       
   473         if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10) {
       
   474             $list = $this->_xpath->query($this->getXpathPrefix() . '/atom:category');
       
   475         } else {
       
   476             /**
       
   477              * Since Atom 0.3 did not support categories, it would have used the
       
   478              * Dublin Core extension. However there is a small possibility Atom 0.3
       
   479              * may have been retrofittied to use Atom 1.0 instead.
       
   480              */
       
   481             $this->_xpath->registerNamespace('atom10', Zend_Feed_Reader::NAMESPACE_ATOM_10);
       
   482             $list = $this->_xpath->query($this->getXpathPrefix() . '/atom10:category');
       
   483         }
       
   484 
       
   485         if ($list->length) {
       
   486             $categoryCollection = new Zend_Feed_Reader_Collection_Category;
       
   487             foreach ($list as $category) {
       
   488                 $categoryCollection[] = array(
       
   489                     'term' => $category->getAttribute('term'),
       
   490                     'scheme' => $category->getAttribute('scheme'),
       
   491                     'label' => $category->getAttribute('label')
       
   492                 );
       
   493             }
       
   494         } else {
       
   495             return new Zend_Feed_Reader_Collection_Category;
       
   496         }
       
   497 
       
   498         $this->_data['categories'] = $categoryCollection;
       
   499 
       
   500         return $this->_data['categories'];
       
   501     }
       
   502 
       
   503     /**
       
   504      * Get an author entry in RSS format
       
   505      *
       
   506      * @param  DOMElement $element
       
   507      * @return string
       
   508      */
       
   509     protected function _getAuthor(DOMElement $element)
       
   510     {
       
   511         $author = array();
       
   512 
       
   513         $emailNode = $element->getElementsByTagName('email');
       
   514         $nameNode  = $element->getElementsByTagName('name');
       
   515         $uriNode   = $element->getElementsByTagName('uri');
       
   516         
       
   517         if ($emailNode->length && strlen($emailNode->item(0)->nodeValue) > 0) {
       
   518             $author['email'] = $emailNode->item(0)->nodeValue;
       
   519         }
       
   520 
       
   521         if ($nameNode->length && strlen($nameNode->item(0)->nodeValue) > 0) {
       
   522             $author['name'] = $nameNode->item(0)->nodeValue;
       
   523         }
       
   524 
       
   525         if ($uriNode->length && strlen($uriNode->item(0)->nodeValue) > 0) {
       
   526             $author['uri'] = $uriNode->item(0)->nodeValue;
       
   527         }
       
   528 
       
   529         if (empty($author)) {
       
   530             return null;
       
   531         }
       
   532         return $author;
       
   533     }
       
   534 
       
   535     /**
       
   536      *  Attempt to absolutise the URI, i.e. if a relative URI apply the
       
   537      *  xml:base value as a prefix to turn into an absolute URI.
       
   538      */
       
   539     protected function _absolutiseUri($link)
       
   540     {
       
   541         if (!Zend_Uri::check($link)) {
       
   542             if ($this->getBaseUrl() !== null) {
       
   543                 $link = $this->getBaseUrl() . $link;
       
   544                 if (!Zend_Uri::check($link)) {
       
   545                     $link = null;
       
   546                 }
       
   547             }
       
   548         }
       
   549         return $link;
       
   550     }
       
   551 
       
   552     /**
       
   553      * Register the default namespaces for the current feed format
       
   554      */
       
   555     protected function _registerNamespaces()
       
   556     {
       
   557         if ($this->getType() == Zend_Feed_Reader::TYPE_ATOM_10
       
   558             || $this->getType() == Zend_Feed_Reader::TYPE_ATOM_03
       
   559         ) {
       
   560             return; // pre-registered at Feed level
       
   561         }
       
   562         $atomDetected = $this->_getAtomType();
       
   563         switch ($atomDetected) {
       
   564             case Zend_Feed_Reader::TYPE_ATOM_03:
       
   565                 $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_03);
       
   566                 break;
       
   567             default:
       
   568                 $this->_xpath->registerNamespace('atom', Zend_Feed_Reader::NAMESPACE_ATOM_10);
       
   569                 break;
       
   570         }
       
   571     }
       
   572 
       
   573     /**
       
   574      * Detect the presence of any Atom namespaces in use
       
   575      */
       
   576     protected function _getAtomType()
       
   577     {
       
   578         $dom = $this->getDomDocument();
       
   579         $prefixAtom03 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_03);
       
   580         $prefixAtom10 = $dom->lookupPrefix(Zend_Feed_Reader::NAMESPACE_ATOM_10);
       
   581         if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_10)
       
   582         || !empty($prefixAtom10)) {
       
   583             return Zend_Feed_Reader::TYPE_ATOM_10;
       
   584         }
       
   585         if ($dom->isDefaultNamespace(Zend_Feed_Reader::NAMESPACE_ATOM_03)
       
   586         || !empty($prefixAtom03)) {
       
   587             return Zend_Feed_Reader::TYPE_ATOM_03;
       
   588         }
       
   589     }
       
   590 }