2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * RSS0.9 class for XML_Feed_Parser
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: RSS09.php,v 1.5 2006/07/26 21:18:46 jystewart Exp $
21 * @link http://pear.php.net/package/XML_Feed_Parser/
25 * This class handles RSS0.9 feeds.
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
32 class XML_Feed_Parser_RSS09 extends XML_Feed_Parser_Type
35 * The URI of the RelaxNG schema used to (optionally) validate the feed
41 * We're likely to use XPath, so let's keep it global
47 * The feed type we are parsing
50 public $version = 'RSS 0.9';
53 * The class used to represent individual items
56 protected $itemClass = 'XML_Feed_Parser_RSS09Element';
59 * The element containing entries
62 protected $itemElement = 'item';
65 * Here we map those elements we're not going to handle individually
66 * to the constructs they are. The optional second parameter in the array
67 * tells the parser whether to 'fall back' (not apt. at the feed level) or
68 * fail if the element is missing. If the parameter is not set, the function
69 * will simply return false and leave it to the client to decide what to do.
72 protected $map = array(
73 'title' => array('Text'),
74 'link' => array('Text'),
75 'description' => array('Text'),
76 'image' => array('Image'),
77 'textinput' => array('TextInput'));
80 * Here we map some elements to their atom equivalents. This is going to be
81 * quite tricky to pull off effectively (and some users' methods may vary)
82 * but is worth trying. The key is the atom version, the value is RSS2.
85 protected $compatMap = array(
86 'title' => array('title'),
87 'link' => array('link'),
88 'subtitle' => array('description'));
91 * We will be working with multiple namespaces and it is useful to
95 protected $namespaces = array(
96 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
99 * Our constructor does nothing more than its parent.
101 * @todo RelaxNG validation
102 * @param DOMDocument $xml A DOM object representing the feed
103 * @param bool (optional) $string Whether or not to validate this feed
105 function __construct(DOMDocument $model, $strict = false)
107 $this->model = $model;
109 $this->xpath = new DOMXPath($model);
110 foreach ($this->namespaces as $key => $value) {
111 $this->xpath->registerNamespace($key, $value);
113 $this->numberEntries = $this->count('item');
117 * Included for compatibility -- will not work with RSS 0.9
119 * This is not something that will work with RSS0.9 as it does not have
120 * clear restrictions on the global uniqueness of IDs.
122 * @param string $id any valid ID.
125 function getEntryById($id)
131 * Get details of the image associated with the feed.
133 * @return array|false an array simply containing the child elements
135 protected function getImage()
137 $images = $this->model->getElementsByTagName('image');
138 if ($images->length > 0) {
139 $image = $images->item(0);
141 if ($image->hasChildNodes()) {
143 'title' => $image->getElementsByTagName('title')->item(0)->value,
144 'link' => $image->getElementsByTagName('link')->item(0)->value,
145 'url' => $image->getElementsByTagName('url')->item(0)->value);
147 $details = array('title' => false,
149 'url' => $image->attributes->getNamedItem('resource')->nodeValue);
151 $details = array_merge($details,
152 array('description' => false, 'height' => false, 'width' => false));
153 if (! empty($details)) {
161 * The textinput element is little used, but in the interests of
162 * completeness we will support it.
164 * @return array|false
166 protected function getTextInput()
168 $inputs = $this->model->getElementsByTagName('textinput');
169 if ($inputs->length > 0) {
170 $input = $inputs->item(0);
172 $results['title'] = isset(
173 $input->getElementsByTagName('title')->item(0)->value) ?
174 $input->getElementsByTagName('title')->item(0)->value : null;
175 $results['description'] = isset(
176 $input->getElementsByTagName('description')->item(0)->value) ?
177 $input->getElementsByTagName('description')->item(0)->value : null;
178 $results['name'] = isset(
179 $input->getElementsByTagName('name')->item(0)->value) ?
180 $input->getElementsByTagName('name')->item(0)->value : null;
181 $results['link'] = isset(
182 $input->getElementsByTagName('link')->item(0)->value) ?
183 $input->getElementsByTagName('link')->item(0)->value : null;
184 if (empty($results['link']) &&
185 $input->attributes->getNamedItem('resource')) {
186 $results['link'] = $input->attributes->getNamedItem('resource')->nodeValue;
188 if (! empty($results)) {
196 * Get details of a link from the feed.
198 * In RSS1 a link is a text element but in order to ensure that we resolve
199 * URLs properly we have a special function for them.
203 function getLink($offset = 0, $attribute = 'href', $params = false)
205 $links = $this->model->getElementsByTagName('link');
206 if ($links->length <= $offset) {
209 $link = $links->item($offset);
210 return $this->addBase($link->nodeValue, $link);