summaryrefslogtreecommitdiff
path: root/plugins/OStatus/extlib/XML/Feed/Parser/RSS2.php
blob: 0936bd2f5e6406e179bde98371cd0bd3d04268ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Class representing feed-level data for an RSS2 feed
 *
 * PHP versions 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category   XML
 * @package    XML_Feed_Parser
 * @author     James Stewart <james@jystewart.net>
 * @copyright  2005 James Stewart <james@jystewart.net>
 * @license    http://www.gnu.org/copyleft/lesser.html  GNU LGPL 2.1
 * @version    CVS: $Id: RSS2.php,v 1.12 2008/03/08 18:16:45 jystewart Exp $
 * @link       http://pear.php.net/package/XML_Feed_Parser/
 */

/**
 * This class handles RSS2 feeds.
 * 
 * @author    James Stewart <james@jystewart.net>
 * @version    Release: 1.0.3
 * @package XML_Feed_Parser
 */
class XML_Feed_Parser_RSS2 extends XML_Feed_Parser_Type
{
    /**
     * The URI of the RelaxNG schema used to (optionally) validate the feed
     * @var string
     */
    private $relax = 'rss20.rnc';

    /**
     * We're likely to use XPath, so let's keep it global
     * @var DOMXPath
     */
    protected $xpath;

    /**
     * The feed type we are parsing
     * @var string
     */
    public $version = 'RSS 2.0';

    /**
     * The class used to represent individual items
     * @var string
     */     
    protected $itemClass = 'XML_Feed_Parser_RSS2Element';
    
    /**
     * The element containing entries 
     * @var string
     */
    protected $itemElement = 'item';

    /**
     * Here we map those elements we're not going to handle individually
     * to the constructs they are. The optional second parameter in the array
     * tells the parser whether to 'fall back' (not apt. at the feed level) or
     * fail if the element is missing. If the parameter is not set, the function
     * will simply return false and leave it to the client to decide what to do.
     * @var array
     */
    protected $map = array(
        'ttl' => array('Text'),
        'pubDate' => array('Date'),
        'lastBuildDate' => array('Date'),
        'title' => array('Text'),
        'link' => array('Link'),
        'description' => array('Text'),
        'language' => array('Text'),
        'copyright' => array('Text'),
        'managingEditor' => array('Text'),
        'webMaster' => array('Text'),
        'category' => array('Text'),
        'generator' => array('Text'),
        'docs' => array('Text'),
        'ttl' => array('Text'),
        'image' => array('Image'),
        'skipDays' => array('skipDays'),
        'skipHours' => array('skipHours'));

    /**
     * Here we map some elements to their atom equivalents. This is going to be
     * quite tricky to pull off effectively (and some users' methods may vary)
     * but is worth trying. The key is the atom version, the value is RSS2.
     * @var array
     */
    protected $compatMap = array(
        'title' => array('title'),
        'rights' => array('copyright'),
        'updated' => array('lastBuildDate'),
        'subtitle' => array('description'),
        'date' => array('pubDate'),
        'author' => array('managingEditor'));

    protected $namespaces = array(
        'dc' => 'http://purl.org/rss/1.0/modules/dc/',
        'content' => 'http://purl.org/rss/1.0/modules/content/');

    /**
     * Our constructor does nothing more than its parent.
     * 
     * @param    DOMDocument    $xml    A DOM object representing the feed
     * @param    bool (optional) $string    Whether or not to validate this feed
     */
    function __construct(DOMDocument $model, $strict = false)
    {
        $this->model = $model;

        if ($strict) {
            if (! $this->model->relaxNGValidate($this->relax)) {
                throw new XML_Feed_Parser_Exception('Failed required validation');
            }
        }

        $this->xpath = new DOMXPath($this->model);
        foreach ($this->namespaces as $key => $value) {
            $this->xpath->registerNamespace($key, $value);
        }
        $this->numberEntries = $this->count('item');
    }

    /**
     * Retrieves an entry by ID, if the ID is specified with the guid element
     *
     * This is not really something that will work with RSS2 as it does not have
     * clear restrictions on the global uniqueness of IDs. But we can emulate
     * it by allowing access based on the 'guid' element. If DOMXPath::evaluate
     * is available, we also use that to store a reference to the entry in the array
     * used by getEntryByOffset so that method does not have to seek out the entry
     * if it's requested that way.
     *
     * @param    string    $id    any valid ID.
     * @return    XML_Feed_Parser_RSS2Element
     */
    function getEntryById($id)
    {
        if (isset($this->idMappings[$id])) {
            return $this->entries[$this->idMappings[$id]];
        }

        $entries = $this->xpath->query("//item[guid='$id']");
        if ($entries->length > 0) {
            $entry = new $this->itemElement($entries->item(0), $this);
            if (in_array('evaluate', get_class_methods($this->xpath))) {
                $offset = $this->xpath->evaluate("count(preceding-sibling::item)", $entries->item(0));
                $this->entries[$offset] = $entry;
            }
            $this->idMappings[$id] = $entry;
            return $entry;
        }        
    }

