<commit_before><?php

namespace Articles;

/*
Articles at the base directory of site data don't have a category.
Article is a folder with an article.json definition file and other content used by the article

Article definition file (article.json):

{
	"languages" = {
		"en" = {
			"title" = "English article Title",
			"description" = "Short description (english)",
			"file" = "English article file",
			"keywords" = "comma,separated,keywords" // optional
		},
		"pt" = {
			"title" =  "Portuguese article title",
			"description" = "Short description (portuguese)",
			"file" = "Portuguese article file",
		},
		etc...
	}
	"published" = "mm-dd-yyyy date to publish" // optional
	"editeded" = "mm-dd-yyyy date of last edit" // optional
	"author" = "Article author"
}
*/

class Article {

	private $database;

	public $id;
	public $category;
	public $lang;
	public $title;
	public $author;
	public $description;
	public $file;
	public $keywords;
	public $published;
	public $edited;



	public function __construct(DataBase $db) {
		$this->database = $db;
	}

}

/*
Category definition:
Organize articles inside distict categories, using filesystem folders.

A folder that represents a category must have the category.json file, with the following information

{
	"languages" = {
		"en" = "English category name",
		"pt" = "Portuguese categoy name",
		etc...
	}
	
}

*/

class Category {

	private $database;
	public $id;
	public $parent;
	public $identifier;
	public $lang;
	public $name;

	public function __construct(DataBase $db) {
		$this->database = $db;
	}
	
	/**
	 * Returns a Generator that generates Category objects from categories that are immediate children of this category.
	 * @param string $language language to get the 'name' property from
	 * @return \Generator
	 */
	public function subCategories() {
		return $this->database->categories($this->id);
	}

	/**
	 * Returns a Generator that returns Article objects from this Category
	 * @param string $order how to sort the articles
	 * @param string $limit how much articles to fetch
	 * @return \Generator
	 */
	public function articles( $order = NULL, int $limit = 0, int $start = 0) {
		$pdo = $this->database->PDO();
		$lang = $this->database->getLanguage();
		if ($order !== NULL) $order = "ORDER BY ".$order;
		$strlimit = "";
		if ($limit > 0) $strlimit .= "LIMIT $limit ";
		if ($start > 0) $strlimit .= "OFFSET $start";
		$res = $pdo->query("SELECT * FROM articles WHERE lang == \"$lang\" AND category == $this->id $order $strlimit;");
		if ($res === FALSE) return;
		while ($o = $res->fetchObject("Articles\Article",[$this->database])) {
			yield $o;
		}
	}

	/**
	 * Returns a Generator that returns Article objects from this Category and Sub-Categories
	 * @param string $order how to sort the articles
	 * @param string $limit how much articles to fetch
	 * @return \Generator
	 */
	public function articlesRecursive( $order = NULL, int $limit = 0, int $start = 0) {
		$pdo = $this->database->PDO();
		$lang = $this->database->getLanguage();
		if ($order !== NULL) $order = "ORDER BY ".$order;
		$strlimit = "";
		if ($limit > 0) $strlimit .= "LIMIT $limit ";
		if ($start > 0) $strlimit .= "OFFSET $start";
		$res = $pdo->query("WITH RECURSIVE pcat(category) AS (
				VALUES($this->id)
				UNION ALL SELECT ci.id from categories_indexex ci, pcat
				WHERE ci.parent = pcat.category LIMIT 100
			)
			SELECT * FROM articles WHERE lang == \"$lang\" AND category IN pcat $order $strlimit;");
		if ($res === FALSE) return;
		while ($o = $res->fetchObject("Articles\Article",[$this->database])) {
			yield $o;
		}<commit_msg>Corrected mistyped table name at SQL query for Category.articlesRecursive<commit_after><?php

namespace Articles;

/*
Articles at the base directory of site data don't have a category.
Article is a folder with an article.json definition file and other content used by the article

Article definition file (article.json):

{
	"languages" = {
		"en" = {
			"title" = "English article Title",
			"description" = "Short description (english)",
			"file" = "English article file",
			"keywords" = "comma,separated,keywords" // optional
		},
		"pt" = {
			"title" =  "Portuguese article title",
			"description" = "Short description (portuguese)",
			"file" = "Portuguese article file",
		},
		etc...
	}
	"published" = "mm-dd-yyyy date to publish" // optional
	"editeded" = "mm-dd-yyyy date of last edit" // optional
	"author" = "Article author"
}
*/

class Article {

	private $database;

	public $id;
	public $category;
	public $lang;
	public $title;
	public $author;
	public $description;
	public $file;
	public $keywords;
	public $published;
	public $edited;



	public function __construct(DataBase $db) {
		$this->database = $db;
	}

}

/*
Category definition:
Organize articles inside distict categories, using filesystem folders.

A folder that represents a category must have the category.json file, with the following information

{
	"languages" = {
		"en" = "English category name",
		"pt" = "Portuguese categoy name",
		etc...
	}
	
}

*/

class Category {

	private $database;
	public $id;
	public $parent;
	public $identifier;
	public $lang;
	public $name;

	public function __construct(DataBase $db) {
		$this->database = $db;
	}
	
	/**
	 * Returns a Generator that generates Category objects from categories that are immediate children of this category.
	 * @param string $language language to get the 'name' property from
	 * @return \Generator
	 */
	public function subCategories() {
		return $this->database->categories($this->id);
	}

	/**
	 * Returns a Generator that returns Article objects from this Category
	 * @param string $order how to sort the articles
	 * @param string $limit how much articles to fetch
	 * @return \Generator
	 */
	public function articles( $order = NULL, int $limit = 0, int $start = 0) {
		$pdo = $this->database->PDO();
		$lang = $this->database->getLanguage();
		if ($order !== NULL) $order = "ORDER BY ".$order;
		$strlimit = "";
		if ($limit > 0) $strlimit .= "LIMIT $limit ";
		if ($start > 0) $strlimit .= "OFFSET $start";
		$res = $pdo->query("SELECT * FROM articles WHERE lang == \"$lang\" AND category == $this->id $order $strlimit;");
		if ($res === FALSE) return;
		while ($o = $res->fetchObject("Articles\Article",[$this->database])) {
			yield $o;
		}
	}

	/**
	 * Returns a Generator that returns Article objects from this Category and all Sub-Categories
	 * @param string $order how to sort the articles
	 * @param string $limit how much articles to fetch
	 * @return \Generator
	 */
	public function articlesRecursive( $order = NULL, int $limit = 0, int $start = 0) {
		$pdo = $this->database->PDO();
		$lang = $this->database->getLanguage();
		if ($order !== NULL) $order = "ORDER BY ".$order;
		$strlimit = "";
		if ($limit > 0) $strlimit .= "LIMIT $limit ";
		if ($start > 0) $strlimit .= "OFFSET $start";
		$res = $pdo->query("WITH RECURSIVE pcat(category) AS (
				VALUES($this->id)
				UNION ALL SELECT ci.id from categories_indexes ci, pcat
				WHERE ci.parent = pcat.category LIMIT 100
			)
			SELECT * FROM articles WHERE lang == \"$lang\" AND category IN pcat $order $strlimit;");
		if ($res === FALSE) return;
		while ($o = $res->fetchObject("Articles\Article",[$this->database])) {
			yield $o;
		}
{'fraction_non_alphanumeric': 0.14421226285400054, 'fraction_numerical': 0.0030244707176244156, 'mean_word_length': 3.702650290885585, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '1114139', 'n_tokens_mistral': 2482, 'n_tokens_neox': 2230, 'n_words': 787}