Subversion Repositories Applications.papyrus

Rev

Details | 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
 * AtomElement class for XML_Feed_Parser package
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: AtomElement.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 provides support for atom entries. It will usually be called by
26
 * XML_Feed_Parser_Atom with which it shares many methods.
27
 *
28
 * @author    James Stewart <james@jystewart.net>
29
 * @version    Release: 1.0.2
30
 * @package XML_Feed_Parser
31
 */
32
class XML_Feed_Parser_AtomElement extends XML_Feed_Parser_Atom
33
{
34
    /**
35
     * This will be a reference to the parent object for when we want
36
     * to use a 'fallback' rule
37
     * @var XML_Feed_Parser_Atom
38
     */
39
    protected $parent;
40
 
41
    /**
42
     * When performing XPath queries we will use this prefix
43
     * @var string
44
     */
45
    private $xpathPrefix = '';
46
 
47
    /**
48
     * xml:base values inherited by the element
49
     * @var string
50
     */
51
    protected $xmlBase;
52
 
53
    /**
54
     * Here we provide a few mappings for those very special circumstances in
55
     * which it makes sense to map back to the RSS2 spec or to manage other
56
     * compatibilities (eg. with the Univeral Feed Parser). Key is the other version's
57
     * name for the command, value is an array consisting of the equivalent in our atom
58
     * api and any attributes needed to make the mapping.
59
     * @var array
60
     */
61
    protected $compatMap = array(
62
        'guid' => array('id'),
63
        'links' => array('link'),
64
        'tags' => array('category'),
65
        'contributors' => array('contributor'));
66
 
67
    /**
68
     * Our specific element map
69
     * @var array
70
     */
71
    protected $map = array(
72
        'author' => array('Person', 'fallback'),
73
        'contributor' => array('Person'),
74
        'id' => array('Text', 'fail'),
75
        'published' => array('Date'),
76
        'updated' => array('Date', 'fail'),
77
        'title' => array('Text', 'fail'),
78
        'rights' => array('Text', 'fallback'),
79
        'summary' => array('Text'),
80
        'content' => array('Content'),
81
        'link' => array('Link'),
82
        'enclosure' => array('Enclosure'),
83
        'category' => array('Category'));
84
 
85
    /**
86
     * Store useful information for later.
87
     *
88
     * @param   DOMElement  $element - this item as a DOM element
89
     * @param   XML_Feed_Parser_Atom    $parent - the feed of which this is a member
90
     */
91
    function __construct(DOMElement $element, $parent, $xmlBase = '')
92
    {
93
        $this->model = $element;
94
        $this->parent = $parent;
95
        $this->xmlBase = $xmlBase;
96
        $this->xpathPrefix = "//atom:entry[atom:id='" . $this->id . "']/";
97
        $this->xpath = $this->parent->xpath;
98
    }
99
 
100
    /**
101
     * Provides access to specific aspects of the author data for an atom entry
102
     *
103
     * Author data at the entry level is more complex than at the feed level.
104
     * If atom:author is not present for the entry we need to look for it in
105
     * an atom:source child of the atom:entry. If it's not there either, then
106
     * we look to the parent for data.
107
     *
108
     * @param   array
109
     * @return  string
110
     */
111
    function getAuthor($arguments)
112
    {
113
        /* Find out which part of the author data we're looking for */
114
        if (isset($arguments['param'])) {
115
            $parameter = $arguments['param'];
116
        } else {
117
            $parameter = 'name';
118
        }
119
 
120
        $test = $this->model->getElementsByTagName('author');
121
        if ($test->length > 0) {
122
            $item = $test->item(0);
123
            return $item->getElementsByTagName($parameter)->item(0)->nodeValue;
124
        }
125
 
126
        $source = $this->model->getElementsByTagName('source');
127
        if ($source->length > 0) {
128
            $test = $this->model->getElementsByTagName('author');
129
            if ($test->length > 0) {
130
                $item = $test->item(0);
131
                return $item->getElementsByTagName($parameter)->item(0)->nodeValue;
132
            }
133
        }
134
        return $this->parent->getAuthor($arguments);
135
    }
136
 
137
    /**
138
     * Returns the content of the content element or info on a specific attribute
139
     *
140
     * This element may or may not be present. It cannot be present more than
141
     * once. It may have a 'src' attribute, in which case there's no content
142
     * If not present, then the entry must have link with rel="alternate".
143
     * If there is content we return it, if not and there's a 'src' attribute
144
     * we return the value of that instead. The method can take an 'attribute'
145
     * argument, in which case we return the value of that attribute if present.
146
     * eg. $item->content("type") will return the type of the content. It is
147
     * recommended that all users check the type before getting the content to
148
     * ensure that their script is capable of handling the type of returned data.
149
     * (data carried in the content element can be either 'text', 'html', 'xhtml',
150
     * or any standard MIME type).
151
     *
152
     * @return  string|false
153
     */
154
    protected function getContent($method, $arguments = array())
155
    {
156
        $attribute = empty($arguments[0]) ? false : $arguments[0];
157
        $tags = $this->model->getElementsByTagName('content');
158
 
159
        if ($tags->length == 0) {
160
            return false;
161
        }
162
 
163
        $content = $tags->item(0);
164
 
165
        if (! $content->hasAttribute('type')) {
166
            $content->setAttribute('type', 'text');
167
        }
168
        if (! empty($attribute)) {
169
            return $content->getAttribute($attribute);
170
        }
171
 
172
        $type = $content->getAttribute('type');
173
 
174
        if (! empty($attribute)) {
175
            if ($content->hasAttribute($attribute))
176
            {
177
                return $content->getAttribute($attribute);
178
            }
179
            return false;
180
        }
181
 
182
        if ($content->hasAttribute('src')) {
183
            return $content->getAttribute('src');
184
        }
185
 
186
        return $this->parseTextConstruct($content);
187
     }
188
 
189
    /**
190
     * For compatibility, this method provides a mapping to access enclosures.
191
     *
192
     * The Atom spec doesn't provide for an enclosure element, but it is
193
     * generally supported using the link element with rel='enclosure'.
194
     *
195
     * @param   string  $method - for compatibility with our __call usage
196
     * @param   array   $arguments - for compatibility with our __call usage
197
     * @return  array|false
198
     */
199
    function getEnclosure($method, $arguments = array())
200
    {
201
        $offset = isset($arguments[0]) ? $arguments[0] : 0;
202
        $query = "//atom:entry[atom:id='" . $this->getText('id', false) .
203
            "']/atom:link[@rel='enclosure']";
204
 
205
        $encs = $this->parent->xpath->query($query);
206
        if ($encs->length > $offset) {
207
            try {
208
                if (! $encs->item($offset)->hasAttribute('href')) {
209
                    return false;
210
                }
211
                $attrs = $encs->item($offset)->attributes;
212
                $length = $encs->item($offset)->hasAttribute('length') ?
213
                    $encs->item($offset)->getAttribute('length') : false;
214
                return array(
215
                    'url' => $attrs->getNamedItem('href')->value,
216
                    'type' => $attrs->getNamedItem('type')->value,
217
                    'length' => $length);
218
            } catch (Exception $e) {
219
                return false;
220
            }
221
        }
222
        return false;
223
    }
224
 
225
    /**
226
     * Get details of this entry's source, if available/relevant
227
     *
228
     * Where an atom:entry is taken from another feed then the aggregator
229
     * is supposed to include an atom:source element which replicates at least
230
     * the atom:id, atom:title, and atom:updated metadata from the original
231
     * feed. Atom:source therefore has a very similar structure to atom:feed
232
     * and if we find it we will return it as an XML_Feed_Parser_Atom object.
233
     *
234
     * @return  XML_Feed_Parser_Atom|false
235
     */
236
    function getSource()
237
    {
238
        $test = $this->model->getElementsByTagName('source');
239
        if ($test->length == 0) {
240
            return false;
241
        }
242
        $source = new XML_Feed_Parser_Atom($test->item(0));
243
    }
244
 
245
    /**
246
     * Get the entry as an XML string
247
     *
248
     * Return an XML serialization of the feed, should it be required. Most
249
     * users however, will already have a serialization that they used when
250
     * instantiating the object.
251
     *
252
     * @return    string    XML serialization of element
253
     */
254
    function __toString()
255
    {
256
        $simple = simplexml_import_dom($this->model);
257
        return $simple->asXML();
258
    }
259
}
260
 
261
?>