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
 */
612 jpm 34
class XmlFeedParserRss11 extends XmlFeedParserType {
580 jpm 35
    /**
36
     * The URI of the RelaxNG schema used to (optionally) validate the feed
37
     * @var string
38
     */
39
    protected $relax = 'rss11.rng';
40
 
41
    /**
42
     * We're likely to use XPath, so let's keep it global
43
     * @var DOMXPath
44
     */
45
    protected $xpath;
46
 
47
    /**
48
     * The feed type we are parsing
49
     * @var string
50
     */
51
    public $version = 'RSS 1.0';
52
 
53
    /**
54
     * The class used to represent individual items
55
     * @var string
56
     */
612 jpm 57
    protected $itemClass = 'XmlFeedParserRss11Element';
580 jpm 58
 
59
    /**
60
     * The element containing entries
61
     * @var string
62
     */
63
    protected $itemElement = 'item';
64
 
65
    /**
66
     * Here we map those elements we're not going to handle individually
67
     * to the constructs they are. The optional second parameter in the array
68
     * tells the parser whether to 'fall back' (not apt. at the feed level) or
69
     * fail if the element is missing. If the parameter is not set, the function
70
     * will simply return false and leave it to the client to decide what to do.
71
     * @var array
72
     */
73
    protected $map = array(
74
        'title' => array('Text'),
75
        'link' => array('Text'),
76
        'description' => array('Text'),
77
        'image' => array('Image'),
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. We will retain support for some common RSS1.0 modules
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/net/rss1.1#',
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
     */
612 jpm 120
    function __construct(DOMDocument $model, $strict = false) {
580 jpm 121
        $this->model = $model;
122
 
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
     * Attempts to identify an element by ID given by the rdf:about attribute
138
     *
139
     * This is not really something that will work with RSS1.1 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. Please note that this is even more hit and miss with RSS1.1 than
143
     * with RSS1.0 since RSS1.1 does not require the rdf:about attribute for items.
144
     *
145
     * @param    string    $id    any valid ID.
146
     * @return    XML_Feed_Parser_RSS1Element
147
     */
612 jpm 148
    function getEntryById($id) {
580 jpm 149
        if (isset($this->idMappings[$id])) {
150
            return $this->entries[$this->idMappings[$id]];
151
        }
152
 
153
        $entries = $this->xpath->query("//rss:item[@rdf:about='$id']");
154
        if ($entries->length > 0) {
155
            $classname = $this->itemClass;
156
            $entry = new $classname($entries->item(0), $this);
157
            return $entry;
158
        }
159
        return false;
160
    }
161
 
162
    /**
163
     * Get details of the image associated with the feed.
164
     *
165
     * @return  array|false an array simply containing the child elements
166
     */
612 jpm 167
    protected function getImage() {
580 jpm 168
        $images = $this->model->getElementsByTagName('image');
169
        if ($images->length > 0) {
170
            $image = $images->item(0);
171
            $details = array();
172
            if ($image->hasChildNodes()) {
173
                $details = array(
174
                    'title' => $image->getElementsByTagName('title')->item(0)->value,
175
                    'url' => $image->getElementsByTagName('url')->item(0)->value);
176
                if ($image->getElementsByTagName('link')->length > 0) {
177
                    $details['link'] =
178
                        $image->getElementsByTagName('link')->item(0)->value;
179
                }
180
            } else {
181
                $details = array('title' => false,
182
                    'link' => false,
183
                    'url' => $image->attributes->getNamedItem('resource')->nodeValue);
184
            }
185
            $details = array_merge($details,
186
                array('description' => false, 'height' => false, 'width' => false));
187
            if (! empty($details)) {
188
                return $details;
189
            }
190
        }
191
        return false;
192
    }
193
 
194
    /**
195
     * The textinput element is little used, but in the interests of
196
     * completeness we will support it.
197
     *
198
     * @return  array|false
199
     */
612 jpm 200
    protected function getTextInput() {
580 jpm 201
        $inputs = $this->model->getElementsByTagName('textinput');
202
        if ($inputs->length > 0) {
203
            $input = $inputs->item(0);
204
            $results = array();
205
            $results['title'] = isset(
206
                $input->getElementsByTagName('title')->item(0)->value) ?
207
                $input->getElementsByTagName('title')->item(0)->value : null;
208
            $results['description'] = isset(
209
                $input->getElementsByTagName('description')->item(0)->value) ?
210
                $input->getElementsByTagName('description')->item(0)->value : null;
211
            $results['name'] = isset(
212
                $input->getElementsByTagName('name')->item(0)->value) ?
213
                $input->getElementsByTagName('name')->item(0)->value : null;
214
            $results['link'] = isset(
215
                   $input->getElementsByTagName('link')->item(0)->value) ?
216
                   $input->getElementsByTagName('link')->item(0)->value : null;
217
            if (empty($results['link']) and
218
                $input->attributes->getNamedItem('resource')) {
219
                $results['link'] = $input->attributes->getNamedItem('resource')->nodeValue;
220
            }
221
            if (! empty($results)) {
222
                return $results;
223
            }
224
        }
225
        return false;
226
    }
227
 
228
    /**
229
     * Attempts to discern authorship
230
     *
231
     * Dublin Core provides the dc:creator, dc:contributor, and dc:publisher
232
     * elements for defining authorship in RSS1. We will try each of those in
233
     * turn in order to simulate the atom author element and will return it
234
     * as text.
235
     *
236
     * @return  array|false
237
     */
612 jpm 238
    function getAuthor() {
580 jpm 239
        $options = array('creator', 'contributor', 'publisher');
240
        foreach ($options as $element) {
241
            $test = $this->model->getElementsByTagName($element);
242
            if ($test->length > 0) {
243
                return $test->item(0)->value;
244
            }
245
        }
246
        return false;
247
    }
248
 
249
    /**
250
     * Retrieve a link
251
     *
252
     * In RSS1 a link is a text element but in order to ensure that we resolve
253
     * URLs properly we have a special function for them.
254
     *
255
     * @return  string
256
     */
612 jpm 257
    function getLink($offset = 0, $attribute = 'href', $params = false) {
580 jpm 258
        $links = $this->model->getElementsByTagName('link');
259
        if ($links->length <= $offset) {
260
            return false;
261
        }
262
        $link = $links->item($offset);
263
        return $this->addBase($link->nodeValue, $link);
264
    }
265
}
612 jpm 266
?>