Subversion Repositories eFlore/Applications.cel

Rev

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