Subversion Repositories Applications.projet

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
431 mathias 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * RSS1.1 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: RSS11.php,v 1.2 2007-07-25 15:05:34 jp_milcent Exp $
21
 * @link       http://pear.php.net/package/XML_Feed_Parser/
22
 */
23
 
24
/**
25
 * This class handles RSS1.1 feeds. RSS1.1 is documented at:
26
 * http://inamidst.com/rss1.1/
27
 *
28
 * @author    James Stewart <james@jystewart.net>
29
 * @version    Release: 1.0.2
30
 * @package XML_Feed_Parser
31
 * @todo    Support for RDF:List
32
 * @todo    Ensure xml:lang is accessible to users
33
 */
34
class XML_Feed_Parser_RSS11 extends XML_Feed_Parser_Type
35
{
36
    /**
37
     * The URI of the RelaxNG schema used to (optionally) validate the feed
38
     * @var string
39
     */
40
    private $relax = 'rss11.rnc';
41
 
42
    /**
43
     * We're likely to use XPath, so let's keep it global
44
     * @var DOMXPath
45
     */
46
    protected $xpath;
47
 
48
    /**
49
     * The feed type we are parsing
50
     * @var string
51
     */
52
    public $version = 'RSS 1.0';
53
 
54
    /**
55
     * The class used to represent individual items
56
     * @var string
57
     */
58
    protected $itemClass = 'XML_Feed_Parser_RSS1Element';
59
 
60
    /**
61
     * The element containing entries
62
     * @var string
63
     */
64
    protected $itemElement = 'item';
65
 
66
    /**
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.
72
     * @var array
73
     */
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
88
        );
89
 
90
    /**
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.
94
     * @var array
95
     */
96
    protected $compatMap = array(
97
        'title' => array('title'),
98
        'link' => array('link'),
99
        'subtitle' => array('description'),
100
        'author' => array('creator'),
101
        'updated' => array('date'));
102
 
103
    /**
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
106
     * @var array
107
     */
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/');
114
 
115
    /**
116
     * Our constructor does nothing more than its parent.
117
     *
118
     * @param    DOMDocument    $xml    A DOM object representing the feed
119
     * @param    bool (optional) $string    Whether or not to validate this feed
120
     */
121
    function __construct(DOMDocument $model, $strict = false)
122
    {
123
        $this->model = $model;
124
 
125
        if ($strict) {
126
            $validate = $this->model->relaxNGValidate(self::getSchemaDir .
127
                DIRECTORY_SEPARATOR . $this->relax);
128
            if (! $validate) {
129
                throw new XML_Feed_Parser_Exception('Failed required validation');
130
            }
131
        }
132
 
133
        $this->xpath = new DOMXPath($model);
134
        foreach ($this->namespaces as $key => $value) {
135
            $this->xpath->registerNamespace($key, $value);
136
        }
137
        $this->numberEntries = $this->count('item');
138
    }
139
 
140
    /**
141
     * Attempts to identify an element by ID given by the rdf:about attribute
142
     *
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.
148
     *
149
     * @param    string    $id    any valid ID.
150
     * @return    XML_Feed_Parser_RSS1Element
151
     */
152
    function getEntryById($id)
153
    {
154
        if (isset($this->idMappings[$id])) {
155
            return $this->entries[$this->idMappings[$id]];
156
        }
157
 
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);
162
            return $entry;
163
        }
164
        return false;
165
    }
166
 
167
    /**
168
     * Get details of the image associated with the feed.
169
     *
170
     * @return  array|false an array simply containing the child elements
171
     */
172
    protected function getImage()
173
    {
174
        $images = $this->model->getElementsByTagName('image');
175
        if ($images->length > 0) {
176
            $image = $images->item(0);
177
            $details = array();
178
            if ($image->hasChildNodes()) {
179
                $details = array(
180
                    'title' => $image->getElementsByTagName('title')->item(0)->value,
181
                    'url' => $image->getElementsByTagName('url')->item(0)->value);
182
                if ($image->getElementsByTagName('link')->length > 0) {
183
                    $details['link'] =
184
                        $image->getElementsByTagName('link')->item(0)->value;
185
                }
186
            } else {
187
                $details = array('title' => false,
188
                    'link' => false,
189
                    'url' => $image->attributes->getNamedItem('resource')->nodeValue);
190
            }
191
            $details = array_merge($details,
192
                array('description' => false, 'height' => false, 'width' => false));
193
            if (! empty($details)) {
194
                return $details;
195
            }
196
        }
197
        return false;
198
    }
199
 
200
    /**
201
     * The textinput element is little used, but in the interests of
202
     * completeness we will support it.
203
     *
204
     * @return  array|false
205
     */
206
    protected function getTextInput()
207
    {
208
        $inputs = $this->model->getElementsByTagName('textinput');
209
        if ($inputs->length > 0) {
210
            $input = $inputs->item(0);
211
            $results = array();
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;
227
            }
228
            if (! empty($results)) {
229
                return $results;
230
            }
231
        }
232
        return false;
233
    }
234
 
235
    /**
236
     * Attempts to discern authorship
237
     *
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
241
     * as text.
242
     *
243
     * @return  array|false
244
     */
245
    function getAuthor()
246
    {
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;
252
            }
253
        }
254
        return false;
255
    }
256
 
257
    /**
258
     * Retrieve a link
259
     *
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.
262
     *
263
     * @return  string
264
     */
265
    function getLink($offset = 0, $attribute = 'href', $params = false)
266
    {
267
        $links = $this->model->getElementsByTagName('link');
268
        if ($links->length <= $offset) {
269
            return false;
270
        }
271
        $link = $links->item($offset);
272
        return $this->addBase($link->nodeValue, $link);
273
    }
274
}
275
 
276
?>