]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/lib/twitterimport.php
Merge branch 'tagprofile-ajax-fix' into 'nightly'
[quix0rs-gnu-social.git] / plugins / TwitterBridge / lib / twitterimport.php
index eb58bc7373920302bfb457f1b78d4b957ed62ee9..45b7547ce2fe7e5b79b6fef2bba0f2686eab0cd9 100644 (file)
@@ -31,7 +31,7 @@ if (!defined('STATUSNET')) {
     exit(1);
 }
 
-require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
+require_once dirname(__DIR__) . '/twitter.php';
 
 /**
  * Encapsulation of the Twitter status -> notice incoming bridge import.
@@ -165,12 +165,6 @@ class TwitterImport
             }
         }
 
-        if (empty($notice->conversation)) {
-            $conv = Conversation::create();
-            $notice->conversation = $conv->id;
-            common_log(LOG_INFO, "No known conversation for status {$statusId} so making a new one {$conv->id}.");
-        }
-
         $notice->is_local   = Notice::GATEWAY;
 
         $notice->content  = html_entity_decode($this->linkify($status, FALSE), ENT_QUOTES, 'UTF-8');
@@ -180,11 +174,19 @@ class TwitterImport
 
             $id = $notice->insert();
 
-            if (!$id) {
+            if ($id === false) {
                 common_log_db_error($notice, 'INSERT', __FILE__);
                 common_log(LOG_ERR, __METHOD__ . ' - Problem saving notice.');
             }
 
+            if (empty($notice->conversation)) {
+                $orig = clone($notice);
+                $conv = Conversation::create($notice);
+                common_log(LOG_INFO, "No known conversation for status {$statusId} so a new one ({$conv->id}) was created.");
+                $notice->conversation = $conv->id;
+                $notice->update($orig);
+            }
+
             Event::handle('EndNoticeSave', array($notice));
         }
 
@@ -230,44 +232,19 @@ class TwitterImport
         $profile->profileurl = $profileurl;
         $profile->limit(1);
 
-        if (!$profile->find()) {
+        if (!$profile->find(true)) {
             throw new NoResultException($profile);
         }
-
-        $profile->fetch();
         return $profile;
     }
 
-    /**
-     * Check to see if this Twitter status has already been imported
-     *
-     * @param Profile $profile   Twitter user's local profile
-     * @param string  $statusUri URI of the status on Twitter
-     *
-     * @return mixed value a matching Notice or null
-     */
-    function checkDupe($profile, $statusUri)
-    {
-        $notice = new Notice();
-        $notice->uri = $statusUri;
-        $notice->profile_id = $profile->id;
-        $notice->limit(1);
-
-        if ($notice->find()) {
-            $notice->fetch();
-            return $notice;
-        }
-
-        return null;
-    }
-
     protected function ensureProfile($twuser)
     {
         // check to see if there's already a profile for this user
         $profileurl = 'http://twitter.com/' . $twuser->screen_name;
         try {
             $profile = $this->getProfileByUrl($twuser->screen_name, $profileurl);
-            $this->checkAvatar($twuser, $profile);
+            $this->updateAvatar($twuser, $profile);
             return $profile;
         } catch (NoResultException $e) {
             common_debug(__METHOD__ . ' - Adding profile and remote profile ' .
@@ -296,66 +273,41 @@ class TwitterImport
             return false;
         }
 
-        // check for remote profile
-        $remote_pro = Remote_profile::getKV('uri', $profileurl);
-
-        if (!($remote_pro instanceof Remote_profile)) {
-            $remote_pro = new Remote_profile();
-            $remote_pro->id = $id;
-            $remote_pro->uri = $profileurl;
-            $remote_pro->created = common_sql_now();
-
-            try {
-                $rid = $remote_pro->insert();
-                if (empty($rid)) {
-                    throw new Exception('Failed insert');
-                }
-            } catch (Exception $e) {
-                common_log(LOG_WARNING, __METHOD__ . " Couldn't save remote profile: " . $e->getMessage());
-                common_log_db_error($profile, 'INSERT', __FILE__);
-                $profile->query("ROLLBACK");
-                return false;
-            }
-        }
-
         $profile->query("COMMIT");
         $this->updateAvatar($twuser, $profile);
         return $profile;
     }
 
-    protected function checkAvatar($twuser, Profile $profile)
+    /*
+     * Checks whether we have to update the profile's avatar
+     *
+     * @return true when updated, false on failure, null when no action taken
+     */
+    protected function updateAvatar($twuser, Profile $profile)
     {
         $path_parts = pathinfo($twuser->profile_image_url);
         $ext        = isset($path_parts['extension'])
                         ? '.'.$path_parts['extension']
                         : '';  // some lack extension
         $img_root   = basename($path_parts['basename'], '_normal'.$ext);       // cut off extension
-        $newname = "Twitter_{$twuser->id}_{$img_root}_{$this->avatarsizename}{$ext}";
+        $filename   = "Twitter_{$twuser->id}_{$img_root}_{$this->avatarsizename}{$ext}";
 
         try {
             $avatar = Avatar::getUploaded($profile);
-            $oldname = $avatar->filename;
-            $avatar->free();
-        } catch (Exception $e) {
-            $oldname = null;
+            if ($avatar->filename === $filename) {
+                return null;
+            }
+            common_debug(__METHOD__ . " - Updating profile avatar (profile_id={$profile->id}) " .
+                       "from {$avatar->filename} to {$filename}");
+            // else we continue with creating a new avatar
+        } catch (NoAvatarException $e) {
+            // Avatar was not found. We can catch NoAvatarException or FileNotFoundException
+            // but generally we just want to continue creating a new avatar.
+            common_debug(__METHOD__ . " - No avatar found for (profile_id={$profile->id})");
         }
         
-        if ($newname != $oldname || !Avatar::hasUploaded($profile)) {
-            common_debug(__METHOD__ . " - Avatar for {$profile->nickname} has changed.");
-            $this->updateAvatar($twuser, $profile);
-        }
-    }
-
-    protected function updateAvatar($twuser, Profile $profile)
-    {
-        $path_parts = pathinfo($twuser->profile_image_url);
-        $ext        = isset($path_parts['extension'])
-                        ? '.'.$path_parts['extension']
-                        : '';  // some lack extension
-        $img_root   = basename($path_parts['basename'], '_normal'.$ext);       // cut off extension
         $url        = "{$path_parts['dirname']}/{$img_root}_{$this->avatarsizename}{$ext}";
-        $filename   = "Twitter_{$twuser->id}_{$img_root}_{$this->avatarsizename}{$ext}";
-        $mediatype  = $this->getMediatype(substr($ext, 1));
+        $mediatype  = $this->getMediatype(mb_substr($ext, 1));
 
         try {
             $this->newAvatar($profile, $url, $filename, $mediatype);
@@ -363,7 +315,10 @@ class TwitterImport
             if (file_exists(Avatar::path($filename))) {
                 unlink(Avatar::path($filename));
             }
+            return false;
         }
+
+        return true;
     }
 
     protected function getMediatype($ext)
@@ -453,8 +408,10 @@ class TwitterImport
             $statusId = twitter_id($status);
             common_log(LOG_WARNING, "No entities data for {$statusId}; trying to fake up links ourselves.");
             $text = common_replace_urls_callback($text, 'common_linkify');
-            $text = preg_replace('/(^|\&quot\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/e', "'\\1#'.TwitterStatusFetcher::tagLink('\\2')", $text);
-            $text = preg_replace('/(^|\s+)@([a-z0-9A-Z_]{1,64})/e', "'\\1@'.TwitterStatusFetcher::atLink('\\2')", $text);
+            $text = preg_replace_callback('/(^|\&quot\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/',
+                        function ($m) { return $m[1].'#'.TwitterStatusFetcher::tagLink($m[2]); }, $text);
+            $text = preg_replace_callback('/(^|\s+)@([a-z0-9A-Z_]{1,64})/',
+                        function ($m) { return $m[1].'@'.TwitterStatusFetcher::atLink($m[2]); }, $text);
             return $text;
         }
 
@@ -564,7 +521,7 @@ class TwitterImport
 
     static function tagLink($tag, $orig)
     {
-        return "<a href='https://search.twitter.com/search?q=%23{$tag}' class='hashtag'>{$orig}</a>";
+        return "<a href='https://twitter.com/search?q=%23{$tag}' class='hashtag'>{$orig}</a>";
     }
 
     static function atLink($screenName, $fullName, $orig)
@@ -607,12 +564,16 @@ class TwitterImport
      * @param Notice $notice
      * @param object $status
      */
-    function saveStatusAttachments($notice, $status)
+    function saveStatusAttachments(Notice $notice, $status)
     {
         if (common_config('attachments', 'process_links')) {
             if (!empty($status->entities) && !empty($status->entities->urls)) {
                 foreach ($status->entities->urls as $url) {
-                    File::processNew($url->url, $notice->id);
+                    try {
+                        File::processNew($url->url, $notice);
+                    } catch (ServerException $e) {
+                        // Could not process attached URL
+                    }
                 }
             }
         }