]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Bookmark/Bookmark.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / plugins / Bookmark / Bookmark.php
index 87715ecad621263fce7dbfd59742a0483907f4f3..777b50f7243d74f7244f84640cf163d902045b57 100644 (file)
@@ -46,13 +46,13 @@ if (!defined('STATUSNET')) {
 class Bookmark extends Memcached_DataObject
 {
     public $__table = 'bookmark'; // table name
-    public $profile_id;           // int(4)  primary_key not_null
-    public $url;                  // varchar(255) primary_key not_null
-    public $title;                // varchar(255)
-    public $description;          // text
-    public $uri;                  // varchar(255)
-    public $url_crc32;            // int(4) not_null
-    public $created;              // datetime
+    public $id;          // char(36) primary_key not_null
+    public $profile_id;  // int(4) not_null
+    public $url;         // varchar(255) not_null
+    public $title;       // varchar(255)
+    public $description; // text
+    public $uri;         // varchar(255)
+    public $created;     // datetime
 
     /**
      * Get an instance by key
@@ -100,13 +100,14 @@ class Bookmark extends Memcached_DataObject
 
     function table()
     {
-        return array('profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
+        return array('id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
+                     'profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
                      'url' => DB_DATAOBJECT_STR,
                      'title' => DB_DATAOBJECT_STR,
                      'description' => DB_DATAOBJECT_STR,
                      'uri' => DB_DATAOBJECT_STR,
-                     'url_crc32' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
-                     'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
+                     'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + 
+                     DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
     }
 
     /**
@@ -128,8 +129,7 @@ class Bookmark extends Memcached_DataObject
 
     function keyTypes()
     {
-        return array('profile_id' => 'K',
-                     'url' => 'K',
+        return array('id' => 'K',
                      'uri' => 'U');
     }
 
@@ -168,36 +168,16 @@ class Bookmark extends Memcached_DataObject
      
     static function getByURL($profile, $url)
     {
-        return self::pkeyGet(array('profile_id' => $profile->id,
-                                   'url' => $url));
-        return null;
-    }
-
-    /**
-     * Get the bookmark that a user made for an URL
-     *
-     * @param Profile $profile Profile to check for
-     * @param integer $crc32   CRC-32 of URL to check for
-     *
-     * @return array Bookmark objects found (usually 1 or 0)
-     */
-     
-    static function getByCRC32($profile, $crc32)
-    {
-        $bookmarks = array();
-
         $nb = new Bookmark();
         
         $nb->profile_id = $profile->id;
-        $nb->url_crc32  = $crc32;
+        $nb->url        = $url;
 
-        if ($nb->find()) {
-            while ($nb->fetch()) {
-                $bookmarks[] = clone($nb);
-            }
+        if ($nb->find(true)) {
+            return $nb;
+        } else {
+            return null;
         }
-
-        return $bookmarks;
     }
 
     /**
@@ -234,16 +214,20 @@ class Bookmark extends Memcached_DataObject
         }
 
         if (is_string($rawtags)) {
-            $rawtags = preg_split('/[\s,]+/', $rawtags);
+            if (empty($rawtags)) {
+                $rawtags = array();
+            } else {
+                $rawtags = preg_split('/[\s,]+/', $rawtags);
+            }
         }
 
         $nb = new Bookmark();
 
+        $nb->id          = UUID::gen();
         $nb->profile_id  = $profile->id;
         $nb->url         = $url;
         $nb->title       = $title;
         $nb->description = $description;
-        $nb->url_crc32   = crc32($nb->url);
 
         if (array_key_exists('created', $options)) {
             $nb->created = $options['created'];
@@ -254,22 +238,8 @@ class Bookmark extends Memcached_DataObject
         if (array_key_exists('uri', $options)) {
             $nb->uri = $options['uri'];
         } else {
-            $dt = new DateTime($nb->created, new DateTimeZone('UTC'));
-
-            // I posit that it's sufficiently impossible
-            // for the same user to generate two CRC-32-clashing
-            // URLs in the same second that this is a safe unique identifier.
-            // If you find a real counterexample, contact me at acct:evan@status.net
-            // and I will publicly apologize for my hubris.
-
-            $created = $dt->format('YmdHis');
-
-            $crc32   = sprintf('%08x', $nb->url_crc32);
-
             $nb->uri = common_local_url('showbookmark',
-                                        array('user' => $profile->id,
-                                              'created' => $created,
-                                              'crc32' => $crc32));
+                                        array('id' => $nb->id));
         }
 
         $nb->insert();
@@ -281,7 +251,8 @@ class Bookmark extends Memcached_DataObject
 
         foreach ($rawtags as $tag) {
             if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
-                if (!array_key_exists('replies', $options)) { // skip if done by caller
+                // skip if done by caller
+                if (!array_key_exists('replies', $options)) {
                     $nickname = mb_substr($tag, 4);
                     $other    = common_relative_profile($profile,
                                                         $nickname);
@@ -307,10 +278,15 @@ class Bookmark extends Memcached_DataObject
 
         // Use user's preferences for short URLs, if possible
 
-        $user = User::staticGet('id', $profile->id);
+        try {
+            $user = User::staticGet('id', $profile->id);
 
-        $shortUrl = File_redirection::makeShort($url, 
-                                                empty($user) ? null : $user);
+            $shortUrl = File_redirection::makeShort($url, 
+                                                    empty($user) ? null : $user);
+        } catch (Exception $e) {
+            // Don't let this stop us.
+            $shortUrl = $url;
+        }
 
         $content = sprintf(_('"%s" %s %s %s'),
                            $title,