]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/bookmark/LinkDB.php
Merge branch 'v0.11' into stable
[github/shaarli/Shaarli.git] / application / bookmark / LinkDB.php
similarity index 84%
rename from application/LinkDB.php
rename to application/bookmark/LinkDB.php
index 803757cae5c02c1fffed2f5257b57ff798ef5a07..76ba95f0dbd7a5a309ddb2f832f86fe6b5c81273 100644 (file)
@@ -1,4 +1,15 @@
 <?php
+
+namespace Shaarli\Bookmark;
+
+use ArrayAccess;
+use Countable;
+use DateTime;
+use Iterator;
+use Shaarli\Bookmark\Exception\LinkNotFoundException;
+use Shaarli\Exceptions\IOException;
+use Shaarli\FileUtils;
+
 /**
  * Data storage for links.
  *
  *  - private:  Is this link private? 0=no, other value=yes
  *  - tags:     tags attached to this entry (separated by spaces)
  *  - title     Title of the link
- *  - url       URL of the link. Used for displayable links (no redirector, relative, etc.).
- *              Can be absolute or relative.
- *              Relative URLs are permalinks (e.g.'?m-ukcw')
- *  - real_url  Absolute processed URL.
+ *  - url       URL of the link. Used for displayable links.
+ *              Can be absolute or relative in the database but the relative links
+ *              will be converted to absolute ones in templates.
+ *  - real_url  Raw URL in stored in the DB (absolute or relative).
  *  - shorturl  Permalink smallhash
  *
  * Implements 3 interfaces:
@@ -77,19 +88,6 @@ class LinkDB implements Iterator, Countable, ArrayAccess
     // Hide public links
     private $hidePublicLinks;
 
-    // link redirector set in user settings.
-    private $redirector;
-
-    /**
-     * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
-     *
-     * Example:
-     *   anonym.to needs clean URL while dereferer.org needs urlencoded URL.
-     *
-     * @var boolean $redirectorEncode parameter: true or false
-     */
-    private $redirectorEncode;
-
     /**
      * Creates a new LinkDB
      *
@@ -98,21 +96,16 @@ class LinkDB implements Iterator, Countable, ArrayAccess
      * @param string  $datastore        datastore file path.
      * @param boolean $isLoggedIn       is the user logged in?
      * @param boolean $hidePublicLinks  if true all links are private.
-     * @param string  $redirector       link redirector set in user settings.
-     * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
      */
     public function __construct(
         $datastore,
         $isLoggedIn,
-        $hidePublicLinks,
-        $redirector = '',
-        $redirectorEncode = true
+        $hidePublicLinks
     ) {
+
         $this->datastore = $datastore;
         $this->loggedIn = $isLoggedIn;
         $this->hidePublicLinks = $hidePublicLinks;
-        $this->redirector = $redirector;
-        $this->redirectorEncode = $redirectorEncode === true;
         $this->check();
         $this->read();
     }
@@ -137,7 +130,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
         if (!isset($value['id']) || empty($value['url'])) {
             die(t('Internal Error: A link should always have an id and URL.'));
         }
