XML_Feed_Parser
[ class tree: XML_Feed_Parser ] [ index: XML_Feed_Parser ] [ all elements ]

Source for file RSS2Element.php

Documentation is available at RSS2Element.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Class representing entries in an RSS2 feed.
  6.  *
  7.  * PHP versions 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category   XML
  16.  * @package    XML_Feed_Parser
  17.  * @author     James Stewart <james@jystewart.net>
  18.  * @copyright  2005 James Stewart <james@jystewart.net>
  19.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU LGPL 2.1
  20.  * @version    CVS: $Id: RSS2Element.php,v 1.11 2006/07/26 21:18:47 jystewart Exp $
  21.  * @link       http://pear.php.net/package/XML_Feed_Parser/
  22.  */
  23.  
  24. /**
  25.  * This class provides support for RSS 2.0 entries. It will usually be
  26.  * called by XML_Feed_Parser_RSS2 with which it shares many methods.
  27.  *
  28.  * @author    James Stewart <james@jystewart.net>
  29.  * @version    Release: 1.0.3
  30.  * @package XML_Feed_Parser
  31.  */
  32. {
  33.     /**
  34.      * This will be a reference to the parent object for when we want
  35.      * to use a 'fallback' rule
  36.      * @var XML_Feed_Parser_RSS2 
  37.      */
  38.     protected $parent;
  39.  
  40.     /**
  41.      * Our specific element map
  42.      * @var array 
  43.      */
  44.     protected $map = array(
  45.         'title' => array('Text'),
  46.         'guid' => array('Guid'),
  47.         'description' => array('Text'),
  48.         'author' => array('Text'),
  49.         'comments' => array('Text'),
  50.         'enclosure' => array('Enclosure'),
  51.         'pubDate' => array('Date'),
  52.         'source' => array('Source'),
  53.         'link' => array('Text'),
  54.         'content' => array('Content'));
  55.  
  56.     /**
  57.      * Here we map some elements to their atom equivalents. This is going to be
  58.      * quite tricky to pull off effectively (and some users' methods may vary)
  59.      * but is worth trying. The key is the atom version, the value is RSS2.
  60.      * @var array 
  61.      */
  62.     protected $compatMap = array(
  63.         'id' => array('guid'),
  64.         'updated' => array('lastBuildDate'),
  65.         'published' => array('pubdate'),
  66.         'guidislink' => array('guid''ispermalink'),
  67.         'summary' => array('description'));
  68.  
  69.     /**
  70.      * Store useful information for later.
  71.      *
  72.      * @param   DOMElement  $element - this item as a DOM element
  73.      * @param   XML_Feed_Parser_RSS2    $parent - the feed of which this is a member
  74.      */
  75.     function __construct(DOMElement $element$parent$xmlBase '')
  76.     {
  77.         $this->model = $element;
  78.         $this->parent = $parent;
  79.     }
  80.  
  81.     /**
  82.      * Get the value of the guid element, if specified
  83.      *
  84.      * guid is the closest RSS2 has to atom's ID. It is usually but not always a
  85.      * URI. The one attribute that RSS2 can posess is 'ispermalink' which specifies
  86.      * whether the guid is itself dereferencable. Use of guid is not obligatory,
  87.      * but is advisable. To get the guid you would call $item->id() (for atom
  88.      * compatibility) or $item->guid(). To check if this guid is a permalink call
  89.      * $item->guid("ispermalink").
  90.      *
  91.      * @param   string  $method - the method name being called
  92.      * @param   array   $params - parameters required
  93.      * @return  string  the guid or value of ispermalink
  94.      */
  95.     protected function getGuid($method$params)
  96.     {
  97.         $attribute (isset($params[0]and $params[0== 'ispermalink'
  98.             true : false;
  99.         $tag $this->model->getElementsByTagName('guid');
  100.         if ($tag->length > 0{
  101.             if ($attribute{
  102.                 if ($tag->hasAttribute("ispermalink")) {
  103.                     return $tag->getAttribute("ispermalink");
  104.                 }
  105.             }
  106.             return $tag->item(0)->nodeValue;
  107.         }
  108.         return false;
  109.     }
  110.  
  111.     /**
  112.      * Access details of file enclosures
  113.      *
  114.      * The RSS2 spec is ambiguous as to whether an enclosure element must be
  115.      * unique in a given entry. For now we will assume it needn't, and allow
  116.      * for an offset.
  117.      *
  118.      * @param   string $method - the method being called
  119.      * @param   array   $parameters - we expect the first of these to be our offset
  120.      * @return  array|false
  121.      */
  122.     protected function getEnclosure($method$parameters)
  123.     {
  124.         $encs $this->model->getElementsByTagName('enclosure');
  125.         $offset = isset($parameters[0]$parameters[0: 0;
  126.         if ($encs->length > $offset{
  127.             try {
  128.                 if ($encs->item($offset)->hasAttribute('url')) {
  129.                     return false;
  130.                 }
  131.                 $attrs $encs->item($offset)->attributes;
  132.                 return array(
  133.                     'url' => $attrs->getNamedItem('url')->value,
  134.                     'length' => $attrs->getNamedItem('length')->value,
  135.                     'type' => $attrs->getNamedItem('type')->value);
  136.             catch (Exception $e{
  137.                 return false;
  138.             }
  139.         }
  140.         return false;
  141.     }
  142.  
  143.     /**
  144.      * Get the entry source if specified
  145.      *
  146.      * source is an optional sub-element of item. Like atom:source it tells
  147.      * us about where the entry came from (eg. if it's been copied from another
  148.      * feed). It is not a rich source of metadata in the same way as atom:source
  149.      * and while it would be good to maintain compatibility by returning an
  150.      * XML_Feed_Parser_RSS2 element, it makes a lot more sense to return an array.
  151.      *
  152.      * @return array|false
  153.      */
  154.     protected function getSource()
  155.     {
  156.         $get $this->model->getElementsByTagName('source');
  157.         if ($get->length{
  158.             $source $get->item(0);
  159.             $array = array(
  160.                 'content' => $source->nodeValue);
  161.             foreach ($source->attributes as $attribute{
  162.                 $array[$attribute->name$attribute->value;
  163.             }
  164.             return $array;
  165.         }
  166.         return false;
  167.     }
  168. }
  169.  
  170. ?>

Documentation generated on Wed, 19 Nov 2008 08:30:11 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.