diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-02-23 19:21:14 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-02-28 14:17:40 +0100 |
commit | c51fae92dc7d3080def81a2797e0d683b3e6d82a (patch) | |
tree | 394f9419589c1983d9be9834b89c8be2e8ef237c /index.php | |
parent | 6c3d6a31f413862941fe514e7167c04fe71ba1a7 (diff) | |
download | Shaarli-c51fae92dc7d3080def81a2797e0d683b3e6d82a.tar.gz Shaarli-c51fae92dc7d3080def81a2797e0d683b3e6d82a.tar.zst Shaarli-c51fae92dc7d3080def81a2797e0d683b3e6d82a.zip |
Allow crossed search between terms and tags
* Partial fix of #449
* Current use case: search term + click on tag.
* LinkFilter now returns all links if no filter is given.
* Unit tests.
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 120 |
1 files changed, 79 insertions, 41 deletions
@@ -623,7 +623,7 @@ class pageBuilder | |||
623 | if (!empty($_GET['searchtags'])) { | 623 | if (!empty($_GET['searchtags'])) { |
624 | $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']); | 624 | $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']); |
625 | } | 625 | } |
626 | elseif (!empty($_GET['searchterm'])) { | 626 | if (!empty($_GET['searchterm'])) { |
627 | $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']); | 627 | $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']); |
628 | } | 628 | } |
629 | $this->tpl->assign('searchcrits', $searchcrits); | 629 | $this->tpl->assign('searchcrits', $searchcrits); |
@@ -709,11 +709,19 @@ function showRSS() | |||
709 | // Read links from database (and filter private links if user it not logged in). | 709 | // Read links from database (and filter private links if user it not logged in). |
710 | 710 | ||
711 | // Optionally filter the results: | 711 | // Optionally filter the results: |
712 | if (!empty($_GET['searchterm'])) { | 712 | $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; |
713 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']); | 713 | $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; |
714 | if (! empty($searchtags) && ! empty($searchterm)) { | ||
715 | $linksToDisplay = $LINKSDB->filter( | ||
716 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | ||
717 | array($searchtags, $searchterm) | ||
718 | ); | ||
714 | } | 719 | } |
715 | elseif (!empty($_GET['searchtags'])) { | 720 | elseif ($searchtags) { |
716 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags'])); | 721 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); |
722 | } | ||
723 | elseif ($searchterm) { | ||
724 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); | ||
717 | } | 725 | } |
718 | else { | 726 | else { |
719 | $linksToDisplay = $LINKSDB; | 727 | $linksToDisplay = $LINKSDB; |
@@ -807,11 +815,19 @@ function showATOM() | |||
807 | ); | 815 | ); |
808 | 816 | ||
809 | // Optionally filter the results: | 817 | // Optionally filter the results: |
810 | if (!empty($_GET['searchterm'])) { | 818 | $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; |
811 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']); | 819 | $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; |
820 | if (! empty($searchtags) && ! empty($searchterm)) { | ||
821 | $linksToDisplay = $LINKSDB->filter( | ||
822 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | ||
823 | array($searchtags, $searchterm) | ||
824 | ); | ||
812 | } | 825 | } |
813 | else if (!empty($_GET['searchtags'])) { | 826 | elseif ($searchtags) { |
814 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags'])); | 827 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); |
828 | } | ||
829 | elseif ($searchterm) { | ||
830 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); | ||
815 | } | 831 | } |
816 | else { | 832 | else { |
817 | $linksToDisplay = $LINKSDB; | 833 | $linksToDisplay = $LINKSDB; |
@@ -1165,11 +1181,19 @@ function renderPage() | |||
1165 | if ($targetPage == Router::$PAGE_PICWALL) | 1181 | if ($targetPage == Router::$PAGE_PICWALL) |
1166 | { | 1182 | { |
1167 | // Optionally filter the results: | 1183 | // Optionally filter the results: |
1168 | if (!empty($_GET['searchterm'])) { | 1184 | $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; |
1169 | $links = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']); | 1185 | $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : ''; |
1186 | if (! empty($searchtags) && ! empty($searchterm)) { | ||
1187 | $links = $LINKSDB->filter( | ||
1188 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | ||
1189 | array($searchtags, $searchterm) | ||
1190 | ); | ||
1170 | } | 1191 | } |
1171 | elseif (! empty($_GET['searchtags'])) { | 1192 | elseif ($searchtags) { |
1172 | $links = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags'])); | 1193 | $links = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags); |
1194 | } | ||
1195 | elseif ($searchterm) { | ||
1196 | $links = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm); | ||
1173 | } | 1197 | } |
1174 | else { | 1198 | else { |
1175 | $links = $LINKSDB; | 1199 | $links = $LINKSDB; |
@@ -1963,29 +1987,46 @@ function importFile() | |||
1963 | // This function fills all the necessary fields in the $PAGE for the template 'linklist.html' | 1987 | // This function fills all the necessary fields in the $PAGE for the template 'linklist.html' |
1964 | function buildLinkList($PAGE,$LINKSDB) | 1988 | function buildLinkList($PAGE,$LINKSDB) |
1965 | { | 1989 | { |
1966 | // ---- Filter link database according to parameters | 1990 | // Filter link database according to parameters. |
1967 | $search_type = ''; | 1991 | $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : ''; |
1968 | $search_crits = ''; | 1992 | $searchterm = !empty($_GET['searchterm']) ? escape(trim($_GET['searchterm'])) : ''; |
1969 | $privateonly = !empty($_SESSION['privateonly']) ? true : false; | 1993 | $privateonly = !empty($_SESSION['privateonly']) ? true : false; |
1970 | 1994 | ||
1971 | // Fulltext search | 1995 | // Search tags + fullsearch. |
1972 | if (isset($_GET['searchterm'])) { | 1996 | if (! empty($searchtags) && ! empty($searchterm)) { |
1973 | $search_crits = escape(trim($_GET['searchterm'])); | 1997 | $linksToDisplay = $LINKSDB->filter( |
1974 | $search_type = LinkFilter::$FILTER_TEXT; | 1998 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, |
1975 | $linksToDisplay = $LINKSDB->filter($search_type, $search_crits, false, $privateonly); | 1999 | array($searchtags, $searchterm), |
2000 | false, | ||
2001 | $privateonly | ||
2002 | ); | ||
1976 | } | 2003 | } |
1977 | // Search by tag | 2004 | // Search by tags. |
1978 | elseif (isset($_GET['searchtags'])) { | 2005 | elseif (! empty($searchtags)) { |
1979 | $search_crits = explode(' ', escape(trim($_GET['searchtags']))); | 2006 | $linksToDisplay = $LINKSDB->filter( |
1980 | $search_type = LinkFilter::$FILTER_TAG; | 2007 | LinkFilter::$FILTER_TAG, |
1981 | $linksToDisplay = $LINKSDB->filter($search_type, $search_crits, false, $privateonly); | 2008 | $searchtags, |
2009 | false, | ||
2010 | $privateonly | ||
2011 | ); | ||
2012 | } | ||
2013 | // Fulltext search. | ||
2014 | elseif (! empty($searchterm)) { | ||
2015 | $linksToDisplay = $LINKSDB->filter( | ||
2016 | LinkFilter::$FILTER_TEXT, | ||
2017 | $searchterm, | ||
2018 | false, | ||
2019 | $privateonly | ||
2020 | ); | ||
1982 | } | 2021 | } |
1983 | // Detect smallHashes in URL. | 2022 | // Detect smallHashes in URL. |
1984 | elseif (isset($_SERVER['QUERY_STRING']) | 2023 | elseif (! empty($_SERVER['QUERY_STRING']) |
1985 | && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/', $_SERVER['QUERY_STRING'])) { | 2024 | && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/', $_SERVER['QUERY_STRING']) |
1986 | $search_type = LinkFilter::$FILTER_HASH; | 2025 | ) { |
1987 | $search_crits = substr(trim($_SERVER["QUERY_STRING"], '/'), 0, 6); | 2026 | $linksToDisplay = $LINKSDB->filter( |
1988 | $linksToDisplay = $LINKSDB->filter($search_type, $search_crits); | 2027 | LinkFilter::$FILTER_HASH, |
2028 | substr(trim($_SERVER["QUERY_STRING"], '/'), 0, 6) | ||
2029 | ); | ||
1989 | 2030 | ||
1990 | if (count($linksToDisplay) == 0) { | 2031 | if (count($linksToDisplay) == 0) { |
1991 | $PAGE->render404('The link you are trying to reach does not exist or has been deleted.'); | 2032 | $PAGE->render404('The link you are trying to reach does not exist or has been deleted.'); |
@@ -2041,21 +2082,18 @@ function buildLinkList($PAGE,$LINKSDB) | |||
2041 | } | 2082 | } |
2042 | 2083 | ||
2043 | // Compute paging navigation | 2084 | // Compute paging navigation |
2044 | $searchterm = empty($_GET['searchterm']) ? '' : '&searchterm=' . $_GET['searchterm']; | 2085 | $searchtagsUrl = empty($searchtags) ? '' : '&searchtags=' . urlencode($searchtags); |
2045 | $searchtags = empty($_GET['searchtags']) ? '' : '&searchtags=' . $_GET['searchtags']; | 2086 | $searchtermUrl = empty($searchterm) ? '' : '&searchterm=' . urlencode($searchterm); |
2046 | $previous_page_url = ''; | 2087 | $previous_page_url = ''; |
2047 | if ($i != count($keys)) { | 2088 | if ($i != count($keys)) { |
2048 | $previous_page_url = '?page=' . ($page+1) . $searchterm . $searchtags; | 2089 | $previous_page_url = '?page=' . ($page+1) . $searchtermUrl . $searchtagsUrl; |
2049 | } | 2090 | } |
2050 | $next_page_url=''; | 2091 | $next_page_url=''; |
2051 | if ($page>1) { | 2092 | if ($page>1) { |
2052 | $next_page_url = '?page=' . ($page-1) . $searchterm . $searchtags; | 2093 | $next_page_url = '?page=' . ($page-1) . $searchtermUrl . $searchtagsUrl; |
2053 | } | 2094 | } |
2054 | 2095 | ||
2055 | $token = ''; | 2096 | $token = isLoggedIn() ? getToken() : ''; |
2056 | if (isLoggedIn()) { | ||
2057 | $token = getToken(); | ||
2058 | } | ||
2059 | 2097 | ||
2060 | // Fill all template fields. | 2098 | // Fill all template fields. |
2061 | $data = array( | 2099 | $data = array( |
@@ -2065,8 +2103,8 @@ function buildLinkList($PAGE,$LINKSDB) | |||
2065 | 'page_current' => $page, | 2103 | 'page_current' => $page, |
2066 | 'page_max' => $pagecount, | 2104 | 'page_max' => $pagecount, |
2067 | 'result_count' => count($linksToDisplay), | 2105 | 'result_count' => count($linksToDisplay), |
2068 | 'search_type' => $search_type, | 2106 | 'search_term' => $searchterm, |
2069 | 'search_crits' => $search_crits, | 2107 | 'search_tags' => $searchtags, |
2070 | 'redirector' => empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'], // Optional redirector URL. | 2108 | 'redirector' => empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'], // Optional redirector URL. |
2071 | 'token' => $token, | 2109 | 'token' => $token, |
2072 | 'links' => $linkDisp, | 2110 | 'links' => $linkDisp, |