2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Class representing entries in an RSS2 feed.
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.
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/
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.
28 * @author James Stewart <james@jystewart.net>
29 * @version Release: 1.0.3
30 * @package XML_Feed_Parser
32 class XML_Feed_Parser_RSS2Element extends XML_Feed_Parser_RSS2
35 * This will be a reference to the parent object for when we want
36 * to use a 'fallback' rule
37 * @var XML_Feed_Parser_RSS2
42 * Our specific element map
45 protected $map = array(
46 'title' => array('Text'),
47 'guid' => array('Guid'),
48 'description' => array('Text'),
49 'author' => array('Text'),
50 'comments' => array('Text'),
51 'enclosure' => array('Enclosure'),
52 'pubDate' => array('Date'),
53 'source' => array('Source'),
54 'link' => array('Text'),
55 'content' => array('Content'));
58 * Here we map some elements to their atom equivalents. This is going to be
59 * quite tricky to pull off effectively (and some users' methods may vary)
60 * but is worth trying. The key is the atom version, the value is RSS2.
63 protected $compatMap = array(
64 'id' => array('guid'),
65 'updated' => array('lastBuildDate'),
66 'published' => array('pubdate'),
67 'guidislink' => array('guid', 'ispermalink'),
68 'summary' => array('description'));
71 * Store useful information for later.
73 * @param DOMElement $element - this item as a DOM element
74 * @param XML_Feed_Parser_RSS2 $parent - the feed of which this is a member
76 function __construct(DOMElement $element, $parent, $xmlBase = '')
78 $this->model = $element;
79 $this->parent = $parent;
83 * Get the value of the guid element, if specified
85 * guid is the closest RSS2 has to atom's ID. It is usually but not always a
86 * URI. The one attribute that RSS2 can posess is 'ispermalink' which specifies
87 * whether the guid is itself dereferencable. Use of guid is not obligatory,
88 * but is advisable. To get the guid you would call $item->id() (for atom
89 * compatibility) or $item->guid(). To check if this guid is a permalink call
90 * $item->guid("ispermalink").
92 * @param string $method - the method name being called
93 * @param array $params - parameters required
94 * @return string the guid or value of ispermalink
96 protected function getGuid($method, $params)
98 $attribute = (isset($params[0]) and $params[0] == 'ispermalink') ?
100 $tag = $this->model->getElementsByTagName('guid');
101 if ($tag->length > 0) {
103 if ($tag->hasAttribute("ispermalink")) {
104 return $tag->getAttribute("ispermalink");
107 return $tag->item(0)->nodeValue;
113 * Access details of file enclosures
115 * The RSS2 spec is ambiguous as to whether an enclosure element must be
116 * unique in a given entry. For now we will assume it needn't, and allow
119 * @param string $method - the method being called
120 * @param array $parameters - we expect the first of these to be our offset
121 * @return array|false
123 protected function getEnclosure($method, $parameters)
125 $encs = $this->model->getElementsByTagName('enclosure');
126 $offset = isset($parameters[0]) ? $parameters[0] : 0;
127 if ($encs->length > $offset) {
129 if (! $encs->item($offset)->hasAttribute('url')) {
132 $attrs = $encs->item($offset)->attributes;
134 'url' => $attrs->getNamedItem('url')->value,
135 'length' => $attrs->getNamedItem('length')->value,
136 'type' => $attrs->getNamedItem('type')->value);
137 } catch (Exception $e) {
145 * Get the entry source if specified
147 * source is an optional sub-element of item. Like atom:source it tells
148 * us about where the entry came from (eg. if it's been copied from another
149 * feed). It is not a rich source of metadata in the same way as atom:source
150 * and while it would be good to maintain compatibility by returning an
151 * XML_Feed_Parser_RSS2 element, it makes a lot more sense to return an array.
153 * @return array|false
155 protected function getSource()
157 $get = $this->model->getElementsByTagName('source');
159 $source = $get->item(0);
161 'content' => $source->nodeValue);
162 foreach ($source->attributes as $attribute) {
163 $array[$attribute->name] = $attribute->value;