<commit_before><?php

/**
 * Primitive entity used for working with the entities tables.
 * @abstract
*/
abstract class Entity extends Model
{
    /**
     * @var int Globally unique identifier.
    */
    public $guid;
    /**
     * @var int GUID of the user who authored the entity.
    */
    public $authorGuid;
    /**
     * @var int Unix timestamp of when the entity was authored.
    */
    public $authoredDateTime;
    /**
     * @var string Entity datatype.
    */
    public $entityType;
    
    /**
     * @var array Array of taxonomy instances the entity belongs to.
    */
    public $belongsToTaxonomies;
	
	public function __construct()
	{
		$this->guid = 0;
	}
    
    /**
     * Saves the entity to the database.
     * @param bool $newRevision If false update the current record for the entity, if true create a new record and deactive the current record. Defaults to true.
    */
    public function save($newRevision = true)
    {
		$data = array();
		
        if ($this->guid == 0)
		{
			
		}
		else
		{
			
		}
    }
    
    /**
     * Loads any entity from the database.
     * @param int $entityGuid GUID of the entity to load.
     * @param mixed $type Entity type restrictions. String restricts to a single type an array will restrict to multiple types.
     * @param bool $skipCache When set to true any cached instances of the entity requested will be ignore. Defaults to false.
     * @param bool $storeInCache When set to true a copy of the entity will be saved for later retreival in the cache. Defaults to true.
     * @return Entity
    */
    public static function getByGuid($entityGuid, $type = null, $skipCache = false, $storeInCache = true)
    {
        $ret = self::getByMeta("guid", $entityGuid, 1, 0, $type, $skipCache, $storeInCache);
		if (sizeof($ret) > 0)
			return array_shift($ret);
		return null;
    }
    
    /**
     * Loads entities from the database using the given types.
     * @param int $count Maximum number of entities to return.
     * @param int $offset Offset to start entity listing at.
     * @param mixed $type Entity type restrictions. String restricts to a single type an array will restrict to multiple types.
     * @param bool $skipCache When set to true any cached instances of the entity requested will be ignore. Defaults to false.
     * @param bool $storeInCache When set to true a copy of the entity will be saved for later retreival in the cache. Defaults to true.
     * @return array
    */
    public static function getByType($count = 20, $offset = 0, $type = null, $skipCache = false, $storeInCache = true)
    {
		return self::getByMeta(null, null, $count, $offset, $type, $skipCache, $storeInCache);
    }
    
