<commit_before><?php

class Model_Post extends Model_Record
{
    protected $_data;
    protected $_user;

    protected function _getTable() { return 'posts'; }
    protected function _getTableIdFieldname() { return 'post_id'; }
    protected function _getColumns()
    {
        return array('user_id', 'is_active', 'image_url', 'subject', 'body');
    }

    /**
     * @var Model_LocalConfig
     */
    protected $_localConfig;

    public function __construct(Model_LocalConfig $config)
    {
        $this->_localConfig = $config;
    }

    public function getSubject()    { return $this->get('subject'); }
    public function getBody()    { return $this->get('body'); }

    public function getBodyAsHtml()
    {
        $parseDown = new Parsedown();
        $purifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
        $body = $parseDown->text($this->get('body'));
        $body = $purifier->purify($body);

        return $body;
    }

    public function getUserId()     { return $this->get('user_id'); }
    public function getImageUrl()   { return $this->get('image_url'); }
    public function getIsActive()   { return $this->get('is_active'); }
    public function getCreatedAt()   { return $this->get('created_at'); }
    public function voteCount() { return $this->get('vote_count'); }
    public function getUpvotersCsv() { return $this->get('upvoters_csv'); }

    public function getCreatedAtFriendly()
    {
        try {
            return \Carbon\Carbon::parse($this->getCreatedAt())->diffForHumans();
        } catch (Exception $e) {
            return $this->getCreatedAt();
        }
    }

    /**
     * @return Model_User
     */
    public function getUser()
    {
        if (isset($this->_user)) {
            return $this->_user;
        }

        if ($this->getUserId()) {
            $this->_user = $this->_getContainer()->User()->load($this->getUserId());
        }
        return $this->_user;
    }

    public function fetchTags()
    {
        $tags = $this->_getContainer()->Tag()->fetchByPostId($this->getId());
        return $tags;
    }

    public function selectAll()
    {
        $table = $this->_getTable();

        $query = $this->_localConfig->database()->select()
            ->from($table)
            ->joinLeft(array('post_vote' => 'post_vote'),
                'post_vote.post_id = posts.post_id',
                array(
                    'vote_count' => 'COUNT(DISTINCT post_vote_id)',
                    'upvoters_csv' => 'GROUP_CONCAT(DISTINCT voting_user.username)'
                )
            )
            ->joinLeft(array('voting_user' => 'users'),
                'voting_user.user_id = post_vote.voting_user_id',
                array()
            )
            ->order(array("COUNT(DISTINCT post_vote_id) DESC", "posts.created_at DESC"))
            ->group('posts.post_id');

        return $query;
    }

    public function load($entityId)
    {
        $table = $this->_getTable();
        $tableIdFieldname = $this->_getTableIdFieldname();

        $query = $this->_localConfig->database()->select()
            ->from($table)
            ->joinLeft(array('post_vote' => 'post_vote'),
                'post_vote.post_id = posts.post_id',
                array(
                    'vote_count' => 'COUNT(DISTINCT post_vote_id)',
                )
            )
            ->group('posts.post_id')
            ->where("$table.$tableIdFieldname = ?", $entityId);

        $this->_data = $this->_localConfig->database()->fetchRow($query);
        return $this;
    }

    public function fetchAllRecent()
    {
        $recentTimePeriod = $this->_localConfig->getRecentTimePeriod();
        if (! $recentTimePeriod) {
            throw new Exception("Missing recent_time_period in config");
        }

        $query = $this->selectAll()
            ->where("posts.created_at > DATE_SUB(NOW(), INTERVAL $recentTimePeriod)")
            ->where('posts.is_active = 1');
        $results = $this->_localConfig->database()->fetchAll($query);

        return $results;
    }

    public function fetchAllWithAuthor()
    {
        $query = $this->selectAll()
            ->where('posts.is_active = 1')
            ->joinLeft(
                'users',
                "users.user_id = posts.user_id",
                array('name')
            );
        $rows = $this->_localConfig->database()->fetchAll($query);

        $models = array();
        foreach ($rows as $row) {
            $model = $this->_getContainer()->Post()->setData($row);
            $models[] = $model;
        }

        return $models;
    }

    public function fetchByUserId($userId)
    {
        $query = $this->selectAll();
        $query->where('posts.user_id = ?', $userId);
        $query->where('posts.is_active = 1', $userId);
        $rows = $this->_localConfig->database()->fetchAll($query);

        $models = array();
        foreach ($rows as $row) {
            $model = $this->_getContainer()->Post()->setData($row);
            $models[] = $model;
        }

        return $models;
    }

    public function getUrl()
    {
        $url = implode("/", array($this->_localConfig->get('base_url'), "posts", $this->getId(), $this->getSlug()));
        return $url;
    }
<commit_msg>Use getTable function for 'posts' in any sql in this model.<commit_after><?php

class Model_Post extends Model_Record
{
    protected $_data;
    protected $_user;

