From 3261b7bafbb8e7cd0003c0cfbf26e4d4a4b65def Mon Sep 17 00:00:00 2001 From: maTh <1645099+math-GH@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:00:17 +0200 Subject: i18n improved: dotted path -> dot-notation (#6317) * dotted path -> dot-notation * dot-notation -> dot notation * rename json_dotpath => json_dotnotation * Update app/i18n/fr/sub.php Co-authored-by: Alexandre Alapetite * Update app/i18n/fr/sub.php Co-authored-by: Alexandre Alapetite * Update app/i18n/fr/sub.php Co-authored-by: Alexandre Alapetite * Update app/i18n/nl/sub.php Co-authored-by: Frans de Jonge * Update app/i18n/nl/sub.php Co-authored-by: Frans de Jonge * Update app/i18n/nl/sub.php Co-authored-by: Frans de Jonge * Update app/i18n/nl/sub.php Co-authored-by: Frans de Jonge * Rename corresponding class --------- Co-authored-by: Alexandre Alapetite Co-authored-by: Frans de Jonge --- app/Utils/dotNotationUtil.php | 205 ++++++++++++++++++++++++++++++++++++++++++ app/Utils/dotpathUtil.php | 204 ----------------------------------------- 2 files changed, 205 insertions(+), 204 deletions(-) create mode 100644 app/Utils/dotNotationUtil.php delete mode 100644 app/Utils/dotpathUtil.php (limited to 'app/Utils') diff --git a/app/Utils/dotNotationUtil.php b/app/Utils/dotNotationUtil.php new file mode 100644 index 000000000..8ec20d349 --- /dev/null +++ b/app/Utils/dotNotationUtil.php @@ -0,0 +1,205 @@ +|array|mixed $array + * @param string|null $key + * @param mixed $default + * @return mixed + */ + public static function get($array, ?string $key, mixed $default = null) { + if (!static::accessible($array)) { + return static::value($default); + } + /** @var \ArrayAccess|array $array */ + if ($key === null || $key === '') { + return $array; + } + + // Compatibility with brackets path such as `items[0].value` + $key = preg_replace('/\[(\d+)\]/', '.$1', $key); + if ($key === null) { + return null; + } + + if (static::exists($array, $key)) { + return $array[$key]; + } + if (strpos($key, '.') === false) { + return $array[$key] ?? static::value($default); + } + foreach (explode('.', $key) as $segment) { + if (static::accessible($array) && static::exists($array, $segment)) { + $array = $array[$segment]; + } else { + return static::value($default); + } + } + return $array; + } + + /** + * Get a string from an array using "dot" notation. + * + * @param \ArrayAccess|array|mixed $array + * @param string|null $key + */ + public static function getString($array, ?string $key): ?string { + $result = self::get($array, $key, null); + return is_string($result) ? $result : null; + } + + /** + * Determine whether the given value is array accessible. + * + * @param mixed $value + * @return bool + */ + private static function accessible($value): bool { + return is_array($value) || $value instanceof \ArrayAccess; + } + + /** + * Determine if the given key exists in the provided array. + * + * @param \ArrayAccess|array|mixed $array + * @param string $key + * @return bool + */ + private static function exists($array, string $key): bool { + if ($array instanceof \ArrayAccess) { + return $array->offsetExists($key); + } + if (is_array($array)) { + return array_key_exists($key, $array); + } + return false; + } + + /** @param mixed $value */ + private static function value($value): mixed { + return $value instanceof Closure ? $value() : $value; + } + + /** + * Convert a JSON object to a RSS document + * mapping fields from the JSON object into RSS equivalents + * according to the dot-separated paths + * + * @param array $jf json feed + * @param string $feedSourceUrl the source URL for the feed + * @param array $dotPaths dot paths to map JSON into RSS + * @param string $defaultRssTitle Default title of the RSS feed, if not already provided in dotPath `feedTitle` + */ + public static function convertJsonToRss(array $jf, string $feedSourceUrl, array $dotPaths, string $defaultRssTitle = ''): ?string { + if (!isset($dotPaths['item']) || $dotPaths['item'] === '') { + return null; //no definition of item path, but we can't scrape anything without knowing this + } + + $view = new FreshRSS_View(); + $view->_path('index/rss.phtml'); + $view->internal_rendering = true; + $view->rss_url = htmlspecialchars($feedSourceUrl, ENT_COMPAT, 'UTF-8'); + $view->html_url = $view->rss_url; + $view->entries = []; + + $view->rss_title = isset($dotPaths['feedTitle']) + ? (htmlspecialchars(FreshRSS_dotNotation_Util::getString($jf, $dotPaths['feedTitle']) ?? '', ENT_COMPAT, 'UTF-8') ?: $defaultRssTitle) + : $defaultRssTitle; + + $jsonItems = FreshRSS_dotNotation_Util::get($jf, $dotPaths['item']); + if (!is_array($jsonItems) || count($jsonItems) === 0) { + return null; + } + + foreach ($jsonItems as $jsonItem) { + $rssItem = []; + $rssItem['link'] = isset($dotPaths['itemUri']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotPaths['itemUri']) ?? '' : ''; + if (empty($rssItem['link'])) { + continue; + } + $rssItem['title'] = isset($dotPaths['itemTitle']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotPaths['itemTitle']) ?? '' : ''; + $rssItem['author'] = isset($dotPaths['itemAuthor']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotPaths['itemAuthor']) ?? '' : ''; + $rssItem['timestamp'] = isset($dotPaths['itemTimestamp']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotPaths['itemTimestamp']) ?? '' : ''; + + //get simple content, but if a path for HTML content has been provided, replace the simple content with HTML content + $rssItem['content'] = isset($dotPaths['itemContent']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotPaths['itemContent']) ?? '' : ''; + $rssItem['content'] = isset($dotPaths['itemContentHTML']) + ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotPaths['itemContentHTML']) ?? '' + : $rssItem['content']; + + if (isset($dotPaths['itemTimeFormat']) && is_string($dotPaths['itemTimeFormat'])) { + $dateTime = DateTime::createFromFormat($dotPaths['itemTimeFormat'], $rssItem['timestamp']); + if ($dateTime != false) { + $rssItem['timestamp'] = $dateTime->format(DateTime::ATOM); + } + } + + if (isset($dotPaths['itemCategories'])) { + $jsonItemCategories = FreshRSS_dotNotation_Util::get($jsonItem, $dotPaths['itemCategories']); + if (is_string($jsonItemCategories) && $jsonItemCategories !== '') { + $rssItem['tags'] = [$jsonItemCategories]; + } elseif (is_array($jsonItemCategories) && count($jsonItemCategories) > 0) { + $rssItem['tags'] = []; + foreach ($jsonItemCategories as $jsonItemCategory) { + if (is_string($jsonItemCategory)) { + $rssItem['tags'][] = $jsonItemCategory; + } + } + } + } + + $rssItem['thumbnail'] = isset($dotPaths['itemThumbnail']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotPaths['itemThumbnail']) ?? '' : ''; + + //Enclosures? + if (isset($dotPaths['itemAttachment'])) { + $jsonItemAttachments = FreshRSS_dotNotation_Util::get($jsonItem, $dotPaths['itemAttachment']); + if (is_array($jsonItemAttachments) && count($jsonItemAttachments) > 0) { + $rssItem['attachments'] = []; + foreach ($jsonItemAttachments as $attachment) { + $rssAttachment = []; + $rssAttachment['url'] = isset($dotPaths['itemAttachmentUrl']) + ? FreshRSS_dotNotation_Util::getString($attachment, $dotPaths['itemAttachmentUrl']) + : ''; + $rssAttachment['type'] = isset($dotPaths['itemAttachmentType']) + ? FreshRSS_dotNotation_Util::getString($attachment, $dotPaths['itemAttachmentType']) + : ''; + $rssAttachment['length'] = isset($dotPaths['itemAttachmentLength']) + ? FreshRSS_dotNotation_Util::get($attachment, $dotPaths['itemAttachmentLength']) + : ''; + $rssItem['attachments'][] = $rssAttachment; + } + } + } + + if (isset($dotPaths['itemUid'])) { + $rssItem['guid'] = FreshRSS_dotNotation_Util::getString($jsonItem, $dotPaths['itemUid']); + } + + if (empty($rssItem['guid'])) { + $rssItem['guid'] = 'urn:sha1:' . sha1($rssItem['title'] . $rssItem['content'] . $rssItem['link']); + } + + if ($rssItem['title'] != '' || $rssItem['content'] != '' || $rssItem['link'] != '') { + // HTML-encoding/escaping of the relevant fields (all except 'content') + foreach (['author', 'guid', 'link', 'thumbnail', 'timestamp', 'tags', 'title'] as $key) { + if (!empty($rssItem[$key]) && is_string($rssItem[$key])) { + $rssItem[$key] = Minz_Helper::htmlspecialchars_utf8($rssItem[$key]); + } + } + $view->entries[] = FreshRSS_Entry::fromArray($rssItem); + } + } + + return $view->renderToString(); + } +} diff --git a/app/Utils/dotpathUtil.php b/app/Utils/dotpathUtil.php deleted file mode 100644 index 939434c5a..000000000 --- a/app/Utils/dotpathUtil.php +++ /dev/null @@ -1,204 +0,0 @@ -|array|mixed $array - * @param string|null $key - * @param mixed $default - * @return mixed - */ - public static function get($array, ?string $key, mixed $default = null) { - if (!static::accessible($array)) { - return static::value($default); - } - /** @var \ArrayAccess|array $array */ - if ($key === null || $key === '') { - return $array; - } - - // Compatibility with brackets path such as `items[0].value` - $key = preg_replace('/\[(\d+)\]/', '.$1', $key); - if ($key === null) { - return null; - } - - if (static::exists($array, $key)) { - return $array[$key]; - } - if (strpos($key, '.') === false) { - return $array[$key] ?? static::value($default); - } - foreach (explode('.', $key) as $segment) { - if (static::accessible($array) && static::exists($array, $segment)) { - $array = $array[$segment]; - } else { - return static::value($default); - } - } - return $array; - } - - /** - * Get a string from an array using "dot" notation. - * - * @param \ArrayAccess|array|mixed $array - * @param string|null $key - */ - public static function getString($array, ?string $key): ?string { - $result = self::get($array, $key, null); - return is_string($result) ? $result : null; - } - - /** - * Determine whether the given value is array accessible. - * - * @param mixed $value - * @return bool - */ - private static function accessible($value): bool { - return is_array($value) || $value instanceof \ArrayAccess; - } - - /** - * Determine if the given key exists in the provided array. - * - * @param \ArrayAccess|array|mixed $array - * @param string $key - * @return bool - */ - private static function exists($array, string $key): bool { - if ($array instanceof \ArrayAccess) { - return $array->offsetExists($key); - } - if (is_array($array)) { - return array_key_exists($key, $array); - } - return false; - } - - /** @param mixed $value */ - private static function value($value): mixed { - return $value instanceof Closure ? $value() : $value; - } - - /** - * Convert a JSON object to a RSS document - * mapping fields from the JSON object into RSS equivalents - * according to the dot-separated paths - * - * @param array $jf json feed - * @param string $feedSourceUrl the source URL for the feed - * @param array $dotPaths dot paths to map JSON into RSS - * @param string $defaultRssTitle Default title of the RSS feed, if not already provided in dotPath `feedTitle` - */ - public static function convertJsonToRss(array $jf, string $feedSourceUrl, array $dotPaths, string $defaultRssTitle = ''): ?string { - if (!isset($dotPaths['item']) || $dotPaths['item'] === '') { - return null; //no definition of item path, but we can't scrape anything without knowing this - } - - $view = new FreshRSS_View(); - $view->_path('index/rss.phtml'); - $view->internal_rendering = true; - $view->rss_url = htmlspecialchars($feedSourceUrl, ENT_COMPAT, 'UTF-8'); - $view->html_url = $view->rss_url; - $view->entries = []; - - $view->rss_title = isset($dotPaths['feedTitle']) - ? (htmlspecialchars(FreshRSS_dotpath_Util::getString($jf, $dotPaths['feedTitle']) ?? '', ENT_COMPAT, 'UTF-8') ?: $defaultRssTitle) - : $defaultRssTitle; - - $jsonItems = FreshRSS_dotpath_Util::get($jf, $dotPaths['item']); - if (!is_array($jsonItems) || count($jsonItems) === 0) { - return null; - } - - foreach ($jsonItems as $jsonItem) { - $rssItem = []; - $rssItem['link'] = isset($dotPaths['itemUri']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemUri']) ?? '' : ''; - if (empty($rssItem['link'])) { - continue; - } - $rssItem['title'] = isset($dotPaths['itemTitle']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemTitle']) ?? '' : ''; - $rssItem['author'] = isset($dotPaths['itemAuthor']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemAuthor']) ?? '' : ''; - $rssItem['timestamp'] = isset($dotPaths['itemTimestamp']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemTimestamp']) ?? '' : ''; - - //get simple content, but if a path for HTML content has been provided, replace the simple content with HTML content - $rssItem['content'] = isset($dotPaths['itemContent']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemContent']) ?? '' : ''; - $rssItem['content'] = isset($dotPaths['itemContentHTML']) - ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemContentHTML']) ?? '' - : $rssItem['content']; - - if (isset($dotPaths['itemTimeFormat']) && is_string($dotPaths['itemTimeFormat'])) { - $dateTime = DateTime::createFromFormat($dotPaths['itemTimeFormat'], $rssItem['timestamp']); - if ($dateTime != false) { - $rssItem['timestamp'] = $dateTime->format(DateTime::ATOM); - } - } - - if (isset($dotPaths['itemCategories'])) { - $jsonItemCategories = FreshRSS_dotpath_Util::get($jsonItem, $dotPaths['itemCategories']); - if (is_string($jsonItemCategories) && $jsonItemCategories !== '') { - $rssItem['tags'] = [$jsonItemCategories]; - } elseif (is_array($jsonItemCategories) && count($jsonItemCategories) > 0) { - $rssItem['tags'] = []; - foreach ($jsonItemCategories as $jsonItemCategory) { - if (is_string($jsonItemCategory)) { - $rssItem['tags'][] = $jsonItemCategory; - } - } - } - } - - $rssItem['thumbnail'] = isset($dotPaths['itemThumbnail']) ? FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemThumbnail']) ?? '' : ''; - - //Enclosures? - if (isset($dotPaths['itemAttachment'])) { - $jsonItemAttachments = FreshRSS_dotpath_Util::get($jsonItem, $dotPaths['itemAttachment']); - if (is_array($jsonItemAttachments) && count($jsonItemAttachments) > 0) { - $rssItem['attachments'] = []; - foreach ($jsonItemAttachments as $attachment) { - $rssAttachment = []; - $rssAttachment['url'] = isset($dotPaths['itemAttachmentUrl']) - ? FreshRSS_dotpath_Util::getString($attachment, $dotPaths['itemAttachmentUrl']) - : ''; - $rssAttachment['type'] = isset($dotPaths['itemAttachmentType']) - ? FreshRSS_dotpath_Util::getString($attachment, $dotPaths['itemAttachmentType']) - : ''; - $rssAttachment['length'] = isset($dotPaths['itemAttachmentLength']) - ? FreshRSS_dotpath_Util::get($attachment, $dotPaths['itemAttachmentLength']) - : ''; - $rssItem['attachments'][] = $rssAttachment; - } - } - } - - if (isset($dotPaths['itemUid'])) { - $rssItem['guid'] = FreshRSS_dotpath_Util::getString($jsonItem, $dotPaths['itemUid']); - } - - if (empty($rssItem['guid'])) { - $rssItem['guid'] = 'urn:sha1:' . sha1($rssItem['title'] . $rssItem['content'] . $rssItem['link']); - } - - if ($rssItem['title'] != '' || $rssItem['content'] != '' || $rssItem['link'] != '') { - // HTML-encoding/escaping of the relevant fields (all except 'content') - foreach (['author', 'guid', 'link', 'thumbnail', 'timestamp', 'tags', 'title'] as $key) { - if (!empty($rssItem[$key]) && is_string($rssItem[$key])) { - $rssItem[$key] = Minz_Helper::htmlspecialchars_utf8($rssItem[$key]); - } - } - $view->entries[] = FreshRSS_Entry::fromArray($rssItem); - } - } - - return $view->renderToString(); - } -} -- cgit v1.2.3