]>
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 | } | |
252 | } | |
253 | ||
254 | /** | |
255 | * Saves the database from memory to disk | |
256 | */ | |
257 | public function savedb() | |
258 | { | |
259 | if (!$this->loggedIn) { | |
260 | // TODO: raise an Exception instead | |
261 | die('You are not authorized to change the database.'); | |
262 | } | |
263 | file_put_contents( | |
264 | $GLOBALS['config']['DATASTORE'], | |
265 | PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX | |
266 | ); | |
267 | invalidateCaches(); | |
268 | } | |
269 | ||
270 | /** | |
271 | * Returns the link for a given URL, or False if it does not exist. | |
272 | */ | |
273 | public function getLinkFromUrl($url) | |
274 | { | |
275 | if (isset($this->urls[$url])) { | |
276 | return $this->links[$this->urls[$url]]; | |
277 | } | |
278 | return false; | |
279 | } | |
280 | ||
281 | /** | |
282 | * Returns the list of links corresponding to a full-text search | |
283 | * | |
284 | * Searches: | |
285 | * - in the URLs, title and description; | |
286 | * - are case-insensitive. | |
287 | * | |
288 | * Example: | |
289 | * print_r($mydb->filterFulltext('hollandais')); | |
290 | * | |
291 | * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8') | |
292 | * - allows to perform searches on Unicode text | |
293 | * - see https://github.com/shaarli/Shaarli/issues/75 for examples | |
294 | */ | |
295 | public function filterFulltext($searchterms) | |
296 | { | |
297 | // FIXME: explode(' ',$searchterms) and perform a AND search. | |
298 | // FIXME: accept double-quotes to search for a string "as is"? | |
299 | $filtered = array(); | |
300 | $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8'); | |
301 | $keys = ['title', 'description', 'url', 'tags']; | |
302 | ||
303 | foreach ($this->links as $link) { | |
304 | $found = false; | |
305 | ||
306 | foreach ($keys as $key) { | |
307 | if (strpos(mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'), | |
308 | $search) !== false) { | |
309 | $found = true; | |
310 | } | |
311 | } | |
312 | ||
313 | if ($found) { | |
314 | $filtered[$link['linkdate']] = $link; | |
315 | } | |
316 | } | |
317 | krsort($filtered); | |
318 | return $filtered; | |
319 | } | |
320 | ||
321 | /** | |
322 | * Returns the list of links associated with a given list of tags | |
323 | * | |
324 | * You can specify one or more tags, separated by space or a comma, e.g. | |
325 | * print_r($mydb->filterTags('linux programming')); | |
326 | */ | |
327 | public function filterTags($tags, $casesensitive=false) | |
328 | { | |
329 | // Same as above, we use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek) | |
330 | // FIXME: is $casesensitive ever true? | |
331 | $t = str_replace( | |
332 | ',', ' ', | |
333 | ($casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8')) | |
334 | ); | |
335 | ||
336 | $searchtags = explode(' ', $t); | |
337 | $filtered = array(); | |
338 | ||
339 | foreach ($this->links as $l) { | |
340 | $linktags = explode( | |
341 | ' ', | |
342 | ($casesensitive ? $l['tags']:mb_convert_case($l['tags'], MB_CASE_LOWER, 'UTF-8')) | |
343 | ); | |
344 | ||
345 | if (count(array_intersect($linktags, $searchtags)) == count($searchtags)) { | |
346 | $filtered[$l['linkdate']] = $l; | |
347 | } | |
348 | } | |
349 | krsort($filtered); | |
350 | return $filtered; | |
351 | } | |
352 | ||
353 | ||
354 | /** | |
355 | * Returns the list of articles for a given day, chronologically sorted | |
356 | * | |
357 | * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g. | |
358 | * print_r($mydb->filterDay('20120125')); | |
359 | */ | |
360 | public function filterDay($day) | |
361 | { | |
362 | // TODO: check input format | |
363 | $filtered = array(); | |
364 | foreach ($this->links as $l) { | |
365 | if (startsWith($l['linkdate'], $day)) { | |
366 | $filtered[$l['linkdate']] = $l; | |
367 | } | |
368 | } | |
369 | ksort($filtered); | |
370 | return $filtered; | |
371 | } | |
372 | ||
373 | /** | |
374 | * Returns the article corresponding to a smallHash | |
375 | */ | |
376 | public function filterSmallHash($smallHash) | |
377 | { | |
378 | $filtered = array(); | |
379 | foreach ($this->links as $l) { | |
380 | if ($smallHash == smallHash($l['linkdate'])) { | |
381 | // Yes, this is ugly and slow | |
382 | $filtered[$l['linkdate']] = $l; | |
383 | return $filtered; | |
384 | } | |
385 | } | |
386 | return $filtered; | |
387 | } | |
388 | ||
389 | /** | |
390 | * Returns the list of all tags | |
391 | * Output: associative array key=tags, value=0 | |
392 | */ | |
393 | public function allTags() | |
394 | { | |
395 | $tags = array(); | |
396 | foreach ($this->links as $link) { | |
397 | foreach (explode(' ', $link['tags']) as $tag) { | |
398 | if (!empty($tag)) { | |
399 | $tags[$tag] = (empty($tags[$tag]) ? 1 : $tags[$tag] + 1); | |
400 | } | |
401 | } | |
402 | } | |
403 | // Sort tags by usage (most used tag first) | |
404 | arsort($tags); | |
405 | return $tags; | |
406 | } | |
407 | ||
408 | /** | |
409 | * Returns the list of days containing articles (oldest first) | |
410 | * Output: An array containing days (in format YYYYMMDD). | |
411 | */ | |
412 | public function days() | |
413 | { | |
414 | $linkDays = array(); | |
415 | foreach (array_keys($this->links) as $day) { | |
416 | $linkDays[substr($day, 0, 8)] = 0; | |
417 | } | |
418 | $linkDays = array_keys($linkDays); | |
419 | sort($linkDays); | |
420 | return $linkDays; | |
421 | } | |
422 | } | |
423 | ?> |