<commit_before><?php
/**
 * Table Definition for avatar
 */
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';

class Avatar extends Managed_DataObject
{
    const PROFILE_SIZE = 128;
    const STREAM_SIZE  =  96;
    const MINI_SIZE    =  24;

    public $__table = 'avatar';              // table name
    public $profile_id;                      // int(4)  primary_key not_null
    public $original;                        // tinyint(1)
    public $width;                           // int(4)  primary_key not_null
    public $height;                          // int(4)  primary_key not_null
    public $mediatype;                       // varchar(32)   not_null
    public $filename;                        // varchar(255)
    public $url;                             // varchar(255)  unique_key
    public $created;                         // datetime()   not_null
    public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP

    /* Static get */
    function staticGet($k,$v=null)
    { return Memcached_DataObject::staticGet('Avatar',$k,$v); }

    static function pivotGet($keyCol, $keyVals, $otherCols)
    {
        return Memcached_DataObject::pivotGet('Avatar', $keyCol, $keyVals, $otherCols);
    }
    
    public static function schemaDef()
    {
        return array(
            'fields' => array(
                'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
                'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
                'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
                'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
                'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
                'filename' => array('type' => 'varchar', 'length' => 255, 'description' => 'local filename, if local'),
                'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'avatar location'),
                'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
            ),
            'primary key' => array('profile_id', 'width', 'height'),
            'unique keys' => array(
                'avatar_url_key' => array('url'),
            ),
            'foreign keys' => array(
                'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
            ),
            'indexes' => array(
                'avatar_profile_id_idx' => array('profile_id'),
            ),
        );
    }
    // We clean up the file, too

    function delete()
    {
        $filename = $this->filename;
        if (parent::delete()) {
            @unlink(Avatar::path($filename));
        }
    }

    function pkeyGet($kv)
    {
        return Memcached_DataObject::pkeyGet('Avatar', $kv);
    }

    static function getOriginal($profile_id)
    {
        $avatar = new Avatar();
        $avatar->profile_id = $profile_id;
        $avatar->original = true;
        if (!$avatar->find(true)) {
            throw new Exception (_m('No original avatar filename found for profile'));
        }
        return $avatar;
    }
	static function getUrlByProfile(Profile $profile, $width=self::PROFILE_SIZE, $height=null, $fallback=true) {
		$avatar = self::getByProfile($profile, $width, $height);
        if ($avatar) {
            return $avatar->displayUrl();
        } else {
            return self::defaultImage($width);
        }
	}

    static function getByProfile(Profile $profile, $width=self::PROFILE_SIZE, $height=null) {
        if (empty($height)) {
            $height = $width;
        }

        $avatar = null;

        if (Event::handle('StartProfileGetAvatar', array($profile, $width, &$avatar))) {
            $avatar = Avatar::pkeyGet(
                array(
                    'profile_id' => $profile->id,
                    'width'      => $width,
                    'height'     => $height
                )
            ); 
            Event::handle('EndProfileGetAvatar', array($profile, $width, &$avatar));
        }

        if (empty($avatar)) {
            try {
                $avatar = Avatar::newSize($profile->id, $width);
            } catch (Exception $e) {
                common_debug($e->getMessage());
            }
        }

        return $avatar;
    }
    static function getProfileAvatars($profile_id) {
        $avatar = new Avatar();
        $avatar->profile_id = $profile_id;

        return $avatar->fetchAll();
    }

    /**
     * Where should the avatar go for this user?
     */
    static function filename($id, $extension, $size=null, $extra=null)
    {
        if ($size) {
            return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
        } else {
            return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
        }
    }

    static function path($filename)
    {
        $dir = common_config('avatar', 'dir');

        if ($dir[strlen($dir)-1] != '/') {
            $dir .= '/';
        }

        return $dir . $filename;
    }

