*/
class TwitterImport
{
+ public $avatarsizename = 'reasonably_small'; // a Twitter size name for 128x128 px
+ public $avatarsize = 128; // they're square...
+
public function importStatus($status)
{
// Hacktastic: filter out stuff coming from this StatusNet
$source = mb_strtolower(common_config('integration', 'source'));
if (preg_match("/$source/", mb_strtolower($status->source))) {
- common_debug($this->name() . ' - Skipping import of status ' .
- twitter_id($status) . ' with source ' . $source);
+ common_debug(__METHOD__ . ' - Skipping import of status ' .
+ twitter_id($status) . " with source {$source}");
return null;
}
$profile = $this->ensureProfile($status->user);
if (empty($profile)) {
- common_log(LOG_ERR, $this->name() .
- ' - Problem saving notice. No associated Profile.');
+ common_log(LOG_ERR, __METHOD__ . ' - Problem saving notice. No associated Profile.');
return null;
}
if (!empty($n2s)) {
common_log(
LOG_INFO,
- $this->name() .
- " - Ignoring duplicate import: {$statusId}"
+ __METHOD__ . " - Ignoring duplicate import: {$statusId}"
);
return Notice::getKV('id', $n2s->notice_id);
}
}
}
- 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');
$id = $notice->insert();
- if (!$id) {
+ if ($id === false) {
common_log_db_error($notice, 'INSERT', __FILE__);
- common_log(LOG_ERR, $this->name() .
- ' - Problem saving notice.');
+ 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));
*
* @return mixed value the first Profile with that url, or null
*/
- function getProfileByUrl($nickname, $profileurl)
+ protected function getProfileByUrl($nickname, $profileurl)
{
$profile = new Profile();
$profile->nickname = $nickname;
$profile->profileurl = $profileurl;
$profile->limit(1);
- if ($profile->find()) {
- $profile->fetch();
- return $profile;
- }
-
- return null;
- }
-
- /**
- * 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;
+ if (!$profile->find(true)) {
+ throw new NoResultException($profile);
}
-
- return null;
+ return $profile;
}
- function ensureProfile($user)
+ protected function ensureProfile($twuser)
{
// check to see if there's already a profile for this user
- $profileurl = 'http://twitter.com/' . $user->screen_name;
- $profile = $this->getProfileByUrl($user->screen_name, $profileurl);
-
- if (!empty($profile)) {
- common_debug($this->name() .
- " - Profile for $profile->nickname found.");
-
- // Check to see if the user's Avatar has changed
-
- $this->checkAvatar($user, $profile);
+ $profileurl = 'http://twitter.com/' . $twuser->screen_name;
+ try {
+ $profile = $this->getProfileByUrl($twuser->screen_name, $profileurl);
+ $this->updateAvatar($twuser, $profile);
return $profile;
-
- } else {
- common_debug($this->name() . ' - Adding profile and remote profile ' .
+ } catch (NoResultException $e) {
+ common_debug(__METHOD__ . ' - Adding profile and remote profile ' .
"for Twitter user: $profileurl.");
+ }
- $profile = new Profile();
- $profile->query("BEGIN");
-
- $profile->nickname = $user->screen_name;
- $profile->fullname = $user->name;
- $profile->homepage = $user->url;
- $profile->bio = $user->description;
- $profile->location = $user->location;
- $profile->profileurl = $profileurl;
- $profile->created = common_sql_now();
-
- try {
- $id = $profile->insert();
- } catch(Exception $e) {
- common_log(LOG_WARNING, $this->name() . ' Couldn\'t insert profile - ' . $e->getMessage());
- }
+ $profile = new Profile();
+ $profile->query("BEGIN");
+ $profile->nickname = $twuser->screen_name;
+ $profile->fullname = $twuser->name;
+ $profile->homepage = $twuser->url;
+ $profile->bio = $twuser->description;
+ $profile->location = $twuser->location;
+ $profile->profileurl = $profileurl;
+ $profile->created = common_sql_now();
+ try {
+ $id = $profile->insert(); // insert _should_ throw exception on failure
if (empty($id)) {
- common_log_db_error($profile, 'INSERT', __FILE__);
- $profile->query("ROLLBACK");
- return false;
- }
-
- // check for remote profile
-
- $remote_pro = Remote_profile::getKV('uri', $profileurl);
-
- if (empty($remote_pro)) {
- $remote_pro = new Remote_profile();
-
- $remote_pro->id = $id;
- $remote_pro->uri = $profileurl;
- $remote_pro->created = common_sql_now();
-
- try {
- $rid = $remote_pro->insert();
- } catch (Exception $e) {
- common_log(LOG_WARNING, $this->name() . ' Couldn\'t save remote profile - ' . $e->getMessage());
- }
-
- if (empty($rid)) {
- common_log_db_error($profile, 'INSERT', __FILE__);
- $profile->query("ROLLBACK");
- return false;
- }
+ throw new Exception('Failed insert');
}
-
- $profile->query("COMMIT");
-
- $this->saveAvatars($user, $id);
-
- return $profile;
- }
- }
-
- function checkAvatar($twitter_user, $profile)
- {
- global $config;
-
- $newname = 'Twitter_' . $twitter_user->id . '_' . basename($twitter_user->profile_image_url);
-
- $oldname = $profile->getAvatar(48)->filename;
-
- if ($newname != $oldname) {
- common_debug($this->name() . ' - Avatar for Twitter user ' .
- "$profile->nickname has changed.");
- common_debug($this->name() . " - old: $oldname new: $newname");
-
- $this->updateAvatars($twitter_user, $profile);
+ } catch(Exception $e) {
+ common_log(LOG_WARNING, __METHOD__ . " Couldn't insert profile: " . $e->getMessage());
+ common_log_db_error($profile, 'INSERT', __FILE__);
+ $profile->query("ROLLBACK");
+ return false;
}
- if ($this->missingAvatarFile($profile)) {
- common_debug($this->name() . ' - Twitter user ' .
- $profile->nickname .
- ' is missing one or more local avatars.');
- common_debug($this->name() ." - old: $oldname new: $newname");
-
- $this->updateAvatars($twitter_user, $profile);
- }
+ $profile->query("COMMIT");
+ $this->updateAvatar($twuser, $profile);
+ return $profile;
}
- function updateAvatars($twitter_user, $profile) {
-
- global $config;
-
- $path_parts = pathinfo($twitter_user->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
- $mediatype = $this->getMediatype(substr($ext, 1));
-
- foreach (array('mini', 'normal', 'bigger') as $size) {
- $url = $path_parts['dirname'] . '/' .
- $img_root . '_' . $size . $ext;
- $filename = 'Twitter_' . $twitter_user->id . '_' .
- $img_root . '_' . $size . $ext;
+ /*
+ * 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
+ $filename = "Twitter_{$twuser->id}_{$img_root}_{$this->avatarsizename}{$ext}";
- $this->updateAvatar($profile->id, $size, $mediatype, $filename);
- $this->fetchAvatar($url, $filename);
- }
- }
+ try {
+ $avatar = Avatar::getUploaded($profile);
+ 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})");
+ }
+
+ $url = "{$path_parts['dirname']}/{$img_root}_{$this->avatarsizename}{$ext}";
+ $mediatype = $this->getMediatype(mb_substr($ext, 1));
- function missingAvatarFile($profile) {
- foreach (array(24, 48, 73) as $size) {
- $filename = $profile->getAvatar($size)->filename;
- $avatarpath = Avatar::path($filename);
- if (file_exists($avatarpath) == FALSE) {
- return true;
+ try {
+ $this->newAvatar($profile, $url, $filename, $mediatype);
+ } catch (Exception $e) {
+ if (file_exists(Avatar::path($filename))) {
+ unlink(Avatar::path($filename));
}
+ return false;
}
- return false;
+
+ return true;
}
- function getMediatype($ext)
+ protected function getMediatype($ext)
{
$mediatype = null;
return $mediatype;
}
- function saveAvatars($user, $id)
+ protected function newAvatar(Profile $profile, $url, $filename, $mediatype)
{
- global $config;
-
- $path_parts = pathinfo($user->profile_image_url);
- $ext = (isset($path_parts['extension']) ? '.'.$path_parts['extension'] : '');
- $img_root = basename($path_parts['basename'], '_normal'.$ext);
- $mediatype = $this->getMediatype(substr($ext, 1));
-
- foreach (array('mini', 'normal', 'bigger') as $size) {
- $url = $path_parts['dirname'] . '/' .
- $img_root . '_' . $size . $ext;
- $filename = 'Twitter_' . $user->id . '_' .
- $img_root . '_' . $size . $ext;
-
- if ($this->fetchAvatar($url, $filename)) {
- $this->newAvatar($id, $size, $mediatype, $filename);
- } else {
- common_log(LOG_WARNING, $id() .
- " - Problem fetching Avatar: $url");
- }
- }
- }
+ // Clear out old avatars, won't do anything if there are none
+ Avatar::deleteFromProfile($profile);
- function updateAvatar($profile_id, $size, $mediatype, $filename) {
-
- common_debug($this->name() . " - Updating avatar: $size");
-
- $profile = Profile::getKV($profile_id);
-
- if (empty($profile)) {
- common_debug($this->name() . " - Couldn't get profile: $profile_id!");
- return;
- }
-
- $sizes = array('mini' => 24, 'normal' => 48, 'bigger' => 73);
- $avatar = $profile->getAvatar($sizes[$size]);
-
- // Delete the avatar, if present
- if ($avatar) {
- $avatar->delete();
- }
-
- $this->newAvatar($profile->id, $size, $mediatype, $filename);
- }
-
- function newAvatar($profile_id, $size, $mediatype, $filename)
- {
- global $config;
+ // throws exception if unable to fetch
+ $this->fetchRemoteUrl($url, Avatar::path($filename));
$avatar = new Avatar();
- $avatar->profile_id = $profile_id;
-
- switch($size) {
- case 'mini':
- $avatar->width = 24;
- $avatar->height = 24;
- break;
- case 'normal':
- $avatar->width = 48;
- $avatar->height = 48;
- break;
- default:
- // Note: Twitter's big avatars are a different size than
- // StatusNet's (StatusNet's = 96)
- $avatar->width = 73;
- $avatar->height = 73;
- }
-
- $avatar->original = 0; // we don't have the original
- $avatar->mediatype = $mediatype;
- $avatar->filename = $filename;
- $avatar->url = Avatar::url($filename);
+ $avatar->profile_id = $profile->id;
+ $avatar->original = 1; // this is an original/"uploaded" avatar
+ $avatar->mediatype = $mediatype;
+ $avatar->filename = $filename;
+ $avatar->url = Avatar::url($filename);
+ $avatar->width = $this->avatarsize;
+ $avatar->height = $this->avatarsize;
$avatar->created = common_sql_now();
- try {
- $id = $avatar->insert();
- } catch (Exception $e) {
- common_log(LOG_WARNING, $this->name() . ' Couldn\'t insert avatar - ' . $e->getMessage());
- }
+ $id = $avatar->insert();
if (empty($id)) {
+ common_log(LOG_WARNING, __METHOD__ . " Couldn't insert avatar - " . $e->getMessage());
common_log_db_error($avatar, 'INSERT', __FILE__);
- return null;
+ throw new ServerException('Could not insert avatar');
}
- common_debug($this->name() .
- " - Saved new $size avatar for $profile_id.");
+ common_debug(__METHOD__ . " - Saved new avatar for {$profile->id}.");
- return $id;
+ return $avatar;
}
/**
* @param string $filename bare local filename for download
* @return bool true on success, false on failure
*/
- function fetchAvatar($url, $filename)
+ protected function fetchRemoteUrl($url, $filename)
{
- common_debug($this->name() . " - Fetching Twitter avatar: $url");
-
+ common_debug(__METHOD__ . " - Fetching Twitter avatar: {$url} to {$filename}");
$request = HTTPClient::start();
+ $request->setConfig('connect_timeout', 3); // I had problems with throttling
+ $request->setConfig('timeout', 6); // and locking the process sucks.
$response = $request->get($url);
if ($response->isOk()) {
- $avatarfile = Avatar::path($filename);
- $ok = file_put_contents($avatarfile, $response->getBody());
- if (!$ok) {
- common_log(LOG_WARNING, $this->name() .
- " - Couldn't open file $filename");
- return false;
+ if (!file_put_contents($filename, $response->getBody())) {
+ throw new ServerException('Failed saving fetched file');
}
} else {
- return false;
+ throw new Exception('Unexpected HTTP status code');
}
-
return true;
}
$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('/(^|\"\;|\'|\(|\[|\{|\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('/(^|\"\;|\'|\(|\[|\{|\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;
}
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)
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->id);
+ } catch (ServerException $e) {
+ // Could not process attached URL
+ }
}
}
}