aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-03-12 16:08:01 +0100
committerArthurHoaro <arthur@hoa.ro>2016-03-25 19:17:55 +0100
commit82e3680203896f024958ae969e2c4fccee9682f4 (patch)
treea4b39acfce291c40daa971f2e1b0eb009041250e /index.php
parentd4542fdb0d15f07810a4bc740bfceaa4189a3604 (diff)
downloadShaarli-82e3680203896f024958ae969e2c4fccee9682f4.tar.gz
Shaarli-82e3680203896f024958ae969e2c4fccee9682f4.tar.zst
Shaarli-82e3680203896f024958ae969e2c4fccee9682f4.zip
Create a FeedBuilder class which build data for both ATOM and RSS feed.
Diffstat (limited to 'index.php')
-rw-r--r--index.php279
1 files changed, 41 insertions, 238 deletions
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'])) {
154require_once 'application/ApplicationUtils.php'; 154require_once 'application/ApplicationUtils.php';
155require_once 'application/Cache.php'; 155require_once 'application/Cache.php';
156require_once 'application/CachedPage.php'; 156require_once 'application/CachedPage.php';
157require_once 'application/FeedBuilder.php';
157require_once 'application/FileUtils.php'; 158require_once 'application/FileUtils.php';
158require_once 'application/HttpUtils.php'; 159require_once 'application/HttpUtils.php';
159require_once 'application/LinkDB.php'; 160require_once 'application/LinkDB.php';
@@ -682,237 +683,6 @@ class pageBuilder
682} 683}
683 684
684// ------------------------------------------------------------------------------------------ 685// ------------------------------------------------------------------------------------------
685// Output the last N links in RSS 2.0 format.
686function showRSS($pageBuilder, $linkDB)
687{
688 header('Content-Type: application/rss+xml; charset=utf-8');
689
690 // $usepermalink : If true, use permalink instead of final link.
691 // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=rss&permalinks
692 // Also enabled through a config option
693 $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS'];
694
695 // Cache system
696 $query = $_SERVER['QUERY_STRING'];
697 $cache = new CachedPage(
698 $GLOBALS['config']['PAGECACHE'],
699 page_url($_SERVER),
700 startsWith($query, 'do=rss') && !isLoggedIn()
701 );
702 $cached = $cache->cachedVersion();
703 if (! empty($cached)) {
704 echo $cached;
705 exit;
706 }
707
708 // Optionally filter the results:
709 $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
710 $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
711 if (! empty($searchtags) && ! empty($searchterm)) {
712 $linksToDisplay = $linkDB->filter(
713 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
714 array($searchtags, $searchterm)
715 );
716 }
717 elseif ($searchtags) {
718 $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags);
719 }
720 elseif ($searchterm) {
721 $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm);
722 }
723 else {
724 $linksToDisplay = $linkDB;
725 }
726
727 $nblinksToDisplay = 50; // Number of links to display.
728 // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
729 if (!empty($_GET['nb'])) {
730 $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1);
731 }
732
733 $keys = array();
734 foreach ($linksToDisplay as $key=>$value) {
735 $keys[] = $key; // No, I can't use array_keys().
736 }
737
738 $pageaddr = escape(index_url($_SERVER));
739 $latestDate = '';
740 $i = 0;
741 $linkDisp = array();
742 while ($i < $nblinksToDisplay && $i < count($keys))
743 {
744 $link = $linksToDisplay[$keys[$i]];
745 $link['guid'] = $pageaddr. '?' .smallHash($link['linkdate']);
746 // Check for both signs of a note: starting with ? and 7 chars long.
747 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
748 $link['url'] = $pageaddr . $link['url'];
749 }
750 if ($usepermalinks) {
751 $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
752 } else {
753 $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
754 }
755 $link['description'] = format_description($link['description']) . PHP_EOL .'<br>&#8212; '. $permalink;
756
757 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
758 $link['iso_date'] = $date->format(DateTime::RSS);
759 $latestDate = max($latestDate, $link['iso_date']);
760 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
761 uasort($taglist, 'strcasecmp');
762 $link['taglist'] = $taglist;
763
764 $linkDisp[$keys[$i]] = $link;
765 $i++;
766 }
767
768 $data = array();
769 if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) {
770 $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']);
771 }
772
773 // Use the locale do define the language, if available.
774 $locale = strtolower(setlocale(LC_COLLATE, 0));
775 if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) {
776 $data['language'] = str_replace('_', '-', substr($locale, 0, 5));
777 } else {
778 $data['language'] = 'en-en';
779 }
780 $data['last_update'] = escape($latestDate);
781 $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn();
782 // Remove starting slash from REQUEST_URI.
783 $data['self_link'] = escape($pageaddr . substr($_SERVER['REQUEST_URI'], 1));
784 $data['index_url'] = escape($pageaddr);
785 $data['usepermalinks'] = $usepermalinks;
786 $data['links'] = $linkDisp;
787
788 $pluginManager = PluginManager::getInstance();
789 $pluginManager->executeHooks('render_feed', $data, array(
790 'loggedin' => isLoggedIn(),
791 'target' => Router::$PAGE_RSS,
792 ));
793
794 $pageBuilder->assignAll($data);
795 $pageBuilder->renderPage('feed.rss', false);
796 $cache->cache(ob_get_contents());
797 ob_end_flush();
798 exit;
799}
800
801// ------------------------------------------------------------------------------------------
802// Output the last N links in ATOM format.
803function showATOM($pageBuilder, $linkDB)
804{
805 header('Content-Type: application/atom+xml; charset=utf-8');
806
807 // Cache system
808 $query = $_SERVER["QUERY_STRING"];
809 $cache = new CachedPage(
810 $GLOBALS['config']['PAGECACHE'],
811 page_url($_SERVER),
812 startsWith($query,'do=atom') && !isLoggedIn()
813 );
814 $cached = $cache->cachedVersion();
815 if (!empty($cached)) {
816 echo $cached;
817 exit;
818 }
819
820 // $usepermalink : If true, use permalink instead of final link.
821 // User just has to add 'permalink' in URL parameters. e.g. http://mysite.com/shaarli/?do=atom&permalinks
822 $usepermalinks = isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS'];
823
824 // Optionally filter the results:
825 $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
826 $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
827 if (! empty($searchtags) && ! empty($searchterm)) {
828 $linksToDisplay = $linkDB->filter(
829 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
830 array($searchtags, $searchterm)
831 );
832 }
833 elseif ($searchtags) {
834 $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TAG, $searchtags);
835 }
836 elseif ($searchterm) {
837 $linksToDisplay = $linkDB->filter(LinkFilter::$FILTER_TEXT, $searchterm);
838 }
839 else {
840 $linksToDisplay = $linkDB;
841 }
842
843 $nblinksToDisplay = 50; // Number of links to display.
844 // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links.
845 if (!empty($_GET['nb'])) {
846 $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1);
847 }
848
849 $keys = array();
850 foreach ($linksToDisplay as $key=>$value) {
851 $keys[] = $key; // No, I can't use array_keys().
852 }
853
854 $pageaddr = escape(index_url($_SERVER));
855 $latestDate = '';
856 $i = 0;
857 $linkDisp = array();
858 while ($i < $nblinksToDisplay && $i < count($keys))
859 {
860 $link = $linksToDisplay[$keys[$i]];
861 $link['guid'] = $pageaddr. '?' .smallHash($link['linkdate']);
862 // Check for both signs of a note: starting with ? and 7 chars long.
863 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
864 $link['url'] = $pageaddr . $link['url'];
865 }
866 if ($usepermalinks) {
867 $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
868 } else {
869 $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
870 }
871 $link['description'] = format_description($link['description']) . PHP_EOL .'<br>&#8212; '. $permalink;
872
873 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
874 $link['iso_date'] = $date->format(DateTime::ATOM);
875 $latestDate = max($latestDate, $link['iso_date']);
876 $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
877 uasort($taglist, 'strcasecmp');
878 $link['taglist'] = $taglist;
879
880 $linkDisp[$keys[$i]] = $link;
881 $i++;
882 }
883
884 $data = array();
885 if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) {
886 $data['pubsubhub_url'] = escape($GLOBALS['config']['PUBSUBHUB_URL']);
887 }
888 // Use the locale do define the language, if available.
889 $locale = strtolower(setlocale(LC_COLLATE, 0));
890 if (! empty($locale) && preg_match('/^\w{2}[_\-]\w{2}/', $locale)) {
891 $data['language'] = substr($locale, 0, 2);
892 } else {
893 $data['language'] = 'en';
894 }
895 $data['last_update'] = escape($latestDate);
896 $data['show_dates'] = !$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn();
897 $data['self_link'] = escape($pageaddr . $_SERVER['REQUEST_URI']);
898 $data['index_url'] = escape($pageaddr);
899 $data['usepermalinks'] = $usepermalinks;
900 $data['links'] = $linkDisp;
901
902 $pluginManager = PluginManager::getInstance();
903 $pluginManager->executeHooks('render_feed', $data, array(
904 'loggedin' => isLoggedIn(),
905 'target' => Router::$PAGE_ATOM,
906 ));
907
908 $pageBuilder->assignAll($data);
909 $pageBuilder->renderPage('feed.atom', false);
910 $cache->cache(ob_get_contents());
911 ob_end_flush();
912 exit;
913}
914
915// ------------------------------------------------------------------------------------------
916// Daily RSS feed: 1 RSS entry per day giving all the links on that day. 686// Daily RSS feed: 1 RSS entry per day giving all the links on that day.
917// Gives the last 7 days (which have links). 687// Gives the last 7 days (which have links).
918// This RSS feed cannot be filtered. 688// This RSS feed cannot be filtered.
@@ -1288,14 +1058,47 @@ function renderPage()
1288 showDaily($PAGE); 1058 showDaily($PAGE);
1289 } 1059 }
1290 1060
1291 // ATOM feed. 1061 // ATOM and RSS feed.
1292 if ($targetPage == Router::$PAGE_ATOM) { 1062 if ($targetPage == Router::$PAGE_FEED_ATOM || $targetPage == Router::$PAGE_FEED_RSS) {
1293 showATOM($PAGE, $LINKSDB); 1063 $feedType = $targetPage == Router::$PAGE_FEED_RSS ? FeedBuilder::$FEED_RSS : FeedBuilder::$FEED_ATOM;
1294 } 1064 header('Content-Type: application/'. $feedType .'+xml; charset=utf-8');
1295 1065
1296 // RSS feed. 1066 // Cache system
1297 if ($targetPage == Router::$PAGE_RSS) { 1067 $query = $_SERVER['QUERY_STRING'];
1298 showRSS($PAGE, $LINKSDB); 1068 $cache = new CachedPage(
1069 $GLOBALS['config']['PAGECACHE'],
1070 page_url($_SERVER),
1071 startsWith($query,'do='. $targetPage) && !isLoggedIn()
1072 );
1073 $cached = $cache->cachedVersion();
1074 if (!empty($cached)) {
1075 echo $cached;
1076 exit;
1077 }
1078
1079 // Generate data.
1080 $feedGenerator = new FeedBuilder($LINKSDB, $feedType, $_SERVER, $_GET, isLoggedIn());
1081 $feedGenerator->setLocale(strtolower(setlocale(LC_COLLATE, 0)));
1082 $feedGenerator->setHideDates($GLOBALS['config']['HIDE_TIMESTAMPS'] && !isLoggedIn());
1083 $feedGenerator->setUsePermalinks(isset($_GET['permalinks']) || !$GLOBALS['config']['ENABLE_RSS_PERMALINKS']);
1084 if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) {
1085 $feedGenerator->setPubsubhubUrl($GLOBALS['config']['PUBSUBHUB_URL']);
1086 }
1087 $data = $feedGenerator->buildData();
1088
1089 // Process plugin hook.
1090 $pluginManager = PluginManager::getInstance();
1091 $pluginManager->executeHooks('render_feed', $data, array(
1092 'loggedin' => isLoggedIn(),
1093 'target' => $targetPage,
1094 ));
1095
1096 // Render the template.
1097 $PAGE->assignAll($data);
1098 $PAGE->renderPage('feed.'. $feedType);
1099 $cache->cache(ob_get_contents());
1100 ob_end_flush();
1101 exit;
1299 } 1102 }
1300 1103
1301 // Display openseach plugin (XML) 1104 // Display openseach plugin (XML)