Subversion Repositories eFlore/Applications.cel

Rev

Rev 1328 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1328 aurelien 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
    	'content' => array('Text'));
95
 
96
    /**
97
     * Here we provide a few mappings for those very special circumstances in
98
     * which it makes sense to map back to the RSS2 spec. Key is RSS2 version
99
     * value is an array consisting of the equivalent in atom and any attributes
100
     * needed to make the mapping.
101
     * @var array
102
     */
103
    protected $compatMap = array(
104
        'guid' => array('id'),
105
        'links' => array('link'),
106
        'tags' => array('category'),
107
        'contributors' => array('contributor'));
108
 
109
    /**
110
     * Our constructor does nothing more than its parent.
111
     *
112
     * @param    DOMDocument    $xml    A DOM object representing the feed
113
     * @param    bool (optional) $string    Whether or not to validate this feed
114
     */
115
    function __construct(DOMDocument $model, $strict = false) {
116
        $this->model = $model;
117
 
118
        if ($strict) {
119
            if (! $this->relaxNGValidate()) {
120
                throw new XML_Feed_Parser_Exception('Failed required validation');
121
            }
122
        }
123
 
124
        $this->xpath = new DOMXPath($this->model);
125
        $this->xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
126
        $this->numberEntries = $this->count('entry');
127
    }
128
 
129
    /**
130
     * Implement retrieval of an entry based on its ID for atom feeds.
131
     *
132
     * This function uses XPath to get the entry based on its ID. If DOMXPath::evaluate
133
     * is available, we also use that to store a reference to the entry in the array
134
     * used by getEntryByOffset so that method does not have to seek out the entry
135
     * if it's requested that way.
136
     *
137
     * @param    string    $id    any valid Atom ID.
138
     * @return    XML_Feed_Parser_AtomElement
139
     */
140
    function getEntryById($id) {
141
        if (isset($this->idMappings[$id])) {
142
            return $this->entries[$this->idMappings[$id]];
143
        }
144
 
145
        $entries = $this->xpath->query("//atom:entry[atom:id='$id']");
146
 
147
        if ($entries->length > 0) {
148
            $xmlBase = $entries->item(0)->baseURI;
149
            $entry = new $this->itemClass($entries->item(0), $this, $xmlBase);
150
 
151
            if (in_array('evaluate', get_class_methods($this->xpath))) {
152
                $offset = $this->xpath->evaluate("count(preceding-sibling::atom:entry)", $entries->item(0));
153
                $this->entries[$offset] = $entry;
154
            }
155
 
156
            $this->idMappings[$id] = $entry;
157
 
158
            return $entry;
159
        }
160
 
161
    }
162
 
163
    /**
164
     * Retrieves data from a person construct.
165
     *
166
     * Get a person construct. We default to the 'name' element but allow
167
     * access to any of the elements.
168
     *
169
     * @param    string    $method    The name of the person construct we want
170
     * @param    array     $arguments    An array which we hope gives a 'param'
171
     * @return    string|false
172
     */
173
    protected function getPerson($method, $arguments) {
174
        $offset = empty($arguments[0]) ? 0 : $arguments[0];
175
        $parameter = empty($arguments[1]['param']) ? 'name' : $arguments[1]['param'];
176
        $section = $this->model->getElementsByTagName($method);
177
 
178
        if ($parameter == 'url') {
179
            $parameter = 'uri';
180
        }
181
 
182
        if ($section->length <= $offset) {
183
            return false;
184
        }
185
 
186
        $param = $section->item($offset)->getElementsByTagName($parameter);
187
        if ($param->length == 0) {
188
            return false;
189
        }
190
        return $param->item(0)->nodeValue;
191
    }
192
 
193
    /**
194
     * Retrieves an element's content where that content is a text construct.
195
     *
196
     * Get a text construct. When calling this method, the two arguments
197
     * allowed are 'offset' and 'attribute', so $parser->subtitle() would
198
     * return the content of the element, while $parser->subtitle(false, 'type')
199
     * would return the value of the type attribute.
200
     *
201
     * @todo    Clarify overlap with getContent()
202
     * @param    string    $method    The name of the text construct we want
203
     * @param    array     $arguments    An array which we hope gives a 'param'
204
     * @return    string
205
     */
3021 mathias 206
    protected function getText($method, $arguments = Array()) {
1328 aurelien 207
        $offset = empty($arguments[0]) ? 0: $arguments[0];
208
        $attribute = empty($arguments[1]) ? false : $arguments[1];
209
        $tags = $this->model->getElementsByTagName($method);
210
 
211
        if ($tags->length <= $offset) {
212
            return false;
213
        }
214
 
215
        $content = $tags->item($offset);
216
 
217
        if (! $content->hasAttribute('type')) {
218
            $content->setAttribute('type', 'text');
219
        }
220
        $type = $content->getAttribute('type');
221
 
222
        if (! empty($attribute) and
223
            ! ($method == 'generator' and $attribute == 'name')) {
224
            if ($content->hasAttribute($attribute)) {
225
                return $content->getAttribute($attribute);
226
            } else if ($attribute == 'href' and $content->hasAttribute('uri')) {
227
                return $content->getAttribute('uri');
228
            }
229
            return false;
230
        }
231
 
232
        return $this->parseTextConstruct($content);
233
    }
