]> 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 d8b9f7a6bb6cb2ef0ee5347bd4986f0934aba221..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));
         }
 
@@ -271,28 +273,6 @@ 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;
@@ -317,17 +297,19 @@ class TwitterImport
             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 (Exception $e) {
-            // Avatar was not found. We can catch NoResultException or FileNotFoundException
+        } 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})");
         }
         
         $url        = "{$path_parts['dirname']}/{$img_root}_{$this->avatarsizename}{$ext}";
         $mediatype  = $this->getMediatype(mb_substr($ext, 1));
 
         try {
-            common_debug(__METHOD__ . " - Updating profile avatar (profile_id={$profile->id} to {$filename}");
             $this->newAvatar($profile, $url, $filename, $mediatype);
         } catch (Exception $e) {
             if (file_exists(Avatar::path($filename))) {
@@ -426,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;
         }
 
@@ -537,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)
@@ -580,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
+                    }
                 }
             }
         }