miniflux-legacy/vendor/miniflux/picofeed/lib/PicoFeed/Parser/Parser.php

397 lines
11 KiB
PHP
Raw Permalink Normal View History

2013-02-18 03:48:21 +01:00
<?php
namespace PicoFeed\Parser;
2013-02-18 03:48:21 +01:00
use PicoFeed\Processor\ContentFilterProcessor;
use PicoFeed\Processor\ContentGeneratorProcessor;
use PicoFeed\Processor\ItemPostProcessor;
use PicoFeed\Processor\ScraperProcessor;
use SimpleXMLElement;
2015-04-28 18:08:42 +02:00
use PicoFeed\Client\Url;
use PicoFeed\Encoding\Encoding;
use PicoFeed\Filter\Filter;
use PicoFeed\Logging\Logger;
2013-02-18 03:48:21 +01:00
2014-03-17 02:35:57 +01:00
/**
* Base parser class.
2014-03-17 02:35:57 +01:00
*
2016-07-29 03:14:51 +02:00
* @package PicoFeed\Parser
2014-03-17 02:35:57 +01:00
* @author Frederic Guillot
*/
2016-07-29 03:14:51 +02:00
abstract class Parser implements ParserInterface
2013-02-18 03:48:21 +01:00
{
2014-05-20 20:20:27 +02:00
/**
* Config object.
2014-05-20 20:20:27 +02:00
*
* @var \PicoFeed\Config\Config
2014-05-20 20:20:27 +02:00
*/
private $config;
2014-05-20 20:20:27 +02:00
2014-03-17 02:35:57 +01:00
/**
* DateParser object.
2014-03-17 02:35:57 +01:00
*
* @var \PicoFeed\Parser\DateParser
2014-03-17 02:35:57 +01:00
*/
private $dateParser;
2014-05-20 20:20:27 +02:00
/**
* Hash algorithm used to generate item id, any value supported by PHP, see hash_algos().
2014-05-20 20:20:27 +02:00
*
* @var string
*/
private $hash_algo = 'sha256';
2014-03-17 02:35:57 +01:00
/**
* Feed content (XML data).
2014-03-17 02:35:57 +01:00
*
* @var string
*/
2013-02-18 03:48:21 +01:00
protected $content = '';
/**
* Fallback url.
*
* @var string
*/
protected $fallback_url = '';
2014-03-17 02:35:57 +01:00
/**
* XML namespaces supported by parser.
2014-03-17 02:35:57 +01:00
*
2014-05-20 20:20:27 +02:00
* @var array
2014-03-17 02:35:57 +01:00
*/
2014-05-20 20:20:27 +02:00
protected $namespaces = array();
2014-03-17 02:35:57 +01:00
2015-07-19 17:19:26 +02:00
/**
* XML namespaces used in document.
2015-07-19 17:19:26 +02:00
*
* @var array
*/
protected $used_namespaces = array();
2014-10-19 20:42:31 +02:00
/**
* Item Post Processor instance
2014-10-19 20:42:31 +02:00
*
* @access private
* @var ItemPostProcessor
2014-10-19 20:42:31 +02:00
*/
private $itemPostProcessor;
2013-02-18 03:48:21 +01:00
2014-03-17 02:35:57 +01:00
/**
* Constructor.
2014-03-17 02:35:57 +01:00
*
* @param string $content Feed content
* @param string $http_encoding HTTP encoding (headers)
* @param string $fallback_url Fallback url when the feed provide relative or broken url
2014-03-17 02:35:57 +01:00
*/
public function __construct($content, $http_encoding = '', $fallback_url = '')
2013-02-18 03:48:21 +01:00
{
$this->fallback_url = $fallback_url;
2014-05-25 14:47:03 +02:00
$xml_encoding = XmlParser::getEncodingFromXmlTag($content);
2013-10-04 05:14:39 +02:00
// Strip XML tag to avoid multiple encoding/decoding in the next XML processing
$this->content = Filter::stripXmlTag($content);
2013-06-01 20:57:17 +02:00
// Encode everything in UTF-8
Logger::setMessage(get_called_class().': HTTP Encoding "'.$http_encoding.'" ; XML Encoding "'.$xml_encoding.'"');
2014-05-25 14:47:03 +02:00
$this->content = Encoding::convert($this->content, $xml_encoding ?: $http_encoding);
$this->itemPostProcessor = new ItemPostProcessor($this->config);
$this->itemPostProcessor->register(new ContentGeneratorProcessor($this->config));
$this->itemPostProcessor->register(new ContentFilterProcessor($this->config));
2013-02-18 03:48:21 +01:00
}
2014-05-20 20:20:27 +02:00
/**
* Parse the document.
2017-06-21 01:41:55 +02:00
* @return Feed
* @throws MalformedXmlException
2014-05-20 20:20:27 +02:00
*/
public function execute()
{
Logger::setMessage(get_called_class().': begin parsing');
2014-05-20 20:20:27 +02:00
$xml = XmlParser::getSimpleXml($this->content);
if ($xml === false) {
2015-06-21 15:56:36 +02:00
Logger::setMessage(get_called_class().': Applying XML workarounds');
$this->content = Filter::normalizeData($this->content);
$xml = XmlParser::getSimpleXml($this->content);
if ($xml === false) {
Logger::setMessage(get_called_class().': XML parsing error');
Logger::setMessage(XmlParser::getErrors());
throw new MalformedXmlException('XML parsing error');
}
2014-05-20 20:20:27 +02:00
}
2015-07-19 17:19:26 +02:00
$this->used_namespaces = $xml->getNamespaces(true);
$xml = $this->registerSupportedNamespaces($xml);
2014-05-20 20:20:27 +02:00
$feed = new Feed();
2014-05-20 20:20:27 +02:00
$this->findFeedUrl($xml, $feed);
$this->checkFeedUrl($feed);
$this->findSiteUrl($xml, $feed);
$this->checkSiteUrl($feed);
2014-05-20 20:20:27 +02:00
$this->findFeedTitle($xml, $feed);
2014-10-19 20:42:31 +02:00
$this->findFeedDescription($xml, $feed);
2014-05-20 20:20:27 +02:00
$this->findFeedLanguage($xml, $feed);
$this->findFeedId($xml, $feed);
$this->findFeedDate($xml, $feed);
2014-10-19 20:42:31 +02:00
$this->findFeedLogo($xml, $feed);
$this->findFeedIcon($xml, $feed);
2014-05-20 20:20:27 +02:00
foreach ($this->getItemsTree($xml) as $entry) {
2015-07-19 17:19:26 +02:00
$entry = $this->registerSupportedNamespaces($entry);
$item = new Item();
2015-03-26 00:59:41 +01:00
$item->xml = $entry;
2015-07-19 17:19:26 +02:00
$item->namespaces = $this->used_namespaces;
2015-03-26 00:59:41 +01:00
2014-05-20 20:20:27 +02:00
$this->findItemAuthor($xml, $entry, $item);
2014-05-20 20:20:27 +02:00
$this->findItemUrl($entry, $item);
$this->checkItemUrl($feed, $item);
2014-05-20 20:20:27 +02:00
$this->findItemTitle($entry, $item);
$this->findItemContent($entry, $item);
// Id generation can use the item url/title/content (order is important)
2014-05-20 20:20:27 +02:00
$this->findItemId($entry, $item, $feed);
$this->findItemDate($entry, $item, $feed);
2014-05-20 20:20:27 +02:00
$this->findItemEnclosure($entry, $item, $feed);
$this->findItemLanguage($entry, $item, $feed);
2017-06-12 03:08:00 +02:00
$this->findItemCategories($entry, $item, $feed);
2014-10-19 20:42:31 +02:00
$this->itemPostProcessor->execute($feed, $item);
2014-05-20 20:20:27 +02:00
$feed->items[] = $item;
}
Logger::setMessage(get_called_class().PHP_EOL.$feed);
2014-05-20 20:20:27 +02:00
return $feed;
}
/**
* Check if the feed url is correct.
*
* @param Feed $feed Feed object
*/
public function checkFeedUrl(Feed $feed)
{
if ($feed->getFeedUrl() === '') {
$feed->feedUrl = $this->fallback_url;
} else {
$feed->feedUrl = Url::resolve($feed->getFeedUrl(), $this->fallback_url);
}
}
/**
* Check if the site url is correct.
*
* @param Feed $feed Feed object
*/
public function checkSiteUrl(Feed $feed)
{
if ($feed->getSiteUrl() === '') {
$feed->siteUrl = Url::base($feed->getFeedUrl());
} else {
$feed->siteUrl = Url::resolve($feed->getSiteUrl(), $this->fallback_url);
}
}
/**
* Check if the item url is correct.
*
* @param Feed $feed Feed object
* @param Item $item Item object
*/
public function checkItemUrl(Feed $feed, Item $item)
{
$item->url = Url::resolve($item->getUrl(), $feed->getSiteUrl());
}
2016-07-29 03:14:51 +02:00
/**
* 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)
{
$this->findItemPublishedDate($entry, $item, $feed);
$this->findItemUpdatedDate($entry, $item, $feed);
2016-12-26 23:32:18 +01:00
if ($item->getPublishedDate() === null) {
// Use the updated date if available, otherwise use the feed date
$item->setPublishedDate($item->getUpdatedDate() ?: $feed->getDate());
2016-07-29 03:14:51 +02:00
}
2016-12-26 23:32:18 +01:00
if ($item->getUpdatedDate() === null) {
// Use the published date as fallback
$item->setUpdatedDate($item->getPublishedDate());
}
// Use the most recent of published and updated dates
$item->setDate(max($item->getPublishedDate(), $item->getUpdatedDate()));
2016-07-29 03:14:51 +02:00
}
2014-03-17 02:35:57 +01:00
/**
* Get Item Post Processor instance
2014-03-17 02:35:57 +01:00
*
* @access public
* @return ItemPostProcessor
2014-03-17 02:35:57 +01:00
*/
public function getItemPostProcessor()
2013-02-18 03:48:21 +01:00
{
return $this->itemPostProcessor;
2013-02-18 03:48:21 +01:00
}
2014-03-17 02:35:57 +01:00
/**
* Get DateParser instance
2014-03-17 02:35:57 +01:00
*
* @access public
* @return DateParser
2014-03-17 02:35:57 +01:00
*/
public function getDateParser()
2013-02-18 03:48:21 +01:00
{
if ($this->dateParser === null) {
2017-04-05 22:05:48 +02:00
$this->dateParser = new DateParser($this->config);
}
return $this->dateParser;
}
2014-03-17 02:35:57 +01:00
/**
* Generate a unique id for an entry (hash all arguments).
2014-03-17 02:35:57 +01:00
*
* @return string
2014-03-17 02:35:57 +01:00
*/
public function generateId()
{
2014-05-20 20:20:27 +02:00
return hash($this->hash_algo, implode(func_get_args()));
}
2013-10-24 00:39:21 +02:00
2014-03-17 02:35:57 +01:00
/**
* Return true if the given language is "Right to Left".
2014-03-17 02:35:57 +01:00
*
* @static
* @param string $language Language: fr-FR, en-US
2014-03-17 02:35:57 +01:00
* @return bool
*/
public static function isLanguageRTL($language)
{
$language = strtolower($language);
2014-05-20 20:20:27 +02:00
$rtl_languages = array(
'ar', // Arabic (ar-**)
'fa', // Farsi (fa-**)
'ur', // Urdu (ur-**)
'ps', // Pashtu (ps-**)
'syr', // Syriac (syr-**)
'dv', // Divehi (dv-**)
'he', // Hebrew (he-**)
'yi', // Yiddish (yi-**)
);
2014-03-17 02:35:57 +01:00
2014-05-20 20:20:27 +02:00
foreach ($rtl_languages as $prefix) {
if (strpos($language, $prefix) === 0) {
return true;
}
}
2014-03-17 02:35:57 +01:00
2014-05-20 20:20:27 +02:00
return false;
}
2014-03-17 02:35:57 +01:00
2014-05-20 20:20:27 +02:00
/**
* Set Hash algorithm used for id generation.
*
* @param string $algo Algorithm name
* @return \PicoFeed\Parser\Parser
2014-05-20 20:20:27 +02:00
*/
public function setHashAlgo($algo)
{
$this->hash_algo = $algo ?: $this->hash_algo;
return $this;
}
2014-03-17 02:35:57 +01:00
2014-05-20 20:20:27 +02:00
/**
* Set config object.
*
* @param \PicoFeed\Config\Config $config Config instance
* @return \PicoFeed\Parser\Parser
2014-05-20 20:20:27 +02:00
*/
public function setConfig($config)
{
$this->config = $config;
2017-01-10 00:20:34 +01:00
$this->itemPostProcessor->setConfig($config);
2014-05-20 20:20:27 +02:00
return $this;
}
2014-03-17 02:35:57 +01:00
2014-05-20 20:20:27 +02:00
/**
* Enable the content grabber.
2014-05-20 20:20:27 +02:00
*
* @return \PicoFeed\Parser\Parser
2014-05-20 20:20:27 +02:00
*/
2014-10-19 20:42:31 +02:00
public function disableContentFiltering()
2014-05-20 20:20:27 +02:00
{
$this->itemPostProcessor->unregister('PicoFeed\Processor\ContentFilterProcessor');
return $this;
2014-05-20 20:20:27 +02:00
}
2014-03-17 02:35:57 +01:00
2014-05-20 20:20:27 +02:00
/**
* Enable the content grabber.
2014-05-20 20:20:27 +02:00
*
2016-03-31 04:43:08 +02:00
* @param bool $needsRuleFile true if only pages with rule files should be
* scraped
* @param null|\Closure $scraperCallback Callback function that gets called for each
* scraper execution
* @return \PicoFeed\Parser\Parser
2014-05-20 20:20:27 +02:00
*/
2016-03-31 04:43:08 +02:00
public function enableContentGrabber($needsRuleFile = false, $scraperCallback = null)
2014-05-20 20:20:27 +02:00
{
$processor = new ScraperProcessor($this->config);
2016-03-31 04:43:08 +02:00
if ($needsRuleFile) {
$processor->getScraper()->disableCandidateParser();
}
2016-03-31 04:43:08 +02:00
if ($scraperCallback !== null) {
$processor->setExecutionCallback($scraperCallback);
}
$this->itemPostProcessor->register($processor);
return $this;
2014-10-19 20:42:31 +02:00
}
2014-05-20 20:20:27 +02:00
2014-10-19 20:42:31 +02:00
/**
* Set ignored URLs for the content grabber.
*
* @param array $urls URLs
* @return \PicoFeed\Parser\Parser
2014-10-19 20:42:31 +02:00
*/
public function setGrabberIgnoreUrls(array $urls)
{
$this->itemPostProcessor->getProcessor('PicoFeed\Processor\ScraperProcessor')->ignoreUrls($urls);
return $this;
2014-03-17 02:35:57 +01:00
}
2015-07-19 17:19:26 +02:00
/**
* Register all supported namespaces to be used within an xpath query.
*
* @param SimpleXMLElement $xml Feed xml
2015-07-19 17:19:26 +02:00
* @return SimpleXMLElement
*/
public function registerSupportedNamespaces(SimpleXMLElement $xml)
{
foreach ($this->namespaces as $prefix => $ns) {
$xml->registerXPathNamespace($prefix, $ns);
}
return $xml;
}
2013-02-18 03:48:21 +01:00
}