Subversion Repositories Applications.papyrus

Rev

Rev 1527 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1527 Rev 2150
1
<?php
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
3
 
4
/**
4
/**
5
 * Class representing feed-level data for an RSS2 feed
5
 * Class representing feed-level data for an RSS2 feed
6
 *
6
 *
7
 * PHP versions 5
7
 * PHP versions 5
8
 *
8
 *
9
 * LICENSE: This source file is subject to version 3.0 of the PHP license
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:
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
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
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.
13
 * send a note to license@php.net so we can mail you a copy immediately.
14
 *
14
 *
15
 * @category   XML
15
 * @category   XML
16
 * @package    XML_Feed_Parser
16
 * @package    XML_Feed_Parser
17
 * @author     James Stewart <james@jystewart.net>
17
 * @author     James Stewart <james@jystewart.net>
18
 * @copyright  2005 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
19
 * @license    http://www.gnu.org/copyleft/lesser.html  GNU LGPL 2.1
20
 * @version    CVS: $Id: RSS2.php,v 1.2 2007-07-25 15:05:34 jp_milcent Exp $
20
 * @version    CVS: $Id: RSS2.php,v 1.2 2007-07-25 15:05:34 jp_milcent Exp $
21
 * @link       http://pear.php.net/package/XML_Feed_Parser/
21
 * @link       http://pear.php.net/package/XML_Feed_Parser/
22
 */
22
 */
23
 
23
 
24
/**
24
/**
25
 * This class handles RSS2 feeds.
25
 * This class handles RSS2 feeds.
26
 * 
26
 * 
27
 * @author    James Stewart <james@jystewart.net>
27
 * @author    James Stewart <james@jystewart.net>
28
 * @version    Release: 1.0.2
28
 * @version    Release: 1.0.2
29
 * @package XML_Feed_Parser
29
 * @package XML_Feed_Parser
30
 */
30
 */