-        if (($offset !== null && ! is_int($offset)) || ! is_int($value['id'])) {
+        if (($offset !== null && !is_int($offset)) || !is_int($value['id'])) {
             die(t('You must specify an integer as a key.'));
         }
         if ($offset !== null && $offset !== $value['id']) {
@@ -247,19 +240,19 @@ class LinkDB implements Iterator, Countable, ArrayAccess
         $this->links = array();
         $link = array(
             'id' => 1,
-            'title'=> t('The personal, minimalist, super-fast, database free, bookmarking service'),
-            'url'=>'https://shaarli.readthedocs.io',
-            'description'=>t(
+            'title' => t('The personal, minimalist, super-fast, database free, bookmarking service'),
+            'url' => 'https://shaarli.readthedocs.io',
+            'description' => t(
                 'Welcome to Shaarli! This is your first public bookmark. '
-                .'To edit or delete me, you must first login.
+                . 'To edit or delete me, you must first login.
 
 To learn how to use Shaarli, consult the link "Documentation" at the bottom of this page.
 
 You use the community supported version of the original Shaarli project, by Sebastien Sauvage.'
             ),
-            'private'=>0,
-            'created'=> new DateTime(),
-            'tags'=>'opensource software',
+            'private' => 0,
+            'created' => new DateTime(),
+            'tags' => 'opensource software',
             'sticky' => false,
         );
         $link['shorturl'] = link_small_hash($link['created'], $link['id']);
@@ -267,12 +260,12 @@ You use the community supported version of the original Shaarli project, by Seba
 
         $link = array(
             'id' => 0,
-            'title'=> t('My secret stuff... - Pastebin.com'),
-            'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
-            'description'=> t('Shhhh! I\'m a private link only YOU can see. You can delete me too.'),
-            'private'=>1,
-            'created'=> new DateTime('1 minute ago'),
-            'tags'=>'secretstuff',
+            'title' => t('My secret stuff... - Pastebin.com'),
+            'url' => 'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
+            'description' => t('Shhhh! I\'m a private link only YOU can see. You can delete me too.'),
+            'private' => 1,
+            'created' => new DateTime('1 minute ago'),
+            'tags' => 'secretstuff',
             'sticky' => false,
         );
         $link['shorturl'] = link_small_hash($link['created'], $link['id']);
@@ -299,7 +292,7 @@ You use the community supported version of the original Shaarli project, by Seba
 
         $toremove = array();
         foreach ($this->links as $key => &$link) {
-            if (! $this->loggedIn && $link['private'] != 0) {
+            if (!$this->loggedIn && $link['private'] != 0) {
                 // Transition for not upgraded databases.
                 unset($this->links[$key]);
                 continue;
@@ -309,29 +302,19 @@ You use the community supported version of the original Shaarli project, by Seba
             sanitizeLink($link);
 
             // Remove private tags if the user is not logged in.
-            if (! $this->loggedIn) {
+            if (!$this->loggedIn) {
                 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
             }
 
-            // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
-            if (!empty($this->redirector) && !startsWith($link['url'], '?')) {
-                $link['real_url'] = $this->redirector;
-                if ($this->redirectorEncode) {
-                    $link['real_url'] .= urlencode(unescape($link['url']));
-                } else {
-                    $link['real_url'] .= $link['url'];
-                }
-            } else {
-                $link['real_url'] = $link['url'];
-            }
+            $link['real_url'] = $link['url'];
 
             $link['sticky'] = isset($link['sticky']) ? $link['sticky'] : false;
 
             // To be able to load links before running the update, and prepare the update
-            if (! isset($link['created'])) {
+            if (!isset($link['created'])) {
                 $link['id'] = $link['linkdate'];
                 $link['created'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['linkdate']);
-                if (! empty($link['updated'])) {
+                if (!empty($link['updated'])) {
                     $link['updated'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['updated']);
                 }
                 $link['shorturl'] = smallHash($link['linkdate']);
@@ -417,12 +400,12 @@ You use the community supported version of the original Shaarli project, by Seba
     /**
      * Filter links according to search parameters.
      *
-     * @param array  $filterRequest Search request content. Supported keys:
+     * @param array  $filterRequest  Search request content. Supported keys:
      *                                - searchtags: list of tags
      *                                - searchterm: term search
-     * @param bool   $casesensitive Optional: Perform case sensitive filter
-     * @param string $visibility    return only all/private/public links
-     * @param string $untaggedonly  return only untagged links
+     * @param bool   $casesensitive  Optional: Perform case sensitive filter
+     * @param string $visibility     return only all/private/public links
+     * @param bool   $untaggedonly   return only untagged links
      *
      * @return array filtered links, all links if no suitable filter was provided.
      */
@@ -432,6 +415,7 @@ You use the community supported version of the original Shaarli project, by Seba
         $visibility = 'all',
         $untaggedonly = false
     ) {
+
         // Filter link database according to parameters.
         $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
         $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
@@ -447,8 +431,8 @@ You use the community supported version of the original Shaarli project, by Seba
     /**
      * Returns the list tags appearing in the links with the given tags
      *
-     * @param array $filteringTags tags selecting the links to consider
-     * @param string $visibility   process only all/private/public links
+     * @param array  $filteringTags tags selecting the links to consider
+     * @param string $visibility    process only all/private/public links
      *
      * @return array tag => linksCount
      */