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