Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
580 jpm 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * RSS1 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: RSS1.php 304308 2010-10-11 12:05:50Z clockwerx $
21
 * @link       http://pear.php.net/package/XML_Feed_Parser/
22
 */
23
 
24
/**
25
 * This class handles RSS1.0 feeds.
26
 *
27
 * @author    James Stewart <james@jystewart.net>
28
 * @version    Release: @package_version@
29
 * @package XML_Feed_Parser
30
 * @todo    Find a Relax NG URI we can use
31
 */
32
class XML_Feed_Parser_RSS1 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
    protected $relax = 'rss10.rng';
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 1.0';
51
 
52
    /**
53
     * The class used to represent individual items
54
     * @var string
55
     */
56
    protected $itemClass = 'XML_Feed_Parser_RSS1Element';
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
        'updatePeriod' => array('Text'),
79
        'updateFrequency' => array('Text'),
80
        'updateBase' => array('Date'),
81
        'rights' => array('Text'), # dc:rights
82
        'description' => array('Text'), # dc:description
83
        'creator' => array('Text'), # dc:creator
84
        'publisher' => array('Text'), # dc:publisher
85
        'contributor' => array('Text'), # dc:contributor
86
        'date' => array('Date') # dc:contributor
87
        );
88
 
89
    /**
90
     * 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)
92
     * but is worth trying. The key is the atom version, the value is RSS2.
93
     * @var array
94
     */
95
    protected $compatMap = array(
96
        'title' => array('title'),
97
        'link' => array('link'),
98
        'subtitle' => array('description'),
99
        'author' => array('creator'),
100
        'updated' => array('date'));
101
 
102
    /**
103
     * We will be working with multiple namespaces and it is useful to
104
     * keep them together
105
     * @var array
106
     */
107
    protected $namespaces = array(
108
        'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
109
        'rss' => 'http://purl.org/rss/1.0/',
110
        'dc' => 'http://purl.org/rss/1.0/modules/dc/',
111
        'content' => 'http://purl.org/rss/1.0/modules/content/',
112
        'sy' => 'http://web.resource.org/rss/1.0/modules/syndication/');
113
 
114
    /**
115
     * Our constructor does nothing more than its parent.
116
     *
117
     * @param    DOMDocument    $xml    A DOM object representing the feed
118
     * @param    bool (optional) $string    Whether or not to validate this feed
119
     */
120
    function __construct(DOMDocument $model, $strict = false)
121
    {
122
        $this->model = $model;
123
        if ($strict) {
124
            if (! $this->relaxNGValidate()) {
125
                throw new XML_Feed_Parser_Exception('Failed required validation');
126
            }
127
        }
128
 
129
        $this->xpath = new DOMXPath($model);
130
        foreach ($this->namespaces as $key => $value) {
131
            $this->xpath->registerNamespace($key, $value);
132
        }
133
        $this->numberEntries = $this->count('item');
134
    }
135
 
136
    /**
137
     * Allows retrieval of an entry by ID where the rdf:about attribute is used
138
     *
139
     * 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
141
     * _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
143
     * 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.
145
     *
146
     * @param    string    $id    any valid ID.
147
     * @return    XML_Feed_Parser_RSS1Element
148
     */
149
    function getEntryById($id)
150
    {
151
        if (isset($this->idMappings[$id])) {
152
            return $this->entries[$this->idMappings[$id]];
153
        }
154
 
155
        $entries = $this->xpath->query("//rss:item[@rdf:about='$id']");
156
        if ($entries->length > 0) {
157
            $classname = $this->itemClass;
158
            $entry = new $classname($entries->item(0), $this);
159
            if (in_array('evaluate', get_class_methods($this->xpath))) {
160
                $offset = $this->xpath->evaluate("count(preceding-sibling::rss:item)", $entries->item(0));
161
                $this->entries[$offset] = $entry;
162
            }
163
            $this->idMappings[$id] = $entry;
164
            return $entry;
165
        }
166
        return false;
167
    }
168
 
169
    /**
170
     * Get details of the image associated with the feed.
171
     *
172
     * @return  array|false an array simply containing the child elements
173
     */
174
    protected function getImage()
175
    {
176
        $images = $this->model->getElementsByTagName('image');
177
        if ($images->length > 0) {
178
            $image = $images->item(0);
179
            $details = array();
180
            if ($image->hasChildNodes()) {
181
                $details = array(
182
                    'title' => $image->getElementsByTagName('title')->item(0)->value,
183
                    'link' => $image->getElementsByTagName('link')->item(0)->value,
184
                    'url' => $image->getElementsByTagName('url')->item(0)->value);
185
            } else {
186
                $details = array('title' => false,
187
                    'link' => false,
188
                    'url' => $image->attributes->getNamedItem('resource')->nodeValue);
189
            }
190
            $details = array_merge($details, array('description' => false, 'height' => false, 'width' => false));
191
            if (! empty($details)) {
192
                return $details;
193
            }
194
        }
195
        return false;
196
    }
197
 
198
    /**
199
     * The textinput element is little used, but in the interests of
200
     * completeness we will support it.
201
     *
202
     * @return  array|false
203
     */
204
    protected function getTextInput()
205
    {
206
        $inputs = $this->model->getElementsByTagName('textinput');
207
        if ($inputs->length > 0) {
208
            $input = $inputs->item(0);
209
            $results = array();
210
            $results['title'] = isset(
211
                $input->getElementsByTagName('title')->item(0)->value) ?
212
                $input->getElementsByTagName('title')->item(0)->value : null;
213
            $results['description'] = isset(
214
                $input->getElementsByTagName('description')->item(0)->value) ?
215
                $input->getElementsByTagName('description')->item(0)->value : null;
216
            $results['name'] = isset(
217
                $input->getElementsByTagName('name')->item(0)->value) ?
218
                $input->getElementsByTagName('name')->item(0)->value : null;
219
            $results['link'] = isset(
220
                   $input->getElementsByTagName('link')->item(0)->value) ?
221
                   $input->getElementsByTagName('link')->item(0)->value : null;
222
            if (empty($results['link']) and
223
                $input->attributes->getNamedItem('resource')) {
224
                $results['link'] =
225
                    $input->attributes->getNamedItem('resource')->nodeValue;
226
            }
227
            if (! empty($results)) {
228
                return $results;
229
            }
230
        }
231
        return false;
232
    }
233
 
234
    /**
235
     * Employs various techniques to identify the author
236
     *
237
     * 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
239
     * turn in order to simulate the atom author element and will return it
240
     * as text.
241
     *
242
     * @return  array|false
243
     */
244
    function getAuthor()
245
    {
246
        $options = array('creator', 'contributor', 'publisher');
247
        foreach ($options as $element) {
248
            $test = $this->model->getElementsByTagName($element);
249
            if ($test->length > 0) {
250
                return $test->item(0)->value;
251
            }
252
        }
253
        return false;
254
    }
255
 
256
    /**
257
     * Retrieve a link
258
     *
259
     * 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.
261
     *
262
     * @return  string
263
     */
264
    function getLink($offset = 0, $attribute = 'href', $params = false)
265
    {
266
        $links = $this->model->getElementsByTagName('link');
267
        if ($links->length <= $offset) {
268
            return false;
269
        }
270
        $link = $links->item($offset);
271
        return $this->addBase($link->nodeValue, $link);
272
    }
273
}
274
 
275
?>