X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Ffeed%2FFeedBuilder.php;h=ed62af26e1c4b26ae4eb481e37ab32f0a5d9988a;hb=53054b2bf6a919fd4ff9b44b6ad1986f21f488b6;hp=40bd4f153393553bfda8803a3b81e739bb9e155b;hpb=cf92b4dd1521241eefc58eaf6dcd202cd83969d8;p=github%2Fshaarli%2FShaarli.git diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php index 40bd4f15..ed62af26 100644 --- a/application/feed/FeedBuilder.php +++ b/application/feed/FeedBuilder.php @@ -1,4 +1,5 @@ linkDB = $linkDB; $this->formatter = $formatter; - $this->feedType = $feedType; $this->serverInfo = $serverInfo; - $this->userInput = $userInput; $this->isLoggedIn = $isLoggedIn; } /** * Build data for feed templates. * + * @param string $feedType Type of feed (RSS/ATOM). + * @param array $userInput $_GET. + * * @return array Formatted data for feeds templates. */ - public function buildData() + public function buildData(string $feedType, ?array $userInput) { // Search for untagged bookmarks - if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) { - $this->userInput['searchtags'] = false; + if (isset($this->userInput['searchtags']) && empty($userInput['searchtags'])) { + $userInput['searchtags'] = false; } // Optionally filter the results: - $linksToDisplay = $this->linkDB->search($this->userInput); + $linksToDisplay = $this->linkDB->search($userInput ?? [], null, false, false, true); - $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); + $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput); // Can't use array_keys() because $link is a LinkDB instance and not a real array. - $keys = array(); + $keys = []; foreach ($linksToDisplay as $key => $value) { $keys[] = $key; } $pageaddr = escape(index_url($this->serverInfo)); $this->formatter->addContextData('index_url', $pageaddr); - $linkDisplayed = array(); + $linkDisplayed = []; for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { - $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr); + $linkDisplayed[$keys[$i]] = $this->buildItem($feedType, $linksToDisplay[$keys[$i]], $pageaddr); } - $data['language'] = $this->getTypeLanguage(); - $data['last_update'] = $this->getLatestDateFormatted(); + $data['language'] = $this->getTypeLanguage($feedType); + $data['last_update'] = $this->getLatestDateFormatted($feedType); $data['show_dates'] = !$this->hideDates || $this->isLoggedIn; - // Remove leading slash from REQUEST_URI. - $data['self_link'] = escape(server_url($this->serverInfo)) - . escape($this->serverInfo['REQUEST_URI']); + // Remove leading path from REQUEST_URI (already contained in $pageaddr). + $requestUri = preg_replace('#(.*?/)(feed.*)#', '$2', escape($this->serverInfo['REQUEST_URI'])); + $data['self_link'] = $pageaddr . $requestUri; $data['index_url'] = $pageaddr; $data['usepermalinks'] = $this->usePermalinks === true; $data['links'] = $linkDisplayed; @@ -146,32 +133,63 @@ class FeedBuilder return $data; } + /** + * Set this to true to use permalinks instead of direct bookmarks. + * + * @param boolean $usePermalinks true to force permalinks. + */ + public function setUsePermalinks($usePermalinks) + { + $this->usePermalinks = $usePermalinks; + } + + /** + * Set this to true to hide timestamps in feeds. + * + * @param boolean $hideDates true to enable. + */ + public function setHideDates($hideDates) + { + $this->hideDates = $hideDates; + } + + /** + * Set the locale. Used to show feed language. + * + * @param string $locale The locale (eg. 'fr_FR.UTF8'). + */ + public function setLocale($locale) + { + $this->locale = strtolower($locale); + } + /** * Build a feed item (one per shaare). * + * @param string $feedType Type of feed (RSS/ATOM). * @param Bookmark $link Single link array extracted from LinkDB. * @param string $pageaddr Index URL. * * @return array Link array with feed attributes. */ - protected function buildItem($link, $pageaddr) + protected function buildItem(string $feedType, $link, $pageaddr) { $data = $this->formatter->format($link); - $data['guid'] = $pageaddr . '?' . $data['shorturl']; + $data['guid'] = rtrim($pageaddr, '/') . '/shaare/' . $data['shorturl']; if ($this->usePermalinks === true) { - $permalink = ''. t('Direct link') .''; + $permalink = '' . t('Direct link') . ''; } else { - $permalink = ''. t('Permalink') .''; + $permalink = '' . t('Permalink') . ''; } $data['description'] .= PHP_EOL . PHP_EOL . '
— ' . $permalink; - $data['pub_iso_date'] = $this->getIsoDate($data['created']); + $data['pub_iso_date'] = $this->getIsoDate($feedType, $data['created']); // atom:entry elements MUST contain exactly one atom:updated element. if (!empty($link->getUpdated())) { - $data['up_iso_date'] = $this->getIsoDate($data['updated'], DateTime::ATOM); + $data['up_iso_date'] = $this->getIsoDate($feedType, $data['updated'], DateTime::ATOM); } else { - $data['up_iso_date'] = $this->getIsoDate($data['created'], DateTime::ATOM); + $data['up_iso_date'] = $this->getIsoDate($feedType, $data['created'], DateTime::ATOM); } // Save the more recent item. @@ -185,52 +203,24 @@ class FeedBuilder return $data; } - /** - * Set this to true to use permalinks instead of direct bookmarks. - * - * @param boolean $usePermalinks true to force permalinks. - */ - public function setUsePermalinks($usePermalinks) - { - $this->usePermalinks = $usePermalinks; - } - - /** - * Set this to true to hide timestamps in feeds. - * - * @param boolean $hideDates true to enable. - */ - public function setHideDates($hideDates) - { - $this->hideDates = $hideDates; - } - - /** - * Set the locale. Used to show feed language. - * - * @param string $locale The locale (eg. 'fr_FR.UTF8'). - */ - public function setLocale($locale) - { - $this->locale = strtolower($locale); - } - /** * Get the language according to the feed type, based on the locale: * * - RSS format: en-us (default: 'en-en'). * - ATOM format: fr (default: 'en'). * + * @param string $feedType Type of feed (RSS/ATOM). + * * @return string The language. */ - public function getTypeLanguage() + protected function getTypeLanguage(string $feedType) { // Use the locale do define the language, if available. if (!empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) { - $length = ($this->feedType === self::$FEED_RSS) ? 5 : 2; + $length = ($feedType === self::$FEED_RSS) ? 5 : 2; return str_replace('_', '-', substr($this->locale, 0, $length)); } - return ($this->feedType === self::$FEED_RSS) ? 'en-en' : 'en'; + return ($feedType === self::$FEED_RSS) ? 'en-en' : 'en'; } /** @@ -238,32 +228,35 @@ class FeedBuilder * * Return an empty string if invalid DateTime is passed. * + * @param string $feedType Type of feed (RSS/ATOM). + * * @return string Formatted date. */ - protected function getLatestDateFormatted() + protected function getLatestDateFormatted(string $feedType) { if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) { return ''; } - $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; + $type = ($feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; return $this->latestDate->format($type); } /** * Get ISO date from DateTime according to feed type. * + * @param string $feedType Type of feed (RSS/ATOM). * @param DateTime $date Date to format. * @param string|bool $format Force format. * * @return string Formatted date. */ - protected function getIsoDate(DateTime $date, $format = false) + protected function getIsoDate(string $feedType, DateTime $date, $format = false) { if ($format !== false) { return $date->format($format); } - if ($this->feedType == self::$FEED_RSS) { + if ($feedType == self::$FEED_RSS) { return $date->format(DateTime::RSS); } return $date->format(DateTime::ATOM); @@ -275,21 +268,22 @@ class FeedBuilder * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. * If 'nb' is set to 'all', display all filtered bookmarks (max parameter). * - * @param int $max maximum number of bookmarks to display. + * @param int $max maximum number of bookmarks to display. + * @param array $userInput $_GET. * * @return int number of bookmarks to display. */ - public function getNbLinks($max) + protected function getNbLinks($max, ?array $userInput) { - if (empty($this->userInput['nb'])) { + if (empty($userInput['nb'])) { return self::$DEFAULT_NB_LINKS; } - if ($this->userInput['nb'] == 'all') { + if ($userInput['nb'] == 'all') { return $max; } - $intNb = intval($this->userInput['nb']); + $intNb = intval($userInput['nb']); if (!is_int($intNb) || $intNb == 0) { return self::$DEFAULT_NB_LINKS; }