From e67712ba0f8896f0d7e676292f557b5999c2848d Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 14:38:06 +0100 Subject: [PATCH] Refactor showRSS, and make it use the RSS template --- index.php | 118 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 66 insertions(+), 52 deletions(-) diff --git a/index.php b/index.php index 14811c5..261c8a3 100644 --- a/index.php +++ b/index.php @@ -683,7 +683,7 @@ class pageBuilder // ------------------------------------------------------------------------------------------ // Output the last N links in RSS 2.0 format. -function showRSS() +function showRSS($pageBuilder, $linkDB) { header('Content-Type: application/rss+xml; charset=utf-8'); @@ -693,11 +693,11 @@ function showRSS() $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; // Cache system - $query = $_SERVER["QUERY_STRING"]; + $query = $_SERVER['QUERY_STRING']; $cache = new CachedPage( $GLOBALS['config']['PAGECACHE'], page_url($_SERVER), - startsWith($query,'do=rss') && !isLoggedIn() + startsWith($query, 'do=rss') && !isLoggedIn() ); $cached = $cache->cachedVersion(); if (! empty($cached)) { @@ -705,32 +705,23 @@ function showRSS() exit; } - // If cached was not found (or not usable), then read the database and build the response: - $LINKSDB = new LinkDB( - $GLOBALS['config']['DATASTORE'], - isLoggedIn(), - $GLOBALS['config']['HIDE_PUBLIC_LINKS'], - $GLOBALS['redirector'] - ); - // Read links from database (and filter private links if user it not logged in). - // Optionally filter the results: $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; if (! empty($searchtags) && ! empty($searchterm)) { - $linksToDisplay = $LINKSDB->filter( + $linksToDisplay = $linkDB->filter( LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, array($searchtags, $searchterm) ); } elseif ($searchtags) { - $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); + $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags); } elseif ($searchterm) { - $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); + $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); } else { - $linksToDisplay = $LINKSDB; + $linksToDisplay = $linkDB; } $nblinksToDisplay = 50; // Number of links to display. @@ -739,50 +730,63 @@ function showRSS() $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); } - $pageaddr = escape(index_url($_SERVER)); - echo ''; - echo ''.$GLOBALS['title'].''.$pageaddr.''; - echo 'Shared linksen-en'.$pageaddr.''."\n\n"; - if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) - { - echo ''; - echo ''; - echo ''; - echo ''; + $keys = array(); + foreach ($linksToDisplay as $key=>$value) { + $keys[] = $key; // No, I can't use array_keys(). } - $i=0; - $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // No, I can't use array_keys(). - while ($i<$nblinksToDisplay && $i'.$link['title'].''.$guid.''.$guid.''; - else - echo ''.$link['title'].''.$guid.''.$absurl.''; - if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) { - echo ''.escape($date->format(DateTime::RSS))."\n"; + $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 ($link['tags']!='') // Adding tags to each RSS entry (as mentioned in RSS specification) - { - foreach(explode(' ',$link['tags']) as $tag) { echo ''.$tag.''."\n"; } + if ($usepermalinks) { + $permalink = 'Direct link'; + } else { + $permalink = 'Permalink'; } + $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; - // Add permalink in description - $descriptionlink = '(Permalink)'; - // If user wants permalinks first, put the final link in description - if ($usepermalinks===true) $descriptionlink = '(Link)'; - if (strlen($link['description'])>0) $descriptionlink = '
'.$descriptionlink; - echo '' . "\n
\n"; + $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++; } - echo '
'; + $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; + + $pageBuilder->assignAll($data); + $pageBuilder->renderPage('feed.rss', false); $cache->cache(ob_get_contents()); ob_end_flush(); exit; @@ -889,6 +893,12 @@ function showATOM($pageBuilder, $linkDB) $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()); @@ -1277,6 +1287,11 @@ function renderPage() showATOM($PAGE, $LINKSDB); } + // RSS feed. + if ($targetPage == Router::$PAGE_RSS) { + showRSS($PAGE, $LINKSDB); + } + // Display openseach plugin (XML) if ($targetPage == Router::$PAGE_OPENSEARCH) { header('Content-Type: application/xml; charset=utf-8'); @@ -2601,7 +2616,6 @@ function resizeImage($filepath) } if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database. -if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=rss')) { showRSS(); exit; } if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=dailyrss')) { showDailyRSS(); exit; } if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE']; renderPage();