From dfc650aa239d3a2c028d0ba13132ce75b4f4c0b4 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Mon, 3 Dec 2018 00:08:04 +0100 Subject: namespacing: \Shaarli\Feed\{Cache,CachedPage,FeedBuilder} Signed-off-by: VirtualTam --- application/feed/FeedBuilder.php | 299 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 application/feed/FeedBuilder.php (limited to 'application/feed/FeedBuilder.php') diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php new file mode 100644 index 00000000..dcfd2c89 --- /dev/null +++ b/application/feed/FeedBuilder.php @@ -0,0 +1,299 @@ +linkDB = $linkDB; + $this->feedType = $feedType; + $this->serverInfo = $serverInfo; + $this->userInput = $userInput; + $this->isLoggedIn = $isLoggedIn; + } + + /** + * Build data for feed templates. + * + * @return array Formatted data for feeds templates. + */ + public function buildData() + { + // Search for untagged links + if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) { + $this->userInput['searchtags'] = false; + } + + // Optionally filter the results: + $linksToDisplay = $this->linkDB->filterSearch($this->userInput); + + $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); + + // Can't use array_keys() because $link is a LinkDB instance and not a real array. + $keys = array(); + foreach ($linksToDisplay as $key => $value) { + $keys[] = $key; + } + + $pageaddr = escape(index_url($this->serverInfo)); + $linkDisplayed = array(); + for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { + $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr); + } + + $data['language'] = $this->getTypeLanguage(); + $data['last_update'] = $this->getLatestDateFormatted(); + $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']); + $data['index_url'] = $pageaddr; + $data['usepermalinks'] = $this->usePermalinks === true; + $data['links'] = $linkDisplayed; + + return $data; + } + + /** + * Build a feed item (one per shaare). + * + * @param array $link Single link array extracted from LinkDB. + * @param string $pageaddr Index URL. + * + * @return array Link array with feed attributes. + */ + protected function buildItem($link, $pageaddr) + { + $link['guid'] = $pageaddr . '?' . $link['shorturl']; + // Check for both signs of a note: starting with ? and 7 chars long. + if ($link['url'][0] === '?' && strlen($link['url']) === 7) { + $link['url'] = $pageaddr . $link['url']; + } + if ($this->usePermalinks === true) { + $permalink = '' . t('Direct link') . ''; + } else { + $permalink = '' . t('Permalink') . ''; + } + $link['description'] = format_description($link['description'], '', false, $pageaddr); + $link['description'] .= PHP_EOL . '
— ' . $permalink; + + $pubDate = $link['created']; + $link['pub_iso_date'] = $this->getIsoDate($pubDate); + + // atom:entry elements MUST contain exactly one atom:updated element. + if (!empty($link['updated'])) { + $upDate = $link['updated']; + $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM); + } else { + $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM); + } + + // Save the more recent item. + if (empty($this->latestDate) || $this->latestDate < $pubDate) { + $this->latestDate = $pubDate; + } + if (!empty($upDate) && $this->latestDate < $upDate) { + $this->latestDate = $upDate; + } + + $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); + uasort($taglist, 'strcasecmp'); + $link['taglist'] = $taglist; + + return $link; + } + + /** + * Set this to true to use permalinks instead of direct links. + * + * @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'). + * + * @return string The language. + */ + public function getTypeLanguage() + { + // 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; + return str_replace('_', '-', substr($this->locale, 0, $length)); + } + return ($this->feedType === self::$FEED_RSS) ? 'en-en' : 'en'; + } + + /** + * Format the latest item date found according to the feed type. + * + * Return an empty string if invalid DateTime is passed. + * + * @return string Formatted date. + */ + protected function getLatestDateFormatted() + { + if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) { + return ''; + } + + $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; + return $this->latestDate->format($type); + } + + /** + * Get ISO date from DateTime according to feed type. + * + * @param DateTime $date Date to format. + * @param string|bool $format Force format. + * + * @return string Formatted date. + */ + protected function getIsoDate(DateTime $date, $format = false) + { + if ($format !== false) { + return $date->format($format); + } + if ($this->feedType == self::$FEED_RSS) { + return $date->format(DateTime::RSS); + } + return $date->format(DateTime::ATOM); + } + + /** + * Returns the number of link to display according to 'nb' user input parameter. + * + * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. + * If 'nb' is set to 'all', display all filtered links (max parameter). + * + * @param int $max maximum number of links to display. + * + * @return int number of links to display. + */ + public function getNbLinks($max) + { + if (empty($this->userInput['nb'])) { + return self::$DEFAULT_NB_LINKS; + } + + if ($this->userInput['nb'] == 'all') { + return $max; + } + + $intNb = intval($this->userInput['nb']); + if (!is_int($intNb) || $intNb == 0) { + return self::$DEFAULT_NB_LINKS; + } + + return $intNb; + } +} -- cgit v1.2.3 From f24896b237e40718fb6eaa2869592eb0855a47fd Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Mon, 3 Dec 2018 01:10:39 +0100 Subject: namespacing: \Shaarli\Bookmark\LinkDB Signed-off-by: VirtualTam --- application/feed/FeedBuilder.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'application/feed/FeedBuilder.php') diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php index dcfd2c89..737a3128 100644 --- a/application/feed/FeedBuilder.php +++ b/application/feed/FeedBuilder.php @@ -2,7 +2,7 @@ namespace Shaarli\Feed; use DateTime; -use LinkDB; +use Shaarli\Bookmark\LinkDB; /** * FeedBuilder class. @@ -32,7 +32,7 @@ class FeedBuilder public static $DEFAULT_NB_LINKS = 50; /** - * @var LinkDB instance. + * @var \Shaarli\Bookmark\LinkDB instance. */ protected $linkDB; @@ -79,11 +79,12 @@ class FeedBuilder /** * Feed constructor. * - * @param LinkDB $linkDB LinkDB instance. - * @param string $feedType Type of feed. - * @param array $serverInfo $_SERVER. - * @param array $userInput $_GET. - * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise. + * @param \Shaarli\Bookmark\LinkDB $linkDB LinkDB instance. + * @param string $feedType Type of feed. + * @param array $serverInfo $_SERVER. + * @param array $userInput $_GET. + * @param boolean $isLoggedIn True if the user is currently logged in, + * false otherwise. */ public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn) { -- cgit v1.2.3 From dea72c711ff740b3b829d238fcf85648465143a0 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Sat, 12 Jan 2019 23:55:38 +0100 Subject: Optimize and cleanup imports Signed-off-by: VirtualTam --- application/feed/FeedBuilder.php | 1 - 1 file changed, 1 deletion(-) (limited to 'application/feed/FeedBuilder.php') diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php index 737a3128..b66f2f91 100644 --- a/application/feed/FeedBuilder.php +++ b/application/feed/FeedBuilder.php @@ -2,7 +2,6 @@ namespace Shaarli\Feed; use DateTime; -use Shaarli\Bookmark\LinkDB; /** * FeedBuilder class. -- cgit v1.2.3 From 520d29578c57e476ece3bdd20c286d196b7b61b4 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 9 Feb 2019 13:52:12 +0100 Subject: Remove the redirector setting Fixes #1239 --- application/feed/FeedBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'application/feed/FeedBuilder.php') diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php index b66f2f91..e23b3452 100644 --- a/application/feed/FeedBuilder.php +++ b/application/feed/FeedBuilder.php @@ -156,7 +156,7 @@ class FeedBuilder } else { $permalink = '' . t('Permalink') . ''; } - $link['description'] = format_description($link['description'], '', false, $pageaddr); + $link['description'] = format_description($link['description'], $pageaddr); $link['description'] .= PHP_EOL . '
— ' . $permalink; $pubDate = $link['created']; -- cgit v1.2.3 From a8e7da01146455f13ef06b151a7dafedd3acf769 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 9 Feb 2019 14:13:08 +0100 Subject: Do not try to retrieve thumbnails for internal link Also adds a helper function to determine if a link is a note and apply it across multiple files. --- application/feed/FeedBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'application/feed/FeedBuilder.php') diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php index b66f2f91..fec0452b 100644 --- a/application/feed/FeedBuilder.php +++ b/application/feed/FeedBuilder.php @@ -147,8 +147,8 @@ class FeedBuilder protected function buildItem($link, $pageaddr) { $link['guid'] = $pageaddr . '?' . $link['shorturl']; - // Check for both signs of a note: starting with ? and 7 chars long. - if ($link['url'][0] === '?' && strlen($link['url']) === 7) { + // Prepend the root URL for notes + if (is_note($link['url'])) { $link['url'] = $pageaddr . $link['url']; } if ($this->usePermalinks === true) { -- cgit v1.2.3