summaryrefslogtreecommitdiffstats
path: root/vendor/fguillot/picofeed/lib/PicoFeed/Parser/Atom.php
blob: 356453c9d2037da973aae85628c50cad58c35cd9 (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
366
<?php

namespace PicoFeed\Parser;

use SimpleXMLElement;
use PicoFeed\Filter\Filter;
use PicoFeed\Client\Url;

/**
 * Atom parser.
 *
 * @author  Frederic Guillot
 */
class Atom extends Parser
{
    /**
     * Supported namespaces.
     */
    protected $namespaces = array(
        'atom' => 'http://www.w3.org/2005/Atom',
    );

    /**
     * Get the path to the items XML tree.
     *
     * @param SimpleXMLElement $xml Feed xml
     *
     * @return SimpleXMLElement
     */
    public function getItemsTree(SimpleXMLElement $xml)
    {
        return XmlParser::getXPathResult($xml, 'atom:entry', $this->namespaces)
               ?: XmlParser::getXPathResult($xml, 'entry');
    }

    /**
     * Find the feed url.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findFeedUrl(SimpleXMLElement $xml, Feed $feed)
    {
        $feed->feed_url = $this->getUrl($xml, 'self');
    }

    /**
     * Find the site url.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findSiteUrl(SimpleXMLElement $xml, Feed $feed)
    {
        $feed->site_url = $this->getUrl($xml, 'alternate', true);
    }

    /**
     * Find the feed description.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findFeedDescription(SimpleXMLElement $xml, Feed $feed)
    {
        $description = XmlParser::getXPathResult($xml, 'atom:subtitle', $this->namespaces)
                       ?: XmlParser::getXPathResult($xml, 'subtitle');

        $feed->description = (string) current($description);
    }

    /**
     * Find the feed logo url.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findFeedLogo(SimpleXMLElement $xml, Feed $feed)
    {
        $logo = XmlParser::getXPathResult($xml, 'atom:logo', $this->namespaces)
                ?: XmlParser::getXPathResult($xml, 'logo');

        $feed->logo = (string) current($logo);
    }

    /**
     * Find the feed icon.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findFeedIcon(SimpleXMLElement $xml, Feed $feed)
    {
        $icon = XmlParser::getXPathResult($xml, 'atom:icon', $this->namespaces)
                ?: XmlParser::getXPathResult($xml, 'icon');

        $feed->icon = (string) current($icon);
    }

    /**
     * Find the feed title.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findFeedTitle(SimpleXMLElement $xml, Feed $feed)
    {
        $title = XmlParser::getXPathResult($xml, 'atom:title', $this->namespaces)
                ?: XmlParser::getXPathResult($xml, 'title');

        $feed->title = Filter::stripWhiteSpace((string) current($title)) ?: $feed->getSiteUrl();
    }

    /**
     * Find the feed language.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findFeedLanguage(SimpleXMLElement $xml, Feed $feed)
    {
        $language = XmlParser::getXPathResult($xml, '*[not(self::atom:entry)]/@xml:lang', $this->namespaces)
                    ?: XmlParser::getXPathResult($xml, '@xml:lang');

        $feed->language = (string) current($language);
    }

    /**
     * Find the feed id.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findFeedId(SimpleXMLElement $xml, Feed $feed)
    {
        $id = XmlParser::getXPathResult($xml, 'atom:id', $this->namespaces)
              ?: XmlParser::getXPathResult($xml, 'id');

        $feed->id = (string) current($id);
    }

    /**
     * Find the feed date.
     *
     * @param SimpleXMLElement      $xml  Feed xml
     * @param \PicoFeed\Parser\Feed $feed Feed object
     */
    public function findFeedDate(SimpleXMLElement $xml, Feed $feed)
    {
        $updated = XmlParser::getXPathResult($xml, 'atom:updated', $this->namespaces)
                   ?: XmlParser::getXPathResult($xml, 'updated');

        $feed->date = $this->date->getDateTime((string) current($updated));
    }

    /**
     * Find the item date.
     *
     * @param SimpleXMLElement      $entry Feed item
     * @param Item                  $item  Item object
     * @param \PicoFeed\Parser\Feed $feed  Feed object
     */
    public function findItemDate(SimpleXMLElement $entry, Item $item, Feed $feed)
    {
        $published = XmlParser::getXPathResult($entry, 'atom:published', $this->namespaces)
                     ?: XmlParser::getXPathResult($entry, 'published');

        $updated = XmlParser::getXPathResult($entry, 'atom:updated', $this->namespaces)
                   ?: XmlParser::getXPathResult($entry, 'updated');

        $published = !empty($published) ? $this->date->getDateTime((string) current($published)) : null;
        $updated = !empty($updated) ? $this->date->getDateTime((string) current($updated)) : null;

        if ($published === null && $updated === null) {
            $item->date = $feed->getDate(); // We use the feed date if there is no date for the item
        } elseif ($published !== null && $updated !== null) {
            $item->date = max($published, $updated); // We use the most recent date between published and updated
        } else {
            $item->date = $updated ?: $published;
        }
    }

    /**
     * Find the item title.
     *
     * @param SimpleXMLElement $entry Feed item
     * @param Item             $item  Item object
     */
    public function findItemTitle(SimpleXMLElement $entry, Item $item)
    {
        $title = XmlParser::getXPathResult($entry, 'atom:title', $this->namespaces)
                 ?: XmlParser::getXPathResult($entry, 'title');

        $item->title = Filter::stripWhiteSpace((string) current($title)) ?: $item->url;
    }

    /**
     * Find the item author.
     *
     * @param SimpleXMLElement      $xml   Feed
     * @param SimpleXMLElement      $entry Feed item
     * @param \PicoFeed\Parser\Item $item  Item object
     */
    public function findItemAuthor(SimpleXMLElement $xml, SimpleXMLElement $entry, Item $item)
    {
        $author = XmlParser::getXPathResult($entry, 'atom:author/atom:name', $this->namespaces)
                  ?: XmlParser::getXPathResult($entry, 'author/name')
                  ?: XmlParser::getXPathResult($xml, 'atom:author/atom:name', $this->namespaces)
                  ?: XmlParser::getXPathResult($xml, 'author/name');

        $item->author = (string) current($author);
    }

    /**
     * Find the item content.
     *
     * @param SimpleXMLElement      $entry Feed item
     * @param \PicoFeed\Parser\Item $item  Item object
     */
    public function findItemContent(SimpleXMLElement $entry, Item $item)
    {
        $item->content = $this->getContent($entry);
    }

    /**
     * Find the item URL.
     *
     * @param SimpleXMLElement      $entry Feed item
     * @param \PicoFeed\Parser\Item $item  Item object
     */
    public function findItemUrl(SimpleXMLElement $entry, Item $item)
    {
        $item->url = $this->getUrl($entry, 'alternate', true);
    }

    /**
     * Genereate the item id.
     *
     * @param SimpleXMLElement      $entry Feed item
     * @param \PicoFeed\Parser\Item $item  Item object
     * @param \PicoFeed\Parser\Feed $feed  Feed object
     */
    public function findItemId(SimpleXMLElement $entry, Item $item, Feed $feed)
    {
        $id = XmlParser::getXPathResult($entry, 'atom:id', $this->namespaces)
                  ?: XmlParser::getXPathResult($entry, 'id');

        if (!empty($id)) {
            $item->id = $this->generateId((string) current($id));
        } else {
            $item->id = $this->generateId(
                $item->getTitle(), $item->getUrl(), $item->getContent()
            );
        }
    }

    /**
     * Find the item enclosure.
     *
     * @param SimpleXMLElement      $entry Feed item
     * @param \PicoFeed\Parser\Item $item  Item object
     * @param \PicoFeed\Parser\Feed $feed  Feed object
     */
    public function findItemEnclosure(SimpleXMLElement $entry, Item $item, Feed $feed)
    {
        $enclosure = $this->findLink($entry, 'enclosure');

        if ($enclosure) {
            $item->enclosure_url = Url::resolve((string) $enclosure['href'], $feed->getSiteUrl());
            $item->enclosure_type = (string) $enclosure['type'];
        }
    }

    /**
     * Find the item language.
     *
     * @param SimpleXMLElement      $entry Feed item
     * @param \PicoFeed\Parser\Item $item  Item object
     * @param \PicoFeed\Parser\Feed $feed  Feed object
     */
    public function findItemLanguage(SimpleXMLElement $entry, Item $item, Feed $feed)
    {
        $language = XmlParser::getXPathResult($entry, './/@xml:lang');

        $item->language = (string) current($language) ?: $feed->language;
    }

    /**
     * Get the URL from a link tag.
     *
     * @param SimpleXMLElement $xml XML tag
     * @param string           $rel Link relationship: alternate, enclosure, related, self, via
     *
     * @return string
     */
    private function getUrl(SimpleXMLElement $xml, $rel, $fallback = false)
    {
        $link = $this->findLink($xml, $rel);

        if ($link) {
            return (string) $link['href'];
        }

        if ($fallback) {
            $link = $this->findLink($xml, '');

            return $link ? (string) $link['href'] : '';
        }

        return '';
    }

    /**
     * Get a link tag that match a relationship.
     *
     * @param SimpleXMLElement $xml XML tag
     * @param string           $rel Link relationship: alternate, enclosure, related, self, via
     *
     * @return SimpleXMLElement|null
     */
    private function findLink(SimpleXMLElement $xml, $rel)
    {
        $links = XmlParser::getXPathResult($xml, 'atom:link', $this->namespaces)
                ?: XmlParser::getXPathResult($xml, 'link');

        foreach ($links as $link) {
            if ($rel === (string) $link['rel']) {
                return $link;
            }
        }

        return;
    }

    /**
     * Get the entry content.
     *
     * @param SimpleXMLElement $entry XML Entry
     *
     * @return string
     */
    private function getContent(SimpleXMLElement $entry)
    {
        $content = current(
            XmlParser::getXPathResult($entry, 'atom:content', $this->namespaces