]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkFilter.php
3fd803cb81f4d79c08538a8fc3a7079d8f0b9644
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 string $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 private function filterSmallHash($smallHash)
117 foreach ($this->links
as $l) {
118 if ($smallHash == smallHash($l['linkdate'])) {
119 // Yes, this is ugly and slow
120 $filtered[$l['linkdate']] = $l;
128 * Returns the list of links corresponding to a full-text search
131 * - in the URLs, title and description;
132 * - are case-insensitive;
133 * - terms surrounded by quotes " are exact terms search.
134 * - terms starting with a dash - are excluded (except exact terms).
137 * print_r($mydb->filterFulltext('hollandais'));
139 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
140 * - allows to perform searches on Unicode text
141 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
143 * @param string $searchterms search query.
144 * @param bool $privateonly return only private links if true.
146 * @return array search results.
148 private function filterFulltext($searchterms, $privateonly = false)
150 if (empty($searchterms)) {
155 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER
, 'UTF-8');
156 $exactRegex = '/"([^"]+)"/';
157 // Retrieve exact search terms.
158 preg_match_all($exactRegex, $search, $exactSearch);
159 $exactSearch = array_values(array_filter($exactSearch[1]));
161 // Remove exact search terms to get AND terms search.
162 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
163 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
165 // Filter excluding terms and update andSearch.
166 $excludeSearch = array();
167 $andSearch = array();
168 foreach ($explodedSearchAnd as $needle) {
169 if ($needle[0] == '-' && strlen($needle) > 1) {
170 $excludeSearch[] = substr($needle, 1);
172 $andSearch[] = $needle;
176 $keys = array('title', 'description', 'url', 'tags');
178 // Iterate over every stored link.
179 foreach ($this->links
as $link) {
181 // ignore non private links when 'privatonly' is on.
182 if (! $link['private'] && $privateonly === true) {
186 // Concatenate link fields to search across fields.
187 // Adds a '\' separator for exact search terms.
189 foreach ($keys as $key) {
190 $content .= mb_convert_case($link[$key], MB_CASE_LOWER
, 'UTF-8') . '\\';
196 // First, we look for exact term search
197 for ($i = 0; $i < count($exactSearch) && $found; $i++
) {
198 $found = strpos($content, $exactSearch[$i]) !== false;
201 // Iterate over keywords, if keyword is not found,
202 // no need to check for the others. We want all or nothing.
203 for ($i = 0; $i < count($andSearch) && $found; $i++
) {
204 $found = strpos($content, $andSearch[$i]) !== false;
208 for ($i = 0; $i < count($excludeSearch) && $found; $i++
) {
209 $found = strpos($content, $excludeSearch[$i]) === false;
213 $filtered[$link['linkdate']] = $link;
222 * Returns the list of links associated with a given list of tags
224 * You can specify one or more tags, separated by space or a comma, e.g.
225 * print_r($mydb->filterTags('linux programming'));
227 * @param string $tags list of tags separated by commas or blank spaces.
228 * @param bool $casesensitive ignore case if false.
229 * @param bool $privateonly returns private links only.
231 * @return array filtered links.
233 public function filterTags($tags, $casesensitive = false, $privateonly = false)
235 // Implode if array for clean up.
236 $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags;
241 $searchtags = self
::tagsStrToArray($tags, $casesensitive);
243 if (empty($searchtags)) {
247 foreach ($this->links
as $link) {
248 // ignore non private links when 'privatonly' is on.
249 if (! $link['private'] && $privateonly === true) {
253 $linktags = self
::tagsStrToArray($link['tags'], $casesensitive);
256 for ($i = 0 ; $i < count($searchtags) && $found; $i++
) {
257 // Exclusive search, quit if tag found.
258 // Or, tag not found in the link, quit.
259 if (($searchtags[$i][0] == '-' && in_array(substr($searchtags[$i], 1), $linktags))
260 || ($searchtags[$i][0] != '-') && ! in_array($searchtags[$i], $linktags)
267 $filtered[$link['linkdate']] = $link;
275 * Returns the list of articles for a given day, chronologically sorted
277 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
278 * print_r($mydb->filterDay('20120125'));
280 * @param string $day day to filter.
282 * @return array all link matching given day.
284 * @throws Exception if date format is invalid.
286 public function filterDay($day)
288 if (! checkDateFormat('Ymd', $day)) {
289 throw new Exception('Invalid date format');
293 foreach ($this->links
as $l) {
294 if (startsWith($l['linkdate'], $day)) {
295 $filtered[$l['linkdate']] = $l;
303 * Convert a list of tags (str) to an array. Also
304 * - handle case sensitivity.
305 * - accepts spaces commas as separator.
307 * @param string $tags string containing a list of tags.
308 * @param bool $casesensitive will convert everything to lowercase if false.
310 * @return array filtered tags string.
312 public static function tagsStrToArray($tags, $casesensitive)
314 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
315 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER
, 'UTF-8');
316 $tagsOut = str_replace(',', ' ', $tagsOut);
318 return array_filter(explode(' ', trim($tagsOut)), 'strlen');