summaryrefslogtreecommitdiff
path: root/plugins/FeedSub/extlib/XML/Feed/Parser/Type.php
blob: 75052619bdff52ec2c3561b5c504e654f1712d8e (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Abstract class providing common methods for XML_Feed_Parser feeds.
 *
 * 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: Type.php,v 1.25 2008/03/08 18:39:09 jystewart Exp $
 * @link       http://pear.php.net/package/XML_Feed_Parser/
 */

/**
 * This abstract class provides some general methods that are likely to be
 * implemented exactly the same way for all feed types.
 *
 * @package XML_Feed_Parser
 * @author  James Stewart <james@jystewart.net>
 * @version Release: 1.0.3
 */
abstract class XML_Feed_Parser_Type
{
    /**
     * Where we store our DOM object for this feed 
     * @var DOMDocument
     */
    public $model;

    /**
     * For iteration we'll want a count of the number of entries 
     * @var int
     */
    public $numberEntries;

    /**
     * Where we store our entry objects once instantiated 
     * @var array
     */
    public $entries = array();

    /**
     * Store mappings between entry IDs and their position in the feed
     */
    public $idMappings = array();

    /**
     * Proxy to allow use of element names as method names
     *
     * We are not going to provide methods for every entry type so this
     * function will allow for a lot of mapping. We rely pretty heavily
     * on this to handle our mappings between other feed types and atom.
     *
     * @param   string  $call - the method attempted
     * @param   array   $arguments - arguments to that method
     * @return  mixed
     */
    function __call($call, $arguments = array())
    {
        if (! is_array($arguments)) {
            $arguments = array();
        }

        if (isset($this->compatMap[$call])) {
            $tempMap = $this->compatMap;
            $tempcall = array_pop($tempMap[$call]);
            if (! empty($tempMap)) {
                $arguments = array_merge($arguments, $tempMap[$call]);
            }
            $call = $tempcall;
        }

        /* To be helpful, we allow a case-insensitive search for this method */
        if (! isset($this->map[$call])) {
            foreach (array_keys($this->map) as $key) {
                if (strtoupper($key) == strtoupper($call)) {
                    $call = $key;
                    break;
                }
            }
        }

        if (empty($this->map[$call])) {
            return false;
        }

        $method = 'get' . $this->map[$call][0];
        if ($method == 'getLink') {
            $offset = empty($arguments[0]) ? 0 : $arguments[0];
            $attribute = empty($arguments[1]) ? 'href' : $arguments[1];
            $params = isset($arguments[2]) ? $arguments[2] : array();
            return $this->getLink($offset, $attribute, $params);
        }
        if (method_exists($this, $method)) {
            return $this->$method($call, $arguments);
        }

        return false;
    }

    /**
     * Proxy to allow use of element names as attribute names
     *
     * For many elements variable-style access will be desirable. This function
     * provides for that.
     *
     * @param   string  $value - the variable required
     * @return  mixed
     */
    function __get($value)
    {
        return $this->__call($value, array());
    }

    /**
     * Utility function to help us resolve xml:base values
     *
     * We have other methods which will traverse the DOM and work out the different
     * xml:base declarations we need to be aware of. We then need to combine them.
     * If a declaration starts with a protocol then we restart the string. If it 
     * starts with a / then we add on to the domain name. Otherwise we simply tag 
     * it on to the end.
     *
     * @param   string  $base - the base to add the link to
     * @param   string  $link
     */
    function combineBases($base, $link)
    {
        if (preg_match('/^[A-Za-z]+:\/\//', $link)) {
            return $link;
        } else if (preg_match('/^\//', $link)) {
            /* Extract domain and suffix link to that */
            preg_match('/^([A-Za-z]+:\/\/.*)?\/*/', $base, $results);
            $firstLayer = $results[0];
            return $firstLayer . "/" . $link;
        } else if (preg_match('/^\.\.\//', $base)) {
            /* Step up link to find place to be */
            preg_match('/^((\.\.\/)+)(.*)$/', $link, $bases);
            $suffix = $bases[3];
            $count = preg_match_all('/\.\.\//', $bases[1], $steps);
            $url = explode("/", $base);
            for ($i = 0; $i <= $count; $i++) {
                array_pop($url);
            }
            return implode("/", $url) . "/" . $suffix;
        } else if (preg_match('/^(?!\/$)/', $base)) {
            $base = preg_replace('/(.*\/).*$/', '$1', $base)  ;
            return $base . $link;
        } else {
            /* Just stick it on the end */
            return $base . $link;
        }
    }

    /**
     * Determine whether we need to apply our xml:base rules
     *
     * Gets us the xml:base data and then processes that with regard
     * to our current link.
     *
     * @param   string
     * @param   DOMElement
     * @return  string
     */
    function addBase($link, $element)
    {
        if (preg_match('/^[A-Za-z]+:\/\//', $link)) {
            return $link;
        }

        return $this->combineBases($element->baseURI, $link);
    }

    /**
     * Get an entry by its position in the feed, starting from zero
     *
     * As well as allowing the items to be iterated over we want to allow
     * users to be able to access a specific entry. This is one of two ways of
     * doing that, the other being by ID.
     * 
     * @param   int $offset
     * @return  XML_Feed_Parser_RSS1Element
     */
    function getEntryByOffset($offset)
    {
        if (! isset($this->entries[$offset])) {
            $entries = $this->model->getElementsByTagName($this->itemElement);
            if ($entries->length > $offset) {
                $xmlBase = $entries->item($offset)->baseURI;
                $this->entries[$offset] = new $this->itemClass(
                    $entries->item($offset), $this, $xmlBase);
                if ($id = $this->entries[$offset]->id) {
                    $this->idMappings[$id] = $this->entries[$offset];
                }
            } else {
                throw new XML_Feed_Parser_Exception('No entries found');
            }
        }

        return $this->entries[$offset];
    }

    /**
     * Return a date in seconds since epoch.
     *
     * Get a date construct. We use PHP's strtotime to return it as a unix datetime, which
     * is the number of seconds since 1970-01-01 00:00:00.
     * 
     * @link    http://php.net/strtotime
     * @param    string    $method        The name of the date construct we want
     * @param    array     $arguments    Included for compatibility with our __call usage
     * @return    int|false datetime
     */
    protected function getDate($method, $arguments)
    {
        $time = $this->model->getElementsByTagName($method);
        if ($time->length == 0 || empty($time->item(0)->nodeValue)) {
            return false;
        }
        return strtotime($time->item(0)->nodeValue);
    }

    /**
     * Get a text construct. 
     *
     * @param    string    $method    The name of the text construct we want
     * @param    array     $arguments    Included for compatibility with our __call usage
     * @return    string
     */
    protected function getText($method, $arguments = array())
    {
        $tags = $this->model->getElementsByTagName($method);
        if ($tags->length > 0) {
            $value = $tags->item(0)->nodeValue;
            return $value;
        }
        return false;
    }

    /**
     * Apply various rules to retrieve category data.
     *
     * There is no single way of declaring a category in RSS1/1.1 as there is in RSS2 
     * and  Atom. Instead the usual approach is to use the dublin core namespace to 
     * declare  categories. For example delicious use both: 
     * <dc:subject>PEAR</dc:subject> and: <taxo:topics><rdf:Bag>
     * <rdf:li resource="http://del.icio.us/tag/PEAR" /></rdf:Bag></taxo:topics>
     * to declare a categorisation of 'PEAR'.
     *
     * We need to be sensitive to this where possible.
     *
     * @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
     */
    protected function getCategory($call, $arguments)
    {
        $categories = $this->model->getElementsByTagName('subject');
        $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;
    }

    /**
     * Count occurrences of an element
     *
     * This function will tell us how many times the element $type
     * appears at this level of the feed.
     * 
     * @param    string    $type    the element we want to get a count of
     * @return    int
     */
    protected function count($type)
    {
        if ($tags = $this->model->getElementsByTagName($type)) {
            return $tags->length;
        }
        return 0;
    }

    /**
     * Part of our xml:base processing code
     *
     * We need a couple of methods to access XHTML content stored in feeds. 
     * This is because we dereference all xml:base references before returning
     * the element. This method handles the attributes.
     *
     * @param   DOMElement $node    The DOM node we are iterating over
     * @return  string
     */
    function processXHTMLAttributes($node) {
        $return = '';
        foreach ($node->attributes as $attribute) {
            if ($attribute->name == 'src' or $attribute->name == 'href') {
                $attribute->value = $this->addBase(htmlentities($attribute->value, NULL, 'utf-8'), $attribute);
            }
            if ($attribute->name == 'base') {
                continue;
            }
            $return .= $attribute->name . '="' . htmlentities($attribute->value, NULL, 'utf-8') .'" ';
        }
        if (! empty($return)) {
            return ' ' . trim($return);
        }
        return '';
    }

    /**
     * Convert HTML entities based on the current character set.
     * 
     * @param String
     * @return String
     */
    function processEntitiesForNodeValue($node) 
    {
        if (function_exists('iconv')) {
          $current_encoding = $node->ownerDocument->encoding;
          $value = iconv($current_encoding, 'UTF-8', $node->nodeValue);
        } else if ($current_encoding == 'iso-8859-1') {
          $value = utf8_encode($node->nodeValue);
        } else {
          $value = $node->nodeValue;
        }

        $decoded = html_entity_decode($value, NULL, 'UTF-8');
        return htmlentities($decoded, NULL, 'UTF-8');
    }

    /**
     * Part of our xml:base processing code
     *
     * We need a couple of methods to access XHTML content stored in feeds. 
     * This is because we dereference all xml:base references before returning
     * the element. This method recurs through the tree descending from the node
     * and builds our string.
     *
     * @param   DOMElement $node    The DOM node we are processing
     * @return   string
     */
    function traverseNode($node)
    {
        $content = '';

        /* Add the opening of this node to the content */
        if ($node instanceof DOMElement) {
            $content .= '<' . $node->tagName . 
                $this->processXHTMLAttributes($node) . '>';
        }

        /* Process children */
        if ($node->hasChildNodes()) {
            foreach ($node->childNodes as $child) {
                $content .= $this->traverseNode($child);
            }
        }

        if ($node instanceof DOMText) {
            $content .= $this->processEntitiesForNodeValue($node);
        }

        /* Add the closing of this node to the content */
        if ($node instanceof DOMElement) {
            $content .= '</' . $node->tagName . '>';
        }

        return $content;
    }

    /**
     * Get content from RSS feeds (atom has its own implementation)
     *
     * The official way to include full content in an RSS1 entry is to use
     * the content module's element 'encoded', and RSS2 feeds often duplicate that.
     * Often, however, the 'description' element is used instead. We will offer that 
     * as a fallback. Atom uses its own approach and overrides this method.
     *
     * @return  string|false
     */
    protected function getContent()
    {
        $options = array('encoded', 'description');
        foreach ($options as $element) {
            $test = $this->model->getElementsByTagName($element);
            if ($test->length == 0) {
                continue;
            }
            if ($test->item(0)->hasChildNodes()) {
                $value = '';
                foreach ($test->item(0)->childNodes as $child) {
                    if ($child instanceof DOMText) {
                        $value .= $child->nodeValue;
                    } else {
                        $simple = simplexml_import_dom($child);
                        $value .= $simple->asXML();
                    }
                }
                return $value;
            } else if ($test->length > 0) {
                return $test->item(0)->nodeValue;
            }
        }
        return false;
    }

    /**
     * Checks if this element has a particular child element.
     *
     * @param   String
     * @param   Integer
     * @return  bool
     **/
    function hasKey($name, $offset = 0)
    {
        $search = $this->model->getElementsByTagName($name);
        return $search->length > $offset;
    }

    /**
     * Return an XML serialization of the feed, should it be required. Most 
     * users however, will already have a serialization that they used when 
     * instantiating the object.
     *
     * @return    string    XML serialization of element
     */    
    function __toString()
    {
        $simple = simplexml_import_dom($this->model);
        return $simple->asXML();
    }
    
    /**
     * Get directory holding RNG schemas. Method is based on that 
     * found in Contact_AddressBook.
     *
     * @return string PEAR data directory.
     * @access public
     * @static
     */
    static function getSchemaDir()
    {
        require_once 'PEAR/Config.php';
        $config = new PEAR_Config;
        return $config->get('data_dir') . '/XML_Feed_Parser/schemas';
    }
}

?>