2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * RSS1.1 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: RSS11.php,v 1.6 2006/07/27 13:52:05 jystewart Exp $
21 * @link http://pear.php.net/package/XML_Feed_Parser/
25 * This class handles RSS1.1 feeds. RSS1.1 is documented at:
26 * http://inamidst.com/rss1.1/
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
34 class XML_Feed_Parser_RSS11 extends XML_Feed_Parser_Type
37 * The URI of the RelaxNG schema used to (optionally) validate the feed
40 private $relax = 'rss11.rnc';
43 * We're likely to use XPath, so let's keep it global
49 * The feed type we are parsing
52 public $version = 'RSS 1.0';
55 * The class used to represent individual items
58 protected $itemClass = 'XML_Feed_Parser_RSS1Element';
61 * The element containing entries
64 protected $itemElement = 'item';
67 * Here we map those elements we're not going to handle individually
68 * to the constructs they are. The optional second parameter in the array
69 * tells the parser whether to 'fall back' (not apt. at the feed level) or
70 * fail if the element is missing. If the parameter is not set, the function
71 * will simply return false and leave it to the client to decide what to do.
74 protected $map = array(
75 'title' => array('Text'),
76 'link' => array('Text'),
77 'description' => array('Text'),
78 'image' => array('Image'),
79 'updatePeriod' => array('Text'),
80 'updateFrequency' => array('Text'),
81 'updateBase' => array('Date'),
82 'rights' => array('Text'), # dc:rights
83 'description' => array('Text'), # dc:description
84 'creator' => array('Text'), # dc:creator
85 'publisher' => array('Text'), # dc:publisher
86 'contributor' => array('Text'), # dc:contributor
87 'date' => array('Date') # dc:contributor
91 * Here we map some elements to their atom equivalents. This is going to be
92 * quite tricky to pull off effectively (and some users' methods may vary)
93 * but is worth trying. The key is the atom version, the value is RSS2.
96 protected $compatMap = array(
97 'title' => array('title'),
98 'link' => array('link'),
99 'subtitle' => array('description'),
100 'author' => array('creator'),
101 'updated' => array('date'));
104 * We will be working with multiple namespaces and it is useful to
105 * keep them together. We will retain support for some common RSS1.0 modules
108 protected $namespaces = array(
109 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
110 'rss' => 'http://purl.org/net/rss1.1#',
111 'dc' => 'http://purl.org/rss/1.0/modules/dc/',
112 'content' => 'http://purl.org/rss/1.0/modules/content/',
113 'sy' => 'http://web.resource.org/rss/1.0/modules/syndication/');
116 * Our constructor does nothing more than its parent.
118 * @param DOMDocument $xml A DOM object representing the feed
119 * @param bool (optional) $string Whether or not to validate this feed
121 function __construct(DOMDocument $model, $strict = false)
123 $this->model = $model;
126 $validate = $this->model->relaxNGValidate(self::getSchemaDir .
127 DIRECTORY_SEPARATOR . $this->relax);
129 throw new XML_Feed_Parser_Exception('Failed required validation');
133 $this->xpath = new DOMXPath($model);
134 foreach ($this->namespaces as $key => $value) {
135 $this->xpath->registerNamespace($key, $value);
137 $this->numberEntries = $this->count('item');
141 * Attempts to identify an element by ID given by the rdf:about attribute
143 * This is not really something that will work with RSS1.1 as it does not have
144 * clear restrictions on the global uniqueness of IDs. We will employ the
145 * _very_ hit and miss method of selecting entries based on the rdf:about
146 * attribute. Please note that this is even more hit and miss with RSS1.1 than
147 * with RSS1.0 since RSS1.1 does not require the rdf:about attribute for items.
149 * @param string $id any valid ID.
150 * @return XML_Feed_Parser_RSS1Element
152 function getEntryById($id)
154 if (isset($this->idMappings[$id])) {
155 return $this->entries[$this->idMappings[$id]];
158 $entries = $this->xpath->query("//rss:item[@rdf:about='$id']");
159 if ($entries->length > 0) {
160 $classname = $this->itemClass;
161 $entry = new $classname($entries->item(0), $this);
168 * Get details of the image associated with the feed.
170 * @return array|false an array simply containing the child elements
172 protected function getImage()
174 $images = $this->model->getElementsByTagName('image');
175 if ($images->length > 0) {
176 $image = $images->item(0);
178 if ($image->hasChildNodes()) {
180 'title' => $image->getElementsByTagName('title')->item(0)->value,
181 'url' => $image->getElementsByTagName('url')->item(0)->value);
182 if ($image->getElementsByTagName('link')->length > 0) {
184 $image->getElementsByTagName('link')->item(0)->value;
187 $details = array('title' => false,
189 'url' => $image->attributes->getNamedItem('resource')->nodeValue);
191 $details = array_merge($details,
192 array('description' => false, 'height' => false, 'width' => false));
193 if (! empty($details)) {
201 * The textinput element is little used, but in the interests of
202 * completeness we will support it.
204 * @return array|false
206 protected function getTextInput()
208 $inputs = $this->model->getElementsByTagName('textinput');
209 if ($inputs->length > 0) {
210 $input = $inputs->item(0);
212 $results['title'] = isset(
213 $input->getElementsByTagName('title')->item(0)->value) ?
214 $input->getElementsByTagName('title')->item(0)->value : null;
215 $results['description'] = isset(
216 $input->getElementsByTagName('description')->item(0)->value) ?
217 $input->getElementsByTagName('description')->item(0)->value : null;
218 $results['name'] = isset(
219 $input->getElementsByTagName('name')->item(0)->value) ?
220 $input->getElementsByTagName('name')->item(0)->value : null;
221 $results['link'] = isset(
222 $input->getElementsByTagName('link')->item(0)->value) ?
223 $input->getElementsByTagName('link')->item(0)->value : null;
224 if (empty($results['link']) and
225 $input->attributes->getNamedItem('resource')) {
226 $results['link'] = $input->attributes->getNamedItem('resource')->nodeValue;
228 if (! empty($results)) {
236 * Attempts to discern authorship
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
243 * @return array|false
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;
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.
265 function getLink($offset = 0, $attribute = 'href', $params = false)
267 $links = $this->model->getElementsByTagName('link');
268 if ($links->length <= $offset) {
271 $link = $links->item($offset);
272 return $this->addBase($link->nodeValue, $link);