234
 
235
    /**
236
     * Extract content appropriately from atom text constructs
237
     *
238
     * Because of different rules applied to the content element and other text
239
     * constructs, they are deployed as separate functions, but they share quite
240
     * a bit of processing. This method performs the core common process, which is
241
     * to apply the rules for different mime types in order to extract the content.
242
     *
243
     * @param   DOMNode $content    the text construct node to be parsed
244
     * @return String
245
     * @author James Stewart
246
     **/
247
    protected function parseTextConstruct(DOMNode $content) {
248
        if ($content->hasAttribute('type')) {
249
            $type = $content->getAttribute('type');
250
        } else {
251
            $type = 'text';
252
        }
253
 
254
        if (strpos($type, 'text/') === 0) {
255
            $type = 'text';
256
        }
257
 
258
        switch ($type) {
259
            case 'text':
260
            case 'html':
261
                return $content->textContent;
262
                break;
263
            case 'xhtml':
264
                $container = $content->getElementsByTagName('div');
265
                if ($container->length == 0) {
266
                    return false;
267
                }
268
                $contents = $container->item(0);
269
                if ($contents->hasChildNodes()) {
270
                    /* Iterate through, applying xml:base and store the result */
271
                    $result = '';
272
                    foreach ($contents->childNodes as $node) {
273
                        $result .= $this->traverseNode($node);
274
                    }
275
                    return $result;
276
                }
277
                break;
278
            case preg_match('@^[a-zA-Z]+/[a-zA-Z+]*xml@i', $type) > 0:
279
                return $content;
280
                break;
281
            case 'application/octet-stream':
282
            default:
283
                return base64_decode(trim($content->nodeValue));
284
                break;
285
        }
286
        return false;
287
    }
288
    /**
289
     * Get a category from the entry.
290
     *
291
     * A feed or entry can have any number of categories. A category can have the
292
     * attributes term, scheme and label.
293
     *
294
     * @param    string    $method    The name of the text construct we want
295
     * @param    array     $arguments    An array which we hope gives a 'param'
296
     * @return    string
297
     */
298
    function getCategory($method, $arguments) {
299
        $offset = empty($arguments[0]) ? 0: $arguments[0];
300
        $attribute = empty($arguments[1]) ? 'term' : $arguments[1];
301
        $categories = $this->model->getElementsByTagName('category');
302
        if ($categories->length <= $offset) {
303
            $category = $categories->item($offset);
304
            if ($category->hasAttribute($attribute)) {
305
                return $category->getAttribute($attribute);
306
            }
307
        }
308
        return false;
309
    }
310
 
311
    /**
312
     * This element must be present at least once with rel="feed". This element may be
313
     * present any number of further times so long as there is no clash. If no 'rel' is
314
     * present and we're asked for one, we follow the example of the Universal Feed
315
     * Parser and presume 'alternate'.
316
     *
317
     * @param    int    $offset    the position of the link within the container
318
     * @param    string    $attribute    the attribute name required
319
     * @param    array     an array of attributes to search by
320
     * @return    string    the value of the attribute
321
     */
322
    function getLink($offset = 0, $attribute = 'href', $params = false) {
323
        if (is_array($params) and !empty($params)) {
324
            $terms = array();
325
            $alt_predicate = '';
326
            $other_predicate = '';
327
 
328
            foreach ($params as $key => $value) {
329
                if ($key == 'rel' && $value == 'alternate') {
330
                    $alt_predicate = '[not(@rel) or @rel="alternate"]';
331
                } else {
332
                    $terms[] = "@$key='$value'";
333
                }
334
            }
335
            if (!empty($terms)) {
336
                $other_predicate = '[' . join(' and ', $terms) . ']';
337
            }
338
            $query =  $this->xpathPrefix . 'atom:link' . $alt_predicate . $other_predicate;
339
            $links = $this->xpath->query($query);
340
        } else {
341
            $links = $this->model->getElementsByTagName('link');
342
        }
343
        if ($links->length > $offset) {
344
            if ($links->item($offset)->hasAttribute($attribute)) {
345
                $value = $links->item($offset)->getAttribute($attribute);
346
                if ($attribute == 'href') {
347
                    $value = $this->addBase($value, $links->item($offset));
348
                }
349
                return $value;
350
            } else if ($attribute == 'rel') {
351
                return 'alternate';
352
            }
353
        }
354
        return false;
355
    }
356
}
357
 
358
?>