    static function url($filename)
    {<commit_msg>caching avatar objects in memory for performance<commit_after><?php
/**
 * Table Definition for avatar
 */
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';

class Avatar extends Managed_DataObject
{
    const PROFILE_SIZE = 128;
    const STREAM_SIZE  =  96;
    const MINI_SIZE    =  24;

	private static $avatars = array();

    public $__table = 'avatar';              // table name
    public $profile_id;                      // int(4)  primary_key not_null
    public $original;                        // tinyint(1)
    public $width;                           // int(4)  primary_key not_null
    public $height;                          // int(4)  primary_key not_null
    public $mediatype;                       // varchar(32)   not_null
    public $filename;                        // varchar(255)
    public $url;                             // varchar(255)  unique_key
    public $created;                         // datetime()   not_null
    public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP

    /* Static get */
    function staticGet($k,$v=null)
    { return Memcached_DataObject::staticGet('Avatar',$k,$v); }

    static function pivotGet($keyCol, $keyVals, $otherCols)
    {
        return Memcached_DataObject::pivotGet('Avatar', $keyCol, $keyVals, $otherCols);
    }
    
    public static function schemaDef()
    {
        return array(
            'fields' => array(
                'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
                'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
                'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
                'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
                'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
                'filename' => array('type' => 'varchar', 'length' => 255, 'description' => 'local filename, if local'),
                'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'avatar location'),
                'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
            ),
            'primary key' => array('profile_id', 'width', 'height'),
            'unique keys' => array(
                'avatar_url_key' => array('url'),
            ),
            'foreign keys' => array(
                'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
            ),
            'indexes' => array(
                'avatar_profile_id_idx' => array('profile_id'),
            ),
        );
    }
    // We clean up the file, too

    function delete()
    {
        $filename = $this->filename;
        if (parent::delete()) {
            @unlink(Avatar::path($filename));
        }
    }

    function pkeyGet($kv)
    {
        return Memcached_DataObject::pkeyGet('Avatar', $kv);
    }

    static function getOriginal($profile_id)
    {
        $avatar = new Avatar();
        $avatar->profile_id = $profile_id;
        $avatar->original = true;
        if (!$avatar->find(true)) {
            throw new Exception (_m('No original avatar filename found for profile'));
        }
        return $avatar;
    }
	static function getUrlByProfile(Profile $profile, $width=self::PROFILE_SIZE, $height=null, $fallback=true) {
		$avatar = self::getByProfile($profile, $width, $height);
        if ($avatar) {
            return $avatar->displayUrl();
        } else {
            return self::defaultImage($width);
        }
	}

    static function getByProfile(Profile $profile, $width=self::PROFILE_SIZE, $height=null) {
        if (empty($height)) {
            $height = $width;
        }

		if ($avatar = self::getCached($profile->id, $width, $height)) {
			return $avatar;
		}

        if (Event::handle('StartProfileGetAvatar', array($profile, $width, &$avatar))) {
            $avatar = Avatar::pkeyGet(
                array(
                    'profile_id' => $profile->id,
                    'width'      => $width,
                    'height'     => $height
                )
            ); 
            Event::handle('EndProfileGetAvatar', array($profile, $width, &$avatar));
        }

        if (empty($avatar)) {
            try {
                $avatar = Avatar::newSize($profile->id, $width);
            } catch (Exception $e) {
                common_debug($e->getMessage());
            }
        }
		if (!empty($avatar)) {
			self::setCached($avatar, $profile->id, $width, $height);
		}

        return $avatar;
    }
    static function getProfileAvatars($profile_id) {
        $avatar = new Avatar();
        $avatar->profile_id = $profile_id;

        return $avatar->fetchAll();
    }

	static function getCached($profile_id, $width, $height) {
		if (isset(self::$avatars[$profile_id]["{$width}x{$height}"])) {
			return self::$avatars[$profile_id]["{$width}x{$height}"];
		}
		return null;
	}
	static function setCached($avatar, $profile_id, $width, $height) {
		self::$avatars[$profile_id]["{$width}x{$height}"] = $avatar;
	}

    /**
     * Where should the avatar go for this user?
     */
    static function filename($id, $extension, $size=null, $extra=null)
    {
        if ($size) {
            return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
        } else {
            return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
        }
    }

    static function path($filename)
    {
        $dir = common_config('avatar', 'dir');

        if ($dir[strlen($dir)-1] != '/') {
            $dir .= '/';
        }

        return $dir . $filename;
    }

    static function url($filename)
    {
{'fraction_non_alphanumeric': 0.1594943285132912, 'fraction_numerical': 0.00502207983375184, 'mean_word_length': 1.9486852182792953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7537095', 'n_tokens_mistral': 3481, 'n_tokens_neox': 3327, 'n_words': 760}