Subversion Repositories Applications.papyrus

Rev

Rev 1987 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1527 jp_milcent 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,v 1.2 2007-07-25 15:05:34 jp_milcent Exp $
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: 1.0.2
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
    private $relax = 'rss10.rnc';
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
            $validate = $this->model->relaxNGValidate(self::getSchemaDir .
125
                DIRECTORY_SEPARATOR . $this->relax);
126
            if (! $validate) {
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
     * Allows retrieval of an entry by ID where the rdf:about attribute is used
140
     *
141
     * This is not really something that will work with RSS1 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. If DOMXPath::evaluate is available, we also use that to store
145
     * a reference to the entry in the array used by getEntryByOffset so that
146
     * method does not have to seek out the entry if it's requested that way.
147
     *
148
     * @param    string    $id    any valid ID.
149
     * @return    XML_Feed_Parser_RSS1Element
150
     */
151
    function getEntryById($id)
152
    {
153
        if (isset($this->idMappings[$id])) {
154
            return $this->entries[$this->idMappings[$id]];
155
        }
156
 
157
        $entries = $this->xpath->query("//rss:item[@rdf:about='$id']");
158
        if ($entries->length > 0) {
159
            $classname = $this->itemClass;
160
            $entry = new $classname($entries->item(0), $this);
161
            if (in_array('evaluate', get_class_methods($this->xpath))) {
162
                $offset = $this->xpath->evaluate("count(preceding-sibling::rss:item)", $entries->item(0));
163
                $this->entries[$offset] = $entry;
164
            }
165
            $this->idMappings[$id] = $entry;
166
            return $entry;
167
        }
168
        return false;
169
    }
170
 
171
    /**
172
     * Get details of the image associated with the feed.
173
     *
174
     * @return  array|false an array simply containing the child elements
175
     */
176
    protected function getImage()
177
    {
178
        $images = $this->model->getElementsByTagName('image');
179
        if ($images->length > 0) {
180
            $image = $images->item(0);
181
            $details = array();
182
            if ($image->hasChildNodes()) {
183
                $details = array(
184
                    'title' => $image->getElementsByTagName('title')->item(0)->value,
185
                    'link' => $image->getElementsByTagName('link')->item(0)->value,
186
                    'url' => $image->getElementsByTagName('url')->item(0)->value);
187
            } else {
188
                $details = array('title' => false,
189
                    'link' => false,
190
                    'url' => $image->attributes->getNamedItem('resource')->nodeValue);
191
            }
192
            $details = array_merge($details, array('description' => false, 'height' => false, 'width' => false));
193
            if (! empty($details)) {
194
                return $details;
195
            }
196
        }
197
        return false;
198
    }
199
 
200
    /**
201
     * The textinput element is little used, but in the interests of
202
     * completeness we will support it.
203
     *
204
     * @return  array|false
205
     */
206
    protected function getTextInput()
207
    {
208
        $inputs = $this->model->getElementsByTagName('textinput');
209
        if ($inputs->length > 0) {
210
            $input = $inputs->item(0);
211
            $results = array();
212
            $results['title'] = isset(
213
                $input->getElementsByTagName('title')->item(0)->value) ?
214
                $input->getElementsByTagName('title')->item(0)->value : null;
215
            $results['description'] = isset(
216
                $input->getElementsByTagName('description')->item(0)->value) ?
217
                $input->getElementsByTagName('description')->item(0)->value : null;
218
            $results['name'] = isset(
219
                $input->getElementsByTagName('name')->item(0)->value) ?
220
                $input->getElementsByTagName('name')->item(0)->value : null;
221
            $results['link'] = isset(
222
                   $input->getElementsByTagName('link')->item(0)->value) ?
223
                   $input->getElementsByTagName('link')->item(0)->value : null;
224
            if (empty($results['link']) and
225
                $input->attributes->getNamedItem('resource')) {
226
                $results['link'] =
227
                    $input->attributes->getNamedItem('resource')->nodeValue;
228
            }
229
            if (! empty($results)) {
230
                return $results;
231
            }
232
        }
233
        return false;
234
    }
235
 
236
    /**
237
     * Employs various techniques to identify the author
238
     *
239
     * Dublin Core provides the dc:creator, dc:contributor, and dc:publisher
240
     * elements for defining authorship in RSS1. We will try each of those in
241
     * turn in order to simulate the atom author element and will return it
242
     * as text.
243
     *
244
     * @return  array|false
245
     */
246
    function getAuthor()
247
    {
248
        $options = array('creator', 'contributor', 'publisher');
249
        foreach ($options as $element) {
250
            $test = $this->model->getElementsByTagName($element);
251
            if ($test->length > 0) {
252
                return $test->item(0)->value;
253
            }
254
        }
255
        return false;
256
    }
257
 
258
    /**
259
     * Retrieve a link
260
     *
261
     * In RSS1 a link is a text element but in order to ensure that we resolve
262
     * URLs properly we have a special function for them.
263
     *
264
     * @return  string
265
     */
266
    function getLink($offset = 0, $attribute = 'href', $params = false)
267
    {
268
        $links = $this->model->getElementsByTagName('link');
269
        if ($links->length <= $offset) {
270
            return false;
271
        }
272
        $link = $links->item($offset);
273
        return $this->addBase($link->nodeValue, $link);
274
    }
275
}
276
 
277
?>