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