From 69c474b96612dc64fc2cb66f1196251cafa08445 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 10 Mar 2016 19:01:30 +0100 Subject: Refactor showAtom, and make it use the ATOM template Minor changes: * Fix the date which was in a invalid format. * Avoid empty categories (tags). * Use the locale to set the language --- index.php | 125 +++++++++++++++++++++++++++++--------------------------------- 1 file changed, 59 insertions(+), 66 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 9100044e..14811c56 100644 --- a/index.php +++ b/index.php @@ -790,14 +790,10 @@ function showRSS() // ------------------------------------------------------------------------------------------ // Output the last N links in ATOM format. -function showATOM() +function showATOM($pageBuilder, $linkDB) { header('Content-Type: application/atom+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=atom&permalinks - $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']; - // Cache system $query = $_SERVER["QUERY_STRING"]; $cache = new CachedPage( @@ -811,97 +807,90 @@ function showATOM() exit; } - // If cached was not found (or not usable), then read the database and build the response: - // Read links from database (and filter private links if used it not logged in). - $LINKSDB = new LinkDB( - $GLOBALS['config']['DATASTORE'], - isLoggedIn(), - $GLOBALS['config']['HIDE_PUBLIC_LINKS'], - $GLOBALS['redirector'] - ); + // $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 = $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. // 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); + $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)); + $pageaddr = escape(index_url($_SERVER)); $latestDate = ''; - $entries=''; - $i=0; - $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // No, I can't use array_keys(). - while ($i<$nblinksToDisplay && $iformat(DateTime::ISO8601); - $latestDate = max($latestDate, $iso8601date); - $absurl = $link['url']; - if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute - $entries.=''.$link['title'].''; - if ($usepermalinks===true) - $entries.=''.$guid.''; - else - $entries.=''.$guid.''; - - if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) { - $entries.=''.escape($iso8601date).''; + $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; - // 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; + $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; - $entries .= '\n"; - if ($link['tags']!='') // Adding tags to each ATOM entry (as mentioned in ATOM specification) - { - foreach(explode(' ',$link['tags']) as $tag) - { $entries.=''."\n"; } - } - $entries.="
\n"; + $linkDisp[$keys[$i]] = $link; $i++; } - $feed=''; - $feed.=''.$GLOBALS['title'].''; - if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) $feed.=''.escape($latestDate).''; - $feed.=''; - if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) - { - $feed.=''; - $feed.=''; - $feed.=''; + + $data = array(); + if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) { + $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']); } - $feed.=''.$pageaddr.''.$pageaddr.''; - $feed.=''.$pageaddr.''."\n\n"; // Yes, I know I should use a real IRI (RFC3987), but the site URL will do. - $feed.=$entries; - $feed.=''; - echo $feed; + // 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; + $pageBuilder->assignAll($data); + $pageBuilder->renderPage('feed.atom', false); $cache->cache(ob_get_contents()); ob_end_flush(); exit; @@ -1283,6 +1272,11 @@ function renderPage() showDaily($PAGE); } + // ATOM feed. + if ($targetPage == Router::$PAGE_ATOM) { + showATOM($PAGE, $LINKSDB); + } + // Display openseach plugin (XML) if ($targetPage == Router::$PAGE_OPENSEARCH) { header('Content-Type: application/xml; charset=utf-8'); @@ -2608,7 +2602,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=atom')) { showATOM(); 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(); -- cgit v1.2.3