<commit_before><?php
namespace Iekadou\Quickies;

abstract class BaseModel
{
    const _cn = "Iekadou\\Quickies\\BaseModel";

    protected $data = array();
    protected $db_connection = null;
    protected $id = null;
    protected $fields = array();
    public $errors = null;

    protected function _pre_construct()
    {
    }

    public function __construct()
    {
        $this->_pre_construct();
        global $DB_CONNECTOR;
        if (!isset($DB_CONNECTOR)) {
            $DB_CONNECTOR = new DBConnector();
        }
        $this->db_connection = $DB_CONNECTOR;
        if ($this->db_connection->get_connect_errno()) {
            $this->errors[] = "db";
        }
        foreach($this->fields as $field_name => $field) {
            if (isset($field['default'])) {
                $this->data[$field_name] = $field['default'];
            } else {
                $this->data[$field_name] = "";
            }
        }
        $this->data['deleted_files'] = array();
    }

    public function __isset($field_name) {
        return $field_name == '_cn' || isset($this->id) || isset($this->data[$field_name]) || isset($this->data[$field_name.'_id']) || method_exists($this, 'get_'.$field_name) ||
            ((strpos($field_name, '_display') > 0) && isset($this->data[substr($field_name, 0, strpos($field_name, '_display'))])) ||
            ((strpos($field_name, '_file_list') > 0) && isset($this->data[substr($field_name, 0, strpos($field_name, '_file_list'))]));
    }

    public function __get($field_name) {
        if ($field_name == '_cn') {
            return self::_cn;
        }
        if ($field_name == 'id') {
            return $this->id;
        }
        if ($field_name == in_array($field_name, array_keys($this->fields)) || method_exists($this, 'get_'.$field_name)) {
            $method_name = 'get_' . $field_name;
            if (method_exists($this, $method_name)) {
                return $this->$method_name();
            }
            $field = $this->fields[$field_name]['type'];
            if ($field == "Text") {
                return;
            } else {
                $field = new $field($this->fields[$field_name]);
            }
            return $field->_get($this, $field_name);
        } else if ($field_name == in_array($field_name.'_id', array_keys($this->fields))) {
            $field_name .= '_id';
            $method_name = 'get_' . $field_name;
            if (method_exists($this, $method_name)) {
                return $this->$method_name();
            }
            $field = $this->fields[$field_name]['type'];
            $field = new $field($this->fields[$field_name]);
            $id = $field->_get($this, $field_name);
            return _i($this->fields[$field_name]['foreign_type'])->get($id);
        } else if (strpos($field_name, '_display') > 0) {
            $field_name = substr($field_name, 0, strpos($field_name, '_display'));
            $choices = _i($this->fields[$field_name]['choices']);
            return $choices::get_by_id($this->$field_name)[1];
        } else if (strpos($field_name, '_file_list') > 0) {
            $field_name = substr($field_name, 0, strpos($field_name, '_file_list'));
            $files = preg_split('/;/', $this->$field_name);
            if ($files[0] == '') {
                return array();
            }
            $result = array();
            foreach ($files as $file) {
                $re = '/\[(?P<file>[\s\S]+)\](?P<path>[\s\S]+)/';
                preg_match($re, $file, $matches);
                $result[] = array("file" => $matches['file'], "path" => $matches['path']);
            }
            return $result;
        } else {
            return $this->$field_name;<commit_msg>adding DB_CACHING functionality to cache related fields<commit_after><?php
namespace Iekadou\Quickies;

abstract class BaseModel
{
    const _cn = "Iekadou\\Quickies\\BaseModel";

    protected $data = array();
    protected $db_connection = null;
    protected $id = null;
    protected $fields = array();
    protected $cached_fields = array();
    public $errors = null;

    protected function _pre_construct()
    {
    }

    public function __construct()
    {
        $this->_pre_construct();
        global $DB_CONNECTOR;
        if (!isset($DB_CONNECTOR)) {
            $DB_CONNECTOR = new DBConnector();
        }
        $this->db_connection = $DB_CONNECTOR;
        if ($this->db_connection->get_connect_errno()) {
            $this->errors[] = "db";
        }
        foreach($this->fields as $field_name => $field) {
            if (isset($field['default'])) {
                $this->data[$field_name] = $field['default'];
            } else {
                $this->data[$field_name] = "";
            }
        }
        $this->data['deleted_files'] = array();
    }

    public function __isset($field_name) {
        return $field_name == '_cn' || isset($this->id) || isset($this->data[$field_name]) || isset($this->data[$field_name.'_id']) || method_exists($this, 'get_'.$field_name) ||
            ((strpos($field_name, '_display') > 0) && isset($this->data[substr($field_name, 0, strpos($field_name, '_display'))])) ||
            ((strpos($field_name, '_file_list') > 0) && isset($this->data[substr($field_name, 0, strpos($field_name, '_file_list'))]));
    }

    public function __get($field_name) {
        if ($field_name == '_cn') {
            return self::_cn;
        }
        if ($field_name == 'id') {
            return $this->id;
        }
        if ($field_name == in_array($field_name, array_keys($this->fields)) || method_exists($this, 'get_'.$field_name)) {
            $method_name = 'get_' . $field_name;
            if (method_exists($this, $method_name)) {
                return $this->$method_name();
            }
            $field = $this->fields[$field_name]['type'];
            if ($field == "Text") {
                return;
            } else {
                $field = new $field($this->fields[$field_name]);
            }
            return $field->_get($this, $field_name);
        } else if (!empty($field_name) && $field_name == in_array($field_name.'_id', array_keys($this->fields))) {
            $field_name .= '_id';
            $method_name = 'get_' . $field_name;
            if (method_exists($this, $method_name)) {
                return $this->$method_name();
            }
            $field = $this->fields[$field_name]['type'];
            $field = new $field($this->fields[$field_name]);
            $id = $field->_get($this, $field_name);
            if (DB_CACHING) {
                if (isset($this->cached_fields[$field_name])) {
                    return $this->cached_fields[$field_name];
                }
            }
            $result = _i($this->fields[$field_name]['foreign_type'])->get($id);
            if (DB_CACHING) {
                $this->cached_fields[$field_name] = $result;
            }
            return $result;
        } else if (strpos($field_name, '_display') > 0) {
            $field_name = substr($field_name, 0, strpos($field_name, '_display'));
            $choices = _i($this->fields[$field_name]['choices']);
            return $choices::get_by_id($this->$field_name)[1];
        } else if (strpos($field_name, '_file_list') > 0) {
            $field_name = substr($field_name, 0, strpos($field_name, '_file_list'));
            $files = preg_split('/;/', $this->$field_name);
            if ($files[0] == '') {
                return array();
            }
            $result = array();
            foreach ($files as $file) {
                $re = '/\[(?P<file>[\s\S]+)\](?P<path>[\s\S]+)/';
                preg_match($re, $file, $matches);
                $result[] = array("file" => $matches['file'], "path" => $matches['path']);
            }
            return $result;
        } else {
            return $this->$field_name;
{'fraction_non_alphanumeric': 0.17970240366272416, 'fraction_numerical': 0.002543558438255119, 'mean_word_length': 2.118160190325139, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 86, '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': '345713', 'n_tokens_mistral': 2394, 'n_tokens_neox': 2362, 'n_words': 417}