aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-03-10 19:01:30 +0100
committerArthurHoaro <arthur@hoa.ro>2016-03-18 19:13:48 +0100
commit69c474b96612dc64fc2cb66f1196251cafa08445 (patch)
tree0522b68c4977b336579765ae37b916dfe526996e /index.php
parent8395d0b76145969f4b8940a415af5e46528f04a5 (diff)
downloadShaarli-69c474b96612dc64fc2cb66f1196251cafa08445.tar.gz
Shaarli-69c474b96612dc64fc2cb66f1196251cafa08445.tar.zst
Shaarli-69c474b96612dc64fc2cb66f1196251cafa08445.zip
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
Diffstat (limited to 'index.php')
-rw-r--r--index.php125
1 files changed, 59 insertions, 66 deletions
diff --git a/index.php b/index.php
index 9100044e..14811c56 100644
--- a/index.php
+++ b/index.php
@@ -790,14 +790,10 @@ function showRSS()
790 790
791// ------------------------------------------------------------------------------------------ 791// ------------------------------------------------------------------------------------------
792// Output the last N links in ATOM format. 792// Output the last N links in ATOM format.
793function showATOM() 793function showATOM($pageBuilder, $linkDB)
794{ 794{
795 header('Content-Type: application/atom+xml; charset=utf-8'); 795 header('Content-Type: application/atom+xml; charset=utf-8');
796 796
797 // $usepermalink : If true, use permalink instead of final link.
798 // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=atom&permalinks
799 $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS'];
800
801 // Cache system 797 // Cache system
802 $query = $_SERVER["QUERY_STRING"]; 798 $query = $_SERVER["QUERY_STRING"];
803 $cache = new CachedPage( 799 $cache = new CachedPage(
@@ -811,97 +807,90 @@ function showATOM()
811 exit; 807 exit;
812 } 808 }
813 809
814 // If cached was not found (or not usable), then read the database and build the response: 810 // $usepermalink : If true, use permalink instead of final link.
815 // Read links from database (and filter private links if used it not logged in). 811 // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=atom&permalinks
816 $LINKSDB = new LinkDB( 812 $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS'];
817 $GLOBALS['config']['DATASTORE'],
818 isLoggedIn(),
819 $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
820 $GLOBALS['redirector']
821 );
822 813
823 // Optionally filter the results: 814 // Optionally filter the results:
824 $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; 815 $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
825 $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; 816 $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
826 if (! empty($searchtags) && ! empty($searchterm)) { 817 if (! empty($searchtags) && ! empty($searchterm)) {
827 $linksToDisplay = $LINKSDB->filter( 818 $linksToDisplay = $linkDB->filter(
828 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, 819 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
829 array($searchtags, $searchterm) 820 array($searchtags, $searchterm)
830 ); 821 );
831 } 822 }
832 elseif ($searchtags) { 823 elseif ($searchtags) {
833 $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); 824 $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags);
834 } 825 }
835 elseif ($searchterm) { 826 elseif ($searchterm) {
836 $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); 827 $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm);
837 } 828 }
838 else { 829 else {
839 $linksToDisplay = $LINKSDB; 830 $linksToDisplay = $linkDB;
840 } 831 }
841 832
842 $nblinksToDisplay = 50; // Number of links to display. 833 $nblinksToDisplay = 50; // Number of links to display.
843 // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. 834 // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
844 if (!empty($_GET['nb'])) { 835 if (!empty($_GET['nb'])) {
845 $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); 836 $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1);
837 }
838
839 $keys = array();
840 foreach ($linksToDisplay as $key=>$value) {
841 $keys[] = $key; // No, I can't use array_keys().
846 } 842 }
847 843
848 $pageaddr=escape(index_url($_SERVER)); 844 $pageaddr = escape(index_url($_SERVER));
849 $latestDate = ''; 845 $latestDate = '';
850 $entries=''; 846 $i = 0;
851 $i=0; 847 $linkDisp = array();
852 $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // No, I can't use array_keys(). 848 while ($i < $nblinksToDisplay && $i < count($keys))
853 while ($i<$nblinksToDisplay && $i<count($keys))
854 { 849 {
855 $link = $linksToDisplay[$keys[$i]]; 850 $link = $linksToDisplay[$keys[$i]];
856 $guid = $pageaddr.'?'.smallHash($link['linkdate']); 851 $link['guid'] = $pageaddr. '?' .smallHash($link['linkdate']);
857 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); 852 // Check for both signs of a note: starting with ? and 7 chars long.
858 $iso8601date = $date->format(DateTime::ISO8601); 853 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
859 $latestDate = max($latestDate, $iso8601date); 854 $link['url'] = $pageaddr . $link['url'];
860 $absurl = $link['url']; 855 }
861 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute 856 if ($usepermalinks) {
862 $entries.='<entry><title>'.$link['title'].'</title>'; 857 $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
863 if ($usepermalinks===true) 858 } else {
864 $entries.='<link href="'.$guid.'" /><id>'.$guid.'</id>'; 859 $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
865 else
866 $entries.='<link href="'.$absurl.'" /><id>'.$guid.'</id>';
867
868 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) {
869 $entries.='<updated>'.escape($iso8601date).'</updated>';
870 } 860 }
861 $link['description'] = format_description($link['description']) . PHP_EOL .'<br>&#8212; '. $permalink;
871 862
872 // Add permalink in description 863 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
873 $descriptionlink = '(<a href="'.$guid.'">Permalink</a>)'; 864 $link['iso_date'] = $date->format(DateTime::ATOM);
874 // If user wants permalinks first, put the final link in description 865 $latestDate = max($latestDate, $link['iso_date']);
875 if ($usepermalinks===true) $descriptionlink = '(<a href="'.$absurl.'">Link</a>)'; 866 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
876 if (strlen($link['description'])>0) $descriptionlink = '<br>'.$descriptionlink; 867 uasort($taglist, 'strcasecmp');
868 $link['taglist'] = $taglist;
877 869
878 $entries .= '<content type="html"><![CDATA['. 870 $linkDisp[$keys[$i]] = $link;
879 format_description($link['description'], $GLOBALS['redirector']) .
880 $descriptionlink . "]]></content>\n";
881 if ($link['tags']!='') // Adding tags to each ATOM entry (as mentioned in ATOM specification)
882 {
883 foreach(explode(' ',$link['tags']) as $tag)
884 { $entries.='<category scheme="'.$pageaddr.'" term="'.$tag.'" />'."\n"; }
885 }
886 $entries.="</entry>\n";
887 $i++; 871 $i++;
888 } 872 }
889 $feed='<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom">'; 873
890 $feed.='<title>'.$GLOBALS['title'].'</title>'; 874 $data = array();
891 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) $feed.='<updated>'.escape($latestDate).'</updated>'; 875 if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) {
892 $feed.='<link rel="self" href="'.escape(server_url($_SERVER).$_SERVER["REQUEST_URI"]).'" />'; 876 $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']);
893 if (!empty($GLOBALS['config']['PUBSUBHUB_URL']))
894 {
895 $feed.='<!-- PubSubHubbub Discovery -->';
896 $feed.='<link rel="hub" href="'.escape($GLOBALS['config']['PUBSUBHUB_URL']).'" />';
897 $feed.='<!-- End Of PubSubHubbub Discovery -->';
898 } 877 }
899 $feed.='<author><name>'.$pageaddr.'</name><uri>'.$pageaddr.'</uri></author>'; 878 // Use the locale do define the language, if available.
900 $feed.='<id>'.$pageaddr.'</id>'."\n\n"; // Yes, I know I should use a real IRI (RFC3987), but the site URL will do. 879 $locale = strtolower(setlocale(LC_COLLATE, 0));
901 $feed.=$entries; 880 if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) {
902 $feed.='</feed><!-- Cached version of '.escape(page_url($_SERVER)).' -->'; 881 $data['language'] = substr($locale, 0, 2);
903 echo $feed; 882 } else {
883 $data['language'] = 'en';
884 }
885 $data['last_update'] = escape($latestDate);
886 $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn();
887 $data['self_link'] = escape($pageaddr . $_SERVER['REQUEST_URI']);
888 $data['index_url'] = escape($pageaddr);
889 $data['usepermalinks'] = $usepermalinks;
890 $data['links'] = $linkDisp;
904 891
892 $pageBuilder->assignAll($data);
893 $pageBuilder->renderPage('feed.atom', false);
905 $cache->cache(ob_get_contents()); 894 $cache->cache(ob_get_contents());
906 ob_end_flush(); 895 ob_end_flush();
907 exit; 896 exit;
@@ -1283,6 +1272,11 @@ function renderPage()
1283 showDaily($PAGE); 1272 showDaily($PAGE);
1284 } 1273 }
1285 1274
1275 // ATOM feed.
1276 if ($targetPage == Router::$PAGE_ATOM) {
1277 showATOM($PAGE, $LINKSDB);
1278 }
1279
1286 // Display openseach plugin (XML) 1280 // Display openseach plugin (XML)
1287 if ($targetPage == Router::$PAGE_OPENSEARCH) { 1281 if ($targetPage == Router::$PAGE_OPENSEARCH) {
1288 header('Content-Type: application/xml; charset=utf-8'); 1282 header('Content-Type: application/xml; charset=utf-8');
@@ -2608,7 +2602,6 @@ function resizeImage($filepath)
2608 2602
2609if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database. 2603if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database.
2610if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=rss')) { showRSS(); exit; } 2604if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=rss')) { showRSS(); exit; }
2611if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=atom')) { showATOM(); exit; }
2612if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=dailyrss')) { showDailyRSS(); exit; } 2605if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=dailyrss')) { showDailyRSS(); exit; }
2613if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE']; 2606if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE'];
2614renderPage(); 2607renderPage();