    /**
     * Loads entities from the database using the given metadata.
     * @param mixed $metaKey Metadata key used for matching. Can also be supplied as an array.
     * @param mixed $metaValue Metadata value used for matching. Can also be supplied as an array.
     * @param int $count Maximum number of entities to return.
     * @param int $offset Offset to start entity listing at.
     * @param mixed $type Entity type restrictions. String restricts to a single type an array will restrict to multiple types.
     * @param bool $skipCache When set to true any cached instances of the entity requested will be ignore. Defaults to false.
     * @param bool $storeInCache When set to true a copy of the entity will be saved for later retreival in the cache. Defaults to true.
     * @return array
    */
    public static function getByMeta($metaKey = null, $metaValue = null, $count = 20, $offset = 0, $type = null, $skipCache = false, $storeInCache = true)
    {
        $query = DB::select('e.guid','e.authorGuid','e.authoredDateTime','e.entityType')->from(array("entities","e"));
		
        if (!is_array($metaKey) && $metaKey != null)
			$metaKey = array($metaKey);
        if (!is_array($metaValue) && $metaValue != null)
			$metaValue = array($metaValue);
        
		$doneGuidMatch = false;
		if (sizeof($metaKey) > 0 && sizeof($metaValue) > 0)
		{
			$guidMatches = array();
			foreach($metaKey as $index => $key)
			{
				if ($metaKey == "guid")
				{
					$guidMatches[] = $metaValue[$index];
					unset($metaKey[$index]);
					unset($metaValue[$index]);
				}
			}
			
			if (sizeof($guidMatches) > 0)
			{
				$query->where_open();
				$i = 0;
				foreach($guidMatches as $match)
				{
					if ($i == 0)
						$query->where('e.guid','=',$match);
					else
						$query->or_where('e.guid','=',$match);
					$i++;
				}
				$query->where_close();
			}
		}
		
		if (sizeof($metaKey) > 0 && sizeof($metaValue) > 0)
		{
			if ($doneGuidMatch)
				$query->and_where_open();
			
			$i = 0;
			foreach($metaKey as $key)
			{
				$query->join(array('entities_meta', 'm_'.$i))->on('guid','=','m_'.$i.'.entityGuid');
				$query->join(array('entities_metakeys', 'mk_'.$i))->on('m_'.$i.'.metaKey','=','mk_'.$i.'.metaKey');
				if ($i == 0)
					$query->where('mk_'.$i.'.metaKeyName','=',$key);
				else
					$query->and_where('mk_'.$i.'.metaKeyName','=',$key);
				$i++;
			}
			
			$i = 0;
			foreach($metaValue as $val)
			{
				$query->and_where('m_'.$i.'.metaValue','=',$val);
				$i++;
			}
			
			if ($doneGuidMatch)
				$query->and_where_close();
		}
		
        if ($count > 0)
            $query->limit($count);
        if ($offset > 0)
            $query->offset($offset);
        if ($type != null)
        {
            if (is_array($type) && sizeof($type) > 0)
            {
                $query->and_where_open();
                $i = 0;
                foreach($type as $typeName)
                {
                    if ($i++ == 0)
                    {
                        $query->where('e.entityType','=',$typeName);<commit_msg>Enhancing entity lookups<commit_after><?php

/**
 * Primitive entity used for working with the entities tables.
 * @abstract
*/
abstract class Entity extends Model
{
    /**
     * @var int Globally unique identifier.
    */
    public $guid;
    /**
     * @var int GUID of the user who authored the entity.
    */
    public $authorGuid;
    /**
     * @var int Unix timestamp of when the entity was authored.
    */
    public $authoredDateTime;
    /**
     * @var string Entity datatype.
    */
    public $entityType;
    
    /**
     * @var array Array of taxonomy instances the entity belongs to.
    */
    public $belongsToTaxonomies;
	
	public function __construct()
	{
		$this->guid = 0;
	}
    
    /**
     * Saves the entity to the database.
     * @param bool $newRevision If false update the current record for the entity, if true create a new record and deactive the current record. Defaults to true.
    */
    public function save($newRevision = true)
    {
	$data = array();
	
        if ($this->guid == 0)
        {
                
        }
        else
        {
                
        }
    }
    
    /**
     * Loads any entity from the database.
     * @param int $entityGuid GUID of the entity to load.
     * @param mixed $type Entity type restrictions. String restricts to a single type an array will restrict to multiple types.
     * @param bool $skipCache When set to true any cached instances of the entity requested will be ignore. Defaults to false.
     * @param bool $storeInCache When set to true a copy of the entity will be saved for later retreival in the cache. Defaults to true.
     * @return Entity
    */
    public static function getByGuid($entityGuid, $type = null, $skipCache = false, $storeInCache = true)
    {
        $ret = self::getByMeta("guid", $entityGuid, 1, 0, $type, $skipCache, $storeInCache);
        if (sizeof($ret) > 0)
                return array_shift($ret);
        return null;
    }
    
    /**
     * Loads entities from the database using the given types.
     * @param int $count Maximum number of entities to return.
     * @param int $offset Offset to start entity listing at.
     * @param mixed $type Entity type restrictions. String restricts to a single type an array will restrict to multiple types.
     * @param bool $skipCache When set to true any cached instances of the entity requested will be ignore. Defaults to false.
     * @param bool $storeInCache When set to true a copy of the entity will be saved for later retreival in the cache. Defaults to true.
     * @return array
    */
    public static function getByType($count = 20, $offset = 0, $type = null, $skipCache = false, $storeInCache = true)
    {
	return self::getByMeta(null, null, $count, $offset, $type, $skipCache, $storeInCache);
    }
    
    /**
     * Loads entities from the database using the given metadata.
     * @param mixed $metaKey Metadata key used for matching. Can also be supplied as an array.
     * @param mixed $metaValue Metadata value used for matching. Can also be supplied as an array.
     * @param int $count Maximum number of entities to return.
     * @param int $offset Offset to start entity listing at.
     * @param mixed $type Entity type restrictions. String restricts to a single type an array will restrict to multiple types.
     * @param bool $skipCache When set to true any cached instances of the entity requested will be ignore. Defaults to false.
     * @param bool $storeInCache When set to true a copy of the entity will be saved for later retreival in the cache. Defaults to true.
     * @return array
    */
    public static function getByMeta($metaKey = null, $metaValue = null, $count = 20, $offset = 0, $type = null, $skipCache = false, $storeInCache = true)
    {
        $query = DB::select('e.guid','e.authorGuid','e.authoredDateTime','e.entityType')->from(array("entities","e"));
		
        if (!is_array($metaKey) && $metaKey != null)
	    $metaKey = array($metaKey);
        if (!is_array($metaValue) && $metaValue != null)
	    $metaValue = array($metaValue);
        
        $doneGuidMatch = false;
        if (sizeof($metaKey) > 0 && sizeof($metaValue) > 0)
        {
            $guidMatches = array();
            foreach($metaKey as $index => $key)
            {
                if ($key == "guid")
                {
                    $guidMatches[] = $metaValue[$index];
                    unset($metaKey[$index]);
                    unset($metaValue[$index]);
                }
            }
            
            if (sizeof($guidMatches) > 0)
            {
                $doneGuidMatch = true;
                $query->where_open();
                $i = 0;
                foreach($guidMatches as $match)
                {
                    if ($i == 0)
                        $query->where('e.guid','=',$match);
                    else
                        $query->or_where('e.guid','=',$match);
                    $i++;
                }
                $query->where_close();
            }
        }
		
        if (sizeof($metaKey) > 0 && sizeof($metaValue) > 0)
        {
            if ($doneGuidMatch)
                $query->and_where_open();
                
            $i = 0;
            foreach($metaKey as $key)
            {
                $query->join(array('entities_meta', 'm_'.$i))->on('guid','=','m_'.$i.'.entityGuid');
                $query->join(array('entities_metakeys', 'mk_'.$i))->on('m_'.$i.'.metaKey','=','mk_'.$i.'.metaKey');
                if ($i == 0)
                    $query->where('mk_'.$i.'.metaKeyName','=',$key);
                else
                    $query->and_where('mk_'.$i.'.metaKeyName','=',$key);
                $i++;
            }
            
            $i = 0;
            foreach($metaValue as $val)
            {
                $query->and_where('m_'.$i.'.metaValue','=',$val);
                $i++;
            }
            
            if ($doneGuidMatch)
                $query->and_where_close();
        }
		
        if ($count > 0)
            $query->limit($count);
        if ($offset > 0)
            $query->offset($offset);
        if ($type != null)
        {
            if (is_array($type) && sizeof($type) > 0)
            {
                $query->and_where_open();
                $i = 0;
                foreach($type as $typeName)
                {
                    if ($i++ == 0)
                    {
                        $query->where('e.entityType','=',$typeName);
{'fraction_non_alphanumeric': 0.12812984259032706, 'fraction_numerical': 0.004241089633798222, 'mean_word_length': 2.0632025980514612, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '2097972', 'n_tokens_mistral': 3811, 'n_tokens_neox': 3521, 'n_words': 1161}