123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- namespace PicoFeed\Parser;
- use SimpleXMLElement;
- use PicoFeed\Filter\Filter;
- use PicoFeed\Client\Url;
- /**
- * Atom parser.
- *
- * @package PicoFeed\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->setFeedUrl($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->setSiteUrl($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->setDescription(XmlParser::getValue($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->setLogo(XmlParser::getValue($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->setIcon(XmlParser::getValue($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->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($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->setLanguage(XmlParser::getValue($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->setId(XmlParser::getValue($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->setDate($this->getDateParser()->getDateTime(XmlParser::getValue($updated)));
- }
- /**
- * Find the item published date.
- *
- * @param SimpleXMLElement $entry Feed item
- * @param Item $item Item object
- * @param \PicoFeed\Parser\Feed $feed Feed object
- */
- public function findItemPublishedDate(SimpleXMLElement $entry, Item $item, Feed $feed)
- {
- $date = XmlParser::getXPathResult($entry, 'atom:published', $this->namespaces)
- ?: XmlParser::getXPathResult($entry, 'published');
- $item->setPublishedDate(!empty($date) ? $this->getDateParser()->getDateTime((string) current($date)) : null);
- }
- /**
- * Find the item updated date.
- *
- * @param SimpleXMLElement $entry Feed item
- * @param Item $item Item object
- * @param \PicoFeed\Parser\Feed $feed Feed object
- */
- public function findItemUpdatedDate(SimpleXMLElement $entry, Item $item, Feed $feed)
- {
- $date = XmlParser::getXPathResult($entry, 'atom:updated', $this->namespaces)
- ?: XmlParser::getXPathResult($entry, 'updated');
- $item->setUpdatedDate(!empty($date) ? $this->getDateParser()->getDateTime((string) current($date)) : null);
- }
- /**
- * 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->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($title)) ?: $item->getUrl());
- }
- /**
- * 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->setAuthor(XmlParser::getValue($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->setContent($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->setUrl($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->setId($this->generateId(XmlParser::getValue($id)));
- } else {
- $item->setId($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->setEnclosureUrl(Url::resolve((string) $enclosure['href'], $feed->getSiteUrl()));
- $item->setEnclosureType((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->setLanguage(XmlParser::getValue($language) ?: $feed->getLanguage());
- }
- /**
- * Find the item categories.
- *
- * @param SimpleXMLElement $entry Feed item
- * @param Item $item Item object
- * @param Feed $feed Feed object
- */
- public function findItemCategories(SimpleXMLElement $entry, Item $item, Feed $feed)
- {
- $categories = XmlParser::getXPathResult($entry, 'atom:category/@term', $this->namespaces)
- ?: XmlParser::getXPathResult($entry, 'category/@term');
- $item->setCategoriesFromXml($categories);
- }
- /**
- * 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 null;
- }
- /**
- * 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)
- ?: XmlParser::getXPathResult($entry, 'content')
- );
- if (!empty($content) && count($content->children())) {
- $xml_string = '';
- foreach ($content->children() as $child) {
- $xml_string .= $child->asXML();
- }
- return $xml_string;
- } elseif (trim((string) $content) !== '') {
- return (string) $content;
- }
- $summary = XmlParser::getXPathResult($entry, 'atom:summary', $this->namespaces)
- ?: XmlParser::getXPathResult($entry, 'summary');
- return (string) current($summary);
- }
- }
|