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/Cache.php | 38 ----- application/CachedPage.php | 59 -------- application/FeedBuilder.php | 296 -------------------------------------- application/feed/Cache.php | 38 +++++ application/feed/CachedPage.php | 61 ++++++++ application/feed/FeedBuilder.php | 299 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 398 insertions(+), 393 deletions(-) delete mode 100644 application/Cache.php delete mode 100644 application/CachedPage.php delete mode 100644 application/FeedBuilder.php create mode 100644 application/feed/Cache.php create mode 100644 application/feed/CachedPage.php create mode 100644 application/feed/FeedBuilder.php (limited to 'application') diff --git a/application/Cache.php b/application/Cache.php deleted file mode 100644 index e5d43e61..00000000 --- a/application/Cache.php +++ /dev/null @@ -1,38 +0,0 @@ -cacheDir = $cacheDir; - $this->filename = $this->cacheDir.'/'.sha1($url).'.cache'; - $this->shouldBeCached = $shouldBeCached; - } - - /** - * Returns the cached version of a page, if it exists and should be cached - * - * @return string a cached version of the page if it exists, null otherwise - */ - public function cachedVersion() - { - if (!$this->shouldBeCached) { - return null; - } - if (is_file($this->filename)) { - return file_get_contents($this->filename); - } - return null; - } - - /** - * Puts a page in the cache - * - * @param string $pageContent XML content to cache - */ - public function cache($pageContent) - { - if (!$this->shouldBeCached) { - return; - } - file_put_contents($this->filename, $pageContent); - } -} diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php deleted file mode 100644 index 73fafcbe..00000000 --- a/application/FeedBuilder.php +++ /dev/null @@ -1,296 +0,0 @@ -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; - } -} diff --git a/application/feed/Cache.php b/application/feed/Cache.php new file mode 100644 index 00000000..e5d43e61 --- /dev/null +++ b/application/feed/Cache.php @@ -0,0 +1,38 @@ +cacheDir = $cacheDir; + $this->filename = $this->cacheDir . '/' . sha1($url) . '.cache'; + $this->shouldBeCached = $shouldBeCached; + } + + /** + * Returns the cached version of a page, if it exists and should be cached + * + * @return string a cached version of the page if it exists, null otherwise + */ + public function cachedVersion() + { + if (!$this->shouldBeCached) { + return null; + } + if (is_file($this->filename)) { + return file_get_contents($this->filename); + } + return null; + } + + /** + * Puts a page in the cache + * + * @param string $pageContent XML content to cache + */ + public function cache($pageContent) + { + if (!$this->shouldBeCached) { + return; + } + file_put_contents($this->filename, $pageContent); + } +} 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