Source for file RSS2.php
Documentation is available at RSS2.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Class representing feed-level data for an RSS2 feed
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @package XML_Feed_Parser
* @author James Stewart <james@jystewart.net>
* @copyright 2005 James Stewart <james@jystewart.net>
* @license http://www.gnu.org/copyleft/lesser.html GNU LGPL 2.1
* @version CVS: $Id: RSS2.php,v 1.11 2006/07/27 13:52:05 jystewart Exp $
* @link http://pear.php.net/package/XML_Feed_Parser/
* This class handles RSS2 feeds.
* @author James Stewart <james@jystewart.net>
* @version Release: 1.0.2
* @package XML_Feed_Parser
* The URI of the RelaxNG schema used to (optionally) validate the feed
private $relax = 'rss20.rnc';
* We're likely to use XPath, so let's keep it global
* The feed type we are parsing
* The class used to represent individual items
protected $itemClass = 'XML_Feed_Parser_RSS2Element';
* The element containing entries
* Here we map those elements we're not going to handle individually
* to the constructs they are. The optional second parameter in the array
* tells the parser whether to 'fall back' (not apt. at the feed level) or
* fail if the element is missing. If the parameter is not set, the function
* will simply return false and leave it to the client to decide what to do.
'pubDate' => array ('Date'),
'lastBuildDate' => array ('Date'),
'title' => array ('Text'),
'description' => array ('Text'),
'language' => array ('Text'),
'copyright' => array ('Text'),
'managingEditor' => array ('Text'),
'webMaster' => array ('Text'),
'category' => array ('Text'),
'generator' => array ('Text'),
'image' => array ('Image'),
'skipDays' => array ('skipDays'),
'skipHours' => array ('skipHours'));
* Here we map some elements to their atom equivalents. This is going to be
* quite tricky to pull off effectively (and some users' methods may vary)
* but is worth trying. The key is the atom version, the value is RSS2.
'title' => array ('title'),
'rights' => array ('copyright'),
'updated' => array ('lastBuildDate'),
'subtitle' => array ('description'),
'date' => array ('pubDate'),
'author' => array ('managingEditor'));
'dc' => 'http://purl.org/rss/1.0/modules/dc/',
'content' => 'http://purl.org/rss/1.0/modules/content/');
* Our constructor does nothing more than its parent.
* @param DOMDocument $xml A DOM object representing the feed
* @param bool (optional) $string Whether or not to validate this feed
function __construct(DOMDocument $model, $strict = false )
if (! $this->model->relaxNGValidate ($this->relax)) {
$this->xpath->registerNamespace ($key, $value);
* Retrieves an entry by ID, if the ID is specified with the guid element
* This is not really something that will work with RSS2 as it does not have
* clear restrictions on the global uniqueness of IDs. But we can emulate
* it by allowing access based on the 'guid' element. If DOMXPath::evaluate
* is available, we also use that to store a reference to the entry in the array
* used by getEntryByOffset so that method does not have to seek out the entry
* if it's requested that way.
* @param string $id any valid ID.
* @return XML_Feed_Parser_RSS2Element
if (isset ($this->idMappings[$id])) {
return $this->entries[$this->idMappings[$id]];
$entries = $this->xpath->query (" //item[guid='$id']" );
if ($entries->length > 0 ) {
$entry = new $this->itemElement ($entries->item (0 ), $this);
$offset = $this->xpath->evaluate ("count(preceding-sibling::item)", $entries->item (0 ));
$this->idMappings[$id] = $entry;
* Get a category from the element
* The category element is a simple text construct which can occur any number
* of times. We allow access by offset or access to an array of results.
* @param string $call for compatibility with our overloading
* @param array $arguments - arg 0 is the offset, arg 1 is whether to return as array
* @return string|array|false
$categories = $this->model->getElementsByTagName ('category');
$offset = empty ($arguments[0 ]) ? 0 : $arguments[0 ];
$array = empty ($arguments[1 ]) ? false : true;
if ($categories->length <= $offset) {
foreach ($categories as $category) {
return $categories->item ($offset)->nodeValue;
* Get details of the image associated with the feed.
* @return array|falsean array simply containing the child elements
$images = $this->model->getElementsByTagName ('image');
if ($images->length > 0 ) {
$image = $images->item (0 );
$desc = $image->getElementsByTagName ('description');
$description = $desc->length ? $desc->item (0 )->nodeValue : false;
$heigh = $image->getElementsByTagName ('height');
$height = $heigh->length ? $heigh->item (0 )->nodeValue : false;
$widt = $image->getElementsByTagName ('width');
$width = $widt->length ? $widt->item (0 )->nodeValue : false;
'title' => $image->getElementsByTagName ('title')->item (0 )->nodeValue ,
'link' => $image->getElementsByTagName ('link')->item (0 )->nodeValue ,
'url' => $image->getElementsByTagName ('url')->item (0 )->nodeValue ,
'description' => $description,
* The textinput element is little used, but in the interests of
$inputs = $this->model->getElementsByTagName ('input');
if ($inputs->length > 0 ) {
$input = $inputs->item (0 );
'title' => $input->getElementsByTagName ('title')->item (0 )->value ,
$input->getElementsByTagName ('description')->item (0 )->value ,
'name' => $input->getElementsByTagName ('name')->item (0 )->value ,
'link' => $input->getElementsByTagName ('link')->item (0 )->value );
* Utility function for getSkipDays and getSkipHours
* This is a general function used by both getSkipDays and getSkipHours. It simply
* returns an array of the values of the children of the appropriate tag.
* @param string $tagName The tag name (getSkipDays or getSkipHours)
$hours = $this->model->getElementsByTagName ($tagName);
if ($hours->length == 0 ) {
foreach($hours->item (0 )->childNodes as $hour) {
if ($hour instanceof DOMElement ) {
* Retrieve skipHours data
* The skiphours element provides a list of hours on which this feed should
* not be checked. We return an array of those hours (integers, 24 hour clock)
* The skipdays element provides a list of days on which this feed should
* not be checked. We return an array of those days.
* Return content of the little-used 'cloud' element
* The cloud element is rarely used. It is designed to provide some details
* of a location to update the feed.
* @return array an array of the attributes of the element
$cloud = $this->model->getElementsByTagName ('cloud');
if ($cloud->length == 0 ) {
foreach ($cloud->item (0 )->attributes as $attribute) {
$cloudData[$attribute->name ] = $attribute->value;
* In RSS2 a link is a text element but in order to ensure that we resolve
* URLs properly we have a special function for them. We maintain the
* parameter used by the atom getLink method, though we only use the offset
* @param int $offset The position of the link within the feed. Starts from 0
* @param string $attribute The attribute of the link element required
* @param array $params An array of other parameters. Not used.
function getLink($offset, $attribute = 'href', $params = array ())
$links = $this->model->getElementsByTagName ('link');
if ($links->length <= $offset) {
$link = $links->item ($offset);
return $this->addBase($link->nodeValue , $link);
Documentation generated on Mon, 09 Apr 2007 11:02:06 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.
|