3 namespace Shaarli\Legacy
;
9 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
10 use Shaarli\Exceptions\IOException
;
11 use Shaarli\FileUtils
;
14 * Data storage for bookmarks.
16 * This object behaves like an associative array.
19 * $myLinks = new LinkDB();
20 * echo $myLinks[350]['title'];
21 * foreach ($myLinks as $link)
22 * echo $link['title'].' at url '.$link['url'].'; description:'.$link['description'];
25 * - id: primary key, incremental integer identifier (persistent)
26 * - description: description of the entry
27 * - created: creation date of this entry, DateTime object.
28 * - updated: last modification date of this entry, DateTime object.
29 * - private: Is this link private? 0=no, other value=yes
30 * - tags: tags attached to this entry (separated by spaces)
31 * - title Title of the link
32 * - url URL of the link. Used for displayable bookmarks.
33 * Can be absolute or relative in the database but the relative bookmarks
34 * will be converted to absolute ones in templates.
35 * - real_url Raw URL in stored in the DB (absolute or relative).
36 * - shorturl Permalink smallhash
38 * Implements 3 interfaces:
39 * - ArrayAccess: behaves like an associative array;
40 * - Countable: there is a count() method;
41 * - Iterator: usable in foreach () loops.
44 * ArrayAccess is implemented in a way that will allow to access a link
45 * with the unique identifier ID directly with $link[ID].
46 * Note that it's not the real key of the link array attribute.
47 * This mechanism is in place to have persistent link IDs,
48 * even though the internal array is reordered by date.
50 * - DB: link #1 (2010-01-01) link #2 (2016-01-01)
52 * - Import bookmarks containing: link #3 (2013-01-01)
53 * - New DB: link #1 (2010-01-01) link #2 (2016-01-01) link #3 (2013-01-01)
54 * - Real order: #2 #3 #1
58 class LegacyLinkDB
implements Iterator
, Countable
, ArrayAccess
60 // Links are stored as a PHP serialized string
63 // Link date storage format
64 const LINK_DATE_FORMAT
= 'Ymd_His';
66 // List of bookmarks (associative array)
67 // - key: link date (e.g. "20110823_124546"),
68 // - value: associative array (keys: title, description...)
71 // List of all recorded URLs (key=url, value=link offset)
72 // for fast reserve search (url-->link offset)
76 * @var array List of all bookmarks IDS mapped with their array offset.
81 // List of offset keys (for the Iterator interface implementation)
84 // Position in the $this->keys array (for the Iterator interface)
87 // Is the user logged in? (used to filter private bookmarks)
90 // Hide public bookmarks
91 private $hidePublicLinks;
94 * Creates a new LinkDB
96 * Checks if the datastore exists; else, attempts to create a dummy one.
98 * @param string $datastore datastore file path.
99 * @param boolean $isLoggedIn is the user logged in?
100 * @param boolean $hidePublicLinks if true all bookmarks are private.
102 public function __construct(
108 $this->datastore
= $datastore;
109 $this->loggedIn
= $isLoggedIn;
110 $this->hidePublicLinks
= $hidePublicLinks;
116 * Countable - Counts elements of an object
118 public function count()
120 return count($this->links
);
124 * ArrayAccess - Assigns a value to the specified offset
126 public function offsetSet($offset, $value)
128 // TODO: use exceptions instead of "die"
129 if (!$this->loggedIn
) {
130 die(t('You are not authorized to add a link.'));
132 if (!isset($value['id']) || empty($value['url'])) {
133 die(t('Internal Error: A link should always have an id and URL.'));
135 if (($offset !== null && !is_int($offset)) || !is_int($value['id'])) {
136 die(t('You must specify an integer as a key.'));
138 if ($offset !== null && $offset !== $value['id']) {
139 die(t('Array offset and link ID must be equal.'));
142 // If the link exists, we reuse the real offset, otherwise new entry
143 $existing = $this->getLinkOffset($offset);
144 if ($existing !== null) {
147 $offset = count($this->links
);
149 $this->links
[$offset] = $value;
150 $this->urls
[$value['url']] = $offset;
151 $this->ids
[$value['id']] = $offset;
155 * ArrayAccess - Whether or not an offset exists
157 public function offsetExists($offset)
159 return array_key_exists($this->getLinkOffset($offset), $this->links
);
163 * ArrayAccess - Unsets an offset
165 public function offsetUnset($offset)
167 if (!$this->loggedIn
) {
168 // TODO: raise an exception
169 die('You are not authorized to delete a link.');
171 $realOffset = $this->getLinkOffset($offset);
172 $url = $this->links
[$realOffset]['url'];
173 unset($this->urls
[$url]);
174 unset($this->ids
[$realOffset]);
175 unset($this->links
[$realOffset]);
179 * ArrayAccess - Returns the value at specified offset
181 public function offsetGet($offset)
183 $realOffset = $this->getLinkOffset($offset);
184 return isset($this->links
[$realOffset]) ? $this->links
[$realOffset] : null;
188 * Iterator - Returns the current element
190 public function current()
192 return $this[$this->keys
[$this->position
]];
196 * Iterator - Returns the key of the current element
198 public function key()
200 return $this->keys
[$this->position
];
204 * Iterator - Moves forward to next element
206 public function next()
212 * Iterator - Rewinds the Iterator to the first element
214 * Entries are sorted by date (latest first)
216 public function rewind()
218 $this->keys
= array_keys($this->ids
);
223 * Iterator - Checks if current position is valid
225 public function valid()
227 return isset($this->keys
[$this->position
]);
231 * Checks if the DB directory and file exist
233 * If no DB file is found, creates a dummy DB.
235 private function check()
237 if (file_exists($this->datastore
)) {
241 // Create a dummy database for example
242 $this->links
= array();
245 'title' => t('The personal, minimalist, super-fast, database free, bookmarking service'),
246 'url' => 'https://shaarli.readthedocs.io',
248 'Welcome to Shaarli! This is your first public bookmark. '
249 . 'To edit or delete me, you must first login.
251 To learn how to use Shaarli, consult the link "Documentation" at the bottom of this page.
253 You use the community supported version of the original Shaarli project, by Sebastien Sauvage.'
256 'created' => new DateTime(),
257 'tags' => 'opensource software',
260 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
261 $this->links
[1] = $link;
265 'title' => t('My secret stuff... - Pastebin.com'),
266 'url' => 'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
267 'description' => t('Shhhh! I\'m a private link only YOU can see. You can delete me too.'),
269 'created' => new DateTime('1 minute ago'),
270 'tags' => 'secretstuff',
273 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
274 $this->links
[0] = $link;
276 // Write database to disk
281 * Reads database from disk to memory
283 private function read()
285 // Public bookmarks are hidden and user not logged in => nothing to show
286 if ($this->hidePublicLinks
&& !$this->loggedIn
) {
287 $this->links
= array();
293 $this->links
= FileUtils
::readFlatDB($this->datastore
, []);
296 foreach ($this->links
as $key => &$link) {
297 if (!$this->loggedIn
&& $link['private'] != 0) {
298 // Transition for not upgraded databases.
299 unset($this->links
[$key]);
303 // Sanitize data fields.
306 // Remove private tags if the user is not logged in.
307 if (!$this->loggedIn
) {
308 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
311 $link['real_url'] = $link['url'];
313 $link['sticky'] = isset($link['sticky']) ? $link['sticky'] : false;
315 // To be able to load bookmarks before running the update, and prepare the update
316 if (!isset($link['created'])) {
317 $link['id'] = $link['linkdate'];
318 $link['created'] = DateTime
::createFromFormat(self
::LINK_DATE_FORMAT
, $link['linkdate']);
319 if (!empty($link['updated'])) {
320 $link['updated'] = DateTime
::createFromFormat(self
::LINK_DATE_FORMAT
, $link['updated']);
322 $link['shorturl'] = smallHash($link['linkdate']);
325 $this->urls
[$link['url']] = $key;
326 $this->ids
[$link['id']] = $key;
331 * Saves the database from memory to disk
333 * @throws IOException the datastore is not writable
335 private function write()
338 FileUtils
::writeFlatDB($this->datastore
, $this->links
);
342 * Saves the database from memory to disk
344 * @param string $pageCacheDir page cache directory
346 public function save($pageCacheDir)
348 if (!$this->loggedIn
) {
349 // TODO: raise an Exception instead
350 die('You are not authorized to change the database.');
355 invalidateCaches($pageCacheDir);
359 * Returns the link for a given URL, or False if it does not exist.
361 * @param string $url URL to search for
363 * @return mixed the existing link if it exists, else 'false'
365 public function getLinkFromUrl($url)
367 if (isset($this->urls
[$url])) {
368 return $this->links
[$this->urls
[$url]];
374 * Returns the shaare corresponding to a smallHash.
376 * @param string $request QUERY_STRING server parameter.
378 * @return array $filtered array containing permalink data.
380 * @throws BookmarkNotFoundException if the smallhash is malformed or doesn't match any link.
382 public function filterHash($request)
384 $request = substr($request, 0, 6);
385 $linkFilter = new LegacyLinkFilter($this->links
);
386 return $linkFilter->filter(LegacyLinkFilter
::$FILTER_HASH, $request);
390 * Returns the list of articles for a given day.
392 * @param string $request day to filter. Format: YYYYMMDD.
394 * @return array list of shaare found.
396 public function filterDay($request)
398 $linkFilter = new LegacyLinkFilter($this->links
);
399 return $linkFilter->filter(LegacyLinkFilter
::$FILTER_DAY, $request);
403 * Filter bookmarks according to search parameters.
405 * @param array $filterRequest Search request content. Supported keys:
406 * - searchtags: list of tags
407 * - searchterm: term search
408 * @param bool $casesensitive Optional: Perform case sensitive filter
409 * @param string $visibility return only all/private/public bookmarks
410 * @param bool $untaggedonly return only untagged bookmarks
412 * @return array filtered bookmarks, all bookmarks if no suitable filter was provided.
414 public function filterSearch(
415 $filterRequest = array(),
416 $casesensitive = false,
418 $untaggedonly = false
421 // Filter link database according to parameters.
422 $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
423 $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
425 // Search tags + fullsearch - blank string parameter will return all bookmarks.
426 $type = LegacyLinkFilter
::$FILTER_TAG | LegacyLinkFilter
::$FILTER_TEXT; // == "vuotext"
427 $request = [$searchtags, $searchterm];
429 $linkFilter = new LegacyLinkFilter($this);
430 return $linkFilter->filter($type, $request, $casesensitive, $visibility, $untaggedonly);
434 * Returns the list tags appearing in the bookmarks with the given tags
436 * @param array $filteringTags tags selecting the bookmarks to consider
437 * @param string $visibility process only all/private/public bookmarks
439 * @return array tag => linksCount
441 public function linksCountPerTag($filteringTags = [], $visibility = 'all')
443 $links = $this->filterSearch(['searchtags' => $filteringTags], false, $visibility);
446 foreach ($links as $link) {
447 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY
) as $tag) {
451 // The first case found will be displayed.
452 if (!isset($caseMapping[strtolower($tag)])) {
453 $caseMapping[strtolower($tag)] = $tag;
454 $tags[$caseMapping[strtolower($tag)]] = 0;
456 $tags[$caseMapping[strtolower($tag)]]++
;
461 * Formerly used arsort(), which doesn't define the sort behaviour for equal values.
462 * Also, this function doesn't produce the same result between PHP 5.6 and 7.
464 * So we now use array_multisort() to sort tags by DESC occurrences,
465 * then ASC alphabetically for equal values.
467 * @see https://github.com/shaarli/Shaarli/issues/1142
469 $keys = array_keys($tags);
470 $tmpTags = array_combine($keys, $keys);
471 array_multisort($tags, SORT_DESC
, $tmpTags, SORT_ASC
, $tags);
476 * Rename or delete a tag across all bookmarks.
478 * @param string $from Tag to rename
479 * @param string $to New tag. If none is provided, the from tag will be deleted
481 * @return array|bool List of altered bookmarks or false on error
483 public function renameTag($from, $to)
488 $delete = empty($to);
489 // True for case-sensitive tag search.
490 $linksToAlter = $this->filterSearch(['searchtags' => $from], true);
491 foreach ($linksToAlter as $key => &$value) {
492 $tags = preg_split('/\s+/', trim($value['tags']));
493 if (($pos = array_search($from, $tags)) !== false) {
495 unset($tags[$pos]); // Remove tag.
497 $tags[$pos] = trim($to);
499 $value['tags'] = trim(implode(' ', array_unique($tags)));
500 $this[$value['id']] = $value;
504 return $linksToAlter;
508 * Returns the list of days containing articles (oldest first)
509 * Output: An array containing days (in format YYYYMMDD).
511 public function days()
514 foreach ($this->links
as $link) {
515 $linkDays[$link['created']->format('Ymd')] = 0;
517 $linkDays = array_keys($linkDays);
524 * Reorder bookmarks by creation date (newest first).
526 * Also update the urls and ids mapping arrays.
528 * @param string $order ASC|DESC
530 public function reorder($order = 'DESC')
532 $order = $order === 'ASC' ? -1 : 1;
533 // Reorder array by dates.
534 usort($this->links
, function ($a, $b) use ($order) {
535 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
536 return $a['sticky'] ? -1 : 1;
538 if ($a['created'] == $b['created']) {
539 return $a['id'] < $b['id'] ? 1 * $order : -1 * $order;
541 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
546 foreach ($this->links
as $key => $link) {
547 $this->urls
[$link['url']] = $key;
548 $this->ids
[$link['id']] = $key;
553 * Return the next key for link creation.
554 * E.g. If the last ID is 597, the next will be 598.
556 * @return int next ID.
558 public function getNextId()
560 if (!empty($this->ids
)) {
561 return max(array_keys($this->ids
)) +
1;
567 * Returns a link offset in bookmarks array from its unique ID.
569 * @param int $id Persistent ID of a link.
571 * @return int Real offset in local array, or null if doesn't exist.
573 protected function getLinkOffset($id)
575 if (isset($this->ids
[$id])) {
576 return $this->ids
[$id];