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

Source for file RSS09.php

Documentation is available at RSS09.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * RSS0.9 class for XML_Feed_Parser
  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: RSS09.php,v 1.5 2006/07/26 21:18:46 jystewart Exp $
  21.  * @link       http://pear.php.net/package/XML_Feed_Parser/
  22.  */
  23.  
  24. /**
  25.  * This class handles RSS0.9 feeds.
  26.  * 
  27.  * @author    James Stewart <james@jystewart.net>
  28.  * @version    Release: 1.0.3
  29.  * @package XML_Feed_Parser
  30.  * @todo    Find a Relax NG URI we can use
  31.  */
  32. {
  33.     /**
  34.      * The URI of the RelaxNG schema used to (optionally) validate the feed
  35.      * @var string 
  36.      */
  37.     private $relax '';
  38.  
  39.     /**
  40.      * We're likely to use XPath, so let's keep it global
  41.      * @var DOMXPath 
  42.      */
  43.     protected $xpath;
  44.  
  45.     /**
  46.      * The feed type we are parsing
  47.      * @var string 
  48.      */
  49.     public $version = 'RSS 0.9';
  50.  
  51.     /**
  52.      * The class used to represent individual items
  53.      * @var string 
  54.      */
  55.     protected $itemClass = 'XML_Feed_Parser_RSS09Element';
  56.     
  57.     /**
  58.      * The element containing entries
  59.      * @var string 
  60.      */
  61.     protected $itemElement = 'item';
  62.  
  63.     /**
  64.      * Here we map those elements we're not going to handle individually
  65.      * to the constructs they are. The optional second parameter in the array
  66.      * tells the parser whether to 'fall back' (not apt. at the feed level) or
  67.      * fail if the element is missing. If the parameter is not set, the function
  68.      * will simply return false and leave it to the client to decide what to do.
  69.      * @var array 
  70.      */
  71.     protected $map = array(
  72.         'title' => array('Text'),
  73.         'link' => array('Text'),
  74.         'description' => array('Text'),
  75.         'image' => array('Image'),
  76.         'textinput' => array('TextInput'));
  77.  
  78.     /**
  79.      * Here we map some elements to their atom equivalents. This is going to be
  80.      * quite tricky to pull off effectively (and some users' methods may vary)
  81.      * but is worth trying. The key is the atom version, the value is RSS2.
  82.      * @var array 
  83.      */
  84.     protected $compatMap = array(
  85.         'title' => array('title'),
  86.         'link' => array('link'),
  87.         'subtitle' => array('description'));
  88.  
  89.     /**
  90.      * We will be working with multiple namespaces and it is useful to
  91.      * keep them together
  92.      * @var array 
  93.      */
  94.     protected $namespaces = array(
  95.         'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  96.  
  97.     /**
  98.      * Our constructor does nothing more than its parent.
  99.      * 
  100.      * @todo    RelaxNG validation
  101.      * @param    DOMDocument    $xml    A DOM object representing the feed
  102.      * @param    bool (optional) $string    Whether or not to validate this feed
  103.      */
  104.     function __construct(DOMDocument $model$strict = false)
  105.     {
  106.         $this->model = $model;
  107.  
  108.         $this->xpath = new DOMXPath($model);
  109.         foreach ($this->namespaces as $key => $value{
  110.             $this->xpath->registerNamespace($key$value);
  111.         }            
  112.         $this->numberEntries = $this->count('item');
  113.     }
  114.  
  115.     /**
  116.      * Included for compatibility -- will not work with RSS 0.9
  117.      *
  118.      * This is not something that will work with RSS0.9 as it does not have
  119.      * clear restrictions on the global uniqueness of IDs.
  120.      *
  121.      * @param    string    $id    any valid ID.
  122.      * @return    false 
  123.      */
  124.     function getEntryById($id)
  125.     {
  126.         return false;        
  127.     }
  128.  
  129.     /**
  130.      * Get details of the image associated with the feed.
  131.      *
  132.      * @return  array|falsean array simply containing the child elements
  133.      */
  134.     protected function getImage()
  135.     {
  136.         $images $this->model->getElementsByTagName('image');
  137.         if ($images->length > 0{
  138.             $image $images->item(0);
  139.             $details = array();
  140.             if ($image->hasChildNodes()) {
  141.                 $details = array(
  142.                     'title' => $image->getElementsByTagName('title')->item(0)->value,
  143.                     'link' => $image->getElementsByTagName('link')->item(0)->value,
  144.                     'url' => $image->getElementsByTagName('url')->item(0)->value);
  145.             else {
  146.                 $details = array('title' => false,
  147.                     'link' => false,
  148.                     'url' => $image->attributes->getNamedItem('resource')->nodeValue);
  149.             }
  150.             $details array_merge($details
  151.                 array('description' => false'height' => false'width' => false));
  152.             if (empty($details)) {
  153.                 return $details;
  154.             }
  155.         }
  156.         return false;
  157.     }
  158.  
  159.     /**
  160.      * The textinput element is little used, but in the interests of
  161.      * completeness we will support it.
  162.      *
  163.      * @return  array|false
  164.      */
  165.     protected function getTextInput()
  166.     {
  167.         $inputs $this->model->getElementsByTagName('textinput');
  168.         if ($inputs->length > 0{
  169.             $input $inputs->item(0);
  170.             $results = array();
  171.             $results['title'= isset(
  172.                 $input->getElementsByTagName('title')->item(0)->value
  173.                 $input->getElementsByTagName('title')->item(0)->value : null;
  174.             $results['description'= isset(
  175.                 $input->getElementsByTagName('description')->item(0)->value
  176.                 $input->getElementsByTagName('description')->item(0)->value : null;
  177.             $results['name'= isset(
  178.                 $input->getElementsByTagName('name')->item(0)->value
  179.                 $input->getElementsByTagName('name')->item(0)->value : null;
  180.             $results['link'= isset(
  181.                    $input->getElementsByTagName('link')->item(0)->value
  182.                    $input->getElementsByTagName('link')->item(0)->value : null;
  183.             if (empty($results['link']&& 
  184.                 $input->attributes->getNamedItem('resource')) {
  185.                 $results['link'$input->attributes->getNamedItem('resource')->nodeValue;
  186.             }
  187.             if (empty($results)) {
  188.                 return $results;
  189.             }
  190.         }
  191.         return false;
  192.     }
  193.     
  194.     /**
  195.      * Get details of a link from the feed.
  196.      *
  197.      * In RSS1 a link is a text element but in order to ensure that we resolve
  198.      * URLs properly we have a special function for them.
  199.      *
  200.      * @return  string 
  201.      */
  202.     function getLink($offset = 0$attribute 'href'$params = false)
  203.     {
  204.         $links $this->model->getElementsByTagName('link');
  205.         if ($links->length <= $offset{
  206.             return false;
  207.         }
  208.         $link $links->item($offset);
  209.         return $this->addBase($link->nodeValue$link);
  210.     }
  211. }
  212.  
  213. ?>

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