]>
Commit | Line | Data |
---|---|---|
822bffce A |
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 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. | |
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 | break; | |
59 | case self::$FILTER_TEXT: | |
60 | return $this->filterFulltext($request, $privateonly); | |
61 | break; | |
62 | case self::$FILTER_TAG: | |
63 | return $this->filterTags($request, $casesensitive, $privateonly); | |
64 | break; | |
65 | case self::$FILTER_DAY: | |
66 | return $this->filterDay($request); | |
67 | break; | |
68 | default: | |
69 | return $this->noFilter($privateonly); | |
70 | } | |
71 | } | |
72 | ||
73 | /** | |
74 | * Unknown filter, but handle private only. | |
75 | * | |
76 | * @param bool $privateonly returns private link only if true. | |
77 | * | |
78 | * @return array filtered links. | |
79 | */ | |
80 | private function noFilter($privateonly = false) | |
81 | { | |
82 | if (! $privateonly) { | |
83 | krsort($this->links); | |
84 | return $this->links; | |
85 | } | |
86 | ||
87 | $out = array(); | |
88 | foreach ($this->links as $value) { | |
89 | if ($value['private']) { | |
90 | $out[$value['linkdate']] = $value; | |
91 | } | |
92 | } | |
93 | ||
94 | krsort($out); | |
95 | return $out; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Returns the shaare corresponding to a smallHash. | |
100 | * | |
101 | * @param string $smallHash permalink hash. | |
102 | * | |
103 | * @return array $filtered array containing permalink data. | |
104 | */ | |
105 | private function filterSmallHash($smallHash) | |
106 | { | |
107 | $filtered = array(); | |
108 | foreach ($this->links as $l) { | |
109 | if ($smallHash == smallHash($l['linkdate'])) { | |
110 | // Yes, this is ugly and slow | |
111 | $filtered[$l['linkdate']] = $l; | |
112 | return $filtered; | |
113 | } | |
114 | } | |
115 | return $filtered; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Returns the list of links corresponding to a full-text search | |
120 | * | |
121 | * Searches: | |
122 | * - in the URLs, title and description; | |
bedd176a A |
123 | * - are case-insensitive; |
124 | * - terms surrounded by quotes " are exact terms search. | |
125 | * - terms starting with a dash - are excluded (except exact terms). | |
822bffce A |
126 | * |
127 | * Example: | |
128 | * print_r($mydb->filterFulltext('hollandais')); | |
129 | * | |
130 | * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8') | |
131 | * - allows to perform searches on Unicode text | |
132 | * - see https://github.com/shaarli/Shaarli/issues/75 for examples | |
133 | * | |
134 | * @param string $searchterms search query. | |
135 | * @param bool $privateonly return only private links if true. | |
136 | * | |
137 | * @return array search results. | |
138 | */ | |
139 | private function filterFulltext($searchterms, $privateonly = false) | |
140 | { | |
522b278b | 141 | $filtered = array(); |
ebd8075a | 142 | $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER, 'UTF-8'); |
bedd176a A |
143 | $exactRegex = '/"([^"]+)"/'; |
144 | // Retrieve exact search terms. | |
145 | preg_match_all($exactRegex, $search, $exactSearch); | |
146 | $exactSearch = array_values(array_filter($exactSearch[1])); | |
147 | ||
148 | // Remove exact search terms to get AND terms search. | |
149 | $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search))); | |
150 | $explodedSearchAnd = array_values(array_filter($explodedSearchAnd)); | |
151 | ||
152 | // Filter excluding terms and update andSearch. | |
153 | $excludeSearch = array(); | |
154 | $andSearch = array(); | |
155 | foreach ($explodedSearchAnd as $needle) { | |
156 | if ($needle[0] == '-' && strlen($needle) > 1) { | |
157 | $excludeSearch[] = substr($needle, 1); | |
158 | } else { | |
159 | $andSearch[] = $needle; | |
160 | } | |
161 | } | |
162 | ||
822bffce | 163 | $keys = array('title', 'description', 'url', 'tags'); |
ebd8075a | 164 | |
822bffce A |
165 | // Iterate over every stored link. |
166 | foreach ($this->links as $link) { | |
822bffce A |
167 | |
168 | // ignore non private links when 'privatonly' is on. | |
169 | if (! $link['private'] && $privateonly === true) { | |
170 | continue; | |
171 | } | |
172 | ||
522b278b A |
173 | // Concatenate link fields to search across fields. |
174 | // Adds a '\' separator for exact search terms. | |
175 | $content = ''; | |
822bffce | 176 | foreach ($keys as $key) { |
522b278b | 177 | $content .= mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8') . '\\'; |
822bffce | 178 | } |
522b278b A |
179 | |
180 | // Be optimistic | |
181 | $found = true; | |
182 | ||
183 | // First, we look for exact term search | |
184 | for ($i = 0; $i < count($exactSearch) && $found; $i++) { | |
185 | $found = strpos($content, $exactSearch[$i]) !== false; | |
186 | } | |
187 | ||
188 | // Iterate over keywords, if keyword is not found, | |
189 | // no need to check for the others. We want all or nothing. | |
190 | for ($i = 0; $i < count($andSearch) && $found; $i++) { | |
191 | $found = strpos($content, $andSearch[$i]) !== false; | |
192 | } | |
193 | ||
194 | // Exclude terms. | |
195 | for ($i = 0; $i < count($excludeSearch) && $found; $i++) { | |
196 | $found = strpos($content, $excludeSearch[$i]) === false; | |
197 | } | |
198 | ||
822bffce A |
199 | if ($found) { |
200 | $filtered[$link['linkdate']] = $link; | |
201 | } | |
202 | } | |
203 | ||
204 | krsort($filtered); | |
205 | return $filtered; | |
206 | } | |
207 | ||
208 | /** | |
209 | * Returns the list of links associated with a given list of tags | |
210 | * | |
211 | * You can specify one or more tags, separated by space or a comma, e.g. | |
212 | * print_r($mydb->filterTags('linux programming')); | |
213 | * | |
214 | * @param string $tags list of tags separated by commas or blank spaces. | |
215 | * @param bool $casesensitive ignore case if false. | |
216 | * @param bool $privateonly returns private links only. | |
217 | * | |
218 | * @return array filtered links. | |
219 | */ | |
220 | public function filterTags($tags, $casesensitive = false, $privateonly = false) | |
221 | { | |
21979ff1 | 222 | $searchtags = self::tagsStrToArray($tags, $casesensitive); |
822bffce | 223 | $filtered = array(); |
21979ff1 A |
224 | if (empty($searchtags)) { |
225 | return $filtered; | |
226 | } | |
822bffce | 227 | |
21979ff1 | 228 | foreach ($this->links as $link) { |
822bffce | 229 | // ignore non private links when 'privatonly' is on. |
21979ff1 | 230 | if (! $link['private'] && $privateonly === true) { |
822bffce A |
231 | continue; |
232 | } | |
233 | ||
21979ff1 | 234 | $linktags = self::tagsStrToArray($link['tags'], $casesensitive); |
822bffce | 235 | |
21979ff1 A |
236 | $found = true; |
237 | for ($i = 0 ; $i < count($searchtags) && $found; $i++) { | |
238 | // Exclusive search, quit if tag found. | |
239 | // Or, tag not found in the link, quit. | |
240 | if (($searchtags[$i][0] == '-' && in_array(substr($searchtags[$i], 1), $linktags)) | |
241 | || ($searchtags[$i][0] != '-') && ! in_array($searchtags[$i], $linktags) | |
242 | ) { | |
243 | $found = false; | |
244 | } | |
245 | } | |
246 | ||
247 | if ($found) { | |
248 | $filtered[$link['linkdate']] = $link; | |
822bffce A |
249 | } |
250 | } | |
251 | krsort($filtered); | |
252 | return $filtered; | |
253 | } | |
254 | ||
255 | /** | |
256 | * Returns the list of articles for a given day, chronologically sorted | |
257 | * | |
258 | * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g. | |
259 | * print_r($mydb->filterDay('20120125')); | |
260 | * | |
261 | * @param string $day day to filter. | |
262 | * | |
263 | * @return array all link matching given day. | |
264 | * | |
265 | * @throws Exception if date format is invalid. | |
266 | */ | |
267 | public function filterDay($day) | |
268 | { | |
269 | if (! checkDateFormat('Ymd', $day)) { | |
270 | throw new Exception('Invalid date format'); | |
271 | } | |
272 | ||
273 | $filtered = array(); | |
274 | foreach ($this->links as $l) { | |
275 | if (startsWith($l['linkdate'], $day)) { | |
276 | $filtered[$l['linkdate']] = $l; | |
277 | } | |
278 | } | |
279 | ksort($filtered); | |
280 | return $filtered; | |
281 | } | |
282 | ||
283 | /** | |
284 | * Convert a list of tags (str) to an array. Also | |
285 | * - handle case sensitivity. | |
286 | * - accepts spaces commas as separator. | |
822bffce A |
287 | * |
288 | * @param string $tags string containing a list of tags. | |
289 | * @param bool $casesensitive will convert everything to lowercase if false. | |
290 | * | |
291 | * @return array filtered tags string. | |
292 | */ | |
21979ff1 | 293 | public static function tagsStrToArray($tags, $casesensitive) |
822bffce A |
294 | { |
295 | // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek) | |
296 | $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8'); | |
297 | $tagsOut = str_replace(',', ' ', $tagsOut); | |
298 | ||
21979ff1 | 299 | return array_filter(explode(' ', trim($tagsOut)), 'strlen'); |
822bffce A |
300 | } |
301 | } |