]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkFilter.php
Bump version to v0.7.1
[github/shaarli/Shaarli.git] / application / LinkFilter.php
1 <?php
2
3 /**
4 * Class LinkFilter.
5 *
6 * Perform search and filter operation on link data list.
7 */
8 class LinkFilter
9 {
10 /**
11 * @var string permalinks.
12 */
13 public static $FILTER_HASH = 'permalink';
14
15 /**
16 * @var string text search.
17 */
18 public static $FILTER_TEXT = 'fulltext';
19
20 /**
21 * @var string tag filter.
22 */
23 public static $FILTER_TAG = 'tags';
24
25 /**
26 * @var string filter by day.
27 */
28 public static $FILTER_DAY = 'FILTER_DAY';
29
30 /**
31 * @var array all available links.
32 */
33 private $links;
34
35 /**
36 * @param array $links initialization.
37 */
38 public function __construct($links)
39 {
40 $this->links = $links;
41 }
42
43 /**
44 * Filter links according to parameters.
45 *
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.
50 *
51 * @return array filtered link list.
52 */
53 public function filter($type, $request, $casesensitive = false, $privateonly = false)
54 {
55 switch($type) {
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);
63 }
64 if (isset($request[1])) {
65 $lf = new LinkFilter($filtered);
66 $filtered = $lf->filterFulltext($request[1], $privateonly);
67 }
68 return $filtered;
69 }
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);
77 default:
78 return $this->noFilter($privateonly);
79 }
80 }
81
82 /**
83 * Unknown filter, but handle private only.
84 *
85 * @param bool $privateonly returns private link only if true.
86 *
87 * @return array filtered links.
88 */
89 private function noFilter($privateonly = false)
90 {
91 if (! $privateonly) {
92 krsort($this->links);
93 return $this->links;
94 }
95
96 $out = array();
97 foreach ($this->links as $value) {
98 if ($value['private']) {
99 $out[$value['linkdate']] = $value;
100 }
101 }
102
103 krsort($out);
104 return $out;
105 }
106
107 /**
108 * Returns the shaare corresponding to a smallHash.
109 *
110 * @param string $smallHash permalink hash.
111 *
112 * @return array $filtered array containing permalink data.
113 *
114 * @throws LinkNotFoundException if the smallhash doesn't match any link.
115 */
116 private function filterSmallHash($smallHash)
117 {
118 $filtered = array();
119 foreach ($this->links as $l) {
120 if ($smallHash == smallHash($l['linkdate'])) {
121 // Yes, this is ugly and slow
122 $filtered[$l['linkdate']] = $l;
123 return $filtered;
124 }
125 }
126
127 if (empty($filtered)) {
128 throw new LinkNotFoundException();
129 }
130
131 return $filtered;
132 }
133
134 /**
135 * Returns the list of links corresponding to a full-text search
136 *
137 * Searches:
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).
142 *
143 * Example:
144 * print_r($mydb->filterFulltext('hollandais'));
145 *
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
149 *
150 * @param string $searchterms search query.
151 * @param bool $privateonly return only private links if true.
152 *
153 * @return array search results.
154 */
155 private function filterFulltext($searchterms, $privateonly = false)
156 {
157 if (empty($searchterms)) {
158 return $this->links;
159 }
160
161 $filtered = array();
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]));
167
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));
171
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);
178 } else {
179 $andSearch[] = $needle;
180 }
181 }
182
183 $keys = array('title', 'description', 'url', 'tags');
184
185 // Iterate over every stored link.
186 foreach ($this->links as $link) {
187
188 // ignore non private links when 'privatonly' is on.
189 if (! $link['private'] && $privateonly === true) {
190 continue;
191 }
192
193 // Concatenate link fields to search across fields.
194 // Adds a '\' separator for exact search terms.
195 $content = '';
196 foreach ($keys as $key) {
197 $content .= mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8') . '\\';
198 }
199
200 // Be optimistic
201 $found = true;
202
203 // First, we look for exact term search
204 for ($i = 0; $i < count($exactSearch) && $found; $i++) {
205 $found = strpos($content, $exactSearch[$i]) !== false;
206 }
207
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;
212 }
213
214 // Exclude terms.
215 for ($i = 0; $i < count($excludeSearch) && $found; $i++) {
216 $found = strpos($content, $excludeSearch[$i]) === false;
217 }
218
219 if ($found) {
220 $filtered[$link['linkdate']] = $link;
221 }
222 }
223
224 krsort($filtered);
225 return $filtered;
226 }
227
228 /**
229 * Returns the list of links associated with a given list of tags
230 *
231 * You can specify one or more tags, separated by space or a comma, e.g.
232 * print_r($mydb->filterTags('linux programming'));
233 *
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.
237 *
238 * @return array filtered links.
239 */
240 public function filterTags($tags, $casesensitive = false, $privateonly = false)
241 {
242 // Implode if array for clean up.
243 $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags;
244 if (empty($tags)) {
245 return $this->links;
246 }
247
248 $searchtags = self::tagsStrToArray($tags, $casesensitive);
249 $filtered = array();
250 if (empty($searchtags)) {
251 return $filtered;
252 }
253
254 foreach ($this->links as $link) {
255 // ignore non private links when 'privatonly' is on.
256 if (! $link['private'] && $privateonly === true) {
257 continue;
258 }
259
260 $linktags = self::tagsStrToArray($link['tags'], $casesensitive);
261
262 $found = true;
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)
268 ) {
269 $found = false;
270 }
271 }
272
273 if ($found) {
274 $filtered[$link['linkdate']] = $link;
275 }
276 }
277 krsort($filtered);
278 return $filtered;
279 }
280
281 /**
282 * Returns the list of articles for a given day, chronologically sorted
283 *
284 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
285 * print_r($mydb->filterDay('20120125'));
286 *
287 * @param string $day day to filter.
288 *
289 * @return array all link matching given day.
290 *
291 * @throws Exception if date format is invalid.
292 */
293 public function filterDay($day)
294 {
295 if (! checkDateFormat('Ymd', $day)) {
296 throw new Exception('Invalid date format');
297 }
298
299 $filtered = array();
300 foreach ($this->links as $l) {
301 if (startsWith($l['linkdate'], $day)) {
302 $filtered[$l['linkdate']] = $l;
303 }
304 }
305 ksort($filtered);
306 return $filtered;
307 }
308
309 /**
310 * Convert a list of tags (str) to an array. Also
311 * - handle case sensitivity.
312 * - accepts spaces commas as separator.
313 *
314 * @param string $tags string containing a list of tags.
315 * @param bool $casesensitive will convert everything to lowercase if false.
316 *
317 * @return array filtered tags string.
318 */
319 public static function tagsStrToArray($tags, $casesensitive)
320 {
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);
324
325 return array_values(array_filter(explode(' ', trim($tagsOut)), 'strlen'));
326 }
327 }
328
329 class LinkNotFoundException extends Exception
330 {
331 protected $message = 'The link you are trying to reach does not exist or has been deleted.';
332 }