]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkFilter.php
6 * Perform search and filter operation on link data list.
11 * @var string permalinks.
13 public static $FILTER_HASH = 'permalink';
16 * @var string text search.
18 public static $FILTER_TEXT = 'fulltext';
21 * @var string tag filter.
23 public static $FILTER_TAG = 'tags';
26 * @var string filter by day.
28 public static $FILTER_DAY = 'FILTER_DAY';
31 * @var array all available links.
36 * @param array $links initialization.
38 public function __construct($links)
40 $this->links
= $links;
44 * Filter links according to parameters.
46 * @param string $type Type of filter (eg. tags, permalink, etc.).
47 * @param mixed $request Filter content.
48 * @param bool $casesensitive Optional: Perform case sensitive filter if true.
49 * @param bool $privateonly Optional: Only returns private links if true.
51 * @return array filtered link list.
53 public function filter($type, $request, $casesensitive = false, $privateonly = false)
56 case self
::$FILTER_HASH:
57 return $this->filterSmallHash($request);
58 case self
::$FILTER_TAG | self
::$FILTER_TEXT:
59 if (!empty($request)) {
60 $filtered = $this->links
;
61 if (isset($request[0])) {
62 $filtered = $this->filterTags($request[0], $casesensitive, $privateonly);
64 if (isset($request[1])) {
65 $lf = new LinkFilter($filtered);
66 $filtered = $lf->filterFulltext($request[1], $privateonly);
70 return $this->noFilter($privateonly);
71 case self
::$FILTER_TEXT:
72 return $this->filterFulltext($request, $privateonly);
73 case self
::$FILTER_TAG:
74 return $this->filterTags($request, $casesensitive, $privateonly);
75 case self
::$FILTER_DAY:
76 return $this->filterDay($request);
78 return $this->noFilter($privateonly);
83 * Unknown filter, but handle private only.
85 * @param bool $privateonly returns private link only if true.
87 * @return array filtered links.
89 private function noFilter($privateonly = false)
97 foreach ($this->links
as $value) {
98 if ($value['private']) {
99 $out[$value['linkdate']] = $value;
108 * Returns the shaare corresponding to a smallHash.
110 * @param string $smallHash permalink hash.
112 * @return array $filtered array containing permalink data.
114 * @throws LinkNotFoundException if the smallhash doesn't match any link.
116 private function filterSmallHash($smallHash)
119 foreach ($this->links
as $l) {
120 if ($smallHash == smallHash($l['linkdate'])) {
121 // Yes, this is ugly and slow
122 $filtered[$l['linkdate']] = $l;
127 if (empty($filtered)) {
128 throw new LinkNotFoundException();
135 * Returns the list of links corresponding to a full-text search
138 * - in the URLs, title and description;
139 * - are case-insensitive;
140 * - terms surrounded by quotes " are exact terms search.
141 * - terms starting with a dash - are excluded (except exact terms).
144 * print_r($mydb->filterFulltext('hollandais'));
146 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
147 * - allows to perform searches on Unicode text
148 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
150 * @param string $searchterms search query.
151 * @param bool $privateonly return only private links if true.
153 * @return array search results.
155 private function filterFulltext($searchterms, $privateonly = false)
157 if (empty($searchterms)) {
162 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER
, 'UTF-8');
163 $exactRegex = '/"([^"]+)"/';
164 // Retrieve exact search terms.
165 preg_match_all($exactRegex, $search, $exactSearch);
166 $exactSearch = array_values(array_filter($exactSearch[1]));
168 // Remove exact search terms to get AND terms search.
169 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
170 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
172 // Filter excluding terms and update andSearch.
173 $excludeSearch = array();
174 $andSearch = array();
175 foreach ($explodedSearchAnd as $needle) {
176 if ($needle[0] == '-' && strlen($needle) > 1) {
177 $excludeSearch[] = substr($needle, 1);
179 $andSearch[] = $needle;
183 $keys = array('title', 'description', 'url', 'tags');
185 // Iterate over every stored link.
186 foreach ($this->links
as $link) {
188 // ignore non private links when 'privatonly' is on.
189 if (! $link['private'] && $privateonly === true) {
193 // Concatenate link fields to search across fields.
194 // Adds a '\' separator for exact search terms.
196 foreach ($keys as $key) {
197 $content .= mb_convert_case($link[$key], MB_CASE_LOWER
, 'UTF-8') . '\\';
203 // First, we look for exact term search
204 for ($i = 0; $i < count($exactSearch) && $found; $i++
) {
205 $found = strpos($content, $exactSearch[$i]) !== false;
208 // Iterate over keywords, if keyword is not found,
209 // no need to check for the others. We want all or nothing.
210 for ($i = 0; $i < count($andSearch) && $found; $i++
) {
211 $found = strpos($content, $andSearch[$i]) !== false;
215 for ($i = 0; $i < count($excludeSearch) && $found; $i++
) {
216 $found = strpos($content, $excludeSearch[$i]) === false;
220 $filtered[$link['linkdate']] = $link;
229 * Returns the list of links associated with a given list of tags
231 * You can specify one or more tags, separated by space or a comma, e.g.
232 * print_r($mydb->filterTags('linux programming'));
234 * @param string $tags list of tags separated by commas or blank spaces.
235 * @param bool $casesensitive ignore case if false.
236 * @param bool $privateonly returns private links only.
238 * @return array filtered links.
240 public function filterTags($tags, $casesensitive = false, $privateonly = false)
242 // Implode if array for clean up.
243 $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags;
248 $searchtags = self
::tagsStrToArray($tags, $casesensitive);
250 if (empty($searchtags)) {
254 foreach ($this->links
as $link) {
255 // ignore non private links when 'privatonly' is on.
256 if (! $link['private'] && $privateonly === true) {
260 $linktags = self
::tagsStrToArray($link['tags'], $casesensitive);
263 for ($i = 0 ; $i < count($searchtags) && $found; $i++
) {
264 // Exclusive search, quit if tag found.
265 // Or, tag not found in the link, quit.
266 if (($searchtags[$i][0] == '-' && in_array(substr($searchtags[$i], 1), $linktags))
267 || ($searchtags[$i][0] != '-') && ! in_array($searchtags[$i], $linktags)
274 $filtered[$link['linkdate']] = $link;
282 * Returns the list of articles for a given day, chronologically sorted
284 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
285 * print_r($mydb->filterDay('20120125'));
287 * @param string $day day to filter.
289 * @return array all link matching given day.
291 * @throws Exception if date format is invalid.
293 public function filterDay($day)
295 if (! checkDateFormat('Ymd', $day)) {
296 throw new Exception('Invalid date format');
300 foreach ($this->links
as $l) {
301 if (startsWith($l['linkdate'], $day)) {
302 $filtered[$l['linkdate']] = $l;
310 * Convert a list of tags (str) to an array. Also
311 * - handle case sensitivity.
312 * - accepts spaces commas as separator.
314 * @param string $tags string containing a list of tags.
315 * @param bool $casesensitive will convert everything to lowercase if false.
317 * @return array filtered tags string.
319 public static function tagsStrToArray($tags, $casesensitive)
321 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
322 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER
, 'UTF-8');
323 $tagsOut = str_replace(',', ' ', $tagsOut);
325 return array_values(array_filter(explode(' ', trim($tagsOut)), 'strlen'));
329 class LinkNotFoundException
extends Exception
331 protected $message = 'The link you are trying to reach does not exist or has been deleted.';