]>
Commit | Line | Data |
---|---|---|
ca74886f V |
1 | <?php |
2 | /** | |
3 | * Data storage for links. | |
4 | * | |
5 | * This object behaves like an associative array. | |
6 | * | |
7 | * Example: | |
8 | * $myLinks = new LinkDB(); | |
9 | * echo $myLinks['20110826_161819']['title']; | |
10 | * foreach ($myLinks as $link) | |
11 | * echo $link['title'].' at url '.$link['url'].'; description:'.$link['description']; | |
12 | * | |
13 | * Available keys: | |
14 | * - description: description of the entry | |
15 | * - linkdate: date of the creation of this entry, in the form YYYYMMDD_HHMMSS | |
16 | * (e.g.'20110914_192317') | |
17 | * - private: Is this link private? 0=no, other value=yes | |
18 | * - tags: tags attached to this entry (separated by spaces) | |
19 | * - title Title of the link | |
20 | * - url URL of the link. Can be absolute or relative. | |
21 | * Relative URLs are permalinks (e.g.'?m-ukcw') | |
22 | * | |
23 | * Implements 3 interfaces: | |
24 | * - ArrayAccess: behaves like an associative array; | |
25 | * - Countable: there is a count() method; | |
26 | * - Iterator: usable in foreach () loops. | |
27 | */ | |
28 | class LinkDB implements Iterator, Countable, ArrayAccess | |
29 | { | |
30 | // List of links (associative array) | |
31 | // - key: link date (e.g. "20110823_124546"), | |
32 | // - value: associative array (keys: title, description...) | |
33 | private $links; | |
34 | ||
35 | // List of all recorded URLs (key=url, value=linkdate) | |
36 | // for fast reserve search (url-->linkdate) | |
37 | private $urls; | |
38 | ||
39 | // List of linkdate keys (for the Iterator interface implementation) | |
40 | private $keys; | |
41 | ||
42 | // Position in the $this->keys array (for the Iterator interface) | |
43 | private $position; | |
44 | ||
45 | // Is the user logged in? (used to filter private links) | |
46 | private $loggedIn; | |
47 | ||
9f15ca9e V |
48 | // Hide public links |
49 | private $hidePublicLinks; | |
50 | ||
ca74886f V |
51 | /** |
52 | * Creates a new LinkDB | |
53 | * | |
54 | * Checks if the datastore exists; else, attempts to create a dummy one. | |
55 | * | |
56 | * @param $isLoggedIn is the user logged in? | |
57 | */ | |
9f15ca9e | 58 | function __construct($isLoggedIn, $hidePublicLinks) |
ca74886f V |
59 | { |
60 | // FIXME: do not access $GLOBALS, pass the datastore instead | |
61 | $this->loggedIn = $isLoggedIn; | |
9f15ca9e | 62 | $this->hidePublicLinks = $hidePublicLinks; |
ca74886f V |
63 | $this->checkDB(); |
64 | $this->readdb(); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Countable - Counts elements of an object | |
69 | */ | |
70 | public function count() | |
71 | { | |
72 | return count($this->links); | |
73 | } | |
74 | ||
75 | /** | |
76 | * ArrayAccess - Assigns a value to the specified offset | |
77 | */ | |
78 | public function offsetSet($offset, $value) | |
79 | { | |
80 | // TODO: use exceptions instead of "die" | |
81 | if (!$this->loggedIn) { | |
82 | die('You are not authorized to add a link.'); | |
83 | } | |
84 | if (empty($value['linkdate']) || empty($value['url'])) { | |
85 | die('Internal Error: A link should always have a linkdate and URL.'); | |
86 | } | |
87 | if (empty($offset)) { | |
88 | die('You must specify a key.'); | |
89 | } | |
90 | $this->links[$offset] = $value; | |
91 | $this->urls[$value['url']]=$offset; | |
92 | } | |
93 | ||
94 | /** | |
95 | * ArrayAccess - Whether or not an offset exists | |
96 | */ | |
97 | public function offsetExists($offset) | |
98 | { | |
99 | return array_key_exists($offset, $this->links); | |
100 | } | |
101 | ||
102 | /** | |
103 | * ArrayAccess - Unsets an offset | |
104 | */ | |
105 | public function offsetUnset($offset) | |
106 | { | |
107 | if (!$this->loggedIn) { | |
108 | // TODO: raise an exception | |
109 | die('You are not authorized to delete a link.'); | |
110 | } | |
111 | $url = $this->links[$offset]['url']; | |
112 | unset($this->urls[$url]); | |
113 | unset($this->links[$offset]); | |
114 | } | |
115 | ||
116 | /** | |
117 | * ArrayAccess - Returns the value at specified offset | |
118 | */ | |
119 | public function offsetGet($offset) | |
120 | { | |
121 | return isset($this->links[$offset]) ? $this->links[$offset] : null; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Iterator - Returns the current element | |
126 | */ | |
127 | function current() | |
128 | { | |
129 | return $this->links[$this->keys[$this->position]]; | |
130 | } | |
131 | ||
132 | /** | |
133 | * Iterator - Returns the key of the current element | |
134 | */ | |
135 | function key() | |
136 | { | |
137 | return $this->keys[$this->position]; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Iterator - Moves forward to next element | |
142 | */ | |
143 | function next() | |
144 | { | |
145 | ++$this->position; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Iterator - Rewinds the Iterator to the first element | |
150 | * | |
151 | * Entries are sorted by date (latest first) | |
152 | */ | |
153 | function rewind() | |
154 | { | |
155 | $this->keys = array_keys($this->links); | |
156 | rsort($this->keys); | |
157 | $this->position = 0; | |
158 | } | |
159 | ||
160 | /** | |
161 | * Iterator - Checks if current position is valid | |
162 | */ | |
163 | function valid() | |
164 | { | |
165 | return isset($this->keys[$this->position]); | |
166 | } | |
167 | ||
168 | /** | |
169 | * Checks if the DB directory and file exist | |
170 | * | |
171 | * If no DB file is found, creates a dummy DB. | |
172 | */ | |
173 | private function checkDB() | |
174 | { | |
175 | if (file_exists($GLOBALS['config']['DATASTORE'])) { | |
176 | return; | |
177 | } | |
178 | ||
179 | // Create a dummy database for example | |
180 | $this->links = array(); | |
181 | $link = array( | |
182 | 'title'=>'Shaarli - sebsauvage.net', | |
183 | 'url'=>'http://sebsauvage.net/wiki/doku.php?id=php:shaarli', | |
184 | 'description'=>'Welcome to Shaarli! This is a bookmark. To edit or delete me, you must first login.', | |
185 | 'private'=>0, | |
186 | 'linkdate'=>'20110914_190000', | |
187 | 'tags'=>'opensource software' | |
188 | ); | |
189 | $this->links[$link['linkdate']] = $link; | |
190 | ||
191 | $link = array( | |
192 | 'title'=>'My secret stuff... - Pastebin.com', | |
193 | 'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=', | |
194 | 'description'=>'SShhhh!! I\'m a private link only YOU can see. You can delete me too.', | |
195 | 'private'=>1, | |
196 | 'linkdate'=>'20110914_074522', | |
197 | 'tags'=>'secretstuff' | |
198 | ); | |
199 | $this->links[$link['linkdate']] = $link; | |
200 | ||
201 | // Write database to disk | |
202 | // TODO: raise an exception if the file is not write-able | |
203 | file_put_contents( | |
204 | // FIXME: do not use $GLOBALS | |
205 | $GLOBALS['config']['DATASTORE'], | |
206 | PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX | |
207 | ); | |
208 | } | |
209 | ||
210 | /** | |
211 | * Reads database from disk to memory | |
212 | */ | |
213 | private function readdb() | |
214 | { | |
578a84bd | 215 | |
216 | // Public links are hidden and user not logged in => nothing to show | |
9f15ca9e | 217 | if ($this->hidePublicLinks && !$this->loggedIn) { |
578a84bd | 218 | $this->links = array(); |
219 | return; | |
220 | } | |
221 | ||
ca74886f V |
222 | // Read data |
223 | // Note that gzinflate is faster than gzuncompress. | |
224 | // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 | |
225 | // FIXME: do not use $GLOBALS | |
226 | $this->links = array(); | |
227 | ||
228 | if (file_exists($GLOBALS['config']['DATASTORE'])) { | |
229 | $this->links = unserialize(gzinflate(base64_decode( | |
230 | substr(file_get_contents($GLOBALS['config']['DATASTORE']), | |
231 | strlen(PHPPREFIX), -strlen(PHPSUFFIX))))); | |
232 | } | |
233 | ||
234 | // If user is not logged in, filter private links. | |
235 | if (!$this->loggedIn) { | |
236 | $toremove = array(); | |
237 | foreach ($this->links as $link) { | |
238 | if ($link['private'] != 0) { | |
239 | $toremove[] = $link['linkdate']; | |
240 | } | |
241 | } | |
242 | foreach ($toremove as $linkdate) { | |
243 | unset($this->links[$linkdate]); | |
244 | } | |
245 | } | |
246 | ||
247 | // Keep the list of the mapping URLs-->linkdate up-to-date. | |
248 | $this->urls = array(); | |
249 | foreach ($this->links as $link) { | |
250 | $this->urls[$link['url']] = $link['linkdate']; | |
251 | } | |
5f85fcd8 A |
252 | |
253 | // Escape links data | |
254 | foreach($this->links as &$link) { | |
255 | sanitizeLink($link); | |
256 | } | |
ca74886f V |
257 | } |
258 | ||
259 | /** | |
260 | * Saves the database from memory to disk | |
261 | */ | |
262 | public function savedb() | |
263 | { | |
264 | if (!$this->loggedIn) { | |
265 | // TODO: raise an Exception instead | |
266 | die('You are not authorized to change the database.'); | |
267 | } | |
268 | file_put_contents( | |
269 | $GLOBALS['config']['DATASTORE'], | |
270 | PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX | |
271 | ); | |
272 | invalidateCaches(); | |
273 | } | |
274 | ||
275 | /** | |
276 | * Returns the link for a given URL, or False if it does not exist. | |
277 | */ | |
278 | public function getLinkFromUrl($url) | |
279 | { | |
280 | if (isset($this->urls[$url])) { | |
281 | return $this->links[$this->urls[$url]]; | |
282 | } | |
283 | return false; | |
284 | } | |
285 | ||
286 | /** | |
287 | * Returns the list of links corresponding to a full-text search | |
288 | * | |
289 | * Searches: | |
290 | * - in the URLs, title and description; | |
291 | * - are case-insensitive. | |
292 | * | |
293 | * Example: | |
294 | * print_r($mydb->filterFulltext('hollandais')); | |
295 | * | |
296 | * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8') | |
297 | * - allows to perform searches on Unicode text | |
298 | * - see https://github.com/shaarli/Shaarli/issues/75 for examples | |
299 | */ | |
300 | public function filterFulltext($searchterms) | |
301 | { | |
302 | // FIXME: explode(' ',$searchterms) and perform a AND search. | |
303 | // FIXME: accept double-quotes to search for a string "as is"? | |
304 | $filtered = array(); | |
305 | $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8'); | |
306 | $keys = ['title', 'description', 'url', 'tags']; | |
307 | ||
308 | foreach ($this->links as $link) { | |
309 | $found = false; | |
310 | ||
311 | foreach ($keys as $key) { | |
312 | if (strpos(mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'), | |
313 | $search) !== false) { | |
314 | $found = true; | |
315 | } | |
316 | } | |
317 | ||
318 | if ($found) { | |
319 | $filtered[$link['linkdate']] = $link; | |
320 | } | |
321 | } | |
322 | krsort($filtered); | |
323 | return $filtered; | |
324 | } | |
325 | ||
326 | /** | |
327 | * Returns the list of links associated with a given list of tags | |
328 | * | |
329 | * You can specify one or more tags, separated by space or a comma, e.g. | |
330 | * print_r($mydb->filterTags('linux programming')); | |
331 | */ | |
332 | public function filterTags($tags, $casesensitive=false) | |
333 | { | |
334 | // Same as above, we use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek) | |
335 | // FIXME: is $casesensitive ever true? | |
336 | $t = str_replace( | |
337 | ',', ' ', | |
338 | ($casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8')) | |
339 | ); | |
340 | ||
341 | $searchtags = explode(' ', $t); | |
342 | $filtered = array(); | |
343 | ||
344 | foreach ($this->links as $l) { | |
345 | $linktags = explode( | |
346 | ' ', | |
347 | ($casesensitive ? $l['tags']:mb_convert_case($l['tags'], MB_CASE_LOWER, 'UTF-8')) | |
348 | ); | |
349 | ||
350 | if (count(array_intersect($linktags, $searchtags)) == count($searchtags)) { | |
351 | $filtered[$l['linkdate']] = $l; | |
352 | } | |
353 | } | |
354 | krsort($filtered); | |
355 | return $filtered; | |
356 | } | |
357 | ||
358 | ||
359 | /** | |
360 | * Returns the list of articles for a given day, chronologically sorted | |
361 | * | |
362 | * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g. | |
363 | * print_r($mydb->filterDay('20120125')); | |
364 | */ | |
365 | public function filterDay($day) | |
366 | { | |
367 | // TODO: check input format | |
368 | $filtered = array(); | |
369 | foreach ($this->links as $l) { | |
370 | if (startsWith($l['linkdate'], $day)) { | |
371 | $filtered[$l['linkdate']] = $l; | |
372 | } | |
373 | } | |
374 | ksort($filtered); | |
375 | return $filtered; | |
376 | } | |
377 | ||
378 | /** | |
379 | * Returns the article corresponding to a smallHash | |
380 | */ | |
381 | public function filterSmallHash($smallHash) | |
382 | { | |
383 | $filtered = array(); | |
384 | foreach ($this->links as $l) { | |
385 | if ($smallHash == smallHash($l['linkdate'])) { | |
386 | // Yes, this is ugly and slow | |
387 | $filtered[$l['linkdate']] = $l; | |
388 | return $filtered; | |
389 | } | |
390 | } | |
391 | return $filtered; | |
392 | } | |
393 | ||
394 | /** | |
395 | * Returns the list of all tags | |
396 | * Output: associative array key=tags, value=0 | |
397 | */ | |
398 | public function allTags() | |
399 | { | |
400 | $tags = array(); | |
401 | foreach ($this->links as $link) { | |
402 | foreach (explode(' ', $link['tags']) as $tag) { | |
403 | if (!empty($tag)) { | |
404 | $tags[$tag] = (empty($tags[$tag]) ? 1 : $tags[$tag] + 1); | |
405 | } | |
406 | } | |
407 | } | |
408 | // Sort tags by usage (most used tag first) | |
409 | arsort($tags); | |
410 | return $tags; | |
411 | } | |
412 | ||
413 | /** | |
414 | * Returns the list of days containing articles (oldest first) | |
415 | * Output: An array containing days (in format YYYYMMDD). | |
416 | */ | |
417 | public function days() | |
418 | { | |
419 | $linkDays = array(); | |
420 | foreach (array_keys($this->links) as $day) { | |
421 | $linkDays[substr($day, 0, 8)] = 0; | |
422 | } | |
423 | $linkDays = array_keys($linkDays); | |
424 | sort($linkDays); | |
425 | return $linkDays; | |
426 | } | |
427 | } | |
428 | ?> |