[XPathBridge] Allow multiple categories (#4038)

* [XPathAbstract] allow multiple categories

* fix feed icons in two bridges

* fix warning

* fix linter errors
This commit is contained in:
Niehztog 2024-03-31 18:46:07 +02:00 committed by GitHub
parent d23fd2522c
commit 1c3c85d8ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 additions and 19 deletions

View File

@ -57,4 +57,11 @@ class BlizzardNewsBridge extends XPathAbstract
} }
return 'https://news.blizzard.com/' . $locale; return 'https://news.blizzard.com/' . $locale;
} }
public function getIcon()
{
return <<<icon
https://blznews.akamaized.net/images/favicon-cb34a003c6f2f637ee8f4f7b406f3b9b120b918c04cabec7f03a760e708977ea9689a1c638f4396def8dce7b202cd007eae91946cc3c4a578aa8b5694226cfc6.ico
icon;
}
} }

View File

@ -23,6 +23,11 @@ class NiusBridge extends XPathAbstract
const XPATH_EXPRESSION_ITEM_CATEGORIES = './/div[@class="subtitle"]/text()'; const XPATH_EXPRESSION_ITEM_CATEGORIES = './/div[@class="subtitle"]/text()';
const SETTING_FIX_ENCODING = false; const SETTING_FIX_ENCODING = false;
public function getIcon()
{
return 'https://www.nius.de/favicon.ico';
}
protected function formatItemTitle($value) protected function formatItemTitle($value)
{ {
return strip_tags($value); return strip_tags($value);

View File

@ -439,7 +439,8 @@ abstract class XPathAbstract extends BridgeAbstract
} }
$isContent = $param === 'content'; $isContent = $param === 'content';
$value = $this->getItemValueOrNodeValue($typedResult, $isContent, $isContent && !$this->getSettingUseRawItemContent()); $isCategories = 'categories' === $param;
$value = $this->getItemValueOrNodeValue($typedResult, $isContent, $isContent && !$this->getSettingUseRawItemContent(), $isCategories);
$item->__set($param, $this->formatParamValue($param, $value)); $item->__set($param, $this->formatParamValue($param, $value));
} }
@ -459,7 +460,7 @@ abstract class XPathAbstract extends BridgeAbstract
*/ */
protected function formatParamValue($param, $value) protected function formatParamValue($param, $value)
{ {
$value = $this->fixEncoding($value); $value = is_array($value) ? array_map([$this, 'fixEncoding'], $value) : $this->fixEncoding($value);
switch ($param) { switch ($param) {
case 'title': case 'title':
return $this->formatItemTitle($value); return $this->formatItemTitle($value);
@ -572,12 +573,12 @@ abstract class XPathAbstract extends BridgeAbstract
* formatted as array. * formatted as array.
* Can be easily overwritten for in case the values need to be transformed into something * Can be easily overwritten for in case the values need to be transformed into something
* else. * else.
* @param string $value * @param string|array $value
* @return array * @return array
*/ */
protected function formatItemCategories($value) protected function formatItemCategories($value)
{ {
return [$value]; return is_array($value) ? $value : [$value];
} }
/** /**
@ -596,22 +597,21 @@ abstract class XPathAbstract extends BridgeAbstract
/** /**
* @param $typedResult * @param $typedResult
* @return string * @param bool $returnXML
* @param bool $escapeHtml
* @param bool $allowMultiple
* @return string|array
* @throws Exception
*/ */
protected function getItemValueOrNodeValue($typedResult, $returnXML = false, $escapeHtml = false) protected function getItemValueOrNodeValue($typedResult, $returnXML = false, $escapeHtml = false, $allowMultiple = false)
{ {
if ($typedResult instanceof \DOMNodeList) { if ($typedResult instanceof \DOMNodeList && !$allowMultiple) {
$item = $typedResult->item(0); $item = $typedResult->item(0);
if ($item instanceof \DOMElement) { $text = $this->extractNodeListContent($item, $returnXML);
// Don't escape XML } elseif ($typedResult instanceof \DOMNodeList && $allowMultiple) {
if ($returnXML) { $text = [];
return ($item->ownerDocument ?? $item)->saveXML($item); foreach ($typedResult as $item) {
} $text[] = $this->extractNodeListContent($item, $returnXML);
$text = $item->nodeValue;
} elseif ($item instanceof \DOMAttr) {
$text = $item->value;
} elseif ($item instanceof \DOMText) {
$text = $item->wholeText;
} }
} elseif (is_string($typedResult) && strlen($typedResult) > 0) { } elseif (is_string($typedResult) && strlen($typedResult) > 0) {
$text = $typedResult; $text = $typedResult;
@ -619,10 +619,46 @@ abstract class XPathAbstract extends BridgeAbstract
throw new \Exception('Unknown type of XPath expression result.'); throw new \Exception('Unknown type of XPath expression result.');
} }
if (is_array($text)) {
foreach ($text as &$element) {
$element = $this->cleanExtractedText($element, $escapeHtml, $returnXML);
}
} else {
$text = $this->cleanExtractedText($text, $escapeHtml, $returnXML);
}
return $text;
}
/**
* @param $item
* @param $returnXML
* @return false|string
* @throws Exception
*/
protected function extractNodeListContent($item, $returnXML)
{
if ($item instanceof \DOMElement) {
return $returnXML ? ($item->ownerDocument ?? $item)->saveXML($item) : $item->nodeValue;
} elseif ($item instanceof \DOMAttr) {
return $item->value;
} elseif ($item instanceof \DOMText) {
return $item->wholeText;
}
throw new \Exception('Unknown type of XPath expression result.');
}
/**
* @param $text
* @param $escapeHtml
* @param $returnXML
* @return string
*/
protected function cleanExtractedText($text, $escapeHtml, $returnXML)
{
$text = trim($text); $text = trim($text);
if ($escapeHtml) { if ($escapeHtml && !$returnXML) {
return htmlspecialchars($text); $text = htmlspecialchars($text);
} }
return $text; return $text;
} }