summaryrefslogtreecommitdiff
path: root/plugins/OStatus/extlib/XML/Feed/Parser/Atom.php
blob: c7e218a1e6470a433d8831b42ef714107832c4c6 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Atom feed class for XML_Feed_Parser
 *
 * 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: Atom.php,v 1.29 2008/03/30 22:00:36 jystewart Exp $
 * @link       http://pear.php.net/package/XML_Feed_Parser/
*/

/**
 * This is the class that determines how we manage Atom 1.0 feeds
 * 
 * How we deal with constructs:
 *  date - return as unix datetime for use with the 'date' function unless specified otherwise
 *  text - return as is. optional parameter will give access to attributes
 *  person - defaults to name, but parameter based access
 *
 * @author    James Stewart <james@jystewart.net>
 * @version    Release: 1.0.3
 * @package XML_Feed_Parser
 */
class XML_Feed_Parser_Atom extends XML_Feed_Parser_Type
{
    /**
     * The URI of the RelaxNG schema used to (optionally) validate the feed 
     * @var string
     */
    private $relax = 'atom.rnc';

    /**
     * We're likely to use XPath, so let's keep it global 
     * @var DOMXPath
     */
    public $xpath;

    /**
     * When performing XPath queries we will use this prefix 
     * @var string
     */
    private $xpathPrefix = '//';

    /**
     * The feed type we are parsing 
     * @var string
     */
    public $version = 'Atom 1.0';

    /** 
     * The class used to represent individual items 
     * @var string
     */
    protected $itemClass = 'XML_Feed_Parser_AtomElement';
    
    /** 
     * The element containing entries 
     * @var string
     */
    protected $itemElement = 'entry';

    /**
     * Here we map those elements we're not going to handle individually
     * to the constructs they are. The optional second parameter in the array
     * tells the parser whether to 'fall back' (not apt. at the feed level) or
     * fail if the element is missing. If the parameter is not set, the function
     * will simply return false and leave it to the client to decide what to do.
     * @var array
     */
    protected $map = array(
        'author' => array('Person'),
        'contributor' => array('Person'),
        'icon' => array('Text'),
        'logo' => array('Text'),
        'id' => array('Text', 'fail'),
        'rights' => array('Text'),
        'subtitle' => array('Text'),
        'title' => array('Text', 'fail'),
        'updated' => array('Date', 'fail'),
        'link' => array('Link'),
        'generator' => array('Text'),
        'category' => array('Category'));

    /**
     * Here we provide a few mappings for those very special circumstances in
     * which it makes sense to map back to the RSS2 spec. Key is RSS2 version
     * value is an array consisting of the equivalent in atom and any attributes
     * needed to make the mapping.
     * @var array
     */
    protected $compatMap = array(
        'guid' => array('id'),
        'links' => array('link'),
        'tags' => array('category'),
        'contributors' => array('contributor'));

    /**
     * Our constructor does nothing more than its parent.
     * 
     * @param    DOMDocument    $xml    A DOM object representing the feed
     * @param    bool (optional) $string    Whether or not to validate this feed
     */
    function __construct(DOMDocument $model, $strict = false)
    {
        $this->model = $model;

        if ($strict) {
            if (! $this->model->relaxNGValidateSource($this->relax)) {
                throw new XML_Feed_Parser_Exception('Failed required validation');
            }
        }

        $this->xpath = new DOMXPath($this->model);
        $this->xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
        $this->numberEntries = $this->count('entry');
    }

    /**
     * Implement retrieval of an entry based on its ID for atom feeds.
     *
     * This function uses XPath to get the entry based on its ID. If DOMXPath::evaluate
     * is available, we also use that to store a reference to the entry in the array
     * used by getEntryByOffset so that method does not have to seek out the entry
     * if it's requested that way.
     * 
     * @param    string    $id    any valid Atom ID.
     * @return    XML_Feed_Parser_AtomElement
     */
    function getEntryById($id)
    {
        if (isset($this->idMappings[$id])) {
            return $this->entries[$this->idMappings[$id]];
        }

        $entries = $this->xpath->query("//atom:entry[atom:id='$id']");

        if ($entries->length > 0) {
            $xmlBase = $entries->item(0)->baseURI;
            $entry = new $this->itemClass($entries->item(0), $this, $xmlBase);
            
            if (in_array('evaluate', get_class_methods($this->xpath))) {
                $offset = $this->xpath->evaluate("count(preceding-sibling::atom:entry)", $entries->item(0));
                $this->entries[$offset] = $entry;
            }

            $this->idMappings[$id] = $entry;

            return $entry;
        }
        
    }

    /**
     * Retrieves data from a person construct.
     *
     * Get a person construct. We default to the 'name' element but allow
     * access to any of the elements.
     * 
     * @param    string    $method    The name of the person construct we want
     * @param    array     $arguments    An array which we hope gives a 'param'
     * @return    string|false
     */
    protected function getPerson($method, $arguments)
    {
        $offset = empty($arguments[0]) ? 0 : $arguments[0];
        $parameter = empty($arguments[1]['param']) ? 'name' : $arguments[1]['param'];
        $section = $this->model->getElementsByTagName($method);
        
        if ($parameter == 'url') {
            $parameter = 'uri';
        }

        if ($section->length <= $offset) {
            return false;
        }

        $param = $section->item($offset)->getElementsByTagName($parameter);
        if ($param->length == 0) {
            return false;
        }
        return $param->item(0)->nodeValue;
    }

