From 1c2fdb98b1c7fbd85aa086641dd47bc9bd03c8eb Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 10 Mar 2016 18:41:23 +0100 Subject: [PATCH 01/12] Add method assignAll() to pageBuilder to assign an array of data --- index.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/index.php b/index.php index 850b350e..9100044e 100644 --- a/index.php +++ b/index.php @@ -637,6 +637,29 @@ class pageBuilder $this->tpl->assign($what,$where); } + /** + * Assign an array of data to the template builder. + * + * @param array $data Data to assign. + * + * @return false if invalid data. + */ + public function assignAll($data) + { + // Lazy initialization + if ($this->tpl === false) { + $this->initialize(); + } + + if (empty($data) || !is_array($data)){ + return false; + } + + foreach ($data as $key => $value) { + $this->assign($key, $value); + } + } + // Render a specific page (using a template). // e.g. pb.renderPage('picwall') public function renderPage($page) From 56e8ea2089000562533376c9e684303bdb9fdba9 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 10 Mar 2016 18:46:07 +0100 Subject: [PATCH 02/12] Create a template to handle ATOM feed ATOM feed improvement: * Adds a subtitle to match RSS feed behavior. * Better syntax for categories (see http://edward.oconnor.cx/2007/02/representing-tags-in-atom ). * Use locale to set the language --- tpl/feed.atom.html | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tpl/feed.atom.html diff --git a/tpl/feed.atom.html b/tpl/feed.atom.html new file mode 100644 index 00000000..2ebb162a --- /dev/null +++ b/tpl/feed.atom.html @@ -0,0 +1,40 @@ + + + {$pagetitle} + Shaared links + {if="$show_dates"} + {$last_update} + {/if} + + {if="!empty($pubsubhub_url)"} + + + + {/if} + + {$index_url} + {$index_url} + + {$index_url} + Shaarli + {loop="links"} + + {$value.title} + {if="$usepermalinks"} + + {else} + + {/if} + {$value.guid} + {if="$show_dates"} + {$value.iso_date} + {/if} + + + + {loop="$value.taglist"} + + {/loop} + + {/loop} + From 5f8e6ebc6ac1c7dff36feadb08f7ac71d5b55ec9 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 14:13:41 +0100 Subject: [PATCH 03/12] Adds a RSS template file Improvements: * Add searchtags in categories domain URL. * Language is now based on the locale. * Add a generator tag. * self link is always displayed. --- tpl/feed.rss.html | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tpl/feed.rss.html diff --git a/tpl/feed.rss.html b/tpl/feed.rss.html new file mode 100644 index 00000000..26de7f19 --- /dev/null +++ b/tpl/feed.rss.html @@ -0,0 +1,34 @@ + + + + {$pagetitle} + {$index_url} + Shaared links + {$language} + {$index_url} + Shaarli + + {if="!empty($pubsubhub_url)"} + + + {/if} + {loop="links"} + + {$value.title} + {$value.guid} + {if="$usepermalinks"} + {$value.guid} + {else} + {$value.url} + {/if} + {if="$show_dates"} + {$value.iso_date} + {/if} + + {loop="$value.taglist"} + {$value} + {/loop} + + {/loop} + + From 8395d0b76145969f4b8940a415af5e46528f04a5 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 10 Mar 2016 18:48:21 +0100 Subject: [PATCH 04/12] Adds a route for ATOM and RSS feeds page --- application/Router.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/application/Router.php b/application/Router.php index 6185f08e..d98312b5 100644 --- a/application/Router.php +++ b/application/Router.php @@ -15,6 +15,10 @@ class Router public static $PAGE_DAILY = 'daily'; + public static $PAGE_FEED_ATOM = 'atom'; + + public static $PAGE_FEED_RSS = 'rss'; + public static $PAGE_TOOLS = 'tools'; public static $PAGE_CHANGEPASSWORD = 'changepasswd'; @@ -79,6 +83,14 @@ class Router return self::$PAGE_DAILY; } + if (startsWith($query, 'do='. self::$PAGE_FEED_ATOM)) { + return self::$PAGE_FEED_ATOM; + } + + if (startsWith($query, 'do='. self::$PAGE_FEED_RSS)) { + return self::$PAGE_FEED_RSS; + } + // At this point, only loggedin pages. if (!$loggedIn) { return self::$PAGE_LINKLIST; From 69c474b96612dc64fc2cb66f1196251cafa08445 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 10 Mar 2016 19:01:30 +0100 Subject: [PATCH 05/12] 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 --- application/Utils.php | 2 +- index.php | 129 ++++++++++++++++++++---------------------- 2 files changed, 62 insertions(+), 69 deletions(-) diff --git a/application/Utils.php b/application/Utils.php index 3d819716..bcf5bdb5 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -226,7 +226,7 @@ function space2nbsp($text) * * @return string formatted description. */ -function format_description($description, $redirector) { +function format_description($description, $redirector = false) { return nl2br(space2nbsp(text2clickable($description, $redirector))); } 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); } - $pageaddr=escape(index_url($_SERVER)); + $keys = array(); + foreach ($linksToDisplay as $key=>$value) { + $keys[] = $key; // No, I can't use array_keys(). + } + + $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 && $iDirect link'; + } else { + $permalink = 'Permalink'; + } + $link['description'] = format_description($link['description']) . PHP_EOL .'
— '. $permalink; + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - $iso8601date = $date->format(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.''; + $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; - if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) { - $entries.=''.escape($iso8601date).''; - } - - // 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; - - $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.=''; - } - $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; + $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; + + $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(); From e67712ba0f8896f0d7e676292f557b5999c2848d Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 14:38:06 +0100 Subject: [PATCH 06/12] 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 14811c56..261c8a37 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(); From bcd078bf0a5119fe0c8441b803b4fe05fdaa6d18 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 14:39:06 +0100 Subject: [PATCH 07/12] Plugin: add render_feed hook and call it while generating ATOM and RSS feed. Create an example of the new hook in the demo plugin. --- index.php | 6 ++++++ plugins/demo_plugin/demo_plugin.php | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index 261c8a37..73b83533 100644 --- a/index.php +++ b/index.php @@ -785,6 +785,12 @@ function showRSS($pageBuilder, $linkDB) $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()); diff --git a/plugins/demo_plugin/demo_plugin.php b/plugins/demo_plugin/demo_plugin.php index f5f028e0..18834e53 100644 --- a/plugins/demo_plugin/demo_plugin.php +++ b/plugins/demo_plugin/demo_plugin.php @@ -322,4 +322,29 @@ function hook_demo_plugin_delete_link($data) if (strpos($data['url'], 'youtube.com') !== false) { exit('You can not delete a YouTube link. Don\'t ask.'); } -} \ No newline at end of file +} + +/** + * Execute render_feed hook. + * Called with ATOM and RSS feed. + * + * Special data keys: + * - _PAGE_: current page + * - _LOGGEDIN_: true/false + * + * @param array $data data passed to plugin + * + * @return array altered $data. + */ +function hook_demo_plugin_render_feed($data) +{ + foreach ($data['links'] as &$link) { + if ($data['_PAGE_'] == Router::$PAGE_FEED_ATOM) { + $link['description'] .= ' - ATOM Feed' ; + } + elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) { + $link['description'] .= ' - RSS Feed'; + } + } + return $data; +} From d4542fdb0d15f07810a4bc740bfceaa4189a3604 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 14:48:14 +0100 Subject: [PATCH 08/12] Reword the ENABLE_RSS_PERMALINKS in the settings. --- tpl/configure.html | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tpl/configure.html b/tpl/configure.html index 9c725a51..77c8b7d9 100644 --- a/tpl/configure.html +++ b/tpl/configure.html @@ -22,9 +22,12 @@ - Enable RSS Permalinks + RSS direct links - + + From 82e3680203896f024958ae969e2c4fccee9682f4 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 16:08:01 +0100 Subject: [PATCH 09/12] 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) From 89baf23ddfaf82cc663e26f76c307ef8e4bf4b02 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 12 Mar 2016 17:54:56 +0100 Subject: [PATCH 10/12] FeedBuilder unit tests --- tests/FeedBuilderTest.php | 212 ++++++++++++++++++++++++++++++++ tests/LinkFilterTest.php | 10 +- tests/utils/ReferenceLinkDB.php | 20 +-- 3 files changed, 227 insertions(+), 15 deletions(-) create mode 100644 tests/FeedBuilderTest.php diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php new file mode 100644 index 00000000..069b1581 --- /dev/null +++ b/tests/FeedBuilderTest.php @@ -0,0 +1,212 @@ +write(self::$testDatastore); + self::$linkDB = new LinkDB(self::$testDatastore, true, false); + self::$serverInfo = array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/index.php', + 'REQUEST_URI' => '/index.php?do=feed', + ); + } + + /** + * Test GetTypeLanguage(). + */ + public function testGetTypeLanguage() + { + $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage()); + $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage()); + $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false); + $this->assertEquals('en', $feedBuilder->getTypeLanguage()); + $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false); + $this->assertEquals('en-en', $feedBuilder->getTypeLanguage()); + } + + /** + * Test buildData with RSS feed. + */ + public function testRSSBuildData() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + // Test headers (RSS) + $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); + $this->assertEmpty($data['pubsubhub_url']); + $this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $data['last_update']); + $this->assertEquals(true, $data['show_dates']); + $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); + $this->assertEquals('http://host.tld/', $data['index_url']); + $this->assertFalse($data['usepermalinks']); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + + // Test first link (note link) + $link = array_shift($data['links']); + $this->assertEquals('20150310_114651', $link['linkdate']); + $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); + $this->assertEquals('http://host.tld/?WDWyig', $link['url']); + $this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $link['iso_date']); + $this->assertContains('Stallman has a beard', $link['description']); + $this->assertContains('Permalink', $link['description']); + $this->assertContains('http://host.tld/?WDWyig', $link['description']); + $this->assertEquals(1, count($link['taglist'])); + $this->assertEquals('stuff', $link['taglist'][0]); + + // Test URL with external link. + $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links']['20150310_114633']['url']); + + // Test multitags. + $this->assertEquals(5, count($data['links']['20141125_084734']['taglist'])); + $this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]); + } + + /** + * Test buildData with ATOM feed (test only specific to ATOM). + */ + public function testAtomBuildData() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $link = array_shift($data['links']); + $this->assertEquals('2015-03-10T11:46:51+01:00', $link['iso_date']); + } + + /** + * Test buildData with search criteria. + */ + public function testBuildDataFiltered() + { + $criteria = array( + 'searchtags' => 'stuff', + 'searchterm' => 'beard', + ); + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + $this->assertEquals(1, count($data['links'])); + $link = array_shift($data['links']); + $this->assertEquals('20150310_114651', $link['linkdate']); + } + + /** + * Test buildData with nb limit. + */ + public function testBuildDataCount() + { + $criteria = array( + 'nb' => '1', + ); + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + $this->assertEquals(1, count($data['links'])); + $link = array_shift($data['links']); + $this->assertEquals('20150310_114651', $link['linkdate']); + } + + /** + * Test buildData with permalinks on. + */ + public function testBuildDataPermalinks() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $feedBuilder->setUsePermalinks(true); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertTrue($data['usepermalinks']); + // First link is a permalink + $link = array_shift($data['links']); + $this->assertEquals('20150310_114651', $link['linkdate']); + $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); + $this->assertEquals('http://host.tld/?WDWyig', $link['url']); + $this->assertContains('Direct link', $link['description']); + $this->assertContains('http://host.tld/?WDWyig', $link['description']); + // Second link is a direct link + $link = array_shift($data['links']); + $this->assertEquals('20150310_114633', $link['linkdate']); + $this->assertEquals('http://host.tld/?kLHmZg', $link['guid']); + $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); + $this->assertContains('Direct link', $link['description']); + $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); + } + + /** + * Test buildData with hide dates settings. + */ + public function testBuildDataHideDates() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $feedBuilder->setHideDates(true); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertFalse($data['show_dates']); + + // Show dates while logged in + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true); + $feedBuilder->setLocale(self::$LOCALE); + $feedBuilder->setHideDates(true); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertTrue($data['show_dates']); + } + + /** + * Test buildData with hide dates settings. + */ + public function testBuildDataPubsubhub() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $feedBuilder->setPubsubhubUrl('http://pubsubhub.io'); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']); + } +} diff --git a/tests/LinkFilterTest.php b/tests/LinkFilterTest.php index ef1cc10a..f991a9c9 100644 --- a/tests/LinkFilterTest.php +++ b/tests/LinkFilterTest.php @@ -12,8 +12,6 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase */ protected static $linkFilter; - protected static $NB_LINKS_REFDB = 7; - /** * Instanciate linkFilter with ReferenceLinkDB data. */ @@ -29,7 +27,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase public function testFilter() { $this->assertEquals( - self::$NB_LINKS_REFDB, + ReferenceLinkDB::$NB_LINKS_TOTAL, count(self::$linkFilter->filter('', '')) ); @@ -40,12 +38,12 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase ); $this->assertEquals( - self::$NB_LINKS_REFDB, + ReferenceLinkDB::$NB_LINKS_TOTAL, count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '')) ); $this->assertEquals( - self::$NB_LINKS_REFDB, + ReferenceLinkDB::$NB_LINKS_TOTAL, count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '')) ); } @@ -383,7 +381,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase )) ); $this->assertEquals( - self::$NB_LINKS_REFDB, + ReferenceLinkDB::$NB_LINKS_TOTAL, count(self::$linkFilter->filter( LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, '' diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 61faef05..dc4f5dfa 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php @@ -4,6 +4,8 @@ */ class ReferenceLinkDB { + public static $NB_LINKS_TOTAL = 7; + private $_links = array(); private $_publicCount = 0; private $_privateCount = 0; @@ -13,6 +15,15 @@ class ReferenceLinkDB */ function __construct() { + $this->addLink( + 'Link title: @website', + '?WDWyig', + 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this.', + 0, + '20150310_114651', + 'stuff' + ); + $this->addLink( 'Free as in Freedom 2.0 @website', 'https://static.fsf.org/nosvn/faif-2.0.pdf', @@ -22,15 +33,6 @@ class ReferenceLinkDB 'free gnu software stallman -exclude stuff' ); - $this->addLink( - 'Link title: @website', - 'local', - 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this.', - 0, - '20150310_114651', - 'stuff' - ); - $this->addLink( 'MediaGoblin', 'http://mediagoblin.org/', From ee88a4bcc29da721cf43b750663aebeac4969517 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 20 Mar 2016 14:14:38 +0100 Subject: [PATCH 11/12] Makes escape a recursive function which handle array of strings --- application/Utils.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/application/Utils.php b/application/Utils.php index bcf5bdb5..5b8ca508 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -63,14 +63,22 @@ function endsWith($haystack, $needle, $case=true) /** * Htmlspecialchars wrapper + * Support multidimensional array of strings. * - * @param string $str the string to escape. + * @param mixed $input Data to escape: a single string or an array of strings. * * @return string escaped. */ -function escape($str) +function escape($input) { - return htmlspecialchars($str, ENT_COMPAT, 'UTF-8', false); + if (is_array($input)) { + $out = array(); + foreach($input as $key => $value) { + $out[$key] = escape($value); + } + return $out; + } + return htmlspecialchars($input, ENT_COMPAT, 'UTF-8', false); } /** From 528a6f8a232c060faf024008e4f8a09b4aa8dabc Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 21 Mar 2016 21:40:49 +0100 Subject: [PATCH 12/12] Refactor filter in LinkDB * search type now carried by LinkDB in order to factorize code between different search sources. * LinkDB->filter split in 3 method: filterSearch, filterHash, filterDay (we know what type of filter is needed). * filterHash now throw a LinkNotFoundException if it doesn't exist: internal implementation choice, still displays a 404. * Smallhash regex has been rewritten. * Unit tests update --- application/FeedBuilder.php | 18 +------ application/LinkDB.php | 64 ++++++++++++++++++++-- application/LinkFilter.php | 14 ++++- application/Updater.php | 2 +- index.php | 99 ++++++++++------------------------- tests/LinkDBTest.php | 56 ++++++++++++++++++-- tests/LinkFilterTest.php | 7 ++- tests/Updater/UpdaterTest.php | 4 +- 8 files changed, 158 insertions(+), 106 deletions(-) diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php index 50e09831..ddefe6ce 100644 --- a/application/FeedBuilder.php +++ b/application/FeedBuilder.php @@ -103,23 +103,7 @@ class FeedBuilder 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; - } + $linksToDisplay = $this->linkDB->filterSearch($this->userInput); $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); diff --git a/application/LinkDB.php b/application/LinkDB.php index 1b505620..a62341fc 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -341,17 +341,71 @@ You use the community supported version of the original Shaarli project, by Seba } /** - * Filter links. + * Returns the shaare corresponding to a smallHash. * - * @param string $type Type of filter. - * @param mixed $request Search request, string or array. + * @param string $request QUERY_STRING server parameter. + * + * @return array $filtered array containing permalink data. + * + * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link. + */ + public function filterHash($request) + { + $request = substr($request, 0, 6); + $linkFilter = new LinkFilter($this->_links); + return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request); + } + + /** + * Returns the list of articles for a given day. + * + * @param string $request day to filter. Format: YYYYMMDD. + * + * @return array list of shaare found. + */ + public function filterDay($request) { + $linkFilter = new LinkFilter($this->_links); + return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request); + } + + /** + * Filter links according to search parameters. + * + * @param array $filterRequest Search request content. Supported keys: + * - searchtags: list of tags + * - searchterm: term search * @param bool $casesensitive Optional: Perform case sensitive filter * @param bool $privateonly Optional: Returns private links only if true. * - * @return array filtered links + * @return array filtered links, all links if no suitable filter was provided. */ - public function filter($type = '', $request = '', $casesensitive = false, $privateonly = false) + public function filterSearch($filterRequest = array(), $casesensitive = false, $privateonly = false) { + // Filter link database according to parameters. + $searchtags = !empty($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : ''; + $searchterm = !empty($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : ''; + + // Search tags + fullsearch. + if (empty($type) && ! empty($searchtags) && ! empty($searchterm)) { + $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT; + $request = array($searchtags, $searchterm); + } + // Search by tags. + elseif (! empty($searchtags)) { + $type = LinkFilter::$FILTER_TAG; + $request = $searchtags; + } + // Fulltext search. + elseif (! empty($searchterm)) { + $type = LinkFilter::$FILTER_TEXT; + $request = $searchterm; + } + // Otherwise, display without filtering. + else { + $type = ''; + $request = ''; + } + $linkFilter = new LinkFilter($this->_links); return $linkFilter->filter($type, $request, $casesensitive, $privateonly); } diff --git a/application/LinkFilter.php b/application/LinkFilter.php index 3fd803cb..5e0d8015 100644 --- a/application/LinkFilter.php +++ b/application/LinkFilter.php @@ -44,7 +44,7 @@ class LinkFilter * Filter links according to parameters. * * @param string $type Type of filter (eg. tags, permalink, etc.). - * @param string $request Filter content. + * @param mixed $request Filter content. * @param bool $casesensitive Optional: Perform case sensitive filter if true. * @param bool $privateonly Optional: Only returns private links if true. * @@ -110,6 +110,8 @@ class LinkFilter * @param string $smallHash permalink hash. * * @return array $filtered array containing permalink data. + * + * @throws LinkNotFoundException if the smallhash doesn't match any link. */ private function filterSmallHash($smallHash) { @@ -121,6 +123,11 @@ class LinkFilter return $filtered; } } + + if (empty($filtered)) { + throw new LinkNotFoundException(); + } + return $filtered; } @@ -318,3 +325,8 @@ class LinkFilter return array_filter(explode(' ', trim($tagsOut)), 'strlen'); } } + +class LinkNotFoundException extends Exception +{ + protected $message = 'The link you are trying to reach does not exist or has been deleted.'; +} diff --git a/application/Updater.php b/application/Updater.php index 773a1ffa..58c13c07 100644 --- a/application/Updater.php +++ b/application/Updater.php @@ -137,7 +137,7 @@ class Updater */ public function updateMethodRenameDashTags() { - $linklist = $this->linkDB->filter(); + $linklist = $this->linkDB->filterSearch(); foreach ($linklist as $link) { $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); diff --git a/index.php b/index.php index 6e14ff3f..79c66648 100644 --- a/index.php +++ b/index.php @@ -816,7 +816,7 @@ function showDaily($pageBuilder) } try { - $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_DAY, $day); + $linksToDisplay = $LINKSDB->filterDay($day); } catch (Exception $exc) { error_log($exc); $linksToDisplay = array(); @@ -962,24 +962,7 @@ function renderPage() if ($targetPage == Router::$PAGE_PICWALL) { // Optionally filter the results: - $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; - $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; - if (! empty($searchtags) && ! empty($searchterm)) { - $links = $LINKSDB->filter( - LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, - array($searchtags, $searchterm) - ); - } - elseif ($searchtags) { - $links = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); - } - elseif ($searchterm) { - $links = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); - } - else { - $links = $LINKSDB; - } - + $links = $LINKSDB->filterSearch($_GET); $linksToDisplay = array(); // Get only links which have a thumbnail. @@ -1071,7 +1054,7 @@ function renderPage() startsWith($query,'do='. $targetPage) && !isLoggedIn() ); $cached = $cache->cachedVersion(); - if (!empty($cached)) { + if (false && !empty($cached)) { echo $cached; exit; } @@ -1352,9 +1335,9 @@ function renderPage() // Delete a tag: if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { - $needle=trim($_POST['fromtag']); + $needle = trim($_POST['fromtag']); // True for case-sensitive tag search. - $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true); + $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); foreach($linksToAlter as $key=>$value) { $tags = explode(' ',trim($value['tags'])); @@ -1369,9 +1352,9 @@ function renderPage() // Rename a tag: if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) { - $needle=trim($_POST['fromtag']); + $needle = trim($_POST['fromtag']); // True for case-sensitive tag search. - $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true); + $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); foreach($linksToAlter as $key=>$value) { $tags = explode(' ',trim($value['tags'])); @@ -1807,60 +1790,32 @@ function importFile() } } -// ----------------------------------------------------------------------------------------------- -// Template for the list of links (