Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | 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
 * Atom feed class for XML_Feed_Parser
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: Atom.php 304308 2010-10-11 12:05:50Z clockwerx $
21
 * @link       http://pear.php.net/package/XML_Feed_Parser/
22
*/
23
 
24
/**
25
 * This is the class that determines how we manage Atom 1.0 feeds
26
 *
27
 * How we deal with constructs:
28
 *  date - return as unix datetime for use with the 'date' function unless specified otherwise
29
 *  text - return as is. optional parameter will give access to attributes
30
 *  person - defaults to name, but parameter based access
31
 *
32
 * @author    James Stewart <james@jystewart.net>
33
 * @version    Release: @package_version@
34
 * @package XML_Feed_Parser
35
 */
36
class XmlFeedParserAtom extends XmlFeedParserType {
37
    /**
38
     * The URI of the RelaxNG schema used to (optionally) validate the feed
39
     * @var string
40
     */
41
    protected $relax = 'atom.rng';
42
 
43
    /**
44
     * We're likely to use XPath, so let's keep it global
45
     * @var DOMXPath
46
     */
47
    public $xpath;
48
 
49
    /**
50
     * When performing XPath queries we will use this prefix
51
     * @var string
52
     */
53
    private $xpathPrefix = '//';
54
 
55
    /**
56
     * The feed type we are parsing
57
     * @var string
58
     */
59
    public $version = 'Atom 1.0';
60
 
61
    /**
62
     * The class used to represent individual items
63
     * @var string
64
     */
65
    protected $itemClass = 'XmlFeedParserAtomElement';
66
 
67
    /**
68
     * The element containing entries
69
     * @var string
70
     */
71
    protected $itemElement = 'entry';
72
 
73
    /**
74
     * Here we map those elements we're not going to handle individually
75
     * to the constructs they are. The optional second parameter in the array
76
     * tells the parser whether to 'fall back' (not apt. at the feed level) or
77
     * fail if the element is missing. If the parameter is not set, the function
78
     * will simply return false and leave it to the client to decide what to do.
79
     * @var array
80
     */
81
    protected $map = array(
82
        'author' => array('Person'),
83
        'contributor' => array('Person'),
84
        'icon' => array('Text'),
85
        'logo' => array('Text'),
86
        'id' => array('Text', 'fail'),
87
        'rights' => array('Text'),
88
        'subtitle' => array('Text'),
89
        'title' => array('Text', 'fail'),
90
        'updated' => array('Date', 'fail'),
91
        'link' => array('Link'),
92
        'generator' => array('Text'),
93
        'category' => array('Category'));
94
 
95
    /**
96
     * Here we provide a few mappings for those very special circumstances in
97
     * which it makes sense to map back to the RSS2 spec. Key is RSS2 version
98
     * value is an array consisting of the equivalent in atom and any attributes
99
     * needed to make the mapping.
100
     * @var array
101
     */
102
    protected $compatMap = array(
103
        'guid' => array('id'),
104
        'links' => array('link'),
105
        'tags' => array('category'),
106
        'contributors' => array('contributor'));
107
 
108
    /**
109
     * Our constructor does nothing more than its parent.
110
     *
111
     * @param    DOMDocument    $xml    A DOM object representing the feed
112
     * @param    bool (optional) $string    Whether or not to validate this feed
113
     */
114
    function __construct(DOMDocument $model, $strict = false) {
115
        $this->model = $model;
116
 
117
        if ($strict) {
118
            if (! $this->relaxNGValidate()) {
119
                throw new XML_Feed_Parser_Exception('Failed required validation');
120
            }
121
        }
122
 
123
        $this->xpath = new DOMXPath($this->model);
124
        $this->xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
125
        $this->numberEntries = $this->count('entry');
126
    }
127
 
128
    /**
129
     * Implement retrieval of an entry based on its ID for atom feeds.
130
     *
131
     * This function uses XPath to get the entry based on its ID. If DOMXPath::evaluate
132
     * is available, we also use that to store a reference to the entry in the array
133
     * used by getEntryByOffset so that method does not have to seek out the entry
134
     * if it's requested that way.
135
     *
136
     * @param    string    $id    any valid Atom ID.
137
     * @return    XML_Feed_Parser_AtomElement
138
     */
139
    function getEntryById($id) {
140
        if (isset($this->idMappings[$id])) {
141
            return $this->entries[$this->idMappings[$id]];
142
        }
143
 
144
        $entries = $this->xpath->query("//atom:entry[atom:id='$id']");
145
 
146
        if ($entries->length > 0) {
147
            $xmlBase = $entries->item(0)->baseURI;
148
            $entry = new $this->itemClass($entries->item(0), $this, $xmlBase);
149
 
150
            if (in_array('evaluate', get_class_methods($this->xpath))) {
151
                $offset = $this->xpath->evaluate("count(preceding-sibling::atom:entry)", $entries->item(0));
152
                $this->entries[$offset] = $entry;
153
            }
154
 
155
            $this->idMappings[$id] = $entry;
156
 
157
            return $entry;
158
        }
159
 
160
    }
161
 
162
    /**
163
     * Retrieves data from a person construct.
164
     *
165
     * Get a person construct. We default to the 'name' element but allow
166
     * access to any of the elements.
167
     *
168
     * @param    string    $method    The name of the person construct we want
169
     * @param    array     $arguments    An array which we hope gives a 'param'
170
     * @return    string|false
171
     */
172
    protected function getPerson($method, $arguments) {
173
        $offset = empty($arguments[0]) ? 0 : $arguments[0];
174
        $parameter = empty($arguments[1]['param']) ? 'name' : $arguments[1]['param'];
175
        $section = $this->model->getElementsByTagName($method);
176
 
177
        if ($parameter == 'url') {
178
            $parameter = 'uri';
179
        }
180
 
181
        if ($section->length <= $offset) {
182
            return false;
183
        }
184
 
185
        $param = $section->item($offset)->getElementsByTagName($parameter);
186
        if ($param->length == 0) {
187
            return false;
188
        }
189
        return $param->item(0)->nodeValue;
190
    }
191
 
192
    /**
193
     * Retrieves an element's content where that content is a text construct.
194
     *
195
     * Get a text construct. When calling this method, the two arguments
196
     * allowed are 'offset' and 'attribute', so $parser->subtitle() would
197
     * return the content of the element, while $parser->subtitle(false, 'type')
198
     * would return the value of the type attribute.
199
     *
200
     * @todo    Clarify overlap with getContent()
201
     * @param    string    $method    The name of the text construct we want
202
     * @param    array     $arguments    An array which we hope gives a 'param'
203
     * @return    string
204
     */
205
    protected function getText($method, $arguments) {
206
        $offset = empty($arguments[0]) ? 0: $arguments[0];
207
        $attribute = empty($arguments[1]) ? false : $arguments[1];
208
        $tags = $this->model->getElementsByTagName($method);
209
 
210
        if ($tags->length <= $offset) {
211
            return false;
212
        }
213
 
214
        $content = $tags->item($offset);
215
 
216
        if (! $content->hasAttribute('type')) {
217
            $content->setAttribute('type', 'text');
218
        }
219
        $type = $content->getAttribute('type');
220
 
221
        if (! empty($attribute) and
222
            ! ($method == 'generator' and $attribute == 'name')) {
223
            if ($content->hasAttribute($attribute)) {
224
                return $content->getAttribute($attribute);
225
            } else if ($attribute == 'href' and $content->hasAttribute('uri')) {
226
                return $content->getAttribute('uri');
227
            }
228
            return false;
229
        }
230
 
231
        return $this->parseTextConstruct($content);
232
    }
233
 
234
    /**
235
     * Extract content appropriately from atom text constructs
236
     *
237
     * Because of different rules applied to the content element and other text
238
     * constructs, they are deployed as separate functions, but they share quite
239
     * a bit of processing. This method performs the core common process, which is
240
     * to apply the rules for different mime types in order to extract the content.
241
     *
242
     * @param   DOMNode $content    the text construct node to be parsed
243
     * @return String
244
     * @author James Stewart
245
     **/
246
    protected function parseTextConstruct(DOMNode $content) {
247
        if ($content->hasAttribute('type')) {
248
            $type = $content->getAttribute('type');
249
        } else {
250
            $type = 'text';
251
        }
252
 
253
        if (strpos($type, 'text/') === 0) {
254
            $type = 'text';
255
        }
256
 
257
        switch ($type) {
258
            case 'text':
259
            case 'html':
260
                return $content->textContent;
261
                break;
262
            case 'xhtml':
263
                $container = $content->getElementsByTagName('div');
264
                if ($container->length == 0) {
265
                    return false;
266
                }
267
                $contents = $container->item(0);
268
                if ($contents->hasChildNodes()) {
269
                    /* Iterate through, applying xml:base and store the result */
270
                    $result = '';
271
                    foreach ($contents->childNodes as $node) {
272
                        $result .= $this->traverseNode($node);
273
                    }
274
                    return $result;
275
                }
276
                break;
277
            case preg_match('@^[a-zA-Z]+/[a-zA-Z+]*xml@i', $type) > 0:
278
                return $content;
279
                break;
280
            case 'application/octet-stream':
281
            default:
282
                return base64_decode(trim($content->nodeValue));
283
                break;
284
        }
285
        return false;
286
    }
287
    /**
288
     * Get a category from the entry.
289
     *
290
     * A feed or entry can have any number of categories. A category can have the
291
     * attributes term, scheme and label.
292
     *
293
     * @param    string    $method    The name of the text construct we want
294
     * @param    array     $arguments    An array which we hope gives a 'param'
295
     * @return    string
296
     */
297
    function getCategory($method, $arguments) {
298
        $offset = empty($arguments[0]) ? 0: $arguments[0];
299
        $attribute = empty($arguments[1]) ? 'term' : $arguments[1];
300
        $categories = $this->model->getElementsByTagName('category');
301
        if ($categories->length <= $offset) {
302
            $category = $categories->item($offset);
303
            if ($category->hasAttribute($attribute)) {
304
                return $category->getAttribute($attribute);
305
            }
306
        }
307
        return false;
308
    }
309
 
310
    /**
311
     * This element must be present at least once with rel="feed". This element may be
312
     * present any number of further times so long as there is no clash. If no 'rel' is
313
     * present and we're asked for one, we follow the example of the Universal Feed
314
     * Parser and presume 'alternate'.
315
     *
316
     * @param    int    $offset    the position of the link within the container
317
     * @param    string    $attribute    the attribute name required
318
     * @param    array     an array of attributes to search by
319
     * @return    string    the value of the attribute
320
     */
321
    function getLink($offset = 0, $attribute = 'href', $params = false) {
322
        if (is_array($params) and !empty($params)) {
323
            $terms = array();
324
            $alt_predicate = '';
325
            $other_predicate = '';
326
 
327
            foreach ($params as $key => $value) {
328
                if ($key == 'rel' && $value == 'alternate') {
329
                    $alt_predicate = '[not(@rel) or @rel="alternate"]';
330
                } else {
331
                    $terms[] = "@$key='$value'";
332
                }
333
            }
334
            if (!empty($terms)) {
335
                $other_predicate = '[' . join(' and ', $terms) . ']';
336
            }
337
            $query =  $this->xpathPrefix . 'atom:link' . $alt_predicate . $other_predicate;
338
            $links = $this->xpath->query($query);
339
        } else {
340
            $links = $this->model->getElementsByTagName('link');
341
        }
342
        if ($links->length > $offset) {
343
            if ($links->item($offset)->hasAttribute($attribute)) {
344
                $value = $links->item($offset)->getAttribute($attribute);
345
                if ($attribute == 'href') {
346
                    $value = $this->addBase($value, $links->item($offset));
347
                }
348
                return $value;
349
            } else if ($attribute == 'rel') {
350
                return 'alternate';
351
            }
352
        }
353
        return false;
354
    }
355
}
356
 
357
?>