From 82e3680203896f024958ae969e2c4fccee9682f4 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 16:08:01 +0100 Subject: [PATCH] Create a FeedBuilder class which build data for both ATOM and RSS feed. --- application/FeedBuilder.php | 295 ++++++++++++++++++++++++++++++++++++ application/Router.php | 2 +- index.php | 279 +++++----------------------------- 3 files changed, 337 insertions(+), 239 deletions(-) create mode 100644 application/FeedBuilder.php diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php new file mode 100644 index 00000000..50e09831 --- /dev/null +++ b/application/FeedBuilder.php @@ -0,0 +1,295 @@ +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() + { + // Optionally filter the results: + $searchtags = !empty($this->userInput['searchtags']) ? escape($this->userInput['searchtags']) : ''; + $searchterm = !empty($this->userInput['searchterm']) ? escape($this->userInput['searchterm']) : ''; + if (! empty($searchtags) && ! empty($searchterm)) { + $linksToDisplay = $this->linkDB->filter( + LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, + array($searchtags, $searchterm) + ); + } + elseif ($searchtags) { + $linksToDisplay = $this->linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); + } + elseif ($searchterm) { + $linksToDisplay = $this->linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); + } + else { + $linksToDisplay = $this->linkDB; + } + + $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['pubsubhub_url'] = $this->pubsubhubUrl; + $data['last_update'] = $this->getLatestDateFormatted(); + $data['show_dates'] = !$this->hideDates || $this->isLoggedIn; + // Remove leading slash from REQUEST_URI. + $data['self_link'] = $pageaddr . escape(ltrim($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 .'?'. smallHash($link['linkdate']); + // 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 = 'Direct link'; + } else { + $permalink = 'Permalink'; + } + $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; + + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + + if ($this->feedType == self::$FEED_RSS) { + $link['iso_date'] = $date->format(DateTime::RSS); + } else { + $link['iso_date'] = $date->format(DateTime::ATOM); + } + + // Save the more recent item. + if (empty($this->latestDate) || $this->latestDate < $date) { + $this->latestDate = $date; + } + + $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); + uasort($taglist, 'strcasecmp'); + $link['taglist'] = $taglist; + + return $link; + } + + /** + * Assign PubSub hub URL. + * + * @param string $pubsubhubUrl PubSub hub url. + */ + public function setPubsubhubUrl($pubsubhubUrl) + { + $this->pubsubhubUrl = $pubsubhubUrl; + } + + /** + * 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); + } + + /** + * 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/Router.php b/application/Router.php index d98312b5..a1e594a0 100644 --- a/application/Router.php +++ b/application/Router.php @@ -53,7 +53,7 @@ class Router * @param array $get $_SERVER['GET']. * @param bool $loggedIn true if authenticated user. * - * @return self::page found. + * @return string page found. */ public static function findPage($query, $get, $loggedIn) { diff --git a/index.php b/index.php index 73b83533..6e14ff3f 100644 --- a/index.php +++ b/index.php @@ -154,6 +154,7 @@ if (is_file($GLOBALS['config']['CONFIG_FILE'])) { require_once 'application/ApplicationUtils.php'; require_once 'application/Cache.php'; require_once 'application/CachedPage.php'; +require_once 'application/FeedBuilder.php'; require_once 'application/FileUtils.php'; require_once 'application/HttpUtils.php'; require_once 'application/LinkDB.php'; @@ -681,237 +682,6 @@ class pageBuilder } } -// ------------------------------------------------------------------------------------------ -// Output the last N links in RSS 2.0 format. -function showRSS($pageBuilder, $linkDB) -{ - header('Content-Type: application/rss+xml; charset=utf-8'); - - // $usepermalink : If true, use permalink instead of final link. - // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=rss&permalinks - // Also enabled through a config option - $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; - - // Cache system - $query = $_SERVER['QUERY_STRING']; - $cache = new CachedPage( - $GLOBALS['config']['PAGECACHE'], - page_url($_SERVER), - startsWith($query, 'do=rss') && !isLoggedIn() - ); - $cached = $cache->cachedVersion(); - if (! empty($cached)) { - echo $cached; - exit; - } - - // Optionally filter the results: - $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; - $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; - if (! empty($searchtags) && ! empty($searchterm)) { - $linksToDisplay = $linkDB->filter( - LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, - array($searchtags, $searchterm) - ); - } - elseif ($searchtags) { - $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); - } - elseif ($searchterm) { - $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); - } - else { - $linksToDisplay = $linkDB; - } - - $nblinksToDisplay = 50; // Number of links to display. - // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. - if (!empty($_GET['nb'])) { - $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); - } - - $keys = array(); - foreach ($linksToDisplay as $key=>$value) { - $keys[] = $key; // No, I can't use array_keys(). - } - - $pageaddr = escape(index_url($_SERVER)); - $latestDate = ''; - $i = 0; - $linkDisp = array(); - while ($i < $nblinksToDisplay && $i < count($keys)) - { - $link = $linksToDisplay[$keys[$i]]; - $link['guid'] = $pageaddr. '?' .smallHash($link['linkdate']); - // 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 ($usepermalinks) { - $permalink = 'Direct link'; - } else { - $permalink = 'Permalink'; - } - $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; - - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - $link['iso_date'] = $date->format(DateTime::RSS); - $latestDate = max($latestDate, $link['iso_date']); - $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); - uasort($taglist, 'strcasecmp'); - $link['taglist'] = $taglist; - - $linkDisp[$keys[$i]] = $link; - $i++; - } - - $data = array(); - if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { - $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']); - } - - // Use the locale do define the language, if available. - $locale = strtolower(setlocale(LC_COLLATE, 0)); - if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) { - $data['language'] = str_replace('_', '-', substr($locale, 0, 5)); - } else { - $data['language'] = 'en-en'; - } - $data['last_update'] = escape($latestDate); - $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn(); - // Remove starting slash from REQUEST_URI. - $data['self_link'] = escape($pageaddr . substr($_SERVER['REQUEST_URI'], 1)); - $data['index_url'] = escape($pageaddr); - $data['usepermalinks'] = $usepermalinks; - $data['links'] = $linkDisp; - - $pluginManager = PluginManager::getInstance(); - $pluginManager->executeHooks('render_feed', $data, array( - 'loggedin' => isLoggedIn(), - 'target' => Router::$PAGE_RSS, - )); - - $pageBuilder->assignAll($data); - $pageBuilder->renderPage('feed.rss', false); - $cache->cache(ob_get_contents()); - ob_end_flush(); - exit; -} - -// ------------------------------------------------------------------------------------------ -// Output the last N links in ATOM format. -function showATOM($pageBuilder, $linkDB) -{ - header('Content-Type: application/atom+xml; charset=utf-8'); - - // Cache system - $query = $_SERVER["QUERY_STRING"]; - $cache = new CachedPage( - $GLOBALS['config']['PAGECACHE'], - page_url($_SERVER), - startsWith($query,'do=atom') && !isLoggedIn() - ); - $cached = $cache->cachedVersion(); - if (!empty($cached)) { - echo $cached; - exit; - } - - // $usepermalink : If true, use permalink instead of final link. - // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=atom&permalinks - $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; - - // Optionally filter the results: - $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; - $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; - if (! empty($searchtags) && ! empty($searchterm)) { - $linksToDisplay = $linkDB->filter( - LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, - array($searchtags, $searchterm) - ); - } - elseif ($searchtags) { - $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); - } - elseif ($searchterm) { - $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); - } - else { - $linksToDisplay = $linkDB; - } - - $nblinksToDisplay = 50; // Number of links to display. - // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. - if (!empty($_GET['nb'])) { - $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); - } - - $keys = array(); - foreach ($linksToDisplay as $key=>$value) { - $keys[] = $key; // No, I can't use array_keys(). - } - - $pageaddr = escape(index_url($_SERVER)); - $latestDate = ''; - $i = 0; - $linkDisp = array(); - while ($i < $nblinksToDisplay && $i < count($keys)) - { - $link = $linksToDisplay[$keys[$i]]; - $link['guid'] = $pageaddr. '?' .smallHash($link['linkdate']); - // 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 ($usepermalinks) { - $permalink = 'Direct link'; - } else { - $permalink = 'Permalink'; - } - $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; - - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - $link['iso_date'] = $date->format(DateTime::ATOM); - $latestDate = max($latestDate, $link['iso_date']); - $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); - uasort($taglist, 'strcasecmp'); - $link['taglist'] = $taglist; - - $linkDisp[$keys[$i]] = $link; - $i++; - } - - $data = array(); - if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { - $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']); - } - // Use the locale do define the language, if available. - $locale = strtolower(setlocale(LC_COLLATE, 0)); - if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) { - $data['language'] = substr($locale, 0, 2); - } else { - $data['language'] = 'en'; - } - $data['last_update'] = escape($latestDate); - $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn(); - $data['self_link'] = escape($pageaddr . $_SERVER['REQUEST_URI']); - $data['index_url'] = escape($pageaddr); - $data['usepermalinks'] = $usepermalinks; - $data['links'] = $linkDisp; - - $pluginManager = PluginManager::getInstance(); - $pluginManager->executeHooks('render_feed', $data, array( - 'loggedin' => isLoggedIn(), - 'target' => Router::$PAGE_ATOM, - )); - - $pageBuilder->assignAll($data); - $pageBuilder->renderPage('feed.atom', false); - $cache->cache(ob_get_contents()); - ob_end_flush(); - exit; -} - // ------------------------------------------------------------------------------------------ // Daily RSS feed: 1 RSS entry per day giving all the links on that day. // Gives the last 7 days (which have links). @@ -1288,14 +1058,47 @@ function renderPage() showDaily($PAGE); } - // ATOM feed. - if ($targetPage == Router::$PAGE_ATOM) { - showATOM($PAGE, $LINKSDB); - } + // ATOM and RSS feed. + if ($targetPage == Router::$PAGE_FEED_ATOM || $targetPage == Router::$PAGE_FEED_RSS) { + $feedType = $targetPage == Router::$PAGE_FEED_RSS ? FeedBuilder::$FEED_RSS : FeedBuilder::$FEED_ATOM; + header('Content-Type: application/'. $feedType .'+xml; charset=utf-8'); - // RSS feed. - if ($targetPage == Router::$PAGE_RSS) { - showRSS($PAGE, $LINKSDB); + // Cache system + $query = $_SERVER['QUERY_STRING']; + $cache = new CachedPage( + $GLOBALS['config']['PAGECACHE'], + page_url($_SERVER), + startsWith($query,'do='. $targetPage) && !isLoggedIn() + ); + $cached = $cache->cachedVersion(); + if (!empty($cached)) { + echo $cached; + exit; + } + + // Generate data. + $feedGenerator = new FeedBuilder($LINKSDB, $feedType, $_SERVER, $_GET, isLoggedIn()); + $feedGenerator->setLocale(strtolower(setlocale(LC_COLLATE, 0))); + $feedGenerator->setHideDates($GLOBALS['config']['HIDE_TIMESTAMPS'] && !isLoggedIn()); + $feedGenerator->setUsePermalinks(isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']); + if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { + $feedGenerator->setPubsubhubUrl($GLOBALS['config']['PUBSUBHUB_URL']); + } + $data = $feedGenerator->buildData(); + + // Process plugin hook. + $pluginManager = PluginManager::getInstance(); + $pluginManager->executeHooks('render_feed', $data, array( + 'loggedin' => isLoggedIn(), + 'target' => $targetPage, + )); + + // Render the template. + $PAGE->assignAll($data); + $PAGE->renderPage('feed.'. $feedType); + $cache->cache(ob_get_contents()); + ob_end_flush(); + exit; } // Display openseach plugin (XML) -- 2.41.0