]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/bookmark/LinkDB.php
namespacing: \Shaarli\Bookmark\LinkDB
[github/shaarli/Shaarli.git] / application / bookmark / LinkDB.php
CommitLineData
ca74886f 1<?php
f3d2f257 2
f24896b2
V
3namespace Shaarli\Bookmark;
4
5use ArrayAccess;
6use Countable;
7use DateTime;
8use Iterator;
9use LinkFilter;
10use LinkNotFoundException;
f3d2f257 11use Shaarli\Exceptions\IOException;
a0c4dbd9 12use Shaarli\FileUtils;
f3d2f257 13
ca74886f
V
14/**
15 * Data storage for links.
16 *
17 * This object behaves like an associative array.
18 *
19 * Example:
20 * $myLinks = new LinkDB();
29d10882 21 * echo $myLinks[350]['title'];
ca74886f
V
22 * foreach ($myLinks as $link)
23 * echo $link['title'].' at url '.$link['url'].'; description:'.$link['description'];
24 *
25 * Available keys:
29d10882 26 * - id: primary key, incremental integer identifier (persistent)
ca74886f 27 * - description: description of the entry
29d10882
A
28 * - created: creation date of this entry, DateTime object.
29 * - updated: last modification date of this entry, DateTime object.
ca74886f
V
30 * - private: Is this link private? 0=no, other value=yes
31 * - tags: tags attached to this entry (separated by spaces)
32 * - title Title of the link
49e62f22
A
33 * - url URL of the link. Used for displayable links (no redirector, relative, etc.).
34 * Can be absolute or relative.
ca74886f 35 * Relative URLs are permalinks (e.g.'?m-ukcw')
49e62f22 36 * - real_url Absolute processed URL.
d592daea 37 * - shorturl Permalink smallhash
ca74886f
V
38 *
39 * Implements 3 interfaces:
40 * - ArrayAccess: behaves like an associative array;
41 * - Countable: there is a count() method;
42 * - Iterator: usable in foreach () loops.
29d10882
A
43 *
44 * ID mechanism:
45 * ArrayAccess is implemented in a way that will allow to access a link
46 * with the unique identifier ID directly with $link[ID].
47 * Note that it's not the real key of the link array attribute.
48 * This mechanism is in place to have persistent link IDs,
49 * even though the internal array is reordered by date.
50 * Example:
51 * - DB: link #1 (2010-01-01) link #2 (2016-01-01)
52 * - Order: #2 #1
53 * - Import links containing: link #3 (2013-01-01)
54 * - New DB: link #1 (2010-01-01) link #2 (2016-01-01) link #3 (2013-01-01)
55 * - Real order: #2 #3 #1
ca74886f
V
56 */
57class LinkDB implements Iterator, Countable, ArrayAccess
58{
9c8752a2 59 // Links are stored as a PHP serialized string
628b97cb 60 private $datastore;
9c8752a2 61
205a4277
V
62 // Link date storage format
63 const LINK_DATE_FORMAT = 'Ymd_His';
64
ca74886f
V
65 // List of links (associative array)
66 // - key: link date (e.g. "20110823_124546"),
67 // - value: associative array (keys: title, description...)
628b97cb 68 private $links;
ca74886f 69
29d10882
A
70 // List of all recorded URLs (key=url, value=link offset)
71 // for fast reserve search (url-->link offset)
628b97cb 72 private $urls;
ca74886f 73
29d10882
A
74 /**
75 * @var array List of all links IDS mapped with their array offset.
76 * Map: id->offset.
77 */
78 protected $ids;
79
80 // List of offset keys (for the Iterator interface implementation)
628b97cb 81 private $keys;
ca74886f 82
628b97cb
V
83 // Position in the $this->keys array (for the Iterator interface)
84 private $position;
ca74886f
V
85
86 // Is the user logged in? (used to filter private links)
628b97cb 87 private $loggedIn;
ca74886f 88
9f15ca9e 89 // Hide public links
628b97cb 90 private $hidePublicLinks;
9f15ca9e 91
90e5bd65 92 // link redirector set in user settings.
628b97cb 93 private $redirector;
90e5bd65 94
043eae70
A
95 /**
96 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
97 *
98 * Example:
99 * anonym.to needs clean URL while dereferer.org needs urlencoded URL.
100 *
101 * @var boolean $redirectorEncode parameter: true or false
102 */
103 private $redirectorEncode;
104
ca74886f
V
105 /**
106 * Creates a new LinkDB
107 *
108 * Checks if the datastore exists; else, attempts to create a dummy one.
109 *
f24896b2
V
110 * @param string $datastore datastore file path.
111 * @param boolean $isLoggedIn is the user logged in?
112 * @param boolean $hidePublicLinks if true all links are private.
113 * @param string $redirector link redirector set in user settings.
043eae70 114 * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
ca74886f 115 */
735ed4a9 116 public function __construct(
043eae70
A
117 $datastore,
118 $isLoggedIn,
119 $hidePublicLinks,
120 $redirector = '',
121 $redirectorEncode = true
f211e417 122 ) {
f24896b2 123
628b97cb
V
124 $this->datastore = $datastore;
125 $this->loggedIn = $isLoggedIn;
126 $this->hidePublicLinks = $hidePublicLinks;
127 $this->redirector = $redirector;
043eae70 128 $this->redirectorEncode = $redirectorEncode === true;
f21abf32
V
129 $this->check();
130 $this->read();
ca74886f
V
131 }
132
133 /**
134 * Countable - Counts elements of an object
135 */
136 public function count()
137 {
628b97cb 138 return count($this->links);
ca74886f
V
139 }
140
141 /**
142 * ArrayAccess - Assigns a value to the specified offset
143 */
144 public function offsetSet($offset, $value)
145 {
146 // TODO: use exceptions instead of "die"
628b97cb 147 if (!$this->loggedIn) {
12266213 148 die(t('You are not authorized to add a link.'));
ca74886f 149 }
29d10882 150 if (!isset($value['id']) || empty($value['url'])) {
12266213 151 die(t('Internal Error: A link should always have an id and URL.'));
ca74886f 152 }
f24896b2 153 if (($offset !== null && !is_int($offset)) || !is_int($value['id'])) {
12266213 154 die(t('You must specify an integer as a key.'));
29d10882 155 }
bc5f1597 156 if ($offset !== null && $offset !== $value['id']) {
12266213 157 die(t('Array offset and link ID must be equal.'));
29d10882
A
158 }
159
160 // If the link exists, we reuse the real offset, otherwise new entry
161 $existing = $this->getLinkOffset($offset);
162 if ($existing !== null) {
163 $offset = $existing;
164 } else {
165 $offset = count($this->links);
ca74886f 166 }
628b97cb 167 $this->links[$offset] = $value;
29d10882
A
168 $this->urls[$value['url']] = $offset;
169 $this->ids[$value['id']] = $offset;
ca74886f
V
170 }
171
172 /**
173 * ArrayAccess - Whether or not an offset exists
174 */
175 public function offsetExists($offset)
176 {
29d10882 177 return array_key_exists($this->getLinkOffset($offset), $this->links);
ca74886f
V
178 }
179
180 /**
181 * ArrayAccess - Unsets an offset
182 */
183 public function offsetUnset($offset)
184 {
628b97cb 185 if (!$this->loggedIn) {
ca74886f
V
186 // TODO: raise an exception
187 die('You are not authorized to delete a link.');
188 }
29d10882
A
189 $realOffset = $this->getLinkOffset($offset);
190 $url = $this->links[$realOffset]['url'];
628b97cb 191 unset($this->urls[$url]);
29d10882
A
192 unset($this->ids[$realOffset]);
193 unset($this->links[$realOffset]);
ca74886f
V
194 }
195
196 /**
197 * ArrayAccess - Returns the value at specified offset
198 */
199 public function offsetGet($offset)
200 {
29d10882
A
201 $realOffset = $this->getLinkOffset($offset);
202 return isset($this->links[$realOffset]) ? $this->links[$realOffset] : null;
ca74886f
V
203 }
204
205 /**
206 * Iterator - Returns the current element
207 */
735ed4a9 208 public function current()
ca74886f 209 {
29d10882 210 return $this[$this->keys[$this->position]];
ca74886f
V
211 }
212
213 /**
214 * Iterator - Returns the key of the current element
215 */
735ed4a9 216 public function key()
ca74886f 217 {
628b97cb 218 return $this->keys[$this->position];
ca74886f
V
219 }
220
221 /**
222 * Iterator - Moves forward to next element
223 */
735ed4a9 224 public function next()
ca74886f 225 {
628b97cb 226 ++$this->position;
ca74886f
V
227 }
228
229 /**
230 * Iterator - Rewinds the Iterator to the first element
231 *
232 * Entries are sorted by date (latest first)
233 */
735ed4a9 234 public function rewind()
ca74886f 235 {
29d10882 236 $this->keys = array_keys($this->ids);
628b97cb 237 $this->position = 0;
ca74886f
V
238 }
239
240 /**
241 * Iterator - Checks if current position is valid
242 */
735ed4a9 243 public function valid()
ca74886f 244 {
628b97cb 245 return isset($this->keys[$this->position]);
ca74886f
V
246 }
247
248 /**
249 * Checks if the DB directory and file exist
250 *
251 * If no DB file is found, creates a dummy DB.
252 */
f21abf32 253 private function check()
ca74886f 254 {
628b97cb 255 if (file_exists($this->datastore)) {
ca74886f
V
256 return;
257 }
258
259 // Create a dummy database for example
628b97cb 260 $this->links = array();
ca74886f 261 $link = array(
29d10882 262 'id' => 1,
f24896b2
V
263 'title' => t('The personal, minimalist, super-fast, database free, bookmarking service'),
264 'url' => 'https://shaarli.readthedocs.io',
265 'description' => t(
9d9f6d75 266 'Welcome to Shaarli! This is your first public bookmark. '
f24896b2 267 . 'To edit or delete me, you must first login.
598376d4 268
12266213 269To learn how to use Shaarli, consult the link "Documentation" at the bottom of this page.
598376d4 270
9d9f6d75
V
271You use the community supported version of the original Shaarli project, by Sebastien Sauvage.'
272 ),
f24896b2
V
273 'private' => 0,
274 'created' => new DateTime(),
275 'tags' => 'opensource software'
ca74886f 276 );
d592daea 277 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
29d10882 278 $this->links[1] = $link;
ca74886f
V
279
280 $link = array(
29d10882 281 'id' => 0,
f24896b2
V
282 'title' => t('My secret stuff... - Pastebin.com'),
283 'url' => 'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
284 'description' => t('Shhhh! I\'m a private link only YOU can see. You can delete me too.'),
285 'private' => 1,
286 'created' => new DateTime('1 minute ago'),
287 'tags' => 'secretstuff',
ca74886f 288 );
d592daea 289 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
29d10882 290 $this->links[0] = $link;
ca74886f
V
291
292 // Write database to disk
f21abf32 293 $this->write();
ca74886f
V
294 }
295
296 /**
297 * Reads database from disk to memory
298 */
f21abf32 299 private function read()
ca74886f 300 {
578a84bd 301 // Public links are hidden and user not logged in => nothing to show
628b97cb
V
302 if ($this->hidePublicLinks && !$this->loggedIn) {
303 $this->links = array();
578a84bd 304 return;
305 }
306
9ec0a611
A
307 $this->urls = [];
308 $this->ids = [];
b2306b0c 309 $this->links = FileUtils::readFlatDB($this->datastore, []);
ca74886f 310
29d10882
A
311 $toremove = array();
312 foreach ($this->links as $key => &$link) {
f24896b2 313 if (!$this->loggedIn && $link['private'] != 0) {
29d10882 314 // Transition for not upgraded databases.
9ec0a611 315 unset($this->links[$key]);
29d10882 316 continue;
ca74886f 317 }
195acf9f 318
510377d2 319 // Sanitize data fields.
90e5bd65 320 sanitizeLink($link);
195acf9f
A
321
322 // Remove private tags if the user is not logged in.
f24896b2 323 if (!$this->loggedIn) {
9866b408 324 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
195acf9f
A
325 }
326
90e5bd65 327 // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
628b97cb
V
328 if (!empty($this->redirector) && !startsWith($link['url'], '?')) {
329 $link['real_url'] = $this->redirector;
043eae70
A
330 if ($this->redirectorEncode) {
331 $link['real_url'] .= urlencode(unescape($link['url']));
332 } else {
333 $link['real_url'] .= $link['url'];
334 }
f211e417 335 } else {
90e5bd65
A
336 $link['real_url'] = $link['url'];
337 }
29d10882
A
338
339 // To be able to load links before running the update, and prepare the update
f24896b2 340 if (!isset($link['created'])) {
29d10882 341 $link['id'] = $link['linkdate'];
d592daea 342 $link['created'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['linkdate']);
f24896b2 343 if (!empty($link['updated'])) {
d592daea 344 $link['updated'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['updated']);
29d10882 345 }
d592daea 346 $link['shorturl'] = smallHash($link['linkdate']);
29d10882 347 }
29d10882 348
9ec0a611
A
349 $this->urls[$link['url']] = $key;
350 $this->ids[$link['id']] = $key;
5f85fcd8 351 }
ca74886f
V
352 }
353
2e28269b
V
354 /**
355 * Saves the database from memory to disk
356 *
357 * @throws IOException the datastore is not writable
358 */
f21abf32 359 private function write()
2e28269b 360 {
9ec0a611 361 $this->reorder();
b2306b0c 362 FileUtils::writeFlatDB($this->datastore, $this->links);
2e28269b
V
363 }
364
ca74886f
V
365 /**
366 * Saves the database from memory to disk
01e48f26
V
367 *
368 * @param string $pageCacheDir page cache directory
ca74886f 369 */
f21abf32 370 public function save($pageCacheDir)
ca74886f 371 {
628b97cb 372 if (!$this->loggedIn) {
ca74886f
V
373 // TODO: raise an Exception instead
374 die('You are not authorized to change the database.');
375 }
2e28269b 376
f21abf32 377 $this->write();
2e28269b 378
01e48f26 379 invalidateCaches($pageCacheDir);
ca74886f
V
380 }
381
382 /**
383 * Returns the link for a given URL, or False if it does not exist.
ef591e7e
GV
384 *
385 * @param string $url URL to search for
386 *
387 * @return mixed the existing link if it exists, else 'false'
ca74886f
V
388 */
389 public function getLinkFromUrl($url)
390 {
628b97cb
V
391 if (isset($this->urls[$url])) {
392 return $this->links[$this->urls[$url]];
ca74886f
V
393 }
394 return false;
395 }
396
397 /**
528a6f8a 398 * Returns the shaare corresponding to a smallHash.
ca74886f 399 *
528a6f8a
A
400 * @param string $request QUERY_STRING server parameter.
401 *
402 * @return array $filtered array containing permalink data.
403 *
404 * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link.
405 */
406 public function filterHash($request)
407 {
408 $request = substr($request, 0, 6);
628b97cb 409 $linkFilter = new LinkFilter($this->links);
528a6f8a
A
410 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request);
411 }
412
413 /**
414 * Returns the list of articles for a given day.
415 *
416 * @param string $request day to filter. Format: YYYYMMDD.
417 *
418 * @return array list of shaare found.
419 */
f211e417
V
420 public function filterDay($request)
421 {
628b97cb 422 $linkFilter = new LinkFilter($this->links);
528a6f8a
A
423 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request);
424 }
425
426 /**
427 * Filter links according to search parameters.
428 *
f24896b2 429 * @param array $filterRequest Search request content. Supported keys:
528a6f8a
A
430 * - searchtags: list of tags
431 * - searchterm: term search
f24896b2
V
432 * @param bool $casesensitive Optional: Perform case sensitive filter
433 * @param string $visibility return only all/private/public links
434 * @param string $untaggedonly return only untagged links
ca74886f 435 *
528a6f8a 436 * @return array filtered links, all links if no suitable filter was provided.
ca74886f 437 */
9d9f6d75
V
438 public function filterSearch(
439 $filterRequest = array(),
440 $casesensitive = false,
441 $visibility = 'all',
442 $untaggedonly = false
443 ) {
f24896b2 444
528a6f8a 445 // Filter link database according to parameters.
7d86f40b
A
446 $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
447 $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
528a6f8a 448
7d86f40b 449 // Search tags + fullsearch - blank string parameter will return all links.
f210d94f 450 $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT; // == "vuotext"
7d86f40b 451 $request = [$searchtags, $searchterm];
528a6f8a 452
29d10882 453 $linkFilter = new LinkFilter($this);
f210d94f 454 return $linkFilter->filter($type, $request, $casesensitive, $visibility, $untaggedonly);
ca74886f
V
455 }
456
457 /**
6ccd0b21 458 * Returns the list tags appearing in the links with the given tags
f8c5660d
A
459 *
460 * @param array $filteringTags tags selecting the links to consider
f24896b2 461 * @param string $visibility process only all/private/public links
f8c5660d
A
462 *
463 * @return array tag => linksCount
ca74886f 464 */
6ccd0b21 465 public function linksCountPerTag($filteringTags = [], $visibility = 'all')
ca74886f 466 {
f8c5660d
A
467 $links = $this->filterSearch(['searchtags' => $filteringTags], false, $visibility);
468 $tags = [];
469 $caseMapping = [];
6ccd0b21 470 foreach ($links as $link) {
4b35853d 471 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) {
b1eb5d1d
A
472 if (empty($tag)) {
473 continue;
ca74886f 474 }
b1eb5d1d
A
475 // The first case found will be displayed.
476 if (!isset($caseMapping[strtolower($tag)])) {
477 $caseMapping[strtolower($tag)] = $tag;
478 $tags[$caseMapping[strtolower($tag)]] = 0;
479 }
480 $tags[$caseMapping[strtolower($tag)]]++;
ca74886f
V
481 }
482 }
f8c5660d
A
483
484 /*
485 * Formerly used arsort(), which doesn't define the sort behaviour for equal values.
486 * Also, this function doesn't produce the same result between PHP 5.6 and 7.
487 *
488 * So we now use array_multisort() to sort tags by DESC occurrences,
489 * then ASC alphabetically for equal values.
490 *
491 * @see https://github.com/shaarli/Shaarli/issues/1142
492 */
f28396a2
A
493 $keys = array_keys($tags);
494 $tmpTags = array_combine($keys, $keys);
f28396a2 495 array_multisort($tags, SORT_DESC, $tmpTags, SORT_ASC, $tags);
ca74886f
V
496 return $tags;
497 }
498
3b67b222
A
499 /**
500 * Rename or delete a tag across all links.
501 *
502 * @param string $from Tag to rename
f24896b2 503 * @param string $to New tag. If none is provided, the from tag will be deleted
3b67b222
A
504 *
505 * @return array|bool List of altered links or false on error
506 */
507 public function renameTag($from, $to)
508 {
509 if (empty($from)) {
510 return false;
511 }
512 $delete = empty($to);
513 // True for case-sensitive tag search.
514 $linksToAlter = $this->filterSearch(['searchtags' => $from], true);
f211e417 515 foreach ($linksToAlter as $key => &$value) {
3b67b222
A
516 $tags = preg_split('/\s+/', trim($value['tags']));
517 if (($pos = array_search($from, $tags)) !== false) {
518 if ($delete) {
519 unset($tags[$pos]); // Remove tag.
520 } else {
521 $tags[$pos] = trim($to);
522 }
523 $value['tags'] = trim(implode(' ', array_unique($tags)));
524 $this[$value['id']] = $value;
525 }
526 }
527
528 return $linksToAlter;
529 }
530
ca74886f
V
531 /**
532 * Returns the list of days containing articles (oldest first)
533 * Output: An array containing days (in format YYYYMMDD).
534 */
535 public function days()
536 {
537 $linkDays = array();
29d10882
A
538 foreach ($this->links as $link) {
539 $linkDays[$link['created']->format('Ymd')] = 0;
ca74886f
V
540 }
541 $linkDays = array_keys($linkDays);
542 sort($linkDays);
510377d2 543
ca74886f
V
544 return $linkDays;
545 }
29d10882
A
546
547 /**
548 * Reorder links by creation date (newest first).
549 *
550 * Also update the urls and ids mapping arrays.
551 *
552 * @param string $order ASC|DESC
553 */
554 public function reorder($order = 'DESC')
555 {
556 $order = $order === 'ASC' ? -1 : 1;
557 // Reorder array by dates.
f211e417 558 usort($this->links, function ($a, $b) use ($order) {
4154c25b
A
559 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
560 return $a['sticky'] ? -1 : 1;
561 }
29d10882
A
562 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
563 });
564
9ec0a611
A
565 $this->urls = [];
566 $this->ids = [];
29d10882
A
567 foreach ($this->links as $key => $link) {
568 $this->urls[$link['url']] = $key;
569 $this->ids[$link['id']] = $key;
570 }
571 }
572
573 /**
574 * Return the next key for link creation.
575 * E.g. If the last ID is 597, the next will be 598.
576 *
577 * @return int next ID.
578 */
579 public function getNextId()
580 {
581 if (!empty($this->ids)) {
582 return max(array_keys($this->ids)) + 1;
583 }
584 return 0;
585 }
586
587 /**
588 * Returns a link offset in links array from its unique ID.
589 *
590 * @param int $id Persistent ID of a link.
591 *
d592daea 592 * @return int Real offset in local array, or null if doesn't exist.
29d10882
A
593 */
594 protected function getLinkOffset($id)
595 {
596 if (isset($this->ids[$id])) {
597 return $this->ids[$id];
598 }
599 return null;
600 }
ca74886f 601}