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

Source for file RSS11.php

Documentation is available at RSS11.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * RSS1.1 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: RSS11.php,v 1.6 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.1 feeds. RSS1.1 is documented at:
  26.  * http://inamidst.com/rss1.1/
  27.  * 
  28.  * @author    James Stewart <james@jystewart.net>
  29.  * @version    Release: 1.0.3
  30.  * @package XML_Feed_Parser
  31.  * @todo    Support for RDF:List
  32.  * @todo    Ensure xml:lang is accessible to users
  33.  */
  34. {
  35.     /**
  36.      * The URI of the RelaxNG schema used to (optionally) validate the feed
  37.      * @var string 
  38.      */
  39.     private $relax 'rss11.rnc';
  40.  
  41.     /**
  42.      * We're likely to use XPath, so let's keep it global
  43.      * @var DOMXPath 
  44.      */
  45.     protected $xpath;
  46.  
  47.     /**
  48.      * The feed type we are parsing
  49.      * @var string 
  50.      */
  51.     public $version = 'RSS 1.0';
  52.  
  53.     /**
  54.      * The class used to represent individual items
  55.      * @var string 
  56.      */
  57.     protected $itemClass = 'XML_Feed_Parser_RSS1Element';
  58.     
  59.     /**
  60.      * The element containing entries
  61.      * @var string 
  62.      */
  63.     protected $itemElement = 'item';
  64.  
  65.     /**
  66.      * Here we map those elements we're not going to handle individually
  67.      * to the constructs they are. The optional second parameter in the array
  68.      * tells the parser whether to 'fall back' (not apt. at the feed level) or
  69.      * fail if the element is missing. If the parameter is not set, the function
  70.      * will simply return false and leave it to the client to decide what to do.
  71.      * @var array 
  72.      */
  73.     protected $map = array(
  74.         'title' => array('Text'),
  75.         'link' => array('Text'),
  76.         'description' => array('Text'),
  77.         'image' => array('Image'),
  78.         'updatePeriod' => array('Text'),
  79.         'updateFrequency' => array('Text'),
  80.         'updateBase' => array('Date'),
  81.         'rights' => array('Text')# dc:rights
  82.         'description' => array('Text')# dc:description
  83.         'creator' => array('Text')# dc:creator
  84.         'publisher' => array('Text')# dc:publisher
  85.         'contributor' => array('Text')# dc:contributor
  86.         'date' => array('Date'# dc:contributor
  87.         );
  88.  
  89.     /**
  90.      * Here we map some elements to their atom equivalents. This is going to be
  91.      * quite tricky to pull off effectively (and some users' methods may vary)
  92.      * but is worth trying. The key is the atom version, the value is RSS2.
  93.      * @var array 
  94.      */
  95.     protected $compatMap = array(
  96.         'title' => array('title'),
  97.         'link' => array('link'),
  98.         'subtitle' => array('description'),
  99.         'author' => array('creator'),
  100.         'updated' => array('date'));
  101.  
  102.     /**
  103.      * We will be working with multiple namespaces and it is useful to
  104.      * keep them together. We will retain support for some common RSS1.0 modules
  105.      * @var array 
  106.      */
  107.     protected $namespaces = array(
  108.         'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  109.         'rss' => 'http://purl.org/net/rss1.1#',
  110.         'dc' => 'http://purl.org/rss/1.0/modules/dc/',
  111.         'content' => 'http://purl.org/rss/1.0/modules/content/',
  112.         'sy' => 'http://web.resource.org/rss/1.0/modules/syndication/');
  113.  
  114.     /**
  115.      * Our constructor does nothing more than its parent.
  116.      * 
  117.      * @param    DOMDocument    $xml    A DOM object representing the feed
  118.      * @param    bool (optional) $string    Whether or not to validate this feed
  119.      */
  120.     function __construct(DOMDocument $model$strict = false)
  121.     {
  122.         $this->model = $model;
  123.  
  124.         if ($strict{
  125.             $validate $this->model->relaxNGValidate(self::getSchemaDir . 
  126.                 DIRECTORY_SEPARATOR . $this->relax);
  127.             if ($validate{
  128.                 throw new XML_Feed_Parser_Exception('Failed required validation');
  129.             }
  130.         }
  131.  
  132.         $this->xpath = new DOMXPath($model);
  133.         foreach ($this->namespaces as $key => $value{
  134.             $this->xpath->registerNamespace($key$value);
  135.         }            
  136.         $this->numberEntries = $this->count('item');
  137.     }
  138.  
  139.     /**
  140.      * Attempts to identify an element by ID given by the rdf:about attribute
  141.      *
  142.      * This is not really something that will work with RSS1.1 as it does not have
  143.      * clear restrictions on the global uniqueness of IDs. We will employ the
  144.      * _very_ hit and miss method of selecting entries based on the rdf:about
  145.      * attribute. Please note that this is even more hit and miss with RSS1.1 than
  146.      * with RSS1.0 since RSS1.1 does not require the rdf:about attribute for items.
  147.      *
  148.      * @param    string    $id    any valid ID.
  149.      * @return    XML_Feed_Parser_RSS1Element 
  150.      */
  151.     function getEntryById($id)
  152.     {
  153.         if (isset($this->idMappings[$id])) {
  154.             return $this->entries[$this->idMappings[$id]];
  155.         }
  156.  
  157.         $entries $this->xpath->query("//rss:item[@rdf:about='$id']");
  158.         if ($entries->length > 0{
  159.             $classname $this->itemClass;
  160.             $entry = new $classname($entries->item(0)$this);
  161.             return $entry;
  162.         }
  163.         return false;
  164.     }
  165.  
  166.     /**
  167.      * Get details of the image associated with the feed.
  168.      *
  169.      * @return  array|falsean array simply containing the child elements
  170.      */
  171.     protected function getImage()
  172.     {
  173.         $images $this->model->getElementsByTagName('image');
  174.         if ($images->length > 0{
  175.             $image $images->item(0);
  176.             $details = array();
  177.             if ($image->hasChildNodes()) {
  178.                 $details = array(
  179.                     'title' => $image->getElementsByTagName('title')->item(0)->value,
  180.                     'url' => $image->getElementsByTagName('url')->item(0)->value);
  181.                 if ($image->getElementsByTagName('link')->length > 0{
  182.                     $details['link'
  183.                         $image->getElementsByTagName('link')->item(0)->value;
  184.                 }
  185.             else {
  186.                 $details = array('title' => false,
  187.                     'link' => false,
  188.                     'url' => $image->attributes->getNamedItem('resource')->nodeValue);
  189.             }
  190.             $details array_merge($details
  191.                 array('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'$input->attributes->getNamedItem('resource')->nodeValue;
  226.             }
  227.             if (empty($results)) {
  228.                 return $results;
  229.             }
  230.         }
  231.         return false;
  232.     }
  233.  
  234.     /**
  235.      * Attempts to discern authorship
  236.      *
  237.      * Dublin Core provides the dc:creator, dc:contributor, and dc:publisher
  238.      * elements for defining authorship in RSS1. We will try each of those in
  239.      * turn in order to simulate the atom author element and will return it
  240.      * as text.
  241.      *
  242.      * @return  array|false
  243.      */
  244.     function getAuthor()
  245.     {
  246.         $options = array('creator''contributor''publisher');
  247.         foreach ($options as $element{
  248.             $test $this->model->getElementsByTagName($element);
  249.             if ($test->length > 0{
  250.                 return $test->item(0)->value;
  251.             }
  252.         }
  253.         return false;
  254.     }
  255.     
  256.     /**
  257.      * Retrieve a link
  258.      *
  259.      * In RSS1 a link is a text element but in order to ensure that we resolve
  260.      * URLs properly we have a special function for them.
  261.      *
  262.      * @return  string 
  263.      */
  264.     function getLink($offset = 0$attribute 'href'$params = false)
  265.     {
  266.         $links $this->model->getElementsByTagName('link');
  267.         if ($links->length <= $offset{
  268.             return false;
  269.         }
  270.         $link $links->item($offset);
  271.         return $this->addBase($link->nodeValue$link);
  272.     }
  273. }
  274.  
  275. ?>

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