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.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 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.1 feeds. RSS1.1 is documented at:
26
 * http://inamidst.com/rss1.1/
27
 *
28
 * @author    James Stewart <james@jystewart.net>
29
 * @version    Release: @package_version@
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
    protected $relax = 'rss11.rng';
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
            if (! $this->relaxNGValidate()) {
127
                throw new XML_Feed_Parser_Exception('Failed required validation');
128
            }
129
        }
130
 
131
        $this->xpath = new DOMXPath($model);
132
        foreach ($this->namespaces as $key => $value) {
133
            $this->xpath->registerNamespace($key, $value);
134
        }
135
        $this->numberEntries = $this->count('item');
136
    }
137
 
138
    /**
139
     * Attempts to identify an element by ID given by the rdf:about attribute
140
     *
141
     * This is not really something that will work with RSS1.1 as it does not have
142
     * clear restrictions on the global uniqueness of IDs. We will employ the
143
     * _very_ hit and miss method of selecting entries based on the rdf:about
144
     * attribute. Please note that this is even more hit and miss with RSS1.1 than
145
     * with RSS1.0 since RSS1.1 does not require the rdf:about attribute for items.
146
     *
147
     * @param    string    $id    any valid ID.
148
     * @return    XML_Feed_Parser_RSS1Element
149
     */
150
    function getEntryById($id)
151
    {
152
        if (isset($this->idMappings[$id])) {
153
            return $this->entries[$this->idMappings[$id]];
154
        }
155
 
156
        $entries = $this->xpath->query("//rss:item[@rdf:about='$id']");
157
        if ($entries->length > 0) {
158
            $classname = $this->itemClass;
159
            $entry = new $classname($entries->item(0), $this);
160
            return $entry;
161
        }
162
        return false;
163
    }
164
 
165
    /**
166
     * Get details of the image associated with the feed.
167
     *
168
     * @return  array|false an array simply containing the child elements
169
     */
170
    protected function getImage()
171
    {
172
        $images = $this->model->getElementsByTagName('image');
173
        if ($images->length > 0) {
174
            $image = $images->item(0);
175
            $details = array();
176
            if ($image->hasChildNodes()) {
177
                $details = array(
178
                    'title' => $image->getElementsByTagName('title')->item(0)->value,
179
                    'url' => $image->getElementsByTagName('url')->item(0)->value);
180
                if ($image->getElementsByTagName('link')->length > 0) {
181
                    $details['link'] =
182
                        $image->getElementsByTagName('link')->item(0)->value;
183
                }
184
            } else {
185
                $details = array('title' => false,
186
                    'link' => false,
187
                    'url' => $image->attributes->getNamedItem('resource')->nodeValue);
188
            }
189
            $details = array_merge($details,
190
                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'] = $input->attributes->getNamedItem('resource')->nodeValue;
225
            }
226
            if (! empty($results)) {
227
                return $results;
228
            }
229
        }
230
        return false;
231
    }
232
 
233
    /**
234
     * Attempts to discern authorship
235
     *
236
     * Dublin Core provides the dc:creator, dc:contributor, and dc:publisher
237
     * elements for defining authorship in RSS1. We will try each of those in
238
     * turn in order to simulate the atom author element and will return it
239
     * as text.
240
     *
241
     * @return  array|false
242
     */
243
    function getAuthor()
244
    {
245
        $options = array('creator', 'contributor', 'publisher');
246
        foreach ($options as $element) {
247
            $test = $this->model->getElementsByTagName($element);
248
            if ($test->length > 0) {
249
                return $test->item(0)->value;
250
            }
251
        }
252
        return false;
253
    }
254
 
255
    /**
256
     * Retrieve a link
257
     *
258
     * In RSS1 a link is a text element but in order to ensure that we resolve
259
     * URLs properly we have a special function for them.
260
     *
261
     * @return  string
262
     */
263
    function getLink($offset = 0, $attribute = 'href', $params = false)
264
    {
265
        $links = $this->model->getElementsByTagName('link');
266
        if ($links->length <= $offset) {
267
            return false;
268
        }
269
        $link = $links->item($offset);
270
        return $this->addBase($link->nodeValue, $link);
271
    }
272
}
273
 
274
?>