    /**
     * Retrieves an element's content where that content is a text construct.
     *
     * Get a text construct. When calling this method, the two arguments
     * allowed are 'offset' and 'attribute', so $parser->subtitle() would
     * return the content of the element, while $parser->subtitle(false, 'type')
     * would return the value of the type attribute.
     *
     * @todo    Clarify overlap with getContent()
     * @param    string    $method    The name of the text construct we want
     * @param    array     $arguments    An array which we hope gives a 'param'
     * @return    string
     */
    protected function getText($method, $arguments)
    {
        $offset = empty($arguments[0]) ? 0: $arguments[0];
        $attribute = empty($arguments[1]) ? false : $arguments[1];
        $tags = $this->model->getElementsByTagName($method);

        if ($tags->length <= $offset) {
            return false;
        }

        $content = $tags->item($offset);

        if (! $content->hasAttribute('type')) {
            $content->setAttribute('type', 'text');
        }
        $type = $content->getAttribute('type');

        if (! empty($attribute) and 
            ! ($method == 'generator' and $attribute == 'name')) {
            if ($content->hasAttribute($attribute)) {
                return $content->getAttribute($attribute);
            } else if ($attribute == 'href' and $content->hasAttribute('uri')) {
                return $content->getAttribute('uri');
            }
            return false;
        }

        return $this->parseTextConstruct($content);
    }
    
    /**
     * Extract content appropriately from atom text constructs
     *
     * Because of different rules applied to the content element and other text
     * constructs, they are deployed as separate functions, but they share quite
     * a bit of processing. This method performs the core common process, which is
     * to apply the rules for different mime types in order to extract the content.
     *
     * @param   DOMNode $content    the text construct node to be parsed
     * @return String
     * @author James Stewart
     **/
    protected function parseTextConstruct(DOMNode $content)
    {
        if ($content->hasAttribute('type')) {
            $type = $content->getAttribute('type');
        } else {
            $type = 'text';
        }

        if (strpos($type, 'text/') === 0) {
            $type = 'text';
        }

        switch ($type) {
            case 'text':
            case 'html':
                return $content->textContent;
                break;
            case 'xhtml':
                $container = $content->getElementsByTagName('div');
                if ($container->length == 0) {
                    return false;
                }
                $contents = $container->item(0);
                if ($contents->hasChildNodes()) {
                    /* Iterate through, applying xml:base and store the result */
                    $result = '';
                    foreach ($contents->childNodes as $node) {
                        $result .= $this->traverseNode($node);
                    }
                    return $result;
                }
                break;
            case preg_match('@^[a-zA-Z]+/[a-zA-Z+]*xml@i', $type) > 0:
                return $content;
                break;
            case 'application/octet-stream':
            default:
                return base64_decode(trim($content->nodeValue));
                break;
        }
        return false;
    }
    /**
     * Get a category from the entry.
     *
     * A feed or entry can have any number of categories. A category can have the
     * attributes term, scheme and label.
     * 
     * @param    string    $method    The name of the text construct we want
     * @param    array     $arguments    An array which we hope gives a 'param'
     * @return    string
     */
    function getCategory($method, $arguments)
    {
        $offset = empty($arguments[0]) ? 0: $arguments[0];
        $attribute = empty($arguments[1]) ? 'term' : $arguments[1];
        $categories = $this->model->getElementsByTagName('category');
        if ($categories->length <= $offset) {
            $category = $categories->item($offset);
            if ($category->hasAttribute($attribute)) {
                return $category->getAttribute($attribute);
            }
        }
        return false;
    }

    /**
     * This element must be present at least once with rel="feed". This element may be 
     * present any number of further times so long as there is no clash. If no 'rel' is 
     * present and we're asked for one, we follow the example of the Universal Feed
     * Parser and presume 'alternate'.
     *
     * @param    int    $offset    the position of the link within the container
     * @param    string    $attribute    the attribute name required
     * @param    array     an array of attributes to search by
     * @return    string    the value of the attribute
     */
    function getLink($offset = 0, $attribute = 'href', $params = false)
    {
        if (is_array($params) and !empty($params)) {
            $terms = array();
            $alt_predicate = '';
            $other_predicate = '';

            foreach ($params as $key => $value) {
                if ($key == 'rel' && $value == 'alternate') {
                    $alt_predicate = '[not(@rel) or @rel="alternate"]';
                } else {
                    $terms[] = "@$key='$value'";
                }
            }
            if (!empty($terms)) {
                $other_predicate = '[' . join(' and ', $terms) . ']';
            }
            $query =  $this->xpathPrefix . 'atom:link' . $alt_predicate . $other_predicate;
            $links = $this->xpath->query($query);
        } else {
            $links = $this->model->getElementsByTagName('link');
        }
        if ($links->length > $offset) {
            if ($links->item($offset)->hasAttribute($attribute)) {
                $value = $links->item($offset)->getAttribute($attribute);
                if ($attribute == 'href') {
                    $value = $this->addBase($value, $links->item($offset));
                }
                return $value;
            } else if ($attribute == 'rel') {
                return 'alternate';
            }
        }
        return false;
    }
}

?>