<commit_before><?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System Version 9.00
| Copyright (C) 2002 - 2013 <NAME>
| https://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Project File: Advanced SQL Handling Methods Functions API
| Filename: sqlhandler.inc.php
| Author: PHP-Fusion 8 Development Team
| Coded by : <NAME> (Hien)
| Version : 9.1.1 (please update every commit)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/

// Hierarchy Type 1 - key to index method

/**
 * Hierarchy Id to Category Output
 * Returns cat-id relationships
 * @param      $db
 * @param      $id_col
 * @param      $cat_col
 * @param bool $filter
 * @return array
 */
function dbquery_tree($db, $id_col, $cat_col, $filter = FALSE) {
	$index = array();
	$query = dbquery("SELECT $id_col, $cat_col FROM ".$db." $filter");
	while ($row = dbarray($query)) {
		$id = $row[$id_col];
		$parent_id = $row[$cat_col] === NULL ? "NULL" : $row[$cat_col];
		$index[$parent_id][] = $id;
	}
	return (array) $index;
}

/**
 * Hierarchy Full Data Output
 * Returns cat-id relationships with full data
 * @param      $db
 * @param      $id_col
 * @param      $cat_col
 * @param bool $sql_cond
 * @return array
 */
function dbquery_tree_full($db, $id_col, $cat_col, $filter = FALSE) {
	$data = array();
	$index = array();
	$query = dbquery("SELECT * FROM ".$db." ".$filter."");
	while ($row = dbarray($query)) {
		$id = $row[$id_col];
		$parent_id = $row[$cat_col] === NULL ? "0" : $row[$cat_col];
		$data[$id] = $row;
		$index[$parent_id][$id] = $row;
	}
	return (array) $index;
}

/**
 * Get Tree Root ID of a Child
 * @param array $index
 * @param       $child_id
 * @return int
 */
function get_root(array $index, $child_id) {<commit_msg>Add new tree_index function : convert results from dbquery_tree_full to results of dbquery_tree. There are several functions from this file that runs on indexed results, and there is no need to make another full database query running dbquery_tree.<commit_after><?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System Version 9.00
| Copyright (C) 2002 - 2013 <NAME>
| https://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Project File: Advanced SQL Handling Methods Functions API
| Filename: sqlhandler.inc.php
| Author: PHP-Fusion 8 Development Team
| Coded by : <NAME> (Hien)
| Version : 9.1.1 (please update every commit)
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/

// Hierarchy Type 1 - key to index method

/**
 * Hierarchy Id to Category Output
 * Returns cat-id relationships
 * @param      $db
 * @param      $id_col
 * @param      $cat_col
 * @param bool $filter
 * @return array
 */
function dbquery_tree($db, $id_col, $cat_col, $filter = FALSE) {
	$index = array();
	$query = dbquery("SELECT $id_col, $cat_col FROM ".$db." $filter");
	while ($row = dbarray($query)) {
		$id = $row[$id_col];
		$parent_id = $row[$cat_col] === NULL ? "NULL" : $row[$cat_col];
		$index[$parent_id][] = $id;
	}
	return (array) $index;
}

/**
 * Hierarchy Full Data Output
 * Returns cat-id relationships with full data
 * @param      $db
 * @param      $id_col
 * @param      $cat_col
 * @param bool $sql_cond
 * @return array
 */
function dbquery_tree_full($db, $id_col, $cat_col, $filter = FALSE) {
	$data = array();
	$index = array();
	$query = dbquery("SELECT * FROM ".$db." ".$filter."");
	while ($row = dbarray($query)) {
		$id = $row[$id_col];
		$parent_id = $row[$cat_col] === NULL ? "0" : $row[$cat_col];
		$data[$id] = $row;
		$index[$parent_id][$id] = $row;
	}
	return (array) $index;
}

/**
 * Get index information from dbquery_tree_full.
 * @param $data  - array generated from dbquery_tree_full();
 * @return array
 */
function tree_index($data) {
	$list = array();
	if (!empty($data)) {
		foreach($data as $arr=>$value) {
			$list[$arr] = array_keys($value);
		}
	}
	return $list;
}

/**
 * Get Tree Root ID of a Child from dbquery_tree() result
 * @param array $index
 * @param       $child_id
 * @return int
 */
function get_root(array $index, $child_id) {
{'fraction_non_alphanumeric': 0.2261596655385228, 'fraction_numerical': 0.006768863229145929, 'mean_word_length': 2.578347578347578, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1524396', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1544, 'n_words': 501}