diff options
author | ArthurHoaro <arthur@hoa.ro> | 2015-12-27 10:08:20 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-01-06 19:53:04 +0100 |
commit | 822bffced8212e7f34bcb2ad063b31a78bd57bdb (patch) | |
tree | 04cd1efe9d2f015669a451fc7c853c23e38a44cb /index.php | |
parent | ba83317573a1477d731cbd3d974b601cf9afdba3 (diff) | |
download | Shaarli-822bffced8212e7f34bcb2ad063b31a78bd57bdb.tar.gz Shaarli-822bffced8212e7f34bcb2ad063b31a78bd57bdb.tar.zst Shaarli-822bffced8212e7f34bcb2ad063b31a78bd57bdb.zip |
Link filter refactoring
* introduce class LinkFilter to handle link filter operation (and lighten LinkDB).
* handle 'private only' in filtering.
* update template to prefill search fields with current search terms.
* coding style.
* unit test (mostly move from LinkDB to LinkFilter).
PS: preparation for #358 #315 and 'AND' search.
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 200 |
1 files changed, 111 insertions, 89 deletions
@@ -151,6 +151,7 @@ require_once 'application/CachedPage.php'; | |||
151 | require_once 'application/FileUtils.php'; | 151 | require_once 'application/FileUtils.php'; |
152 | require_once 'application/HttpUtils.php'; | 152 | require_once 'application/HttpUtils.php'; |
153 | require_once 'application/LinkDB.php'; | 153 | require_once 'application/LinkDB.php'; |
154 | require_once 'application/LinkFilter.php'; | ||
154 | require_once 'application/TimeZone.php'; | 155 | require_once 'application/TimeZone.php'; |
155 | require_once 'application/Url.php'; | 156 | require_once 'application/Url.php'; |
156 | require_once 'application/Utils.php'; | 157 | require_once 'application/Utils.php'; |
@@ -730,18 +731,23 @@ function showRSS() | |||
730 | // Read links from database (and filter private links if user it not logged in). | 731 | // Read links from database (and filter private links if user it not logged in). |
731 | 732 | ||
732 | // Optionally filter the results: | 733 | // Optionally filter the results: |
733 | $linksToDisplay=array(); | 734 | if (!empty($_GET['searchterm'])) { |
734 | if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']); | 735 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']); |
735 | else if (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); | 736 | } |
736 | else $linksToDisplay = $LINKSDB; | 737 | elseif (!empty($_GET['searchtags'])) { |
738 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags'])); | ||
739 | } | ||
740 | else { | ||
741 | $linksToDisplay = $LINKSDB; | ||
742 | } | ||
737 | 743 | ||
738 | $nblinksToDisplay = 50; // Number of links to display. | 744 | $nblinksToDisplay = 50; // Number of links to display. |
739 | if (!empty($_GET['nb'])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. | 745 | // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. |
740 | { | 746 | if (!empty($_GET['nb'])) { |
741 | $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max($_GET['nb']+0,1) ; | 747 | $nblinksToDisplay = $_GET['nb'] == 'all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); |
742 | } | 748 | } |
743 | 749 | ||
744 | $pageaddr=escape(index_url($_SERVER)); | 750 | $pageaddr = escape(index_url($_SERVER)); |
745 | echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">'; | 751 | echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">'; |
746 | echo '<channel><title>'.$GLOBALS['title'].'</title><link>'.$pageaddr.'</link>'; | 752 | echo '<channel><title>'.$GLOBALS['title'].'</title><link>'.$pageaddr.'</link>'; |
747 | echo '<description>Shared links</description><language>en-en</language><copyright>'.$pageaddr.'</copyright>'."\n\n"; | 753 | echo '<description>Shared links</description><language>en-en</language><copyright>'.$pageaddr.'</copyright>'."\n\n"; |
@@ -821,15 +827,20 @@ function showATOM() | |||
821 | ); | 827 | ); |
822 | 828 | ||
823 | // Optionally filter the results: | 829 | // Optionally filter the results: |
824 | $linksToDisplay=array(); | 830 | if (!empty($_GET['searchterm'])) { |
825 | if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']); | 831 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']); |
826 | else if (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); | 832 | } |
827 | else $linksToDisplay = $LINKSDB; | 833 | else if (!empty($_GET['searchtags'])) { |
834 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags'])); | ||
835 | } | ||
836 | else { | ||
837 | $linksToDisplay = $LINKSDB; | ||
838 | } | ||
828 | 839 | ||
829 | $nblinksToDisplay = 50; // Number of links to display. | 840 | $nblinksToDisplay = 50; // Number of links to display. |
830 | if (!empty($_GET['nb'])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. | 841 | // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. |
831 | { | 842 | if (!empty($_GET['nb'])) { |
832 | $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max($_GET['nb']+0,1) ; | 843 | $nblinksToDisplay = $_GET['nb']=='all' ? count($linksToDisplay) : max(intval($_GET['nb']), 1); |
833 | } | 844 | } |
834 | 845 | ||
835 | $pageaddr=escape(index_url($_SERVER)); | 846 | $pageaddr=escape(index_url($_SERVER)); |
@@ -1024,7 +1035,7 @@ function showDaily($pageBuilder) | |||
1024 | } | 1035 | } |
1025 | 1036 | ||
1026 | try { | 1037 | try { |
1027 | $linksToDisplay = $LINKSDB->filterDay($day); | 1038 | $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_DAY, $day); |
1028 | } catch (Exception $exc) { | 1039 | } catch (Exception $exc) { |
1029 | error_log($exc); | 1040 | error_log($exc); |
1030 | $linksToDisplay = array(); | 1041 | $linksToDisplay = array(); |
@@ -1149,13 +1160,17 @@ function renderPage() | |||
1149 | if ($targetPage == Router::$PAGE_PICWALL) | 1160 | if ($targetPage == Router::$PAGE_PICWALL) |
1150 | { | 1161 | { |
1151 | // Optionally filter the results: | 1162 | // Optionally filter the results: |
1152 | $links=array(); | 1163 | if (!empty($_GET['searchterm'])) { |
1153 | if (!empty($_GET['searchterm'])) $links = $LINKSDB->filterFulltext($_GET['searchterm']); | 1164 | $links = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $_GET['searchterm']); |
1154 | elseif (!empty($_GET['searchtags'])) $links = $LINKSDB->filterTags(trim($_GET['searchtags'])); | 1165 | } |
1155 | else $links = $LINKSDB; | 1166 | elseif (! empty($_GET['searchtags'])) { |
1167 | $links = $LINKSDB->filter(LinkFilter::$FILTER_TAG, trim($_GET['searchtags'])); | ||
1168 | } | ||
1169 | else { | ||
1170 | $links = $LINKSDB; | ||
1171 | } | ||
1156 | 1172 | ||
1157 | $body=''; | 1173 | $linksToDisplay = array(); |
1158 | $linksToDisplay=array(); | ||
1159 | 1174 | ||
1160 | // Get only links which have a thumbnail. | 1175 | // Get only links which have a thumbnail. |
1161 | foreach($links as $link) | 1176 | foreach($links as $link) |
@@ -1282,7 +1297,7 @@ function renderPage() | |||
1282 | } | 1297 | } |
1283 | 1298 | ||
1284 | if (isset($params['searchtags'])) { | 1299 | if (isset($params['searchtags'])) { |
1285 | $tags = explode(' ',$params['searchtags']); | 1300 | $tags = explode(' ', $params['searchtags']); |
1286 | $tags=array_diff($tags, array($_GET['removetag'])); // Remove value from array $tags. | 1301 | $tags=array_diff($tags, array($_GET['removetag'])); // Remove value from array $tags. |
1287 | if (count($tags)==0) { | 1302 | if (count($tags)==0) { |
1288 | unset($params['searchtags']); | 1303 | unset($params['searchtags']); |
@@ -1467,7 +1482,8 @@ function renderPage() | |||
1467 | if (!empty($_POST['deletetag']) && !empty($_POST['fromtag'])) | 1482 | if (!empty($_POST['deletetag']) && !empty($_POST['fromtag'])) |
1468 | { | 1483 | { |
1469 | $needle=trim($_POST['fromtag']); | 1484 | $needle=trim($_POST['fromtag']); |
1470 | $linksToAlter = $LINKSDB->filterTags($needle,true); // True for case-sensitive tag search. | 1485 | // True for case-sensitive tag search. |
1486 | $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true); | ||
1471 | foreach($linksToAlter as $key=>$value) | 1487 | foreach($linksToAlter as $key=>$value) |
1472 | { | 1488 | { |
1473 | $tags = explode(' ',trim($value['tags'])); | 1489 | $tags = explode(' ',trim($value['tags'])); |
@@ -1484,7 +1500,8 @@ function renderPage() | |||
1484 | if (!empty($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) | 1500 | if (!empty($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) |
1485 | { | 1501 | { |
1486 | $needle=trim($_POST['fromtag']); | 1502 | $needle=trim($_POST['fromtag']); |
1487 | $linksToAlter = $LINKSDB->filterTags($needle,true); // true for case-sensitive tag search. | 1503 | // True for case-sensitive tag search. |
1504 | $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true); | ||
1488 | foreach($linksToAlter as $key=>$value) | 1505 | foreach($linksToAlter as $key=>$value) |
1489 | { | 1506 | { |
1490 | $tags = explode(' ',trim($value['tags'])); | 1507 | $tags = explode(' ',trim($value['tags'])); |
@@ -1865,81 +1882,78 @@ function importFile() | |||
1865 | function buildLinkList($PAGE,$LINKSDB) | 1882 | function buildLinkList($PAGE,$LINKSDB) |
1866 | { | 1883 | { |
1867 | // ---- Filter link database according to parameters | 1884 | // ---- Filter link database according to parameters |
1868 | $linksToDisplay=array(); | 1885 | $search_type = ''; |
1869 | $search_type=''; | 1886 | $search_crits = ''; |
1870 | $search_crits=''; | 1887 | $privateonly = !empty($_SESSION['privateonly']) ? true : false; |
1871 | if (isset($_GET['searchterm'])) // Fulltext search | 1888 | |
1872 | { | 1889 | // Fulltext search |
1873 | $linksToDisplay = $LINKSDB->filterFulltext(trim($_GET['searchterm'])); | 1890 | if (isset($_GET['searchterm'])) { |
1874 | $search_crits=escape(trim($_GET['searchterm'])); | 1891 | $search_crits = escape(trim($_GET['searchterm'])); |
1875 | $search_type='fulltext'; | 1892 | $search_type = LinkFilter::$FILTER_TEXT; |
1876 | } | 1893 | $linksToDisplay = $LINKSDB->filter($search_type, $search_crits, false, $privateonly); |
1877 | elseif (isset($_GET['searchtags'])) // Search by tag | 1894 | } |
1878 | { | 1895 | // Search by tag |
1879 | $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); | 1896 | elseif (isset($_GET['searchtags'])) { |
1880 | $search_crits=explode(' ',escape(trim($_GET['searchtags']))); | 1897 | $search_crits = explode(' ', escape(trim($_GET['searchtags']))); |
1881 | $search_type='tags'; | 1898 | $search_type = LinkFilter::$FILTER_TAG; |
1882 | } | 1899 | $linksToDisplay = $LINKSDB->filter($search_type, $search_crits, false, $privateonly); |
1883 | elseif (isset($_SERVER['QUERY_STRING']) && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/',$_SERVER['QUERY_STRING'])) // Detect smallHashes in URL | 1900 | } |
1884 | { | 1901 | // Detect smallHashes in URL. |
1885 | $linksToDisplay = $LINKSDB->filterSmallHash(substr(trim($_SERVER["QUERY_STRING"], '/'),0,6)); | 1902 | elseif (isset($_SERVER['QUERY_STRING']) |
1886 | if (count($linksToDisplay)==0) | 1903 | && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/', $_SERVER['QUERY_STRING'])) { |
1887 | { | 1904 | $search_type = LinkFilter::$FILTER_HASH; |
1888 | header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); | 1905 | $search_crits = substr(trim($_SERVER["QUERY_STRING"], '/'), 0, 6); |
1889 | echo '<h1>404 Not found.</h1>Oh crap. The link you are trying to reach does not exist or has been deleted.'; | 1906 | $linksToDisplay = $LINKSDB->filter($search_type, $search_crits); |
1907 | |||
1908 | if (count($linksToDisplay) == 0) { | ||
1909 | header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); | ||
1910 | echo '<h1>404 Not found.</h1>Oh crap. | ||
1911 | The link you are trying to reach does not exist or has been deleted.'; | ||
1890 | echo '<br>Would you mind <a href="?">clicking here</a>?'; | 1912 | echo '<br>Would you mind <a href="?">clicking here</a>?'; |
1891 | exit; | 1913 | exit; |
1892 | } | 1914 | } |
1893 | $search_type='permalink'; | ||
1894 | } | 1915 | } |
1895 | else | 1916 | // Otherwise, display without filtering. |
1896 | $linksToDisplay = $LINKSDB; // Otherwise, display without filtering. | 1917 | else { |
1897 | 1918 | $linksToDisplay = $LINKSDB->filter('', '', false, $privateonly); | |
1898 | |||
1899 | // Option: Show only private links | ||
1900 | if (!empty($_SESSION['privateonly'])) | ||
1901 | { | ||
1902 | $tmp = array(); | ||
1903 | foreach($linksToDisplay as $linkdate=>$link) | ||
1904 | { | ||
1905 | if ($link['private']!=0) $tmp[$linkdate]=$link; | ||
1906 | } | ||
1907 | $linksToDisplay=$tmp; | ||
1908 | } | 1919 | } |
1909 | 1920 | ||
1910 | // ---- Handle paging. | 1921 | // ---- Handle paging. |
1911 | /* Can someone explain to me why you get the following error when using array_keys() on an object which implements the interface ArrayAccess??? | 1922 | $keys = array(); |
1912 | "Warning: array_keys() expects parameter 1 to be array, object given in ... " | 1923 | foreach ($linksToDisplay as $key => $value) { |
1913 | If my class implements ArrayAccess, why won't array_keys() accept it ? ( $keys=array_keys($linksToDisplay); ) | 1924 | $keys[] = $key; |
1914 | */ | 1925 | } |
1915 | $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // Stupid and ugly. Thanks PHP. | ||
1916 | 1926 | ||
1917 | // If there is only a single link, we change on-the-fly the title of the page. | 1927 | // If there is only a single link, we change on-the-fly the title of the page. |
1918 | if (count($linksToDisplay)==1) $GLOBALS['pagetitle'] = $linksToDisplay[$keys[0]]['title'].' - '.$GLOBALS['title']; | 1928 | if (count($linksToDisplay) == 1) { |
1929 | $GLOBALS['pagetitle'] = $linksToDisplay[$keys[0]]['title'].' - '.$GLOBALS['title']; | ||
1930 | } | ||
1919 | 1931 | ||
1920 | // Select articles according to paging. | 1932 | // Select articles according to paging. |
1921 | $pagecount = ceil(count($keys)/$_SESSION['LINKS_PER_PAGE']); | 1933 | $pagecount = ceil(count($keys) / $_SESSION['LINKS_PER_PAGE']); |
1922 | $pagecount = ($pagecount==0 ? 1 : $pagecount); | 1934 | $pagecount = $pagecount == 0 ? 1 : $pagecount; |
1923 | $page=( empty($_GET['page']) ? 1 : intval($_GET['page'])); | 1935 | $page= empty($_GET['page']) ? 1 : intval($_GET['page']); |
1924 | $page = ( $page<1 ? 1 : $page ); | 1936 | $page = $page < 1 ? 1 : $page; |
1925 | $page = ( $page>$pagecount ? $pagecount : $page ); | 1937 | $page = $page > $pagecount ? $pagecount : $page; |
1926 | $i = ($page-1)*$_SESSION['LINKS_PER_PAGE']; // Start index. | 1938 | // Start index. |
1927 | $end = $i+$_SESSION['LINKS_PER_PAGE']; | 1939 | $i = ($page-1) * $_SESSION['LINKS_PER_PAGE']; |
1928 | $linkDisp=array(); // Links to display | 1940 | $end = $i + $_SESSION['LINKS_PER_PAGE']; |
1941 | $linkDisp = array(); | ||
1929 | while ($i<$end && $i<count($keys)) | 1942 | while ($i<$end && $i<count($keys)) |
1930 | { | 1943 | { |
1931 | $link = $linksToDisplay[$keys[$i]]; | 1944 | $link = $linksToDisplay[$keys[$i]]; |
1932 | $link['description'] = format_description($link['description'], $GLOBALS['redirector']); | 1945 | $link['description'] = format_description($link['description'], $GLOBALS['redirector']); |
1933 | $classLi = $i%2!=0 ? '' : 'publicLinkHightLight'; | 1946 | $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight'; |
1934 | $link['class'] = ($link['private']==0 ? $classLi : 'private'); | 1947 | $link['class'] = $link['private'] == 0 ? $classLi : 'private'; |
1935 | $link['timestamp']=linkdate2timestamp($link['linkdate']); | 1948 | $link['timestamp'] = linkdate2timestamp($link['linkdate']); |
1936 | $taglist = explode(' ',$link['tags']); | 1949 | $taglist = explode(' ', $link['tags']); |
1937 | uasort($taglist, 'strcasecmp'); | 1950 | uasort($taglist, 'strcasecmp'); |
1938 | $link['taglist']=$taglist; | 1951 | $link['taglist'] = $taglist; |
1939 | $link['shorturl'] = smallHash($link['linkdate']); | 1952 | $link['shorturl'] = smallHash($link['linkdate']); |
1940 | if ($link["url"][0] === '?' && // Check for both signs of a note: starting with ? and 7 chars long. I doubt that you'll post any links that look like this. | 1953 | // Check for both signs of a note: starting with ? and 7 chars long. |
1941 | strlen($link["url"]) === 7) { | 1954 | if ($link['url'][0] === '?' && |
1942 | $link["url"] = index_url($_SERVER) . $link["url"]; | 1955 | strlen($link['url']) === 7) { |
1956 | $link['url'] = index_url($_SERVER) . $link['url']; | ||
1943 | } | 1957 | } |
1944 | 1958 | ||
1945 | $linkDisp[$keys[$i]] = $link; | 1959 | $linkDisp[$keys[$i]] = $link; |
@@ -1947,13 +1961,21 @@ function buildLinkList($PAGE,$LINKSDB) | |||
1947 | } | 1961 | } |
1948 | 1962 | ||
1949 | // Compute paging navigation | 1963 | // Compute paging navigation |
1950 | $searchterm= ( empty($_GET['searchterm']) ? '' : '&searchterm='.$_GET['searchterm'] ); | 1964 | $searchterm = empty($_GET['searchterm']) ? '' : '&searchterm=' . $_GET['searchterm']; |
1951 | $searchtags= ( empty($_GET['searchtags']) ? '' : '&searchtags='.$_GET['searchtags'] ); | 1965 | $searchtags = empty($_GET['searchtags']) ? '' : '&searchtags=' . $_GET['searchtags']; |
1952 | $paging=''; | 1966 | $previous_page_url = ''; |
1953 | $previous_page_url=''; if ($i!=count($keys)) $previous_page_url='?page='.($page+1).$searchterm.$searchtags; | 1967 | if ($i != count($keys)) { |
1954 | $next_page_url='';if ($page>1) $next_page_url='?page='.($page-1).$searchterm.$searchtags; | 1968 | $previous_page_url = '?page=' . ($page+1) . $searchterm . $searchtags; |
1969 | } | ||
1970 | $next_page_url=''; | ||
1971 | if ($page>1) { | ||
1972 | $next_page_url = '?page=' . ($page-1) . $searchterm . $searchtags; | ||
1973 | } | ||
1955 | 1974 | ||
1956 | $token = ''; if (isLoggedIn()) $token=getToken(); | 1975 | $token = ''; |
1976 | if (isLoggedIn()) { | ||
1977 | $token = getToken(); | ||
1978 | } | ||
1957 | 1979 | ||
1958 | // Fill all template fields. | 1980 | // Fill all template fields. |
1959 | $data = array( | 1981 | $data = array( |