31
class XML_Feed_Parser_RSS2 extends XML_Feed_Parser_Type
31
class XML_Feed_Parser_RSS2 extends XML_Feed_Parser_Type
32
{
32
{
33
    /**
33
    /**
34
     * The URI of the RelaxNG schema used to (optionally) validate the feed
34
     * The URI of the RelaxNG schema used to (optionally) validate the feed
35
     * @var string
35
     * @var string
36
     */
36
     */
37
    private $relax = 'rss20.rnc';
37
    private $relax = 'rss20.rnc';
38
 
38
 
39
    /**
39
    /**
40
     * We're likely to use XPath, so let's keep it global
40
     * We're likely to use XPath, so let's keep it global
41
     * @var DOMXPath
41
     * @var DOMXPath
42
     */
42
     */
43
    protected $xpath;
43
    protected $xpath;
44
 
44
 
45
    /**
45
    /**
46
     * The feed type we are parsing
46
     * The feed type we are parsing
47
     * @var string
47
     * @var string
48
     */
48
     */
49
    public $version = 'RSS 2.0';
49
    public $version = 'RSS 2.0';
50
 
50
 
51
    /**
51
    /**
52
     * The class used to represent individual items
52
     * The class used to represent individual items
53
     * @var string
53
     * @var string
54
     */     
54
     */     
55
    protected $itemClass = 'XML_Feed_Parser_RSS2Element';
55
    protected $itemClass = 'XML_Feed_Parser_RSS2Element';
56
    
56
    
57
    /**
57
    /**
58
     * The element containing entries 
58
     * The element containing entries 
59
     * @var string
59
     * @var string
60
     */
60
     */
61
    protected $itemElement = 'item';
61
    protected $itemElement = 'item';
62
 
62
 
63
    /**
63
    /**
64
     * Here we map those elements we're not going to handle individually
64
     * Here we map those elements we're not going to handle individually
65
     * to the constructs they are. The optional second parameter in the array
65
     * to the constructs they are. The optional second parameter in the array
66
     * tells the parser whether to 'fall back' (not apt. at the feed level) or
66
     * tells the parser whether to 'fall back' (not apt. at the feed level) or
67
     * fail if the element is missing. If the parameter is not set, the function
67
     * fail if the element is missing. If the parameter is not set, the function
68
     * will simply return false and leave it to the client to decide what to do.
68
     * will simply return false and leave it to the client to decide what to do.
69
     * @var array
69
     * @var array
70
     */
70
     */
71
    protected $map = array(
71
    protected $map = array(
72
        'ttl' => array('Text'),
72
        'ttl' => array('Text'),
73
        'pubDate' => array('Date'),
73
        'pubDate' => array('Date'),
74
        'lastBuildDate' => array('Date'),
74
        'lastBuildDate' => array('Date'),
75
        'title' => array('Text'),
75
        'title' => array('Text'),
76
        'link' => array('Link'),
76
        'link' => array('Link'),
77
        'description' => array('Text'),
77
        'description' => array('Text'),
78
        'language' => array('Text'),
78
        'language' => array('Text'),
79
        'copyright' => array('Text'),
79
        'copyright' => array('Text'),
80
        'managingEditor' => array('Text'),
80
        'managingEditor' => array('Text'),
81
        'webMaster' => array('Text'),
81
        'webMaster' => array('Text'),
82
        'category' => array('Text'),
82
        'category' => array('Text'),
83
        'generator' => array('Text'),
83
        'generator' => array('Text'),
84
        'docs' => array('Text'),
84
        'docs' => array('Text'),
85
        'ttl' => array('Text'),
85
        'ttl' => array('Text'),
86
        'image' => array('Image'),
86
        'image' => array('Image'),
87
        'skipDays' => array('skipDays'),
87
        'skipDays' => array('skipDays'),
88
        'skipHours' => array('skipHours'));
88
        'skipHours' => array('skipHours'));
89
 
89
 
90
    /**
90
    /**
91
     * Here we map some elements to their atom equivalents. This is going to be
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)
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.
93
     * but is worth trying. The key is the atom version, the value is RSS2.
94
     * @var array
94
     * @var array
95
     */
95
     */
96
    protected $compatMap = array(
96
    protected $compatMap = array(
97
        'title' => array('title'),
97
        'title' => array('title'),
98
        'rights' => array('copyright'),
98
        'rights' => array('copyright'),
99
        'updated' => array('lastBuildDate'),
99
        'updated' => array('lastBuildDate'),
100
        'subtitle' => array('description'),
100
        'subtitle' => array('description'),
101
        'date' => array('pubDate'),
101
        'date' => array('pubDate'),
102
        'author' => array('managingEditor'));
102
        'author' => array('managingEditor'));
103
 
103
 
104
    protected $namespaces = array(
104
    protected $namespaces = array(
105
        'dc' => 'http://purl.org/rss/1.0/modules/dc/',
105
        'dc' => 'http://purl.org/rss/1.0/modules/dc/',
106
        'content' => 'http://purl.org/rss/1.0/modules/content/');
106
        'content' => 'http://purl.org/rss/1.0/modules/content/');
107
 
107
 
108
    /**
108
    /**
109
     * Our constructor does nothing more than its parent.
109
     * Our constructor does nothing more than its parent.
110
     * 
110
     * 
111
     * @param    DOMDocument    $xml    A DOM object representing the feed
111
     * @param    DOMDocument    $xml    A DOM object representing the feed
112
     * @param    bool (optional) $string    Whether or not to validate this feed
112
     * @param    bool (optional) $string    Whether or not to validate this feed
113
     */
113
     */
114
    function __construct(DOMDocument $model, $strict = false)
114
    function __construct(DOMDocument $model, $strict = false)
115
    {
115
    {
116
        $this->model = $model;
116
        $this->model = $model;
117
 
117
 
118
        if ($strict) {
118
        if ($strict) {
119
            if (! $this->model->relaxNGValidate($this->relax)) {
119
            if (! $this->model->relaxNGValidate($this->relax)) {
120
                throw new XML_Feed_Parser_Exception('Failed required validation');
120
                throw new XML_Feed_Parser_Exception('Failed required validation');
121
            }
121
            }
122
        }
122
        }
123
 
123
 
124
        $this->xpath = new DOMXPath($this->model);
124
        $this->xpath = new DOMXPath($this->model);
125
        foreach ($this->namespaces as $key => $value) {
125
        foreach ($this->namespaces as $key => $value) {
126
            $this->xpath->registerNamespace($key, $value);
126
            $this->xpath->registerNamespace($key, $value);
127
        }
127
        }
128
        $this->numberEntries = $this->count('item');
128
        $this->numberEntries = $this->count('item');
129
    }
129
    }
130
 
130
 
131
    /**
131
    /**
132
     * Retrieves an entry by ID, if the ID is specified with the guid element
132
     * Retrieves an entry by ID, if the ID is specified with the guid element
133
     *
133
     *
134
     * This is not really something that will work with RSS2 as it does not have
134
     * This is not really something that will work with RSS2 as it does not have
135
     * clear restrictions on the global uniqueness of IDs. But we can emulate
135
     * clear restrictions on the global uniqueness of IDs. But we can emulate
136
     * it by allowing access based on the 'guid' element. If DOMXPath::evaluate
136
     * it by allowing access based on the 'guid' element. If DOMXPath::evaluate
137
     * is available, we also use that to store a reference to the entry in the array
137
     * is available, we also use that to store a reference to the entry in the array
138
     * used by getEntryByOffset so that method does not have to seek out the entry
138
     * used by getEntryByOffset so that method does not have to seek out the entry
139
     * if it's requested that way.
139
     * if it's requested that way.
140
     *
140
     *
141
     * @param    string    $id    any valid ID.
141
     * @param    string    $id    any valid ID.
142
     * @return    XML_Feed_Parser_RSS2Element
142
     * @return    XML_Feed_Parser_RSS2Element
143
     */
143
     */
144
    function getEntryById($id)
144
    function getEntryById($id)
145
    {
145
    {
146
        if (isset($this->idMappings[$id])) {
146
        if (isset($this->idMappings[$id])) {
147
            return $this->entries[$this->idMappings[$id]];
147
            return $this->entries[$this->idMappings[$id]];
148
        }
148
        }
149
 
149
 
150
        $entries = $this->xpath->query("//item[guid='$id']");
150
        $entries = $this->xpath->query("//item[guid='$id']");
151
        if ($entries->length > 0) {
151
        if ($entries->length > 0) {
152
            $entry = new $this->itemElement($entries->item(0), $this);
152
            $entry = new $this->itemElement($entries->item(0), $this);
153
            if (in_array('evaluate', get_class_methods($this->xpath))) {
153
            if (in_array('evaluate', get_class_methods($this->xpath))) {
154
                $offset = $this->xpath->evaluate("count(preceding-sibling::item)", $entries->item(0));
154
                $offset = $this->xpath->evaluate("count(preceding-sibling::item)", $entries->item(0));
155
                $this->entries[$offset] = $entry;
155
                $this->entries[$offset] = $entry;
156
            }
156
            }
157
            $this->idMappings[$id] = $entry;
157
            $this->idMappings[$id] = $entry;
158
            return $entry;
158
            return $entry;
159
        }        
159
        }        
160
    }
160
    }
161
 
161
 
162
    /**
162
    /**
163
     * Get a category from the element
163
     * Get a category from the element
164
     *
164
     *
165
     * The category element is a simple text construct which can occur any number
165
     * The category element is a simple text construct which can occur any number
166
     * of times. We allow access by offset or access to an array of results.
166
     * of times. We allow access by offset or access to an array of results.
167
     *
167
     *
168
     * @param    string    $call    for compatibility with our overloading
168
     * @param    string    $call    for compatibility with our overloading
169
     * @param   array $arguments - arg 0 is the offset, arg 1 is whether to return as array
169
     * @param   array $arguments - arg 0 is the offset, arg 1 is whether to return as array
170
     * @return  string|array|false
170
     * @return  string|array|false
171
     */
171
     */
172
    function getCategory($call, $arguments = array())
172
    function getCategory($call, $arguments = array())
173
    {
173
    {
174
        $categories = $this->model->getElementsByTagName('category');
174
        $categories = $this->model->getElementsByTagName('category');
175
        $offset = empty($arguments[0]) ? 0 : $arguments[0];
175
        $offset = empty($arguments[0]) ? 0 : $arguments[0];
176
        $array = empty($arguments[1]) ? false : true;
176
        $array = empty($arguments[1]) ? false : true;
177
        if ($categories->length <= $offset) {
177
        if ($categories->length <= $offset) {
178
            return false;
178
            return false;
179
        }
179
        }
180
        if ($array) {
180
        if ($array) {
181
            $list = array();
181
            $list = array();
182
            foreach ($categories as $category) {
182
            foreach ($categories as $category) {
183
                array_push($list, $category->nodeValue);
183
                array_push($list, $category->nodeValue);
184
            }
184
            }
185
            return $list;
185
            return $list;
186
        }
186
        }
187
        return $categories->item($offset)->nodeValue;
187
        return $categories->item($offset)->nodeValue;
188
    }
188
    }
189
 
189
 
190
    /**
190
    /**
191
     * Get details of the image associated with the feed.
191
     * Get details of the image associated with the feed.
192
     *
192
     *
193
     * @return  array|false an array simply containing the child elements
193
     * @return  array|false an array simply containing the child elements
194
     */
194
     */
195
    protected function getImage()
195
    protected function getImage()
196
    {
196
    {
197
        $images = $this->model->getElementsByTagName('image');
197
        $images = $this->model->getElementsByTagName('image');
198
        if ($images->length > 0) {
198
        if ($images->length > 0) {
199
            $image = $images->item(0);
199
            $image = $images->item(0);
200
            $desc = $image->getElementsByTagName('description');
200
            $desc = $image->getElementsByTagName('description');
201
            $description = $desc->length ? $desc->item(0)->nodeValue : false;
201
            $description = $desc->length ? $desc->item(0)->nodeValue : false;
202
            $heigh = $image->getElementsByTagName('height'); 
202
            $heigh = $image->getElementsByTagName('height'); 
203
            $height = $heigh->length ? $heigh->item(0)->nodeValue : false;
203
            $height = $heigh->length ? $heigh->item(0)->nodeValue : false;
204
            $widt = $image->getElementsByTagName('width'); 
204
            $widt = $image->getElementsByTagName('width'); 
205
            $width = $widt->length ? $widt->item(0)->nodeValue : false;
205
            $width = $widt->length ? $widt->item(0)->nodeValue : false;
206
            return array(
206
            return array(
207
                'title' => $image->getElementsByTagName('title')->item(0)->nodeValue,
207
                'title' => $image->getElementsByTagName('title')->item(0)->nodeValue,
208
                'link' => $image->getElementsByTagName('link')->item(0)->nodeValue,
208
                'link' => $image->getElementsByTagName('link')->item(0)->nodeValue,
209
                'url' => $image->getElementsByTagName('url')->item(0)->nodeValue,
209
                'url' => $image->getElementsByTagName('url')->item(0)->nodeValue,
210
                'description' => $description,
210
                'description' => $description,
211
                'height' => $height,
211
                'height' => $height,
212
                'width' => $width);
212
                'width' => $width);
213
        }
213
        }
214
        return false;
214
        return false;
215
    }
215
    }
216
 
216
 
217
    /**
217
    /**
218
     * The textinput element is little used, but in the interests of
218
     * The textinput element is little used, but in the interests of
219
     * completeness...
219
     * completeness...
220
     *
220
     *
221
     * @return  array|false
221
     * @return  array|false
222
     */
222
     */
223
    function getTextInput()
223
    function getTextInput()
224
    {
224
    {
225
        $inputs = $this->model->getElementsByTagName('input');
225
        $inputs = $this->model->getElementsByTagName('input');
226
        if ($inputs->length > 0) {
226
        if ($inputs->length > 0) {
227
            $input = $inputs->item(0);
227
            $input = $inputs->item(0);
228
            return array(
228
            return array(
229
                'title' => $input->getElementsByTagName('title')->item(0)->value,
229
                'title' => $input->getElementsByTagName('title')->item(0)->value,
230
                'description' => 
230
                'description' => 
231
                    $input->getElementsByTagName('description')->item(0)->value,
231
                    $input->getElementsByTagName('description')->item(0)->value,
232
                'name' => $input->getElementsByTagName('name')->item(0)->value,
232
                'name' => $input->getElementsByTagName('name')->item(0)->value,
233
                'link' => $input->getElementsByTagName('link')->item(0)->value);
233
                'link' => $input->getElementsByTagName('link')->item(0)->value,
-
 
234
                'category' => $input->getElementsByTagName('category')->item(0)->value);
234
        }
235
        }
235
        return false;
236
        return false;
236
    }
237
    }
237
 
238
 
238
    /**
239
    /**
239
     * Utility function for getSkipDays and getSkipHours
240
     * Utility function for getSkipDays and getSkipHours
240
     *
241
     *
241
     * This is a general function used by both getSkipDays and getSkipHours. It simply
242
     * This is a general function used by both getSkipDays and getSkipHours. It simply
242
     * returns an array of the values of the children of the appropriate tag.
243
     * returns an array of the values of the children of the appropriate tag.
243
     *
244
     *
244
     * @param   string      $tagName    The tag name (getSkipDays or getSkipHours)
245
     * @param   string      $tagName    The tag name (getSkipDays or getSkipHours)
245
     * @return  array|false
246
     * @return  array|false
246
     */
247
     */
247
    protected function getSkips($tagName)
248
    protected function getSkips($tagName)
248
    {
249
    {
249
        $hours = $this->model->getElementsByTagName($tagName);
250
        $hours = $this->model->getElementsByTagName($tagName);
250
        if ($hours->length == 0) {
251
        if ($hours->length == 0) {
251
            return false;
252
            return false;
252
        }
253
        }
253
        $skipHours = array();
254
        $skipHours = array();
254
        foreach($hours->item(0)->childNodes as $hour) {
255
        foreach($hours->item(0)->childNodes as $hour) {
255
            if ($hour instanceof DOMElement) {
256
            if ($hour instanceof DOMElement) {
256
                array_push($skipHours, $hour->nodeValue);
257
                array_push($skipHours, $hour->nodeValue);
257
            }
258
            }
258
        }
259
        }
259
        return $skipHours;
260
        return $skipHours;
260
    }
261
    }
261
 
262
 
262
    /**
263
    /**
263
     * Retrieve skipHours data
264
     * Retrieve skipHours data
264
     *
265
     *
265
     * The skiphours element provides a list of hours on which this feed should
266
     * The skiphours element provides a list of hours on which this feed should
266
     * not be checked. We return an array of those hours (integers, 24 hour clock)
267
     * not be checked. We return an array of those hours (integers, 24 hour clock)
267
     *
268
     *
268
     * @return  array
269
     * @return  array
269
     */    
270
     */    
270
    function getSkipHours()
271
    function getSkipHours()
271
    {
272
    {
272
        return $this->getSkips('skipHours');
273
        return $this->getSkips('skipHours');
273
    }
274
    }
274
 
275
 
275
    /**
276
    /**
276
     * Retrieve skipDays data
277
     * Retrieve skipDays data
277
     *
278
     *
278
     * The skipdays element provides a list of days on which this feed should
279
     * The skipdays element provides a list of days on which this feed should
279
     * not be checked. We return an array of those days.
280
     * not be checked. We return an array of those days.
280
     *
281
     *
281
     * @return  array
282
     * @return  array
282
     */
283
     */
283
    function getSkipDays()
284
    function getSkipDays()
284
    {
285
    {
285
        return $this->getSkips('skipDays');
286
        return $this->getSkips('skipDays');
286
    }
287
    }
287
 
288
 
288
    /**
289
    /**
289
     * Return content of the little-used 'cloud' element
290
     * Return content of the little-used 'cloud' element
290
     *
291
     *
291
     * The cloud element is rarely used. It is designed to provide some details
292
     * The cloud element is rarely used. It is designed to provide some details
292
     * of a location to update the feed.
293
     * of a location to update the feed.
293
     *
294
     *
294
     * @return  array   an array of the attributes of the element
295
     * @return  array   an array of the attributes of the element
295
     */
296
     */
296
    function getCloud()
297
    function getCloud()
297
    {
298
    {
298
        $cloud = $this->model->getElementsByTagName('cloud');
299
        $cloud = $this->model->getElementsByTagName('cloud');
299
        if ($cloud->length == 0) {
300
        if ($cloud->length == 0) {
300
            return false;
301
            return false;
301
        }
302
        }
302
        $cloudData = array();
303
        $cloudData = array();
303
        foreach ($cloud->item(0)->attributes as $attribute) {
304
        foreach ($cloud->item(0)->attributes as $attribute) {
304
            $cloudData[$attribute->name] = $attribute->value;
305
            $cloudData[$attribute->name] = $attribute->value;
305
        }
306
        }
306
        return $cloudData;
307
        return $cloudData;
307
    }
308
    }
308
    
309
    
309
    /**
310
    /**
310
     * Get link URL
311
     * Get link URL
311
     *
312
     *
312
     * In RSS2 a link is a text element but in order to ensure that we resolve
313
     * In RSS2 a link is a text element but in order to ensure that we resolve
313
     * URLs properly we have a special function for them. We maintain the 
314
     * URLs properly we have a special function for them. We maintain the 
314
     * parameter used by the atom getLink method, though we only use the offset
315
     * parameter used by the atom getLink method, though we only use the offset
315
     * parameter.
316
     * parameter.
316
     *
317
     *
317
     * @param   int     $offset The position of the link within the feed. Starts from 0
318
     * @param   int     $offset The position of the link within the feed. Starts from 0
318
     * @param   string  $attribute  The attribute of the link element required
319
     * @param   string  $attribute  The attribute of the link element required
319
     * @param   array   $params An array of other parameters. Not used.
320
     * @param   array   $params An array of other parameters. Not used.
320
     * @return  string
321
     * @return  string
321
     */
322
     */
322
    function getLink($offset, $attribute = 'href', $params = array())
323
    function getLink($offset, $attribute = 'href', $params = array())
323
    {
324
    {
324
        $links = $this->model->getElementsByTagName('link');
325
        $links = $this->model->getElementsByTagName('link');
325
 
326
 
326
        if ($links->length <= $offset) {
327
        if ($links->length <= $offset) {
327
            return false;
328
            return false;
328
        }
329
        }
329
        $link = $links->item($offset);
330
        $link = $links->item($offset);
330
        return $this->addBase($link->nodeValue, $link);
331
        return $this->addBase($link->nodeValue, $link);
331
    }
332
    }
332
}
333
}
333
 
334
 
334
?>
335
?>