2020-11-08 10:22:41 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An alternative abstract class for bridges utilizing XPath expressions
|
|
|
|
*
|
|
|
|
* This class is meant as an alternative base class for bridge implementations.
|
|
|
|
* It offers preliminary functionality for generating feeds based on XPath
|
|
|
|
* expressions.
|
|
|
|
* As a minimum, extending classes should define XPath expressions pointing
|
|
|
|
* to the feed items contents in the class constants below. In case there is
|
|
|
|
* more manual fine tuning required, it offers a bunch of methods which can
|
|
|
|
* be overridden, for example in order to specify formatting of field values
|
|
|
|
* or more flexible definition of dynamic XPath expressions.
|
|
|
|
*
|
|
|
|
* This class extends {@see BridgeAbstract}, which means it incorporates and
|
|
|
|
* extends all of its functionality.
|
|
|
|
**/
|
|
|
|
abstract class XPathAbstract extends BridgeAbstract
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Source Web page URL (should provide either HTML or XML content)
|
|
|
|
* You can specify any website URL which serves data suited for display in RSS feeds
|
|
|
|
* (for example a news blog).
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getSourceUrl()} to read this parameter
|
|
|
|
*/
|
|
|
|
const FEED_SOURCE_URL = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting the feed title from the source page.
|
|
|
|
* If this is left blank or does not provide any data {@see BridgeAbstract::getName()}
|
|
|
|
* is used instead as the feed's title.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionTitle()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_FEED_TITLE = './/title';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting the feed favicon URL from the source page.
|
|
|
|
* If this is left blank or does not provide any data {@see BridgeAbstract::getIcon()}
|
|
|
|
* is used instead as the feed's favicon URL.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionIcon()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_FEED_ICON = './/link[@rel="icon"]/@href';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting the feed items from the source page
|
|
|
|
* Enter an XPath expression matching a list of dom nodes, each node containing one
|
|
|
|
* feed article item in total (usually a surrounding <div> or <span> tag). This will
|
|
|
|
* be the context nodes for all of the following expressions. This expression usually
|
|
|
|
* starts with a single forward slash.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionItem()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_ITEM = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item title from the item context
|
|
|
|
* This expression should match a node contained within each article item node
|
|
|
|
* containing the article headline. It should start with a dot followed by two
|
|
|
|
* forward slashes, referring to any descendant nodes of the article item node.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionItemTitle()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_ITEM_TITLE = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item's content from the item context
|
|
|
|
* This expression should match a node contained within each article item node
|
|
|
|
* containing the article content or description. It should start with a dot
|
|
|
|
* followed by two forward slashes, referring to any descendant nodes of the
|
|
|
|
* article item node.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionItemContent()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_ITEM_CONTENT = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-15 02:58:06 +03:00
|
|
|
/**
|
|
|
|
* Use raw item content
|
|
|
|
* Whether to use the raw item content or to replace certain characters with
|
|
|
|
* special significance in HTML by HTML entities (using the PHP function htmlspecialchars).
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getSettingUseRawItemContent()} to read this parameter
|
|
|
|
*/
|
|
|
|
const SETTING_USE_RAW_ITEM_CONTENT = false;
|
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item link from the item context
|
|
|
|
* This expression should match a node's attribute containing the article URL
|
|
|
|
* (usually the href attribute of an <a> tag). It should start with a dot
|
|
|
|
* followed by two forward slashes, referring to any descendant nodes of
|
|
|
|
* the article item node. Attributes can be selected by prepending an @ char
|
|
|
|
* before the attributes name.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionItemUri()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_ITEM_URI = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item author from the item context
|
|
|
|
* This expression should match a node contained within each article item
|
|
|
|
* node containing the article author's name. It should start with a dot
|
|
|
|
* followed by two forward slashes, referring to any descendant nodes of
|
|
|
|
* the article item node.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionItemAuthor()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_ITEM_AUTHOR = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item timestamp from the item context
|
|
|
|
* This expression should match a node or node's attribute containing the
|
|
|
|
* article timestamp or date (parsable by PHP's strtotime function). It
|
|
|
|
* should start with a dot followed by two forward slashes, referring to
|
|
|
|
* any descendant nodes of the article item node. Attributes can be
|
|
|
|
* selected by prepending an @ char before the attributes name.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionItemTimestamp()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_ITEM_TIMESTAMP = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting item enclosures (media content like
|
|
|
|
* images or movies) from the item context
|
|
|
|
* This expression should match a node's attribute containing an article
|
|
|
|
* image URL (usually the src attribute of an <img> tag or a style
|
|
|
|
* attribute). It should start with a dot followed by two forward slashes,
|
|
|
|
* referring to any descendant nodes of the article item node. Attributes
|
|
|
|
* can be selected by prepending an @ char before the attributes name.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionItemEnclosures()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_ITEM_ENCLOSURES = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item category from the item context
|
|
|
|
* This expression should match a node or node's attribute contained
|
|
|
|
* within each article item node containing the article category. This
|
|
|
|
* could be inside <div> or <span> tags or sometimes be hidden
|
|
|
|
* in a data attribute. It should start with a dot followed by two
|
|
|
|
* forward slashes, referring to any descendant nodes of the article
|
|
|
|
* item node. Attributes can be selected by prepending an @ char
|
|
|
|
* before the attributes name.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getExpressionItemCategories()} to read this parameter
|
|
|
|
*/
|
|
|
|
const XPATH_EXPRESSION_ITEM_CATEGORIES = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Fix encoding
|
|
|
|
* Set this to true for fixing feed encoding by invoking PHP's utf8_decode
|
|
|
|
* function on all extracted texts. Try this in case you see "broken" or
|
|
|
|
* "weird" characters in your feed where you'd normally expect umlauts
|
|
|
|
* or any other non-ascii characters.
|
|
|
|
*
|
|
|
|
* Use {@see XPathAbstract::getSettingFixEncoding()} to read this parameter
|
|
|
|
*/
|
|
|
|
const SETTING_FIX_ENCODING = false;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Internal storage for resulting feed name, automatically detected
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $feedName;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Internal storage for resulting feed name, automatically detected
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $feedUri;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Internal storage for resulting feed favicon, automatically detected
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $feedIcon;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->feedName ?: parent::getName();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
return $this->feedUri ?: parent::getURI();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
return $this->feedIcon ?: parent::getIcon();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Source Web page URL (should provide either HTML or XML content)
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getSourceUrl()
|
|
|
|
{
|
|
|
|
return static::FEED_SOURCE_URL;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting the feed title from the source page
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionTitle()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_FEED_TITLE;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting the feed favicon from the source page
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionIcon()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_FEED_ICON;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting the feed items from the source page
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionItem()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_ITEM;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item title from the item context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionItemTitle()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_ITEM_TITLE;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item's content from the item context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionItemContent()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_ITEM_CONTENT;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-15 02:58:06 +03:00
|
|
|
/**
|
|
|
|
* Use raw item content
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function getSettingUseRawItemContent(): bool
|
|
|
|
{
|
|
|
|
return static::SETTING_USE_RAW_ITEM_CONTENT;
|
|
|
|
}
|
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item link from the item context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionItemUri()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_ITEM_URI;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item author from the item context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionItemAuthor()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_ITEM_AUTHOR;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item timestamp from the item context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionItemTimestamp()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_ITEM_TIMESTAMP;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting item enclosures (media content like
|
|
|
|
* images or movies) from the item context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionItemEnclosures()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_ITEM_ENCLOSURES;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* XPath expression for extracting an item category from the item context
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpressionItemCategories()
|
|
|
|
{
|
|
|
|
return static::XPATH_EXPRESSION_ITEM_CATEGORIES;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Fix encoding
|
2023-09-15 02:58:06 +03:00
|
|
|
* @return bool
|
2020-11-08 10:22:41 +03:00
|
|
|
*/
|
2023-09-15 02:58:06 +03:00
|
|
|
protected function getSettingFixEncoding(): bool
|
2020-11-08 10:22:41 +03:00
|
|
|
{
|
|
|
|
return static::SETTING_FIX_ENCODING;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Internal helper method for quickly accessing all the user defined constants
|
|
|
|
* in derived classes
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
private function getParam($name)
|
|
|
|
{
|
|
|
|
switch ($name) {
|
|
|
|
case 'url':
|
|
|
|
return $this->getSourceUrl();
|
|
|
|
case 'feed_title':
|
|
|
|
return $this->getExpressionTitle();
|
|
|
|
case 'feed_icon':
|
|
|
|
return $this->getExpressionIcon();
|
|
|
|
case 'item':
|
|
|
|
return $this->getExpressionItem();
|
|
|
|
case 'title':
|
|
|
|
return $this->getExpressionItemTitle();
|
|
|
|
case 'content':
|
|
|
|
return $this->getExpressionItemContent();
|
2023-09-15 02:58:06 +03:00
|
|
|
case 'raw_content':
|
|
|
|
return $this->getSettingUseRawItemContent();
|
2020-11-08 10:22:41 +03:00
|
|
|
case 'uri':
|
|
|
|
return $this->getExpressionItemUri();
|
|
|
|
case 'author':
|
|
|
|
return $this->getExpressionItemAuthor();
|
|
|
|
case 'timestamp':
|
|
|
|
return $this->getExpressionItemTimestamp();
|
|
|
|
case 'enclosures':
|
|
|
|
return $this->getExpressionItemEnclosures();
|
|
|
|
case 'categories':
|
|
|
|
return $this->getExpressionItemCategories();
|
|
|
|
case 'fix_encoding':
|
|
|
|
return $this->getSettingFixEncoding();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Should provide the source website HTML content
|
|
|
|
* can be easily overwritten for example if special headers or auth infos are required
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function provideWebsiteContent()
|
|
|
|
{
|
|
|
|
return getContents($this->feedUri);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Should provide the feeds title
|
|
|
|
*
|
2022-08-06 23:46:28 +03:00
|
|
|
* @param \DOMXPath $xpath
|
2020-11-08 10:22:41 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-08-06 23:46:28 +03:00
|
|
|
protected function provideFeedTitle(\DOMXPath $xpath)
|
2020-11-08 10:22:41 +03:00
|
|
|
{
|
|
|
|
$title = $xpath->query($this->getParam('feed_title'));
|
|
|
|
if (count($title) === 1) {
|
2023-05-11 02:32:01 +03:00
|
|
|
return $this->fixEncoding($this->getItemValueOrNodeValue($title));
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Should provide the URL of the feed's favicon
|
|
|
|
*
|
2022-08-06 23:46:28 +03:00
|
|
|
* @param \DOMXPath $xpath
|
2020-11-08 10:22:41 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-08-06 23:46:28 +03:00
|
|
|
protected function provideFeedIcon(\DOMXPath $xpath)
|
2020-11-08 10:22:41 +03:00
|
|
|
{
|
|
|
|
$icon = $xpath->query($this->getParam('feed_icon'));
|
|
|
|
if (count($icon) === 1) {
|
2022-03-25 02:30:14 +03:00
|
|
|
return $this->cleanMediaUrl($this->getItemValueOrNodeValue($icon));
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Should provide the feed's items.
|
|
|
|
*
|
2022-08-06 23:46:28 +03:00
|
|
|
* @param \DOMXPath $xpath
|
2023-09-23 21:39:02 +03:00
|
|
|
* @return \DOMNodeList|false
|
2020-11-08 10:22:41 +03:00
|
|
|
*/
|
2022-08-06 23:46:28 +03:00
|
|
|
protected function provideFeedItems(\DOMXPath $xpath)
|
2020-11-08 10:22:41 +03:00
|
|
|
{
|
|
|
|
return @$xpath->query($this->getParam('item'));
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
$this->feedUri = $this->getParam('url');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
$webPageHtml = new \DOMDocument();
|
2020-11-08 10:22:41 +03:00
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
$webPageHtml->loadHTML($this->provideWebsiteContent());
|
|
|
|
libxml_clear_errors();
|
|
|
|
libxml_use_internal_errors(false);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-05-18 14:50:50 +03:00
|
|
|
// fix relative links
|
2023-06-17 18:53:00 +03:00
|
|
|
defaultLinkTo($webPageHtml, $webPageHtml->baseURI ?? $this->feedUri);
|
2023-05-18 14:50:50 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
$xpath = new \DOMXPath($webPageHtml);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
$this->feedName = $this->provideFeedTitle($xpath);
|
|
|
|
$this->feedIcon = $this->provideFeedIcon($xpath);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
$entries = $this->provideFeedItems($xpath);
|
|
|
|
if ($entries === false) {
|
2023-09-23 21:39:02 +03:00
|
|
|
// malformed
|
2020-11-08 10:22:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
foreach ($entries as $entry) {
|
2022-08-06 23:46:28 +03:00
|
|
|
$item = new FeedItem();
|
2020-11-08 10:22:41 +03:00
|
|
|
foreach (['title', 'content', 'uri', 'author', 'timestamp', 'enclosures', 'categories'] as $param) {
|
|
|
|
$expression = $this->getParam($param);
|
|
|
|
if ('' === $expression) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
//can be a string or DOMNodeList, depending on the expression result
|
|
|
|
$typedResult = @$xpath->evaluate($expression, $entry);
|
|
|
|
if (
|
2022-08-06 23:46:28 +03:00
|
|
|
$typedResult === false || ($typedResult instanceof \DOMNodeList && count($typedResult) === 0)
|
2020-11-08 10:22:41 +03:00
|
|
|
|| (is_string($typedResult) && strlen(trim($typedResult)) === 0)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-15 02:58:06 +03:00
|
|
|
$isContent = $param === 'content';
|
|
|
|
$value = $this->getItemValueOrNodeValue($typedResult, $isContent, $isContent && !$this->getSettingUseRawItemContent());
|
2023-05-11 02:31:34 +03:00
|
|
|
$item->__set($param, $this->formatParamValue($param, $value));
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
$itemId = $this->generateItemId($item);
|
|
|
|
if (null !== $itemId) {
|
|
|
|
$item->setUid($itemId);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* @param $param
|
|
|
|
* @param $value
|
|
|
|
* @return string|array
|
|
|
|
*/
|
|
|
|
protected function formatParamValue($param, $value)
|
|
|
|
{
|
|
|
|
$value = $this->fixEncoding($value);
|
|
|
|
switch ($param) {
|
|
|
|
case 'title':
|
|
|
|
return $this->formatItemTitle($value);
|
|
|
|
case 'content':
|
|
|
|
return $this->formatItemContent($value);
|
|
|
|
case 'uri':
|
|
|
|
return $this->formatItemUri($value);
|
|
|
|
case 'author':
|
|
|
|
return $this->formatItemAuthor($value);
|
|
|
|
case 'timestamp':
|
|
|
|
return $this->formatItemTimestamp($value);
|
|
|
|
case 'enclosures':
|
2021-10-14 12:18:00 +03:00
|
|
|
return $this->formatItemEnclosures($value);
|
2020-11-08 10:22:41 +03:00
|
|
|
case 'categories':
|
2021-10-14 12:18:00 +03:00
|
|
|
return $this->formatItemCategories($value);
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Formats the title of a feed item. Takes extracted raw title and returns it formatted
|
|
|
|
* as string.
|
|
|
|
* Can be easily overwritten for in case the value needs to be transformed into something
|
|
|
|
* else.
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function formatItemTitle($value)
|
|
|
|
{
|
|
|
|
return $value;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Formats the timestamp of a feed item. Takes extracted raw timestamp and returns unix
|
|
|
|
* timestamp as integer.
|
|
|
|
* Can be easily overwritten for example if a special format has to be expected on the
|
|
|
|
* source website.
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function formatItemContent($value)
|
|
|
|
{
|
|
|
|
return $value;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Formats the URI of a feed item. Takes extracted raw URI and returns it formatted
|
|
|
|
* as string.
|
|
|
|
* Can be easily overwritten for in case the value needs to be transformed into something
|
|
|
|
* else.
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function formatItemUri($value)
|
|
|
|
{
|
|
|
|
if (strlen($value) === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
2024-01-12 03:31:01 +03:00
|
|
|
if (
|
|
|
|
strpos($value, 'http://') === 0
|
|
|
|
|| strpos($value, 'https://') === 0
|
|
|
|
) {
|
2020-11-08 10:22:41 +03:00
|
|
|
return $value;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
return urljoin($this->feedUri, $value);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Formats the author of a feed item. Takes extracted raw author and returns it formatted
|
|
|
|
* as string.
|
|
|
|
* Can be easily overwritten for in case the value needs to be transformed into something
|
|
|
|
* else.
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function formatItemAuthor($value)
|
|
|
|
{
|
|
|
|
return $value;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Formats the timestamp of a feed item. Takes extracted raw timestamp and returns unix
|
|
|
|
* timestamp as integer.
|
|
|
|
* Can be easily overwritten for example if a special format has to be expected on the
|
|
|
|
* source website.
|
|
|
|
* @param string $value
|
|
|
|
* @return false|int
|
|
|
|
*/
|
|
|
|
protected function formatItemTimestamp($value)
|
|
|
|
{
|
|
|
|
return strtotime($value);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Formats the enclosures of a feed item. Takes extracted raw enclosures and returns them
|
|
|
|
* formatted as array.
|
|
|
|
* Can be easily overwritten for in case the values need to be transformed into something
|
|
|
|
* else.
|
|
|
|
* @param string $value
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatItemEnclosures($value)
|
|
|
|
{
|
2022-03-25 02:30:14 +03:00
|
|
|
return [$this->cleanMediaUrl($value)];
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Formats the categories of a feed item. Takes extracted raw categories and returns them
|
|
|
|
* formatted as array.
|
|
|
|
* Can be easily overwritten for in case the values need to be transformed into something
|
|
|
|
* else.
|
|
|
|
* @param string $value
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function formatItemCategories($value)
|
|
|
|
{
|
|
|
|
return [$value];
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
2022-03-25 02:30:14 +03:00
|
|
|
* @param $mediaUrl
|
2020-11-08 10:22:41 +03:00
|
|
|
* @return string|void
|
|
|
|
*/
|
2022-03-25 02:30:14 +03:00
|
|
|
protected function cleanMediaUrl($mediaUrl)
|
2020-11-08 10:22:41 +03:00
|
|
|
{
|
2022-06-19 03:19:32 +03:00
|
|
|
$pattern = '~(?:http(?:s)?:)?[\/a-zA-Z0-9\-=_,\.\%]+\.(?:jpg|gif|png|jpeg|ico|mp3|webp){1}~i';
|
2022-03-25 02:30:14 +03:00
|
|
|
$result = preg_match($pattern, $mediaUrl, $matches);
|
2020-11-08 10:22:41 +03:00
|
|
|
if (1 !== $result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return urljoin($this->feedUri, $matches[0]);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* @param $typedResult
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-09-15 02:58:06 +03:00
|
|
|
protected function getItemValueOrNodeValue($typedResult, $returnXML = false, $escapeHtml = false)
|
2020-11-08 10:22:41 +03:00
|
|
|
{
|
2022-08-06 23:46:28 +03:00
|
|
|
if ($typedResult instanceof \DOMNodeList) {
|
2020-11-08 10:22:41 +03:00
|
|
|
$item = $typedResult->item(0);
|
2022-08-06 23:46:28 +03:00
|
|
|
if ($item instanceof \DOMElement) {
|
2023-05-11 02:31:34 +03:00
|
|
|
// Don't escape XML
|
|
|
|
if ($returnXML) {
|
|
|
|
return ($item->ownerDocument ?? $item)->saveXML($item);
|
|
|
|
}
|
|
|
|
$text = $item->nodeValue;
|
2022-08-06 23:46:28 +03:00
|
|
|
} elseif ($item instanceof \DOMAttr) {
|
2023-05-11 02:31:34 +03:00
|
|
|
$text = $item->value;
|
2022-08-06 23:46:28 +03:00
|
|
|
} elseif ($item instanceof \DOMText) {
|
2023-05-11 02:31:34 +03:00
|
|
|
$text = $item->wholeText;
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
|
|
|
} elseif (is_string($typedResult) && strlen($typedResult) > 0) {
|
2023-05-11 02:31:34 +03:00
|
|
|
$text = $typedResult;
|
|
|
|
} else {
|
|
|
|
throw new \Exception('Unknown type of XPath expression result.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$text = trim($text);
|
|
|
|
|
2023-09-15 02:58:06 +03:00
|
|
|
if ($escapeHtml) {
|
2023-05-11 02:31:34 +03:00
|
|
|
return htmlspecialchars($text);
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
2023-05-11 02:31:34 +03:00
|
|
|
return $text;
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Fixes feed encoding by invoking PHP's utf8_decode function on extracted texts.
|
|
|
|
* Useful in case of "broken" or "weird" characters in the feed where you'd normally
|
|
|
|
* expect umlauts.
|
|
|
|
*
|
|
|
|
* @param $input
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function fixEncoding($input)
|
|
|
|
{
|
|
|
|
return $this->getParam('fix_encoding') ? utf8_decode($input) : $input;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-08 10:22:41 +03:00
|
|
|
/**
|
|
|
|
* Allows overriding default mechanism determining items Uid's
|
|
|
|
*
|
|
|
|
* @param FeedItem $item
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2022-08-06 23:46:28 +03:00
|
|
|
protected function generateItemId(FeedItem $item)
|
2020-11-08 10:22:41 +03:00
|
|
|
{
|
2022-08-06 23:46:28 +03:00
|
|
|
return null;
|
2020-11-08 10:22:41 +03:00
|
|
|
}
|
|
|
|
}
|