]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkDB.php
f026a0418b98335ae861100968f3104f6f76a7b9
3 * Data storage for links.
5 * This object behaves like an associative array.
8 * $myLinks = new LinkDB();
9 * echo $myLinks[350]['title'];
10 * foreach ($myLinks as $link)
11 * echo $link['title'].' at url '.$link['url'].'; description:'.$link['description'];
14 * - id: primary key, incremental integer identifier (persistent)
15 * - description: description of the entry
16 * - created: creation date of this entry, DateTime object.
17 * - updated: last modification date of this entry, DateTime object.
18 * - private: Is this link private? 0=no, other value=yes
19 * - tags: tags attached to this entry (separated by spaces)
20 * - title Title of the link
21 * - url URL of the link. Used for displayable links (no redirector, relative, etc.).
22 * Can be absolute or relative.
23 * Relative URLs are permalinks (e.g.'?m-ukcw')
24 * - real_url Absolute processed URL.
25 * - shorturl Permalink smallhash
27 * Implements 3 interfaces:
28 * - ArrayAccess: behaves like an associative array;
29 * - Countable: there is a count() method;
30 * - Iterator: usable in foreach () loops.
33 * ArrayAccess is implemented in a way that will allow to access a link
34 * with the unique identifier ID directly with $link[ID].
35 * Note that it's not the real key of the link array attribute.
36 * This mechanism is in place to have persistent link IDs,
37 * even though the internal array is reordered by date.
39 * - DB: link #1 (2010-01-01) link #2 (2016-01-01)
41 * - Import links containing: link #3 (2013-01-01)
42 * - New DB: link #1 (2010-01-01) link #2 (2016-01-01) link #3 (2013-01-01)
43 * - Real order: #2 #3 #1
45 class LinkDB
implements Iterator
, Countable
, ArrayAccess
47 // Links are stored as a PHP serialized string
50 // Link date storage format
51 const LINK_DATE_FORMAT
= 'Ymd_His';
53 // List of links (associative array)
54 // - key: link date (e.g. "20110823_124546"),
55 // - value: associative array (keys: title, description...)
58 // List of all recorded URLs (key=url, value=link offset)
59 // for fast reserve search (url-->link offset)
63 * @var array List of all links IDS mapped with their array offset.
68 // List of offset keys (for the Iterator interface implementation)
71 // Position in the $this->keys array (for the Iterator interface)
74 // Is the user logged in? (used to filter private links)
78 private $hidePublicLinks;
80 // link redirector set in user settings.
84 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
87 * anonym.to needs clean URL while dereferer.org needs urlencoded URL.
89 * @var boolean $redirectorEncode parameter: true or false
91 private $redirectorEncode;
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 links are private.
101 * @param string $redirector link redirector set in user settings.
102 * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
104 public function __construct(
109 $redirectorEncode = true
112 $this->datastore
= $datastore;
113 $this->loggedIn
= $isLoggedIn;
114 $this->hidePublicLinks
= $hidePublicLinks;
115 $this->redirector
= $redirector;
116 $this->redirectorEncode
= $redirectorEncode === true;
122 * Countable - Counts elements of an object
124 public function count()
126 return count($this->links
);
130 * ArrayAccess - Assigns a value to the specified offset
132 public function offsetSet($offset, $value)
134 // TODO: use exceptions instead of "die"
135 if (!$this->loggedIn
) {
136 die(t('You are not authorized to add a link.'));
138 if (!isset($value['id']) || empty($value['url'])) {
139 die(t('Internal Error: A link should always have an id and URL.'));
141 if (($offset !== null && ! is_int($offset)) || ! is_int($value['id'])) {
142 die(t('You must specify an integer as a key.'));
144 if ($offset !== null && $offset !== $value['id']) {
145 die(t('Array offset and link ID must be equal.'));
148 // If the link exists, we reuse the real offset, otherwise new entry
149 $existing = $this->getLinkOffset($offset);
150 if ($existing !== null) {
153 $offset = count($this->links
);
155 $this->links
[$offset] = $value;
156 $this->urls
[$value['url']] = $offset;
157 $this->ids
[$value['id']] = $offset;
161 * ArrayAccess - Whether or not an offset exists
163 public function offsetExists($offset)
165 return array_key_exists($this->getLinkOffset($offset), $this->links
);
169 * ArrayAccess - Unsets an offset
171 public function offsetUnset($offset)
173 if (!$this->loggedIn
) {
174 // TODO: raise an exception
175 die('You are not authorized to delete a link.');
177 $realOffset = $this->getLinkOffset($offset);
178 $url = $this->links
[$realOffset]['url'];
179 unset($this->urls
[$url]);
180 unset($this->ids
[$realOffset]);
181 unset($this->links
[$realOffset]);
185 * ArrayAccess - Returns the value at specified offset
187 public function offsetGet($offset)
189 $realOffset = $this->getLinkOffset($offset);
190 return isset($this->links
[$realOffset]) ? $this->links
[$realOffset] : null;
194 * Iterator - Returns the current element
196 public function current()
198 return $this[$this->keys
[$this->position
]];
202 * Iterator - Returns the key of the current element
204 public function key()
206 return $this->keys
[$this->position
];
210 * Iterator - Moves forward to next element
212 public function next()
218 * Iterator - Rewinds the Iterator to the first element
220 * Entries are sorted by date (latest first)
222 public function rewind()
224 $this->keys
= array_keys($this->ids
);
229 * Iterator - Checks if current position is valid
231 public function valid()
233 return isset($this->keys
[$this->position
]);
237 * Checks if the DB directory and file exist
239 * If no DB file is found, creates a dummy DB.
241 private function check()
243 if (file_exists($this->datastore
)) {
247 // Create a dummy database for example
248 $this->links
= array();
251 'title'=> t('The personal, minimalist, super-fast, database free, bookmarking service'),
252 'url'=>'https://shaarli.readthedocs.io',
253 'description'=>t('Welcome to Shaarli! This is your first public bookmark. To edit or delete me, you must first login.
255 To learn how to use Shaarli, consult the link "Documentation" at the bottom of this page.
257 You use the community supported version of the original Shaarli project, by Sebastien Sauvage.'),
259 'created'=> new DateTime(),
260 'tags'=>'opensource software'
262 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
263 $this->links
[1] = $link;
267 'title'=> t('My secret stuff... - Pastebin.com'),
268 'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
269 'description'=> t('Shhhh! I\'m a private link only YOU can see. You can delete me too.'),
271 'created'=> new DateTime('1 minute ago'),
272 'tags'=>'secretstuff',
274 $link['shorturl'] = link_small_hash($link['created'], $link['id']);
275 $this->links
[0] = $link;
277 // Write database to disk
282 * Reads database from disk to memory
284 private function read()
286 // Public links are hidden and user not logged in => nothing to show
287 if ($this->hidePublicLinks
&& !$this->loggedIn
) {
288 $this->links
= array();
292 $this->links
= FileUtils
::readFlatDB($this->datastore
, []);
295 foreach ($this->links
as $key => &$link) {
296 if (! $this->loggedIn
&& $link['private'] != 0) {
297 // Transition for not upgraded databases.
302 // Sanitize data fields.
305 // Remove private tags if the user is not logged in.
306 if (! $this->loggedIn
) {
307 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
310 // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
311 if (!empty($this->redirector
) && !startsWith($link['url'], '?')) {
312 $link['real_url'] = $this->redirector
;
313 if ($this->redirectorEncode
) {
314 $link['real_url'] .= urlencode(unescape($link['url']));
316 $link['real_url'] .= $link['url'];
320 $link['real_url'] = $link['url'];
323 // To be able to load links before running the update, and prepare the update
324 if (! isset($link['created'])) {
325 $link['id'] = $link['linkdate'];
326 $link['created'] = DateTime
::createFromFormat(self
::LINK_DATE_FORMAT
, $link['linkdate']);
327 if (! empty($link['updated'])) {
328 $link['updated'] = DateTime
::createFromFormat(self
::LINK_DATE_FORMAT
, $link['updated']);
330 $link['shorturl'] = smallHash($link['linkdate']);
334 // If user is not logged in, filter private links.
335 foreach ($toremove as $offset) {
336 unset($this->links
[$offset]);
343 * Saves the database from memory to disk
345 * @throws IOException the datastore is not writable
347 private function write()
349 FileUtils
::writeFlatDB($this->datastore
, $this->links
);
353 * Saves the database from memory to disk
355 * @param string $pageCacheDir page cache directory
357 public function save($pageCacheDir)
359 if (!$this->loggedIn
) {
360 // TODO: raise an Exception instead
361 die('You are not authorized to change the database.');
366 invalidateCaches($pageCacheDir);
370 * Returns the link for a given URL, or False if it does not exist.
372 * @param string $url URL to search for
374 * @return mixed the existing link if it exists, else 'false'
376 public function getLinkFromUrl($url)
378 if (isset($this->urls
[$url])) {
379 return $this->links
[$this->urls
[$url]];
385 * Returns the shaare corresponding to a smallHash.
387 * @param string $request QUERY_STRING server parameter.
389 * @return array $filtered array containing permalink data.
391 * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link.
393 public function filterHash($request)
395 $request = substr($request, 0, 6);
396 $linkFilter = new LinkFilter($this->links
);
397 return $linkFilter->filter(LinkFilter
::$FILTER_HASH, $request);
401 * Returns the list of articles for a given day.
403 * @param string $request day to filter. Format: YYYYMMDD.
405 * @return array list of shaare found.
407 public function filterDay($request) {
408 $linkFilter = new LinkFilter($this->links
);
409 return $linkFilter->filter(LinkFilter
::$FILTER_DAY, $request);
413 * Filter links according to search parameters.
415 * @param array $filterRequest Search request content. Supported keys:
416 * - searchtags: list of tags
417 * - searchterm: term search
418 * @param bool $casesensitive Optional: Perform case sensitive filter
419 * @param string $visibility return only all/private/public links
420 * @param string $untaggedonly return only untagged links
422 * @return array filtered links, all links if no suitable filter was provided.
424 public function filterSearch($filterRequest = array(), $casesensitive = false, $visibility = 'all', $untaggedonly = false)
426 // Filter link database according to parameters.
427 $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
428 $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
430 // Search tags + fullsearch - blank string parameter will return all links.
431 $type = LinkFilter
::$FILTER_TAG | LinkFilter
::$FILTER_TEXT; // == "vuotext"
432 $request = [$searchtags, $searchterm];
434 $linkFilter = new LinkFilter($this);
435 return $linkFilter->filter($type, $request, $casesensitive, $visibility, $untaggedonly);
439 * Returns the list tags appearing in the links with the given tags
440 * @param $filteringTags: tags selecting the links to consider
441 * @param $visibility: process only all/private/public links
442 * @return: a tag=>linksCount array
444 public function linksCountPerTag($filteringTags = [], $visibility = 'all')
446 $links = empty($filteringTags) ? $this->links
: $this->filterSearch(['searchtags' => $filteringTags], false, $visibility);
448 $caseMapping = array();
449 foreach ($links as $link) {
450 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY
) as $tag) {
454 // The first case found will be displayed.
455 if (!isset($caseMapping[strtolower($tag)])) {
456 $caseMapping[strtolower($tag)] = $tag;
457 $tags[$caseMapping[strtolower($tag)]] = 0;
459 $tags[$caseMapping[strtolower($tag)]]++
;
462 // Sort tags by usage (most used tag first)
468 * Rename or delete a tag across all links.
470 * @param string $from Tag to rename
471 * @param string $to New tag. If none is provided, the from tag will be deleted
473 * @return array|bool List of altered links or false on error
475 public function renameTag($from, $to)
480 $delete = empty($to);
481 // True for case-sensitive tag search.
482 $linksToAlter = $this->filterSearch(['searchtags' => $from], true);
483 foreach($linksToAlter as $key => &$value)
485 $tags = preg_split('/\s+/', trim($value['tags']));
486 if (($pos = array_search($from, $tags)) !== false) {
488 unset($tags[$pos]); // Remove tag.
490 $tags[$pos] = trim($to);
492 $value['tags'] = trim(implode(' ', array_unique($tags)));
493 $this[$value['id']] = $value;
497 return $linksToAlter;
501 * Returns the list of days containing articles (oldest first)
502 * Output: An array containing days (in format YYYYMMDD).
504 public function days()
507 foreach ($this->links
as $link) {
508 $linkDays[$link['created']->format('Ymd')] = 0;
510 $linkDays = array_keys($linkDays);
517 * Reorder links by creation date (newest first).
519 * Also update the urls and ids mapping arrays.
521 * @param string $order ASC|DESC
523 public function reorder($order = 'DESC')
525 $order = $order === 'ASC' ? -1 : 1;
526 // Reorder array by dates.
527 usort($this->links
, function($a, $b) use ($order) {
528 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
531 $this->urls
= array();
532 $this->ids
= array();
533 foreach ($this->links
as $key => $link) {
534 $this->urls
[$link['url']] = $key;
535 $this->ids
[$link['id']] = $key;
540 * Return the next key for link creation.
541 * E.g. If the last ID is 597, the next will be 598.
543 * @return int next ID.
545 public function getNextId()
547 if (!empty($this->ids
)) {
548 return max(array_keys($this->ids
)) +
1;
554 * Returns a link offset in links array from its unique ID.
556 * @param int $id Persistent ID of a link.
558 * @return int Real offset in local array, or null if doesn't exist.
560 protected function getLinkOffset($id)
562 if (isset($this->ids
[$id])) {
563 return $this->ids
[$id];