<commit_before><?php

//Maakt geen gebruik van oude vertaalmethode


class Articles {

    protected $website_object;
    protected $database_object;

    // Article constants
    const OLDEST_TOP = 1;
    const METAINFO = 2;
    const IMAGES = 2;
    const ARCHIVE = 4;

    /**
     * Constructs the article displayer.
     * @param Website $website_object The website to use.
     * @param Database $database_object Not needed, for backwards compability.
     */
    public function __construct(Website $website_object, Database $database_object = null) {
        $this->website_object = $website_object;
        $this->database_object = $database_object;
        if ($this->database_object == null) {
            $this->database_object = $website_object->get_database();
        }
    }

    // DATA FUNCTIONS

    /**
     * Returns an article with an id. Respects page protection. Id is casted.
     * @param int $id The id of the article
     * @return Article|null The article, or null if it isn't found.
     */
    public function get_article_data($id) {
        try {
            return new Article($id, $this->database_object);
        } catch (InvalidArgumentException $e) {
            return null;
        }
    }

    /**
     * Gets more articles at once. Protected because of it's dangerous
     * $where_clausule, which can be vulnerable to SQL injection.
     * @param type $where_clausule Everything that should come after WHERE.
     * @param type $limit Limit the number of rows.
     * @param type $start Start position of the limit.
     * @return \Article Array of Articles.
     */
    protected function get_articles_data_unsafe($where_clausule = "", $limit = 9, $start = 0, $oldest_top = false) {
        $oDB = $this->database_object; //afkorting
        $oWebsite = $this->website_object; //afkorting
        $logged_in_staff = $oWebsite->logged_in_staff() ? 1 : 0; //ingelogd? (nodig om de juiste artikelen op te halen)
        $limit = (int) $limit; //stuk veiliger
        $sql = "SELECT `artikel_titel`, `artikel_gemaakt`, `artikel_bewerkt`, ";
        $sql.= "`artikel_intro`, `artikel_afbeelding`, `categorie_naam`, ";
        $sql.= "`gebruiker_id`, `gebruiker_naam`, `artikel_gepind`, ";
        $sql.= "`artikel_verborgen`, `artikel_id` FROM `artikel` ";
        $sql.= "LEFT JOIN `categorie` USING (`categorie_id`) ";
        $sql.= "LEFT JOIN `gebruikers` USING (`gebruiker_id`) ";
        if (!$logged_in_staff) {
            $sql.= "WHERE artikel_verborgen = 0 ";
            if (!empty($where_clausule)) {
                $sql.= "AND $where_clausule ";
            }
        } else {
            if (!empty($where_clausule)) {
                $sql.= "WHERE $where_clausule ";
            }
        }

        $sql.= "ORDER BY artikel_gepind DESC, artikel_gemaakt ";
        if (!$oldest_top) {
            $sql.= "DESC ";
        }
        $sql.= "LIMIT $start , $limit";

        $result = $oDB->query($sql);
        if ($result && $oDB->rows($result) > 0) {
            while ($row = $oDB->fetch($result)) {
                $return_value[] = new Article($row[10], $row);
            }
            return $return_value;
        } else {
            return array();
        }
    }

    /**
     * Gets a potentially long list of articles with the given year and
     * category. 0 can be used for both the year and category as a wildcard.
     * @param int $year The year to display.
     * @param int $category_id The category id of the articles.
     * @return \Article List of articles.
     */
    public function get_articles_data_archive($year = -1, $category_id = -1) {
        $year = (int) $year;
        $category_id = (int) $category_id;

        // Set the limit extremely high when viewing the articles of just one
        // year, to prevent strange missing articles at the end of the year.
        $limit = ($year == 0) ? 50 : 500;

        // Add where clausules
        $where_clausules = array();
        if ($year != 0) {
            $where_clausules[] = "YEAR(`artikel_gemaakt`) = $year";
        }
        if ($category_id != 0) {
            $where_clausules[] = "`categorie_id` = $category_id";
        }

        return $this->get_articles_data_unsafe(join(" AND ", $where_clausules), $limit);
    }
    
    /**
     * Gets the latest articles for the given user.
     * @param int $user_id The id of the user.
     * @return \Article List of articles.
     */
    public function get_articles_data_user($user_id) {
        $user_id = (int) $user_id;
        return $this->get_articles_data_unsafe("`gebruiker_id` = $user_id", 5);
    }
    
    /**
     * Gets all articles, optionally from a category.
     * @param int $category_id Id of the category. Set it to 0 to get articles from all categories.
     * @param int $limit Maximum number of articles to return.
     * @return \Article List of articles.
     */<commit_msg>Don't place pinned articles at the top in user profiles<commit_after><?php

//Maakt geen gebruik van oude vertaalmethode


class Articles {

    protected $website_object;
    protected $database_object;

