source: branches/dev/html/test/adachi/LLReader/Lib/XML/Feed/Parser/RSS09.php @ 14612

Revision 14612, 7.1 KB checked in by adati, 17 years ago (diff)
Line 
1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * RSS0.9 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: RSS09.php,v 1.5 2006/07/26 21:18:46 jystewart Exp $
21 * @link       http://pear.php.net/package/XML_Feed_Parser/
22 */
23
24/**
25 * This class handles RSS0.9 feeds.
26 *
27 * @author    James Stewart <james@jystewart.net>
28 * @version    Release: 1.0.2
29 * @package XML_Feed_Parser
30 * @todo    Find a Relax NG URI we can use
31 */
32class XML_Feed_Parser_RSS09 extends XML_Feed_Parser_Type
33{
34    /**
35     * The URI of the RelaxNG schema used to (optionally) validate the feed
36     * @var string
37     */
38    private $relax = '';
39
40    /**
41     * We're likely to use XPath, so let's keep it global
42     * @var DOMXPath
43     */
44    protected $xpath;
45
46    /**
47     * The feed type we are parsing
48     * @var string
49     */
50    public $version = 'RSS 0.9';
51
52    /**
53     * The class used to represent individual items
54     * @var string
55     */
56    protected $itemClass = 'XML_Feed_Parser_RSS09Element';
57   
58    /**
59     * The element containing entries
60     * @var string
61     */
62    protected $itemElement = 'item';
63
64    /**
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.
70     * @var array
71     */
72    protected $map = array(
73        'title' => array('Text'),
74        'link' => array('Text'),
75        'description' => array('Text'),
76        'image' => array('Image'),
77        'textinput' => array('TextInput'));
78
79    /**
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.
83     * @var array
84     */
85    protected $compatMap = array(
86        'title' => array('title'),
87        'link' => array('link'),
88        'subtitle' => array('description'));
89
90    /**
91     * We will be working with multiple namespaces and it is useful to
92     * keep them together
93     * @var array
94     */
95    protected $namespaces = array(
96        'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
97
98    /**
99     * Our constructor does nothing more than its parent.
100     *
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
104     */
105    function __construct(DOMDocument $model, $strict = false)
106    {
107        $this->model = $model;
108
109        $this->xpath = new DOMXPath($model);
110        foreach ($this->namespaces as $key => $value) {
111            $this->xpath->registerNamespace($key, $value);
112        }           
113        $this->numberEntries = $this->count('item');
114    }
115
116    /**
117     * Included for compatibility -- will not work with RSS 0.9
118     *
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.
121     *
122     * @param    string    $id    any valid ID.
123     * @return    false
124     */
125    function getEntryById($id)
126    {
127        return false;       
128    }
129
130    /**
131     * Get details of the image associated with the feed.
132     *
133     * @return  array|false an array simply containing the child elements
134     */
135    protected function getImage()
136    {
137        $images = $this->model->getElementsByTagName('image');
138        if ($images->length > 0) {
139            $image = $images->item(0);
140            $details = array();
141            if ($image->hasChildNodes()) {
142                $details = array(
143                    'title' => $image->getElementsByTagName('title')->item(0)->value,
144                    'link' => $image->getElementsByTagName('link')->item(0)->value,
145                    'url' => $image->getElementsByTagName('url')->item(0)->value);
146            } else {
147                $details = array('title' => false,
148                    'link' => false,
149                    'url' => $image->attributes->getNamedItem('resource')->nodeValue);
150            }
151            $details = array_merge($details,
152                array('description' => false, 'height' => false, 'width' => false));
153            if (! empty($details)) {
154                return $details;
155            }
156        }
157        return false;
158    }
159
160    /**
161     * The textinput element is little used, but in the interests of
162     * completeness we will support it.
163     *
164     * @return  array|false
165     */
166    protected function getTextInput()
167    {
168        $inputs = $this->model->getElementsByTagName('textinput');
169        if ($inputs->length > 0) {
170            $input = $inputs->item(0);
171            $results = array();
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;
187            }
188            if (! empty($results)) {
189                return $results;
190            }
191        }
192        return false;
193    }
194   
195    /**
196     * Get details of a link from the feed.
197     *
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.
200     *
201     * @return  string
202     */
203    function getLink($offset = 0, $attribute = 'href', $params = false)
204    {
205        $links = $this->model->getElementsByTagName('link');
206        if ($links->length <= $offset) {
207            return false;
208        }
209        $link = $links->item($offset);
210        return $this->addBase($link->nodeValue, $link);
211    }
212}
213
214?>
Note: See TracBrowser for help on using the repository browser.