From fb94a16217bd7c944530442614dd9c00abf9d5e7 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sun, 6 Oct 2013 15:54:06 +0200 Subject: [PATCH] Moved Avatar retrieval into Avatar class Backwards compatible functions are still in Profile class. --- actions/avatarbynickname.php | 2 +- actions/avatarsettings.php | 4 +- actions/foaf.php | 2 +- classes/Avatar.php | 71 ++++++++++++++++++--- classes/Profile.php | 41 +----------- lib/activityobject.php | 10 +-- lib/avatarlink.php | 5 +- lib/noavatarexception.php | 38 +++++++++++ lib/noresultexception.php | 2 +- plugins/TwitterBridge/lib/twitterimport.php | 4 +- 10 files changed, 114 insertions(+), 65 deletions(-) create mode 100644 lib/noavatarexception.php diff --git a/actions/avatarbynickname.php b/actions/avatarbynickname.php index 0922bd47ec..c357cdf17e 100644 --- a/actions/avatarbynickname.php +++ b/actions/avatarbynickname.php @@ -75,7 +75,7 @@ class AvatarbynicknameAction extends Action try { $avatar = Avatar::getUploaded($profile); $url = $avatar->displayUrl(); - } catch (Exception $e) { + } catch (NoAvatarException $e) { $url = Avatar::defaultImage(AVATAR_PROFILE_SIZE); } } else { diff --git a/actions/avatarsettings.php b/actions/avatarsettings.php index bc671c621b..76974265e6 100644 --- a/actions/avatarsettings.php +++ b/actions/avatarsettings.php @@ -136,7 +136,7 @@ class AvatarsettingsAction extends SettingsAction 'alt' => $user->nickname)); $this->elementEnd('div'); $this->elementEnd('li'); - } catch (NoResultException $e) { + } catch (NoAvatarException $e) { // No original avatar found! } @@ -157,7 +157,7 @@ class AvatarsettingsAction extends SettingsAction $this->submit('delete', _m('BUTTON','Delete')); } $this->elementEnd('li'); - } catch (Exception $e) { + } catch (NoAvatarException $e) { // No previously uploaded avatar to preview. } diff --git a/actions/foaf.php b/actions/foaf.php index 9bb1df61c6..e0662aa2fa 100644 --- a/actions/foaf.php +++ b/actions/foaf.php @@ -157,7 +157,7 @@ class FoafAction extends Action } $this->elementEnd('Image'); $this->elementEnd('img'); - } catch (Exception $e) { + } catch (NoAvatarException $e) { // No avatar for this user! } diff --git a/classes/Avatar.php b/classes/Avatar.php index 3eada14c97..c7f6a56b04 100644 --- a/classes/Avatar.php +++ b/classes/Avatar.php @@ -74,26 +74,69 @@ class Avatar extends Managed_DataObject } $avatar->delete(); } - } catch (NoResultException $e) { + } catch (NoAvatarException $e) { // There are no avatars to delete, a sort of success. } return true; } + static protected $_avatars = array(); + + /* + * Get an avatar by profile. Currently can't call newSize with $height + */ + public static function byProfile(Profile $target, $width=null, $height=null) + { + $width = (int) floor($width); + $height = !is_null($height) ? (int) floor($height) : null; + if (is_null($height)) { + $height = $width; + } + + $size = "{$width}x{$height}"; + if (!isset(self::$_avatars[$target->id])) { + self::$_avatars[$target->id] = array(); + } elseif (isset(self::$_avatars[$target->id][$size])){ + return self::$_avatars[$target->id][$size]; + } + + $avatar = null; + if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) { + $avatar = self::pkeyGet( + array( + 'profile_id' => $target->id, + 'width' => $width, + 'height' => $height, + ) + ); + Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar)); + } + + if (is_null($avatar)) { + // Obviously we can't find an avatar, so let's resize the original! + $avatar = Avatar::newSize($target, $width); + } elseif (!($avatar instanceof Avatar)) { + throw new NoAvatarException($target, $avatar); + } + + self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar; + return $avatar; + } + public static function getUploaded(Profile $target) { $avatar = new Avatar(); $avatar->profile_id = $target->id; $avatar->original = true; if (!$avatar->find(true)) { - throw new NoResultException($avatar); + throw new NoAvatarException($target, $avatar); } if (!file_exists(Avatar::path($avatar->filename))) { // The delete call may be odd for, say, unmounted filesystems // that cause a file to currently not exist, but actually it does... $avatar->delete(); - throw new FileNotFoundException(Avatar::path($avatar->filename)); + throw new NoAvatarException($target, $avatar); } return $avatar; } @@ -102,7 +145,7 @@ class Avatar extends Managed_DataObject $avatar = new Avatar(); $avatar->profile_id = $target->id; if (!$avatar->find()) { - throw new NoResultException($avatar); + throw new NoAvatarException($target, $avatar); } return $avatar->fetchAll(); } @@ -174,6 +217,14 @@ class Avatar extends Managed_DataObject } } + static function urlByProfile(Profile $target, $width=null, $height=null) { + try { + return self::byProfile($target, $width, $height)->displayUrl(); + } catch (Exception $e) { + return self::defaultImage($width); + } + } + static function defaultImage($size) { static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile', @@ -182,9 +233,9 @@ class Avatar extends Managed_DataObject return Theme::path('default-avatar-'.$sizenames[$size].'.png'); } - static function newSize(Profile $target, $size) { - $size = floor($size); - if ($size < 1 || $size > common_config('avatar', 'maxsize')) { + static function newSize(Profile $target, $width) { + $width = (int) floor($width); + if ($width < 1 || $width > common_config('avatar', 'maxsize')) { // TRANS: An error message when avatar size is unreasonable throw new Exception(_m('Avatar size too large')); } @@ -192,12 +243,12 @@ class Avatar extends Managed_DataObject $original = Avatar::getUploaded($target); $imagefile = new ImageFile($target->id, Avatar::path($original->filename)); - $filename = $imagefile->resize($size); + $filename = $imagefile->resize($width); $scaled = clone($original); $scaled->original = false; - $scaled->width = $size; - $scaled->height = $size; + $scaled->width = $width; + $scaled->height = $width; $scaled->url = Avatar::url($filename); $scaled->filename = $filename; $scaled->created = common_sql_now(); diff --git a/classes/Profile.php b/classes/Profile.php index 1f8e7fc882..c0af2635a7 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -120,38 +120,7 @@ class Profile extends Managed_DataObject public function getAvatar($width, $height=null) { - $width = (int) floor($width); - - if (is_null($height)) { - $height = $width; - } - - if (isset($this->_avatars[$width])) { - return $this->_avatars[$width]; - } - - if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) { - $avatar = Avatar::pkeyGet( - array( - 'profile_id' => $this->id, - 'width' => $width, - 'height' => $height - ) - ); - Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar)); - } - - if (is_null($avatar)) { - // Obviously we can't find an avatar, so let's resize the original! - $avatar = Avatar::newSize($this, $width); - } elseif (!($avatar instanceof Avatar)) { - throw new Exception('Bad Avatar retrieved'); - } - - // cache the avatar for future use - $this->_avatars[$width] = $avatar; - - return $avatar; + return Avatar::byProfile($this, $width, $height); } public function setOriginal($filename) @@ -574,13 +543,7 @@ class Profile extends Managed_DataObject function avatarUrl($size=AVATAR_PROFILE_SIZE) { - $size = floor($size); - try { - $avatar = $this->getAvatar($size); - return $avatar->displayUrl(); - } catch (Exception $e) { - return Avatar::defaultImage($size); - } + return Avatar::urlByProfile($this, $size); } function getSubscribed($offset=0, $limit=null) diff --git a/lib/activityobject.php b/lib/activityobject.php index aa9eb1837b..d2a4f0f838 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -460,9 +460,9 @@ class ActivityObject $object->link = $profile->profileurl; try { - $orig = Avatar::getUploaded($profile); - $object->avatarLinks[] = AvatarLink::fromAvatar($orig); - } catch (Exception $e) { + $avatar = Avatar::getUploaded($profile); + $object->avatarLinks[] = AvatarLink::fromAvatar($avatar); + } catch (NoAvatarException $e) { // Could not find an original avatar to link } @@ -475,9 +475,9 @@ class ActivityObject foreach ($sizes as $size) { $alink = null; try { - $avatar = $profile->getAvatar($size); + $avatar = Avatar::byProfile($profile, $size); $alink = AvatarLink::fromAvatar($avatar); - } catch (Exception $e) { + } catch (NoAvatarException $e) { $alink = new AvatarLink(); $alink->type = 'image/png'; $alink->height = $size; diff --git a/lib/avatarlink.php b/lib/avatarlink.php index 7d4256d6e1..6e3f050960 100644 --- a/lib/avatarlink.php +++ b/lib/avatarlink.php @@ -58,11 +58,8 @@ class AvatarLink } } - static function fromAvatar($avatar) + static function fromAvatar(Avatar $avatar) { - if (empty($avatar)) { - return null; - } $alink = new AvatarLink(); $alink->type = $avatar->mediatype; $alink->height = $avatar->height; diff --git a/lib/noavatarexception.php b/lib/noavatarexception.php new file mode 100644 index 0000000000..1f90aaf71c --- /dev/null +++ b/lib/noavatarexception.php @@ -0,0 +1,38 @@ +. + * + * @category Exception + * @package GNUSocial + * @author Mikael Nordfeldth + * @copyright 2013 Free Software Foundation, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://www.gnu.org/software/social/ + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +class NoAvatarException extends NoResultException +{ + public function __construct(Profile $target, Avatar $avatar) + { + parent::__construct($avatar); + } +} diff --git a/lib/noresultexception.php b/lib/noresultexception.php index 61e1a1f75a..6eb83b89d2 100644 --- a/lib/noresultexception.php +++ b/lib/noresultexception.php @@ -34,6 +34,6 @@ class NoResultException extends ServerException public function __construct(DB_DataObject $obj) { // We could log an entry here with the search parameters - parent::__construct(_('No result found on lookup.')); + parent::__construct(sprintf(_('No result found on %s lookup.'), get_class($obj))); } } diff --git a/plugins/TwitterBridge/lib/twitterimport.php b/plugins/TwitterBridge/lib/twitterimport.php index 7f27818d89..e6ddc3b3ac 100644 --- a/plugins/TwitterBridge/lib/twitterimport.php +++ b/plugins/TwitterBridge/lib/twitterimport.php @@ -320,8 +320,8 @@ class TwitterImport 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})"); }