Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 580 Rev 612
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
 * RSS1 class for XML_Feed_Parser
5
 * RSS1 class for XML_Feed_Parser
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: RSS1.php 304308 2010-10-11 12:05:50Z clockwerx $
20
 * @version    CVS: $Id: RSS1.php 304308 2010-10-11 12:05:50Z clockwerx $
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 RSS1.0 feeds.
25
 * This class handles RSS1.0 feeds.
26
 * 
26
 * 
27
 * @author    James Stewart <james@jystewart.net>
27
 * @author    James Stewart <james@jystewart.net>
28
 * @version    Release: @package_version@
28
 * @version    Release: @package_version@
29
 * @package XML_Feed_Parser
29
 * @package XML_Feed_Parser
30
 * @todo    Find a Relax NG URI we can use
30
 * @todo    Find a Relax NG URI we can use
31
 */
31
 */
32
class XML_Feed_Parser_RSS1 extends XML_Feed_Parser_Type
32
class XmlFeedParserRss1 extends XmlFeedParserType {
33
{
-
 
34
    /**
33
    /**
35
     * 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 
36
     * @var string
35
     * @var string
37
     */
36
     */
38
    protected $relax = 'rss10.rng';
37
    protected $relax = 'rss10.rng';
39
 
38
 
40
    /**
39
    /**
41
     * 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
42
     * @var DOMXPath
41
     * @var DOMXPath
43
     */
42
     */
44
    protected $xpath;
43
    protected $xpath;
45
 
44
 
46
    /**
45
    /**
47
     * The feed type we are parsing 
46
     * The feed type we are parsing 
48
     * @var string
47
     * @var string
49
     */
48
     */
50
    public $version = 'RSS 1.0';
49
    public $version = 'RSS 1.0';
51
 
50
 
52
    /**
51
    /**
53
     * The class used to represent individual items 
52
     * The class used to represent individual items 
54
     * @var string
53
     * @var string
55
     */
54
     */
56
    protected $itemClass = 'XML_Feed_Parser_RSS1Element';
55
    protected $itemClass = 'XmlFeedParserRss1Element';
57
    
56
    
58
    /**
57
    /**
59
     * The element containing entries 
58
     * The element containing entries 
60
     * @var string
59
     * @var string
61
     */
60
     */
62
    protected $itemElement = 'item';
61
    protected $itemElement = 'item';
63
 
62
 
64
    /**
63
    /**
65
     * 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
66
     * 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
67
     * 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
68
     * 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
69
     * 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.
70
     * @var array
69
     * @var array
71
     */
70
     */
72
    protected $map = array(
71
    protected $map = array(
73
        'title' => array('Text'),
72
        'title' => array('Text'),
74
        'link' => array('Text'),
73
        'link' => array('Text'),
75
        'description' => array('Text'),
74
        'description' => array('Text'),
76
        'image' => array('Image'),
75
        'image' => array('Image'),
77
        'textinput' => array('TextInput'),
76
        'textinput' => array('TextInput'),
78
        'updatePeriod' => array('Text'),
77
        'updatePeriod' => array('Text'),
79
        'updateFrequency' => array('Text'),
78
        'updateFrequency' => array('Text'),
80
        'updateBase' => array('Date'),
79
        'updateBase' => array('Date'),
81
        'rights' => array('Text'), # dc:rights
80
        'rights' => array('Text'), # dc:rights
82
        'description' => array('Text'), # dc:description
81
        'description' => array('Text'), # dc:description
83
        'creator' => array('Text'), # dc:creator
82
        'creator' => array('Text'), # dc:creator
84
        'publisher' => array('Text'), # dc:publisher
83
        'publisher' => array('Text'), # dc:publisher
85
        'contributor' => array('Text'), # dc:contributor
84
        'contributor' => array('Text'), # dc:contributor
86
        'date' => array('Date') # dc:contributor
85
        'date' => array('Date') # dc:contributor
87
        );
86
        );
88
 
87
 
89
    /**
88
    /**
90
     * Here we map some elements to their atom equivalents. This is going to be
89
     * Here we map some elements to their atom equivalents. This is going to be
91
     * quite tricky to pull off effectively (and some users' methods may vary)
90
     * quite tricky to pull off effectively (and some users' methods may vary)
92
     * but is worth trying. The key is the atom version, the value is RSS2.
91
     * but is worth trying. The key is the atom version, the value is RSS2.
93
     * @var array
92
     * @var array
94
     */
93
     */
95
    protected $compatMap = array(
94
    protected $compatMap = array(
96
        'title' => array('title'),
95
        'title' => array('title'),
97
        'link' => array('link'),
96
        'link' => array('link'),
98
        'subtitle' => array('description'),
97
        'subtitle' => array('description'),
99
        'author' => array('creator'),
98
        'author' => array('creator'),
100
        'updated' => array('date'));
99
        'updated' => array('date'));
101
 
100
 
102
    /**
101
    /**
103
     * We will be working with multiple namespaces and it is useful to 
102
     * We will be working with multiple namespaces and it is useful to 
104
     * keep them together 
103
     * keep them together 
105
     * @var array
104
     * @var array
106
     */
105
     */
107
    protected $namespaces = array(
106
    protected $namespaces = array(
108
        'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
107
        'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
109
        'rss' => 'http://purl.org/rss/1.0/',
108
        'rss' => 'http://purl.org/rss/1.0/',
110
        'dc' => 'http://purl.org/rss/1.0/modules/dc/',
109
        'dc' => 'http://purl.org/rss/1.0/modules/dc/',
111
        'content' => 'http://purl.org/rss/1.0/modules/content/',
110
        'content' => 'http://purl.org/rss/1.0/modules/content/',
112
        'sy' => 'http://web.resource.org/rss/1.0/modules/syndication/');
111
        'sy' => 'http://web.resource.org/rss/1.0/modules/syndication/');
113
 
112
 
114
    /**
113
    /**
115
     * Our constructor does nothing more than its parent.
114
     * Our constructor does nothing more than its parent.
116
     * 
115
     * 
117
     * @param    DOMDocument    $xml    A DOM object representing the feed
116
     * @param    DOMDocument    $xml    A DOM object representing the feed
118
     * @param    bool (optional) $string    Whether or not to validate this feed
117
     * @param    bool (optional) $string    Whether or not to validate this feed
119
     */
118
     */
120
    function __construct(DOMDocument $model, $strict = false)
119
    function __construct(DOMDocument $model, $strict = false) {
121
    {
-
 
122
        $this->model = $model;
120
        $this->model = $model;
123
        if ($strict) {
121
        if ($strict) {
124
            if (! $this->relaxNGValidate()) {
122
            if (! $this->relaxNGValidate()) {
125
                throw new XML_Feed_Parser_Exception('Failed required validation');
123
                throw new XML_Feed_Parser_Exception('Failed required validation');
126
            }
124
            }
127
        }
125
        }
128
 
126
 
129
        $this->xpath = new DOMXPath($model);
127
        $this->xpath = new DOMXPath($model);
130
        foreach ($this->namespaces as $key => $value) {
128
        foreach ($this->namespaces as $key => $value) {
131
            $this->xpath->registerNamespace($key, $value);
129
            $this->xpath->registerNamespace($key, $value);
132
        }
130
        }
133
        $this->numberEntries = $this->count('item');
131
        $this->numberEntries = $this->count('item');
134
    }
132
    }
135
 
133
 
136
    /**
134
    /**
137
     * Allows retrieval of an entry by ID where the rdf:about attribute is used
135
     * Allows retrieval of an entry by ID where the rdf:about attribute is used
138
     *
136
     *
139
     * This is not really something that will work with RSS1 as it does not have
137
     * This is not really something that will work with RSS1 as it does not have
140
     * clear restrictions on the global uniqueness of IDs. We will employ the
138
     * clear restrictions on the global uniqueness of IDs. We will employ the
141
     * _very_ hit and miss method of selecting entries based on the rdf:about
139
     * _very_ hit and miss method of selecting entries based on the rdf:about
142
     * attribute. If DOMXPath::evaluate is available, we also use that to store 
140
     * attribute. If DOMXPath::evaluate is available, we also use that to store 
143
     * a reference to the entry in the array used by getEntryByOffset so that 
141
     * a reference to the entry in the array used by getEntryByOffset so that 
144
     * method does not have to seek out the entry if it's requested that way.
142
     * method does not have to seek out the entry if it's requested that way.
145
     *
143
     *
146
     * @param    string    $id    any valid ID.
144
     * @param    string    $id    any valid ID.
147
     * @return    XML_Feed_Parser_RSS1Element
145
     * @return    XML_Feed_Parser_RSS1Element
148
     */
146
     */
149
    function getEntryById($id)
147
    function getEntryById($id) {
150
    {
-
 
151
        if (isset($this->idMappings[$id])) {
148
        if (isset($this->idMappings[$id])) {
152
            return $this->entries[$this->idMappings[$id]];
149
            return $this->entries[$this->idMappings[$id]];
153
        }
150
        }
154
 
151
 
155
        $entries = $this->xpath->query("//rss:item[@rdf:about='$id']");
152
        $entries = $this->xpath->query("//rss:item[@rdf:about='$id']");
156
        if ($entries->length > 0) {
153
        if ($entries->length > 0) {
157
            $classname = $this->itemClass;
154
            $classname = $this->itemClass;
158
            $entry = new $classname($entries->item(0), $this);
155
            $entry = new $classname($entries->item(0), $this);
159
            if (in_array('evaluate', get_class_methods($this->xpath))) {
156
            if (in_array('evaluate', get_class_methods($this->xpath))) {
160
                $offset = $this->xpath->evaluate("count(preceding-sibling::rss:item)", $entries->item(0));
157
                $offset = $this->xpath->evaluate("count(preceding-sibling::rss:item)", $entries->item(0));
161
                $this->entries[$offset] = $entry;
158
                $this->entries[$offset] = $entry;
162
            }
159
            }
163
            $this->idMappings[$id] = $entry;
160
            $this->idMappings[$id] = $entry;
164
            return $entry;
161
            return $entry;
165
        }
162
        }
166
        return false;
163
        return false;
167
    }
164
    }
168
 
165
 
169
    /**
166
    /**
170
     * Get details of the image associated with the feed.
167
     * Get details of the image associated with the feed.
171
     *
168
     *
172
     * @return  array|false an array simply containing the child elements
169
     * @return  array|false an array simply containing the child elements
173
     */
170
     */
174
    protected function getImage()
171
    protected function getImage() {
175
    {
-
 
176
        $images = $this->model->getElementsByTagName('image');
172
        $images = $this->model->getElementsByTagName('image');
177
        if ($images->length > 0) {
173
        if ($images->length > 0) {
178
            $image = $images->item(0);
174
            $image = $images->item(0);
179
            $details = array();
175
            $details = array();
180
            if ($image->hasChildNodes()) {
176
            if ($image->hasChildNodes()) {
181
                $details = array(
177
                $details = array(
182
                    'title' => $image->getElementsByTagName('title')->item(0)->value,
178
                    'title' => $image->getElementsByTagName('title')->item(0)->value,
183
                    'link' => $image->getElementsByTagName('link')->item(0)->value,
179
                    'link' => $image->getElementsByTagName('link')->item(0)->value,
184
                    'url' => $image->getElementsByTagName('url')->item(0)->value);
180
                    'url' => $image->getElementsByTagName('url')->item(0)->value);
185
            } else {
181
            } else {
186
                $details = array('title' => false,
182
                $details = array('title' => false,
187
                    'link' => false,
183
                    'link' => false,
188
                    'url' => $image->attributes->getNamedItem('resource')->nodeValue);
184
                    'url' => $image->attributes->getNamedItem('resource')->nodeValue);
189
            }
185
            }
190
            $details = array_merge($details, array('description' => false, 'height' => false, 'width' => false));
186
            $details = array_merge($details, array('description' => false, 'height' => false, 'width' => false));
191
            if (! empty($details)) {
187
            if (! empty($details)) {
192
                return $details;
188
                return $details;
193
            }
189
            }
194
        }
190
        }
195
        return false;
191
        return false;
196
    }
192
    }
197
 
193
 
198
    /**
194
    /**
199
     * The textinput element is little used, but in the interests of
195
     * The textinput element is little used, but in the interests of
200
     * completeness we will support it.
196
     * completeness we will support it.
201
     *
197
     *
202
     * @return  array|false
198
     * @return  array|false
203
     */
199
     */
204
    protected function getTextInput()
200
    protected function getTextInput() {
205
    {
-
 
206
        $inputs = $this->model->getElementsByTagName('textinput');
201
        $inputs = $this->model->getElementsByTagName('textinput');
207
        if ($inputs->length > 0) {
202
        if ($inputs->length > 0) {
208
            $input = $inputs->item(0);
203
            $input = $inputs->item(0);
209
            $results = array();
204
            $results = array();
210
            $results['title'] = isset(
205
            $results['title'] = isset(
211
                $input->getElementsByTagName('title')->item(0)->value) ? 
206
                $input->getElementsByTagName('title')->item(0)->value) ? 
212
                $input->getElementsByTagName('title')->item(0)->value : null;
207
                $input->getElementsByTagName('title')->item(0)->value : null;
213
            $results['description'] = isset(
208
            $results['description'] = isset(
214
                $input->getElementsByTagName('description')->item(0)->value) ? 
209
                $input->getElementsByTagName('description')->item(0)->value) ? 
215
                $input->getElementsByTagName('description')->item(0)->value : null;
210
                $input->getElementsByTagName('description')->item(0)->value : null;
216
            $results['name'] = isset(
211
            $results['name'] = isset(
217
                $input->getElementsByTagName('name')->item(0)->value) ? 
212
                $input->getElementsByTagName('name')->item(0)->value) ? 
218
                $input->getElementsByTagName('name')->item(0)->value : null;
213
                $input->getElementsByTagName('name')->item(0)->value : null;
219
            $results['link'] = isset(
214
            $results['link'] = isset(
220
                   $input->getElementsByTagName('link')->item(0)->value) ? 
215
                   $input->getElementsByTagName('link')->item(0)->value) ? 
221
                   $input->getElementsByTagName('link')->item(0)->value : null;
216
                   $input->getElementsByTagName('link')->item(0)->value : null;
222
            if (empty($results['link']) and 
217
            if (empty($results['link']) and 
223
                $input->attributes->getNamedItem('resource')) {
218
                $input->attributes->getNamedItem('resource')) {
224
                $results['link'] = 
219
                $results['link'] = 
225
                    $input->attributes->getNamedItem('resource')->nodeValue;
220
                    $input->attributes->getNamedItem('resource')->nodeValue;
226
            }
221
            }
227
            if (! empty($results)) {
222
            if (! empty($results)) {
228
                return $results;
223
                return $results;
229
            }
224
            }
230
        }
225
        }
231
        return false;
226
        return false;
232
    }
227
    }
233
 
228
 
234
    /**
229
    /**
235
     * Employs various techniques to identify the author
230
     * Employs various techniques to identify the author
236
     *
231
     *
237
     * Dublin Core provides the dc:creator, dc:contributor, and dc:publisher
232
     * Dublin Core provides the dc:creator, dc:contributor, and dc:publisher
238
     * elements for defining authorship in RSS1. We will try each of those in
233
     * elements for defining authorship in RSS1. We will try each of those in
239
     * turn in order to simulate the atom author element and will return it
234
     * turn in order to simulate the atom author element and will return it
240
     * as text.
235
     * as text.
241
     *
236
     *
242
     * @return  array|false
237
     * @return  array|false
243
     */
238
     */
244
    function getAuthor()
239
    function getAuthor() {
245
    {
-
 
246
        $options = array('creator', 'contributor', 'publisher');
240
        $options = array('creator', 'contributor', 'publisher');
247
        foreach ($options as $element) {
241
        foreach ($options as $element) {
248
            $test = $this->model->getElementsByTagName($element);
242
            $test = $this->model->getElementsByTagName($element);
249
            if ($test->length > 0) {
243
            if ($test->length > 0) {
250
                return $test->item(0)->value;
244
                return $test->item(0)->value;
251
            }
245
            }
252
        }
246
        }
253
        return false;
247
        return false;
254
    }
248
    }
255
    
249
    
256
    /**
250
    /**
257
     * Retrieve a link
251
     * Retrieve a link
258
     * 
252
     * 
259
     * In RSS1 a link is a text element but in order to ensure that we resolve
253
     * In RSS1 a link is a text element but in order to ensure that we resolve
260
     * URLs properly we have a special function for them.
254
     * URLs properly we have a special function for them.
261
     *
255
     *
262
     * @return  string
256
     * @return  string
263
     */
257
     */
264
    function getLink($offset = 0, $attribute = 'href', $params = false)
258
    function getLink($offset = 0, $attribute = 'href', $params = false) {
265
    {
-
 
266
        $links = $this->model->getElementsByTagName('link');
259
        $links = $this->model->getElementsByTagName('link');
267
        if ($links->length <= $offset) {
260
        if ($links->length <= $offset) {
268
            return false;
261
            return false;
269
        }
262
        }
270
        $link = $links->item($offset);
263
        $link = $links->item($offset);
271
        return $this->addBase($link->nodeValue, $link);
264
        return $this->addBase($link->nodeValue, $link);
272
    }
265
    }
273
}
266
}
274
 
-
 
275
?>
267
?>
276
268