aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-03-21 21:40:49 +0100
committerArthurHoaro <arthur@hoa.ro>2016-03-25 19:17:59 +0100
commit528a6f8a232c060faf024008e4f8a09b4aa8dabc (patch)
tree86cac78b7f4d3998bedd923da83145c37ec88ff4 /index.php
parentee88a4bcc29da721cf43b750663aebeac4969517 (diff)
downloadShaarli-528a6f8a232c060faf024008e4f8a09b4aa8dabc.tar.gz
Shaarli-528a6f8a232c060faf024008e4f8a09b4aa8dabc.tar.zst
Shaarli-528a6f8a232c060faf024008e4f8a09b4aa8dabc.zip
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
Diffstat (limited to 'index.php')
-rw-r--r--index.php99
1 files changed, 27 insertions, 72 deletions
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)
816 } 816 }
817 817
818 try { 818 try {
819 $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_DAY, $day); 819 $linksToDisplay = $LINKSDB->filterDay($day);
820 } catch (Exception $exc) { 820 } catch (Exception $exc) {
821 error_log($exc); 821 error_log($exc);
822 $linksToDisplay = array(); 822 $linksToDisplay = array();
@@ -962,24 +962,7 @@ function renderPage()
962 if ($targetPage == Router::$PAGE_PICWALL) 962 if ($targetPage == Router::$PAGE_PICWALL)
963 { 963 {
964 // Optionally filter the results: 964 // Optionally filter the results:
965 $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; 965 $links = $LINKSDB->filterSearch($_GET);
966 $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
967 if (! empty($searchtags) && ! empty($searchterm)) {
968 $links = $LINKSDB->filter(
969 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
970 array($searchtags, $searchterm)
971 );
972 }
973 elseif ($searchtags) {
974 $links = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags);
975 }
976 elseif ($searchterm) {
977 $links = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm);
978 }
979 else {
980 $links = $LINKSDB;
981 }
982
983 $linksToDisplay = array(); 966 $linksToDisplay = array();
984 967
985 // Get only links which have a thumbnail. 968 // Get only links which have a thumbnail.
@@ -1071,7 +1054,7 @@ function renderPage()
1071 startsWith($query,'do='. $targetPage) && !isLoggedIn() 1054 startsWith($query,'do='. $targetPage) && !isLoggedIn()
1072 ); 1055 );
1073 $cached = $cache->cachedVersion(); 1056 $cached = $cache->cachedVersion();
1074 if (!empty($cached)) { 1057 if (false && !empty($cached)) {
1075 echo $cached; 1058 echo $cached;
1076 exit; 1059 exit;
1077 } 1060 }
@@ -1352,9 +1335,9 @@ function renderPage()
1352 1335
1353 // Delete a tag: 1336 // Delete a tag:
1354 if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { 1337 if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) {
1355 $needle=trim($_POST['fromtag']); 1338 $needle = trim($_POST['fromtag']);
1356 // True for case-sensitive tag search. 1339 // True for case-sensitive tag search.
1357 $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true); 1340 $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
1358 foreach($linksToAlter as $key=>$value) 1341 foreach($linksToAlter as $key=>$value)
1359 { 1342 {
1360 $tags = explode(' ',trim($value['tags'])); 1343 $tags = explode(' ',trim($value['tags']));
@@ -1369,9 +1352,9 @@ function renderPage()
1369 1352
1370 // Rename a tag: 1353 // Rename a tag:
1371 if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) { 1354 if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) {
1372 $needle=trim($_POST['fromtag']); 1355 $needle = trim($_POST['fromtag']);
1373 // True for case-sensitive tag search. 1356 // True for case-sensitive tag search.
1374 $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true); 1357 $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
1375 foreach($linksToAlter as $key=>$value) 1358 foreach($linksToAlter as $key=>$value)
1376 { 1359 {
1377 $tags = explode(' ',trim($value['tags'])); 1360 $tags = explode(' ',trim($value['tags']));
@@ -1807,60 +1790,32 @@ function importFile()
1807 } 1790 }
1808} 1791}
1809 1792
1810// ----------------------------------------------------------------------------------------------- 1793/**
1811// Template for the list of links (<div id="linklist">) 1794 * Template for the list of links (<div id="linklist">)
1812// This function fills all the necessary fields in the $PAGE for the template 'linklist.html' 1795 * This function fills all the necessary fields in the $PAGE for the template 'linklist.html'
1796 *
1797 * @param pageBuilder $PAGE pageBuilder instance.
1798 * @param LinkDB $LINKSDB LinkDB instance.
1799 */
1813function buildLinkList($PAGE,$LINKSDB) 1800function buildLinkList($PAGE,$LINKSDB)
1814{ 1801{
1815 // Filter link database according to parameters. 1802 // Used in templates
1816 $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; 1803 $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
1817 $searchterm = !empty($_GET['searchterm']) ? escape(trim($_GET['searchterm'])) : ''; 1804 $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
1818 $privateonly = !empty($_SESSION['privateonly']) ? true : false;
1819
1820 // Search tags + fullsearch.
1821 if (! empty($searchtags) && ! empty($searchterm)) {
1822 $linksToDisplay = $LINKSDB->filter(
1823 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
1824 array($searchtags, $searchterm),
1825 false,
1826 $privateonly
1827 );
1828 }
1829 // Search by tags.
1830 elseif (! empty($searchtags)) {
1831 $linksToDisplay = $LINKSDB->filter(
1832 LinkFilter::$FILTER_TAG,
1833 $searchtags,
1834 false,
1835 $privateonly
1836 );
1837 }
1838 // Fulltext search.
1839 elseif (! empty($searchterm)) {
1840 $linksToDisplay = $LINKSDB->filter(
1841 LinkFilter::$FILTER_TEXT,
1842 $searchterm,
1843 false,
1844 $privateonly
1845 );
1846 }
1847 // Detect smallHashes in URL.
1848 elseif (! empty($_SERVER['QUERY_STRING'])
1849 && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/', $_SERVER['QUERY_STRING'])
1850 ) {
1851 $linksToDisplay = $LINKSDB->filter(
1852 LinkFilter::$FILTER_HASH,
1853 substr(trim($_SERVER["QUERY_STRING"], '/'), 0, 6)
1854 );
1855 1805
1856 if (count($linksToDisplay) == 0) { 1806 // Smallhash filter
1857 $PAGE->render404('The link you are trying to reach does not exist or has been deleted.'); 1807 if (! empty($_SERVER['QUERY_STRING'])
1808 && preg_match('/^[a-zA-Z0-9-_@]{6}($|&|#)/', $_SERVER['QUERY_STRING'])) {
1809 try {
1810 $linksToDisplay = $LINKSDB->filterHash($_SERVER['QUERY_STRING']);
1811 } catch (LinkNotFoundException $e) {
1812 $PAGE->render404($e->getMessage());
1858 exit; 1813 exit;
1859 } 1814 }
1860 } 1815 } else {
1861 // Otherwise, display without filtering. 1816 // Filter links according search parameters.
1862 else { 1817 $privateonly = !empty($_SESSION['privateonly']);
1863 $linksToDisplay = $LINKSDB->filter('', '', false, $privateonly); 1818 $linksToDisplay = $LINKSDB->filterSearch($_GET, false, $privateonly);
1864 } 1819 }
1865 1820
1866 // ---- Handle paging. 1821 // ---- Handle paging.