    protected function _getTable() { return 'posts'; }
    protected function _getTableIdFieldname() { return 'post_id'; }
    protected function _getColumns()
    {
        return array('user_id', 'is_active', 'image_url', 'subject', 'body');
    }

    /**
     * @var Model_LocalConfig
     */
    protected $_localConfig;

    public function __construct(Model_LocalConfig $config)
    {
        $this->_localConfig = $config;
    }

    public function getSubject() { return $this->get('subject'); }
    public function getBody()    { return $this->get('body'); }

    public function getBodyAsHtml()
    {
        $parseDown = new Parsedown();
        $purifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
        $body = $parseDown->text($this->get('body'));
        $body = $purifier->purify($body);

        return $body;
    }

    public function getUserId()      { return $this->get('user_id'); }
    public function getImageUrl()    { return $this->get('image_url'); }
    public function getIsActive()    { return $this->get('is_active'); }
    public function getCreatedAt()   { return $this->get('created_at'); }
    public function voteCount()      { return $this->get('vote_count'); }
    public function getUpvotersCsv() { return $this->get('upvoters_csv'); }

    public function getCreatedAtFriendly()
    {
        try {
            return \Carbon\Carbon::parse($this->getCreatedAt())->diffForHumans();
        } catch (Exception $e) {
            return $this->getCreatedAt();
        }
    }

    /**
     * @return Model_User
     */
    public function getUser()
    {
        if (isset($this->_user)) {
            return $this->_user;
        }

        if ($this->getUserId()) {
            $this->_user = $this->_getContainer()->User()->load($this->getUserId());
        }
        return $this->_user;
    }

    public function fetchTags()
    {
        $tags = $this->_getContainer()->Tag()->fetchByPostId($this->getId());
        return $tags;
    }

    public function selectAll()
    {
        $table = $this->_getTable();

        $query = $this->_localConfig->database()->select()
            ->from($table)
            ->joinLeft(array('post_vote' => 'post_vote'),
                "post_vote.post_id = $table.post_id",
                array(
                    'vote_count' => 'COUNT(DISTINCT post_vote_id)',
                    'upvoters_csv' => 'GROUP_CONCAT(DISTINCT voting_user.username)'
                )
            )
            ->joinLeft(array('voting_user' => 'users'),
                'voting_user.user_id = post_vote.voting_user_id',
                array()
            )
            ->order(array("COUNT(DISTINCT post_vote_id) DESC", $table . ".created_at DESC"))
            ->group($table . '.post_id');

        return $query;
    }

    public function load($entityId)
    {
        $table = $this->_getTable();
        $tableIdFieldname = $this->_getTableIdFieldname();

        $query = $this->_localConfig->database()->select()
            ->from($table)
            ->joinLeft(array('post_vote' => 'post_vote'),
                "post_vote.post_id = $table.post_id",
                array(
                    'vote_count' => 'COUNT(DISTINCT post_vote_id)',
                )
            )
            ->group("$table.post_id")
            ->where("$table.$tableIdFieldname = ?", $entityId);

        $this->_data = $this->_localConfig->database()->fetchRow($query);
        return $this;
    }

    public function fetchAllRecent()
    {
        $table = $this->_getTable();

        $recentTimePeriod = $this->_localConfig->getRecentTimePeriod();
        if (! $recentTimePeriod) {
            throw new Exception("Missing recent_time_period in config");
        }

        $query = $this->selectAll()
            ->where("$table.created_at > DATE_SUB(NOW(), INTERVAL $recentTimePeriod)")
            ->where("$table.is_active = 1");
        $results = $this->_localConfig->database()->fetchAll($query);

        return $results;
    }

    public function fetchAllWithAuthor()
    {
        $table = $this->_getTable();

        $query = $this->selectAll()
            ->where("$table.is_active = 1")
            ->joinLeft(
                'users',
                "users.user_id = $table.user_id",
                array('name')
            );
        $rows = $this->_localConfig->database()->fetchAll($query);

        $models = array();
        foreach ($rows as $row) {
            $model = $this->_getContainer()->Post()->setData($row);
            $models[] = $model;
        }

        return $models;
    }

    public function fetchByUserId($userId)
    {
        $table = $this->_getTable();

        $query = $this->selectAll();
        $query->where("$table.user_id = ?", $userId);
        $query->where("$table.is_active = 1", $userId);
        $rows = $this->_localConfig->database()->fetchAll($query);

        $models = array();
        foreach ($rows as $row) {
            $model = $this->_getContainer()->Post()->setData($row);
            $models[] = $model;
        }

        return $models;
    }

    public function getUrl()
    {
        $url = implode("/", array($this->_localConfig->get('base_url'), "posts", $this->getId(), $this->getSlug()));
        return $url;
    }

{'fraction_non_alphanumeric': 0.16031390134529147, 'fraction_numerical': 0.0005605381165919282, 'mean_word_length': 2.122812135355893, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 180, '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': '5788682', 'n_tokens_mistral': 3165, 'n_tokens_neox': 3061, 'n_words': 556}