Subversion Repositories eFlore/Applications.cel

Rev

Details | Last modification | View Log | RSS feed

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