]>
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; | |
123 | * - are case-insensitive. | |
124 | * | |
125 | * Example: | |
126 | * print_r($mydb->filterFulltext('hollandais')); | |
127 | * | |
128 | * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8') | |
129 | * - allows to perform searches on Unicode text | |
130 | * - see https://github.com/shaarli/Shaarli/issues/75 for examples | |
131 | * | |
132 | * @param string $searchterms search query. | |
133 | * @param bool $privateonly return only private links if true. | |
134 | * | |
135 | * @return array search results. | |
136 | */ | |
137 | private function filterFulltext($searchterms, $privateonly = false) | |
138 | { | |
ebd8075a | 139 | $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER, 'UTF-8'); |
822bffce A |
140 | $explodedSearch = explode(' ', trim($search)); |
141 | $keys = array('title', 'description', 'url', 'tags'); | |
ebd8075a FV |
142 | $found = true; |
143 | $searchExactPhrase = false; | |
144 | ||
145 | // Check if we're using double-quotes to search for the exact string | |
146 | if ($search[0] == '"' && $search[strlen($search) - 1] == '"') { | |
147 | $searchExactPhrase = true; | |
148 | ||
149 | // Remove the double-quotes as they are not what we search for | |
150 | $search = substr($search, 1, -1); | |
151 | } | |
822bffce A |
152 | // Iterate over every stored link. |
153 | foreach ($this->links as $link) { | |
822bffce A |
154 | |
155 | // ignore non private links when 'privatonly' is on. | |
156 | if (! $link['private'] && $privateonly === true) { | |
157 | continue; | |
158 | } | |
159 | ||
160 | // Iterate over searchable link fields. | |
161 | foreach ($keys as $key) { | |
ebd8075a FV |
162 | // Be optimistic |
163 | $found = true; | |
164 | ||
165 | // FIXME: Find a better word for where you're searching in | |
166 | $haystack = mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'); | |
167 | ||
168 | // When searching for the phrase, check if it's in the haystack... | |
169 | if ( $searchExactPhrase && strpos($haystack, $search) !== false) { | |
170 | break; | |
822bffce | 171 | } |
ebd8075a FV |
172 | else { |
173 | // Iterate over keywords, if keyword is not found, | |
174 | // no need to check for the others. We want all or nothing. | |
175 | foreach($explodedSearch as $keyword) { | |
176 | if(strpos($haystack, $keyword) === false) { | |
177 | $found = false; | |
178 | break; | |
179 | } | |
180 | } | |
181 | } | |
182 | ||
183 | // One of the fields of the link matches, no need to check the other. | |
822bffce A |
184 | if ($found) { |
185 | break; | |
186 | } | |
187 | } | |
ebd8075a | 188 | |
822bffce A |
189 | if ($found) { |
190 | $filtered[$link['linkdate']] = $link; | |
191 | } | |
192 | } | |
193 | ||
194 | krsort($filtered); | |
195 | return $filtered; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Returns the list of links associated with a given list of tags | |
200 | * | |
201 | * You can specify one or more tags, separated by space or a comma, e.g. | |
202 | * print_r($mydb->filterTags('linux programming')); | |
203 | * | |
204 | * @param string $tags list of tags separated by commas or blank spaces. | |
205 | * @param bool $casesensitive ignore case if false. | |
206 | * @param bool $privateonly returns private links only. | |
207 | * | |
208 | * @return array filtered links. | |
209 | */ | |
210 | public function filterTags($tags, $casesensitive = false, $privateonly = false) | |
211 | { | |
212 | $searchtags = $this->tagsStrToArray($tags, $casesensitive); | |
213 | $filtered = array(); | |
214 | ||
215 | foreach ($this->links as $l) { | |
216 | // ignore non private links when 'privatonly' is on. | |
217 | if (! $l['private'] && $privateonly === true) { | |
218 | continue; | |
219 | } | |
220 | ||
221 | $linktags = $this->tagsStrToArray($l['tags'], $casesensitive); | |
222 | ||
223 | if (count(array_intersect($linktags, $searchtags)) == count($searchtags)) { | |
224 | $filtered[$l['linkdate']] = $l; | |
225 | } | |
226 | } | |
227 | krsort($filtered); | |
228 | return $filtered; | |
229 | } | |
230 | ||
231 | /** | |
232 | * Returns the list of articles for a given day, chronologically sorted | |
233 | * | |
234 | * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g. | |
235 | * print_r($mydb->filterDay('20120125')); | |
236 | * | |
237 | * @param string $day day to filter. | |
238 | * | |
239 | * @return array all link matching given day. | |
240 | * | |
241 | * @throws Exception if date format is invalid. | |
242 | */ | |
243 | public function filterDay($day) | |
244 | { | |
245 | if (! checkDateFormat('Ymd', $day)) { | |
246 | throw new Exception('Invalid date format'); | |
247 | } | |
248 | ||
249 | $filtered = array(); | |
250 | foreach ($this->links as $l) { | |
251 | if (startsWith($l['linkdate'], $day)) { | |
252 | $filtered[$l['linkdate']] = $l; | |
253 | } | |
254 | } | |
255 | ksort($filtered); | |
256 | return $filtered; | |
257 | } | |
258 | ||
259 | /** | |
260 | * Convert a list of tags (str) to an array. Also | |
261 | * - handle case sensitivity. | |
262 | * - accepts spaces commas as separator. | |
263 | * - remove private tags for loggedout users. | |
264 | * | |
265 | * @param string $tags string containing a list of tags. | |
266 | * @param bool $casesensitive will convert everything to lowercase if false. | |
267 | * | |
268 | * @return array filtered tags string. | |
269 | */ | |
270 | public function tagsStrToArray($tags, $casesensitive) | |
271 | { | |
272 | // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek) | |
273 | $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8'); | |
274 | $tagsOut = str_replace(',', ' ', $tagsOut); | |
275 | ||
276 | return explode(' ', trim($tagsOut)); | |
277 | } | |
278 | } |