    /**
     * Get a category from the element
     *
     * The category element is a simple text construct which can occur any number
     * of times. We allow access by offset or access to an array of results.
     *
     * @param    string    $call    for compatibility with our overloading
     * @param   array $arguments - arg 0 is the offset, arg 1 is whether to return as array
     * @return  string|array|false
     */
    function getCategory($call, $arguments = array())
    {
        $categories = $this->model->getElementsByTagName('category');
        $offset = empty($arguments[0]) ? 0 : $arguments[0];
        $array = empty($arguments[1]) ? false : true;
        if ($categories->length <= $offset) {
            return false;
        }
        if ($array) {
            $list = array();
            foreach ($categories as $category) {
                array_push($list, $category->nodeValue);
            }
            return $list;
        }
        return $categories->item($offset)->nodeValue;
    }

    /**
     * Get details of the image associated with the feed.
     *
     * @return  array|false an array simply containing the child elements
     */
    protected function getImage()
    {
        $images = $this->xpath->query("//image");
        if ($images->length > 0) {
            $image = $images->item(0);
            $desc = $image->getElementsByTagName('description');
            $description = $desc->length ? $desc->item(0)->nodeValue : false;
            $heigh = $image->getElementsByTagName('height'); 
            $height = $heigh->length ? $heigh->item(0)->nodeValue : false;
            $widt = $image->getElementsByTagName('width'); 
            $width = $widt->length ? $widt->item(0)->nodeValue : false;
            return array(
                'title' => $image->getElementsByTagName('title')->item(0)->nodeValue,
                'link' => $image->getElementsByTagName('link')->item(0)->nodeValue,
                'url' => $image->getElementsByTagName('url')->item(0)->nodeValue,
                'description' => $description,
                'height' => $height,
                'width' => $width);
        }
        return false;
    }

    /**
     * The textinput element is little used, but in the interests of
     * completeness...
     *
     * @return  array|false
     */
    function getTextInput()
    {
        $inputs = $this->model->getElementsByTagName('input');
        if ($inputs->length > 0) {
            $input = $inputs->item(0);
            return array(
                'title' => $input->getElementsByTagName('title')->item(0)->value,
                'description' => 
                    $input->getElementsByTagName('description')->item(0)->value,
                'name' => $input->getElementsByTagName('name')->item(0)->value,
                'link' => $input->getElementsByTagName('link')->item(0)->value);
        }
        return false;
    }

    /**
     * Utility function for getSkipDays and getSkipHours
     *
     * This is a general function used by both getSkipDays and getSkipHours. It simply
     * returns an array of the values of the children of the appropriate tag.
     *
     * @param   string      $tagName    The tag name (getSkipDays or getSkipHours)
     * @return  array|false
     */
    protected function getSkips($tagName)
    {
        $hours = $this->model->getElementsByTagName($tagName);
        if ($hours->length == 0) {
            return false;
        }
        $skipHours = array();
        foreach($hours->item(0)->childNodes as $hour) {
            if ($hour instanceof DOMElement) {
                array_push($skipHours, $hour->nodeValue);
            }
        }
        return $skipHours;
    }

    /**
     * Retrieve skipHours data
     *
     * The skiphours element provides a list of hours on which this feed should
     * not be checked. We return an array of those hours (integers, 24 hour clock)
     *
     * @return  array
     */    
    function getSkipHours()
    {
        return $this->getSkips('skipHours');
    }

    /**
     * Retrieve skipDays data
     *
     * The skipdays element provides a list of days on which this feed should
     * not be checked. We return an array of those days.
     *
     * @return  array
     */
    function getSkipDays()
    {
        return $this->getSkips('skipDays');
    }

    /**
     * Return content of the little-used 'cloud' element
     *
     * The cloud element is rarely used. It is designed to provide some details
     * of a location to update the feed.
     *
     * @return  array   an array of the attributes of the element
     */
    function getCloud()
    {
        $cloud = $this->model->getElementsByTagName('cloud');
        if ($cloud->length == 0) {
            return false;
        }
        $cloudData = array();
        foreach ($cloud->item(0)->attributes as $attribute) {
            $cloudData[$attribute->name] = $attribute->value;
        }
        return $cloudData;
    }
    
    /**
     * Get link URL
     *
     * In RSS2 a link is a text element but in order to ensure that we resolve
     * URLs properly we have a special function for them. We maintain the 
     * parameter used by the atom getLink method, though we only use the offset
     * parameter.
     *
     * @param   int     $offset The position of the link within the feed. Starts from 0
     * @param   string  $attribute  The attribute of the link element required
     * @param   array   $params An array of other parameters. Not used.
     * @return  string
     */
    function getLink($offset, $attribute = 'href', $params = array())
    {
        $xPath = new DOMXPath($this->model);
        $links = $xPath->query('//link');

        if ($links->length <= $offset) {
            return false;
        }
        $link = $links->item($offset);
        return $this->addBase($link->nodeValue, $link);
    }
}

?>