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

Source for file RSS1.php

Documentation is available at RSS1.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * RSS1 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: RSS1.php,v 1.10 2006/07/27 13:52:05 jystewart Exp $
  21.  * @link       http://pear.php.net/package/XML_Feed_Parser/
  22.  */
  23.  
  24. /**
  25.  * This class handles RSS1.0 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 'rss10.rnc';
  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 1.0';
  50.  
  51.     /**
  52.      * The class used to represent individual items
  53.      * @var string 
  54.      */
  55.     protected $itemClass = 'XML_Feed_Parser_RSS1Element';
  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.         'updatePeriod' => array('Text'),
  78.         'updateFrequency' => array('Text'),
  79.         'updateBase' => array('Date'),
  80.         'rights' => array('Text')# dc:rights
  81.         'description' => array('Text')# dc:description
  82.         'creator' => array('Text')# dc:creator
  83.         'publisher' => array('Text')# dc:publisher
  84.         'contributor' => array('Text')# dc:contributor
  85.         'date' => array('Date'# dc:contributor
  86.         );
  87.  
  88.     /**
  89.      * Here we map some elements to their atom equivalents. This is going to be
  90.      * quite tricky to pull off effectively (and some users' methods may vary)
  91.      * but is worth trying. The key is the atom version, the value is RSS2.
  92.      * @var array 
  93.      */
  94.     protected $compatMap = array(
  95.         'title' => array('title'),
  96.         'link' => array('link'),
  97.         'subtitle' => array('description'),
  98.         'author' => array('creator'),
  99.         'updated' => array('date'));
  100.  
  101.     /**
  102.      * We will be working with multiple namespaces and it is useful to
  103.      * keep them together
  104.      * @var array 
  105.      */
  106.     protected $namespaces = array(
  107.         'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  108.         'rss' => 'http://purl.org/rss/1.0/',
  109.         'dc' => 'http://purl.org/rss/1.0/modules/dc/',
  110.         'content' => 'http://purl.org/rss/1.0/modules/content/',
  111.         'sy' => 'http://web.resource.org/rss/1.0/modules/syndication/');
  112.  
  113.     /**
  114.      * Our constructor does nothing more than its parent.
  115.      * 
  116.      * @param    DOMDocument    $xml    A DOM object representing the feed
  117.      * @param    bool (optional) $string    Whether or not to validate this feed
  118.      */
  119.     function __construct(DOMDocument $model$strict = false)
  120.     {
  121.         $this->model = $model;
  122.         if ($strict{
  123.             $validate $this->model->relaxNGValidate(self::getSchemaDir . 
  124.                 DIRECTORY_SEPARATOR . $this->relax);
  125.             if ($validate{
  126.                 throw new XML_Feed_Parser_Exception('Failed required validation');
  127.             }
  128.         }
  129.  
  130.         $this->xpath = new DOMXPath($model);
  131.         foreach ($this->namespaces as $key => $value{
  132.             $this->xpath->registerNamespace($key$value);
  133.         }
  134.         $this->numberEntries = $this->count('item');
  135.     }
  136.  
  137.     /**
  138.      * Allows retrieval of an entry by ID where the rdf:about attribute is used
  139.      *
  140.      * This is not really something that will work with RSS1 as it does not have
  141.      * clear restrictions on the global uniqueness of IDs. We will employ the
  142.      * _very_ hit and miss method of selecting entries based on the rdf:about
  143.      * attribute. If DOMXPath::evaluate is available, we also use that to store
  144.      * a reference to the entry in the array used by getEntryByOffset so that
  145.      * method does not have to seek out the entry if it's requested that way.
  146.      *
  147.      * @param    string    $id    any valid ID.
  148.      * @return    XML_Feed_Parser_RSS1Element 
  149.      */
  150.     function getEntryById($id)
  151.     {
  152.         if (isset($this->idMappings[$id])) {
  153.             return $this->entries[$this->idMappings[$id]];
  154.         }
  155.  
  156.         $entries $this->xpath->query("//rss:item[@rdf:about='$id']");
  157.         if ($entries->length > 0{
  158.             $classname $this->itemClass;
  159.             $entry = new $classname($entries->item(0)$this);
  160.             if (in_array('evaluate'get_class_methods($this->xpath))) {
  161.                 $offset $this->xpath->evaluate("count(preceding-sibling::rss:item)"$entries->item(0));
  162.                 $this->entries[$offset$entry;
  163.             }
  164.             $this->idMappings[$id$entry;
  165.             return $entry;
  166.         }
  167.         return false;
  168.     }
  169.  
  170.     /**
  171.      * Get details of the image associated with the feed.
  172.      *
  173.      * @return  array|falsean array simply containing the child elements
  174.      */
  175.     protected function getImage()
  176.     {
  177.         $images $this->model->getElementsByTagName('image');
  178.         if ($images->length > 0{
  179.             $image $images->item(0);
  180.             $details = array();
  181.             if ($image->hasChildNodes()) {
  182.                 $details = array(
  183.                     'title' => $image->getElementsByTagName('title')->item(0)->value,
  184.                     'link' => $image->getElementsByTagName('link')->item(0)->value,
  185.                     'url' => $image->getElementsByTagName('url')->item(0)->value);
  186.             else {
  187.                 $details = array('title' => false,
  188.                     'link' => false,
  189.                     'url' => $image->attributes->getNamedItem('resource')->nodeValue);
  190.             }
  191.             $details array_merge($detailsarray('description' => false'height' => false'width' => false));
  192.             if (empty($details)) {
  193.                 return $details;
  194.             }
  195.         }
  196.         return false;
  197.     }
  198.  
  199.     /**
  200.      * The textinput element is little used, but in the interests of
  201.      * completeness we will support it.
  202.      *
  203.      * @return  array|false
  204.      */
  205.     protected function getTextInput()
  206.     {
  207.         $inputs $this->model->getElementsByTagName('textinput');
  208.         if ($inputs->length > 0{
  209.             $input $inputs->item(0);
  210.             $results = array();
  211.             $results['title'= isset(
  212.                 $input->getElementsByTagName('title')->item(0)->value
  213.                 $input->getElementsByTagName('title')->item(0)->value : null;
  214.             $results['description'= isset(
  215.                 $input->getElementsByTagName('description')->item(0)->value
  216.                 $input->getElementsByTagName('description')->item(0)->value : null;
  217.             $results['name'= isset(
  218.                 $input->getElementsByTagName('name')->item(0)->value
  219.                 $input->getElementsByTagName('name')->item(0)->value : null;
  220.             $results['link'= isset(
  221.                    $input->getElementsByTagName('link')->item(0)->value
  222.                    $input->getElementsByTagName('link')->item(0)->value : null;
  223.             if (empty($results['link']and 
  224.                 $input->attributes->getNamedItem('resource')) {
  225.                 $results['link'
  226.                     $input->attributes->getNamedItem('resource')->nodeValue;
  227.             }
  228.             if (empty($results)) {
  229.                 return $results;
  230.             }
  231.         }
  232.         return false;
  233.     }
  234.  
  235.     /**
  236.      * Employs various techniques to identify the author
  237.      *
  238.      * Dublin Core provides the dc:creator, dc:contributor, and dc:publisher
  239.      * elements for defining authorship in RSS1. We will try each of those in
  240.      * turn in order to simulate the atom author element and will return it
  241.      * as text.
  242.      *
  243.      * @return  array|false
  244.      */
  245.     function getAuthor()
  246.     {
  247.         $options = array('creator''contributor''publisher');
  248.         foreach ($options as $element{
  249.             $test $this->model->getElementsByTagName($element);
  250.             if ($test->length > 0{
  251.                 return $test->item(0)->value;
  252.             }
  253.         }
  254.         return false;
  255.     }
  256.     
  257.     /**
  258.      * Retrieve a link
  259.      * 
  260.      * In RSS1 a link is a text element but in order to ensure that we resolve
  261.      * URLs properly we have a special function for them.
  262.      *
  263.      * @return  string 
  264.      */
  265.     function getLink($offset = 0$attribute 'href'$params = false)
  266.     {
  267.         $links $this->model->getElementsByTagName('link');
  268.         if ($links->length <= $offset{
  269.             return false;
  270.         }
  271.         $link $links->item($offset);
  272.         return $this->addBase($link->nodeValue$link);
  273.     }
  274. }
  275.  
  276. ?>

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