]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkDB.php
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.
26 * Implements 3 interfaces:
27 * - ArrayAccess: behaves like an associative array;
28 * - Countable: there is a count() method;
29 * - Iterator: usable in foreach () loops.
32 * ArrayAccess is implemented in a way that will allow to access a link
33 * with the unique identifier ID directly with $link[ID].
34 * Note that it's not the real key of the link array attribute.
35 * This mechanism is in place to have persistent link IDs,
36 * even though the internal array is reordered by date.
38 * - DB: link #1 (2010-01-01) link #2 (2016-01-01)
40 * - Import links containing: link #3 (2013-01-01)
41 * - New DB: link #1 (2010-01-01) link #2 (2016-01-01) link #3 (2013-01-01)
42 * - Real order: #2 #3 #1
44 class LinkDB
implements Iterator
, Countable
, ArrayAccess
46 // Links are stored as a PHP serialized string
49 // Link date storage format
50 const LINK_DATE_FORMAT
= 'Ymd_His';
52 // Datastore PHP prefix
53 protected static $phpPrefix = '<?php /* ';
55 // Datastore PHP suffix
56 protected static $phpSuffix = ' */ ?>';
58 // List of links (associative array)
59 // - key: link date (e.g. "20110823_124546"),
60 // - value: associative array (keys: title, description...)
63 // List of all recorded URLs (key=url, value=link offset)
64 // for fast reserve search (url-->link offset)
68 * @var array List of all links IDS mapped with their array offset.
73 // List of offset keys (for the Iterator interface implementation)
76 // Position in the $this->keys array (for the Iterator interface)
79 // Is the user logged in? (used to filter private links)
83 private $hidePublicLinks;
85 // link redirector set in user settings.
89 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
92 * anonym.to needs clean URL while dereferer.org needs urlencoded URL.
94 * @var boolean $redirectorEncode parameter: true or false
96 private $redirectorEncode;
99 * Creates a new LinkDB
101 * Checks if the datastore exists; else, attempts to create a dummy one.
103 * @param string $datastore datastore file path.
104 * @param boolean $isLoggedIn is the user logged in?
105 * @param boolean $hidePublicLinks if true all links are private.
106 * @param string $redirector link redirector set in user settings.
107 * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
109 public function __construct(
114 $redirectorEncode = true
117 $this->datastore
= $datastore;
118 $this->loggedIn
= $isLoggedIn;
119 $this->hidePublicLinks
= $hidePublicLinks;
120 $this->redirector
= $redirector;
121 $this->redirectorEncode
= $redirectorEncode === true;
127 * Countable - Counts elements of an object
129 public function count()
131 return count($this->links
);
135 * ArrayAccess - Assigns a value to the specified offset
137 public function offsetSet($offset, $value)
139 // TODO: use exceptions instead of "die"
140 if (!$this->loggedIn
) {
141 die('You are not authorized to add a link.');
143 if (!isset($value['id']) || empty($value['url'])) {
144 die('Internal Error: A link should always have an id and URL.');
146 if ((! empty($offset) && ! is_int($offset)) || ! is_int($value['id'])) {
147 die('You must specify an integer as a key.');
149 if (! empty($offset) && $offset !== $value['id']) {
150 die('Array offset and link ID must be equal.');
153 // If the link exists, we reuse the real offset, otherwise new entry
154 $existing = $this->getLinkOffset($offset);
155 if ($existing !== null) {
158 $offset = count($this->links
);
160 $this->links
[$offset] = $value;
161 $this->urls
[$value['url']] = $offset;
162 $this->ids
[$value['id']] = $offset;
166 * ArrayAccess - Whether or not an offset exists
168 public function offsetExists($offset)
170 return array_key_exists($this->getLinkOffset($offset), $this->links
);
174 * ArrayAccess - Unsets an offset
176 public function offsetUnset($offset)
178 if (!$this->loggedIn
) {
179 // TODO: raise an exception
180 die('You are not authorized to delete a link.');
182 $realOffset = $this->getLinkOffset($offset);
183 $url = $this->links
[$realOffset]['url'];
184 unset($this->urls
[$url]);
185 unset($this->ids
[$realOffset]);
186 unset($this->links
[$realOffset]);
190 * ArrayAccess - Returns the value at specified offset
192 public function offsetGet($offset)
194 $realOffset = $this->getLinkOffset($offset);
195 return isset($this->links
[$realOffset]) ? $this->links
[$realOffset] : null;
199 * Iterator - Returns the current element
201 public function current()
203 return $this[$this->keys
[$this->position
]];
207 * Iterator - Returns the key of the current element
209 public function key()
211 return $this->keys
[$this->position
];
215 * Iterator - Moves forward to next element
217 public function next()
223 * Iterator - Rewinds the Iterator to the first element
225 * Entries are sorted by date (latest first)
227 public function rewind()
229 $this->keys
= array_keys($this->ids
);
234 * Iterator - Checks if current position is valid
236 public function valid()
238 return isset($this->keys
[$this->position
]);
242 * Checks if the DB directory and file exist
244 * If no DB file is found, creates a dummy DB.
246 private function check()
248 if (file_exists($this->datastore
)) {
252 // Create a dummy database for example
253 $this->links
= array();
256 'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone',
257 'url'=>'https://github.com/shaarli/Shaarli/wiki',
258 'description'=>'Welcome to Shaarli! This is your first public bookmark. To edit or delete me, you must first login.
260 To learn how to use Shaarli, consult the link "Help/documentation" at the bottom of this page.
262 You use the community supported version of the original Shaarli project, by Sebastien Sauvage.',
264 'created'=> new DateTime(),
265 'tags'=>'opensource software'
267 $this->links
[1] = $link;
271 'title'=>'My secret stuff... - Pastebin.com',
272 'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
273 'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.',
275 'created'=> new DateTime('1 minute ago'),
276 'tags'=>'secretstuff'
278 $this->links
[0] = $link;
280 // Write database to disk
285 * Reads database from disk to memory
287 private function read()
289 // Public links are hidden and user not logged in => nothing to show
290 if ($this->hidePublicLinks
&& !$this->loggedIn
) {
291 $this->links
= array();
296 // Note that gzinflate is faster than gzuncompress.
297 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
298 $this->links
= array();
300 if (file_exists($this->datastore
)) {
301 $this->links
= unserialize(gzinflate(base64_decode(
302 substr(file_get_contents($this->datastore
),
303 strlen(self
::$phpPrefix), -strlen(self
::$phpSuffix)))));
307 foreach ($this->links
as $key => &$link) {
308 if (! $this->loggedIn
&& $link['private'] != 0) {
309 // Transition for not upgraded databases.
314 // Sanitize data fields.
317 // Remove private tags if the user is not logged in.
318 if (! $this->loggedIn
) {
319 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
322 // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
323 if (!empty($this->redirector
) && !startsWith($link['url'], '?')) {
324 $link['real_url'] = $this->redirector
;
325 if ($this->redirectorEncode
) {
326 $link['real_url'] .= urlencode(unescape($link['url']));
328 $link['real_url'] .= $link['url'];
332 $link['real_url'] = $link['url'];
335 // To be able to load links before running the update, and prepare the update
336 if (! isset($link['created'])) {
337 $link['id'] = $link['linkdate'];
338 $link['created'] = DateTime
::createFromFormat('Ymd_His', $link['linkdate']);
339 if (! empty($link['updated'])) {
340 $link['updated'] = DateTime
::createFromFormat('Ymd_His', $link['updated']);
345 // If user is not logged in, filter private links.
346 foreach ($toremove as $offset) {
347 unset($this->links
[$offset]);
354 * Saves the database from memory to disk
356 * @throws IOException the datastore is not writable
358 private function write()
360 if (is_file($this->datastore
) && !is_writeable($this->datastore
)) {
361 // The datastore exists but is not writeable
362 throw new IOException($this->datastore
);
363 } else if (!is_file($this->datastore
) && !is_writeable(dirname($this->datastore
))) {
364 // The datastore does not exist and its parent directory is not writeable
365 throw new IOException(dirname($this->datastore
));
370 self
::$phpPrefix.base64_encode(gzdeflate(serialize($this->links
))).self
::$phpSuffix
376 * Saves the database from memory to disk
378 * @param string $pageCacheDir page cache directory
380 public function save($pageCacheDir)
382 if (!$this->loggedIn
) {
383 // TODO: raise an Exception instead
384 die('You are not authorized to change the database.');
389 invalidateCaches($pageCacheDir);
393 * Returns the link for a given URL, or False if it does not exist.
395 * @param string $url URL to search for
397 * @return mixed the existing link if it exists, else 'false'
399 public function getLinkFromUrl($url)
401 if (isset($this->urls
[$url])) {
402 return $this->links
[$this->urls
[$url]];
408 * Returns the shaare corresponding to a smallHash.
410 * @param string $request QUERY_STRING server parameter.
412 * @return array $filtered array containing permalink data.
414 * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link.
416 public function filterHash($request)
418 $request = substr($request, 0, 6);
419 $linkFilter = new LinkFilter($this->links
);
420 return $linkFilter->filter(LinkFilter
::$FILTER_HASH, $request);
424 * Returns the list of articles for a given day.
426 * @param string $request day to filter. Format: YYYYMMDD.
428 * @return array list of shaare found.
430 public function filterDay($request) {
431 $linkFilter = new LinkFilter($this->links
);
432 return $linkFilter->filter(LinkFilter
::$FILTER_DAY, $request);
436 * Filter links according to search parameters.
438 * @param array $filterRequest Search request content. Supported keys:
439 * - searchtags: list of tags
440 * - searchterm: term search
441 * @param bool $casesensitive Optional: Perform case sensitive filter
442 * @param bool $privateonly Optional: Returns private links only if true.
444 * @return array filtered links, all links if no suitable filter was provided.
446 public function filterSearch($filterRequest = array(), $casesensitive = false, $privateonly = false)
448 // Filter link database according to parameters.
449 $searchtags = !empty($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
450 $searchterm = !empty($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
452 // Search tags + fullsearch.
453 if (! empty($searchtags) && ! empty($searchterm)) {
454 $type = LinkFilter
::$FILTER_TAG | LinkFilter
::$FILTER_TEXT;
455 $request = array($searchtags, $searchterm);
458 elseif (! empty($searchtags)) {
459 $type = LinkFilter
::$FILTER_TAG;
460 $request = $searchtags;
463 elseif (! empty($searchterm)) {
464 $type = LinkFilter
::$FILTER_TEXT;
465 $request = $searchterm;
467 // Otherwise, display without filtering.
473 $linkFilter = new LinkFilter($this);
474 return $linkFilter->filter($type, $request, $casesensitive, $privateonly);
478 * Returns the list of all tags
479 * Output: associative array key=tags, value=0
481 public function allTags()
484 $caseMapping = array();
485 foreach ($this->links
as $link) {
486 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY
) as $tag) {
490 // The first case found will be displayed.
491 if (!isset($caseMapping[strtolower($tag)])) {
492 $caseMapping[strtolower($tag)] = $tag;
493 $tags[$caseMapping[strtolower($tag)]] = 0;
495 $tags[$caseMapping[strtolower($tag)]]++
;
498 // Sort tags by usage (most used tag first)
504 * Returns the list of days containing articles (oldest first)
505 * Output: An array containing days (in format YYYYMMDD).
507 public function days()
510 foreach ($this->links
as $link) {
511 $linkDays[$link['created']->format('Ymd')] = 0;
513 $linkDays = array_keys($linkDays);
520 * Reorder links by creation date (newest first).
522 * Also update the urls and ids mapping arrays.
524 * @param string $order ASC|DESC
526 public function reorder($order = 'DESC')
528 $order = $order === 'ASC' ? -1 : 1;
529 // Reorder array by dates.
530 usort($this->links
, function($a, $b) use ($order) {
531 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
534 $this->urls
= array();
535 $this->ids
= array();
536 foreach ($this->links
as $key => $link) {
537 $this->urls
[$link['url']] = $key;
538 $this->ids
[$link['id']] = $key;
543 * Return the next key for link creation.
544 * E.g. If the last ID is 597, the next will be 598.
546 * @return int next ID.
548 public function getNextId()
550 if (!empty($this->ids
)) {
551 return max(array_keys($this->ids
)) +
1;
557 * Returns a link offset in links array from its unique ID.
559 * @param int $id Persistent ID of a link.
561 * @return int Real offset in local array, or null if doesn't exists.
563 protected function getLinkOffset($id)
565 if (isset($this->ids
[$id])) {
566 return $this->ids
[$id];