summaryrefslogtreecommitdiff
path: root/plugins/FeedSub/extlib/XML/Feed/Parser/RSS2Element.php
blob: 6edf910dc4ed5f7f254c074d135c567b31678848 (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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Class representing entries in 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: RSS2Element.php,v 1.11 2006/07/26 21:18:47 jystewart Exp $
 * @link       http://pear.php.net/package/XML_Feed_Parser/
 */

/**
 * This class provides support for RSS 2.0 entries. It will usually be 
 * called by XML_Feed_Parser_RSS2 with which it shares many methods.
 *
 * @author    James Stewart <james@jystewart.net>
 * @version    Release: 1.0.3
 * @package XML_Feed_Parser
 */
class XML_Feed_Parser_RSS2Element extends XML_Feed_Parser_RSS2
{
    /**
     * This will be a reference to the parent object for when we want
     * to use a 'fallback' rule
     * @var XML_Feed_Parser_RSS2
     */
    protected $parent;

    /**
     * Our specific element map 
     * @var array
     */
    protected $map = array(
        'title' => array('Text'),
        'guid' => array('Guid'),
        'description' => array('Text'),
        'author' => array('Text'),
        'comments' => array('Text'),
        'enclosure' => array('Enclosure'),
        'pubDate' => array('Date'),
        'source' => array('Source'),
        'link' => array('Text'),
        'content' => array('Content'));

    /**
     * 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(
        'id' => array('guid'),
        'updated' => array('lastBuildDate'),
        'published' => array('pubdate'),
        'guidislink' => array('guid', 'ispermalink'),
        'summary' => array('description'));

    /**
     * Store useful information for later.
     *
     * @param   DOMElement  $element - this item as a DOM element
     * @param   XML_Feed_Parser_RSS2    $parent - the feed of which this is a member
     */
    function __construct(DOMElement $element, $parent, $xmlBase = '')
    {
        $this->model = $element;
        $this->parent = $parent;
    }

    /**
     * Get the value of the guid element, if specified
     *
     * guid is the closest RSS2 has to atom's ID. It is usually but not always a
     * URI. The one attribute that RSS2 can posess is 'ispermalink' which specifies
     * whether the guid is itself dereferencable. Use of guid is not obligatory,
     * but is advisable. To get the guid you would call $item->id() (for atom
     * compatibility) or $item->guid(). To check if this guid is a permalink call
     * $item->guid("ispermalink").
     *
     * @param   string  $method - the method name being called
     * @param   array   $params - parameters required
     * @return  string  the guid or value of ispermalink
     */
    protected function getGuid($method, $params)
    {
        $attribute = (isset($params[0]) and $params[0] == 'ispermalink') ? 
            true : false;
        $tag = $this->model->getElementsByTagName('guid');
        if ($tag->length > 0) {
            if ($attribute) {
                if ($tag->hasAttribute("ispermalink")) {
                    return $tag->getAttribute("ispermalink");
                }
            }
            return $tag->item(0)->nodeValue;
        }
        return false;
    }

    /**
     * Access details of file enclosures
     *
     * The RSS2 spec is ambiguous as to whether an enclosure element must be
     * unique in a given entry. For now we will assume it needn't, and allow
     * for an offset.
     *
     * @param   string $method - the method being called
     * @param   array   $parameters - we expect the first of these to be our offset
     * @return  array|false
     */
    protected function getEnclosure($method, $parameters)
    {
        $encs = $this->model->getElementsByTagName('enclosure');
        $offset = isset($parameters[0]) ? $parameters[0] : 0;
        if ($encs->length > $offset) {
            try {
                if (! $encs->item($offset)->hasAttribute('url')) {
                    return false;
                }
                $attrs = $encs->item($offset)->attributes;
                return array(
                    'url' => $attrs->getNamedItem('url')->value,
                    'length' => $attrs->getNamedItem('length')->value,
                    'type' => $attrs->getNamedItem('type')->value);
            } catch (Exception $e) {
                return false;
            }
        }
        return false;
    }

    /**
     * Get the entry source if specified
     *
     * source is an optional sub-element of item. Like atom:source it tells
     * us about where the entry came from (eg. if it's been copied from another
     * feed). It is not a rich source of metadata in the same way as atom:source
     * and while it would be good to maintain compatibility by returning an
     * XML_Feed_Parser_RSS2 element, it makes a lot more sense to return an array.
     *
     * @return array|false
     */
    protected function getSource()
    {
        $get = $this->model->getElementsByTagName('source');
        if ($get->length) {
            $source = $get->item(0);
            $array = array(
                'content' => $source->nodeValue);
            foreach ($source->attributes as $attribute) {
                $array[$attribute->name] = $attribute->value;
            }
            return $array;
        }
        return false;
    }
}

?>