    // Article constants
    const OLDEST_TOP = 1;
    const METAINFO = 2;
    const IMAGES = 2;
    const ARCHIVE = 4;

    /**
     * Constructs the article displayer.
     * @param Website $website_object The website to use.
     * @param Database $database_object Not needed, for backwards compability.
     */
    public function __construct(Website $website_object, Database $database_object = null) {
        $this->website_object = $website_object;
        $this->database_object = $database_object;
        if ($this->database_object == null) {
            $this->database_object = $website_object->get_database();
        }
    }

    // DATA FUNCTIONS

    /**
     * Returns an article with an id. Respects page protection. Id is casted.
     * @param int $id The id of the article
     * @return Article|null The article, or null if it isn't found.
     */
    public function get_article_data($id) {
        try {
            return new Article($id, $this->database_object);
        } catch (InvalidArgumentException $e) {
            return null;
        }
    }

    /**
     * Gets more articles at once. Protected because of it's dangerous
     * $where_clausule, which can be vulnerable to SQL injection.
     * @param type $where_clausule Everything that should come after WHERE.
     * @param type $limit Limit the number of rows.
     * @param type $start Start position of the limit.
     * @return \Article Array of Articles.
     */
    protected function get_articles_data_unsafe($where_clausule = "", $limit = 9, $start = 0, $oldest_top = false, $pinned_top = true) {
        $oDB = $this->database_object; //afkorting
        $oWebsite = $this->website_object; //afkorting
        $logged_in_staff = $oWebsite->logged_in_staff() ? 1 : 0; //ingelogd? (nodig om de juiste artikelen op te halen)
        $limit = (int) $limit; //stuk veiliger
        $sql = "SELECT `artikel_titel`, `artikel_gemaakt`, `artikel_bewerkt`, ";
        $sql.= "`artikel_intro`, `artikel_afbeelding`, `categorie_naam`, ";
        $sql.= "`gebruiker_id`, `gebruiker_naam`, `artikel_gepind`, ";
        $sql.= "`artikel_verborgen`, `artikel_id` FROM `artikel` ";
        $sql.= "LEFT JOIN `categorie` USING (`categorie_id`) ";
        $sql.= "LEFT JOIN `gebruikers` USING (`gebruiker_id`) ";
        if (!$logged_in_staff) {
            $sql.= "WHERE `artikel_verborgen` = 0 ";
            if (!empty($where_clausule)) {
                $sql.= "AND $where_clausule ";
            }
        } else {
            if (!empty($where_clausule)) {
                $sql.= "WHERE $where_clausule ";
            }
        }

        // Sorting conditions
        $sql.= "ORDER BY ";
        if($pinned_top) {
            $sql.= "`artikel_gepind` DESC, ";
        }
        $sql.= "`artikel_gemaakt` ";
        if (!$oldest_top) {
            $sql.= "DESC ";
        }
        
        // Limit
        $sql.= "LIMIT $start , $limit";

        $result = $oDB->query($sql);
        if ($result && $oDB->rows($result) > 0) {
            while ($row = $oDB->fetch($result)) {
                $return_value[] = new Article($row[10], $row);
            }
            return $return_value;
        } else {
            return array();
        }
    }

    /**
     * Gets a potentially long list of articles with the given year and
     * category. 0 can be used for both the year and category as a wildcard.
     * @param int $year The year to display.
     * @param int $category_id The category id of the articles.
     * @return \Article List of articles.
     */
    public function get_articles_data_archive($year = -1, $category_id = -1) {
        $year = (int) $year;
        $category_id = (int) $category_id;

        // Set the limit extremely high when viewing the articles of just one
        // year, to prevent strange missing articles at the end of the year.
        $limit = ($year == 0) ? 50 : 500;

        // Add where clausules
        $where_clausules = array();
        if ($year != 0) {
            $where_clausules[] = "YEAR(`artikel_gemaakt`) = $year";
        }
        if ($category_id != 0) {
            $where_clausules[] = "`categorie_id` = $category_id";
        }

        return $this->get_articles_data_unsafe(join(" AND ", $where_clausules), $limit);
    }
    
    /**
     * Gets the latest articles for the given user.
     * @param int $user_id The id of the user.
     * @return \Article List of articles.
     */
    public function get_articles_data_user($user_id) {
        $user_id = (int) $user_id;
        return $this->get_articles_data_unsafe("`gebruiker_id` = $user_id", 5, 0, false, false);
    }
    
    /**
     * Gets all articles, optionally from a category.
     * @param int $category_id Id of the category. Set it to 0 to get articles from all categories.
     * @param int $limit Maximum number of articles to return.
     * @return \Article List of articles.
     */
{'fraction_non_alphanumeric': 0.12099430967355496, 'fraction_numerical': 0.005091344713986224, 'mean_word_length': 2.2264090177133657, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '101761', 'n_tokens_mistral': 3207, 'n_tokens_neox': 3017, 'n_words': 912}