<commit_before><?php
/**
 * @file src/Database/DBStructure.php
 */
namespace Friendica\Database;

use Friendica\Core\Config;
use Friendica\Database\DBM;
use dba;

require_once "boot.php";
require_once 'include/dba.php';
require_once 'include/enotify.php';
require_once "include/text.php";

/**
 * @brief This class contain functions for the database management
 *
 * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
 */
class DBStructure {
	/*
	 * Converts all tables from MyISAM to InnoDB
	 */
	public static function convertToInnoDB() {
		$r = q("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `engine` = 'MyISAM' AND `table_schema` = '%s'",
			dbesc(dba::database_name()));

		if (!DBM::is_result($r)) {
			echo t('There are no tables on MyISAM.')."\n";
			return;
		}

		foreach ($r AS $table) {
			$sql = sprintf("ALTER TABLE `%s` engine=InnoDB;", dbesc($table['TABLE_NAME']));
			echo $sql."\n";

			$result = dba::e($sql);
			if (!DBM::is_result($result)) {
				self::printUpdateError($sql);
			}
		}
	}

	/*
	 * send the email and do what is needed to do on update fails
	 *
	 * @param update_id		(int) number of failed update
	 * @param error_message	(str) error message
	 */
	public static function updateFail($update_id, $error_message) {
		$a = get_app();

		//send the administrators an e-mail
		$admin_mail_list = "'".implode("','", array_map(dbesc, explode(",", str_replace(" ", "", $a->config['admin_email']))))."'";
		$adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
			$admin_mail_list
		);

		// No valid result?
		if (!DBM::is_result($adminlist)) {
			logger(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), LOGGER_NORMAL);

			// Don't continue
			return;
		}

		// every admin could had different language
		foreach ($adminlist as $admin) {
			$lang = (($admin['language'])?$admin['language']:'en');
			push_lang($lang);

			$preamble = deindent(t("
				The friendica developers released update %s recently,
				but when I tried to install it, something went terribly wrong.
				This needs to be fixed soon and I can't do it alone. Please contact a
				friendica developer if you can not help me on your own. My database might be invalid."));
			$body = t("The error message is\n[pre]%s[/pre]");
			$preamble = sprintf($preamble, $update_id);
			$body = sprintf($body, $error_message);

			notification(array(
				'type' => SYSTEM_EMAIL,
				'to_email' => $admin['email'],
				'preamble' => $preamble,
				'body' => $body,
				'language' => $lang)
			);
		}

		//try the logger
		logger("CRITICAL: Database structure update failed: ".$error_message);
	}


	private static function tableStructure($table) {
		$structures = q("DESCRIBE `%s`", $table);

		$full_columns = q("SHOW FULL COLUMNS FROM `%s`", $table);

		$indexes = q("SHOW INDEX FROM `%s`", $table);

		$table_status = q("SHOW TABLE STATUS WHERE `name` = '%s'", $table);

		if (DBM::is_result($table_status)) {
			$table_status = $table_status[0];
		} else {
			$table_status = array();
		}

		$fielddata = array();
		$indexdata = array();

		if (DBM::is_result($indexes)) {
			foreach ($indexes AS $index) {
				if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) {
					$indexdata[$index["Key_name"]] = array('UNIQUE');
				}

				$column = $index["Column_name"];

				if ($index["Sub_part"] != "") {
					$column .= "(".$index["Sub_part"].")";
				}

				$indexdata[$index["Key_name"]][] = $column;
			}
		}
		if (DBM::is_result($structures)) {
			foreach ($structures AS $field) {
				// Replace the default size values so that we don't have to define them
				$search = ['tinyint(1)', 'tinyint(4)', 'smallint(6)', 'mediumint(9)', 'bigint(20)', 'int(11)'];
				$replace = ['boolean', 'tinyint', 'smallint', 'mediumint', 'bigint', 'int'];
				$field["Type"] = str_replace($search, $replace, $field["Type"]);

				$fielddata[$field["Field"]]["type"] = $field["Type"];
				if ($field["Null"] == "NO") {
					$fielddata[$field["Field"]]["not null"] = true;<commit_msg>Some SQL servers report different lengths for unsigned types<commit_after><?php
/**
 * @file src/Database/DBStructure.php
 */
namespace Friendica\Database;

use Friendica\Core\Config;
use Friendica\Database\DBM;
use dba;

require_once "boot.php";
require_once 'include/dba.php';
require_once 'include/enotify.php';
require_once "include/text.php";

/**
 * @brief This class contain functions for the database management
 *
 * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
 */
class DBStructure {
	/*
	 * Converts all tables from MyISAM to InnoDB
	 */
	public static function convertToInnoDB() {
		$r = q("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `engine` = 'MyISAM' AND `table_schema` = '%s'",
			dbesc(dba::database_name()));

		if (!DBM::is_result($r)) {
			echo t('There are no tables on MyISAM.')."\n";
			return;
		}

		foreach ($r AS $table) {
			$sql = sprintf("ALTER TABLE `%s` engine=InnoDB;", dbesc($table['TABLE_NAME']));
			echo $sql."\n";

			$result = dba::e($sql);
			if (!DBM::is_result($result)) {
				self::printUpdateError($sql);
			}
		}
	}

	/*
	 * send the email and do what is needed to do on update fails
	 *
	 * @param update_id		(int) number of failed update
	 * @param error_message	(str) error message
	 */
	public static function updateFail($update_id, $error_message) {
		$a = get_app();

		//send the administrators an e-mail
		$admin_mail_list = "'".implode("','", array_map(dbesc, explode(",", str_replace(" ", "", $a->config['admin_email']))))."'";
		$adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
			$admin_mail_list
		);

		// No valid result?
		if (!DBM::is_result($adminlist)) {
			logger(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), LOGGER_NORMAL);

			// Don't continue
			return;
		}

		// every admin could had different language
		foreach ($adminlist as $admin) {
			$lang = (($admin['language'])?$admin['language']:'en');
			push_lang($lang);

			$preamble = deindent(t("
				The friendica developers released update %s recently,
				but when I tried to install it, something went terribly wrong.
				This needs to be fixed soon and I can't do it alone. Please contact a
				friendica developer if you can not help me on your own. My database might be invalid."));
			$body = t("The error message is\n[pre]%s[/pre]");
			$preamble = sprintf($preamble, $update_id);
			$body = sprintf($body, $error_message);

			notification(array(
				'type' => SYSTEM_EMAIL,
				'to_email' => $admin['email'],
				'preamble' => $preamble,
				'body' => $body,
				'language' => $lang)
			);
		}

		//try the logger
		logger("CRITICAL: Database structure update failed: ".$error_message);
	}


	private static function tableStructure($table) {
		$structures = q("DESCRIBE `%s`", $table);

		$full_columns = q("SHOW FULL COLUMNS FROM `%s`", $table);

		$indexes = q("SHOW INDEX FROM `%s`", $table);

		$table_status = q("SHOW TABLE STATUS WHERE `name` = '%s'", $table);

		if (DBM::is_result($table_status)) {
			$table_status = $table_status[0];
		} else {
			$table_status = array();
		}

		$fielddata = array();
		$indexdata = array();

		if (DBM::is_result($indexes)) {
			foreach ($indexes AS $index) {
				if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) {
					$indexdata[$index["Key_name"]] = array('UNIQUE');
				}

				$column = $index["Column_name"];

				if ($index["Sub_part"] != "") {
					$column .= "(".$index["Sub_part"].")";
				}

				$indexdata[$index["Key_name"]][] = $column;
			}
		}
		if (DBM::is_result($structures)) {
			foreach ($structures AS $field) {
				// Replace the default size values so that we don't have to define them
				$search = ['tinyint(1)', 'tinyint(4)', 'smallint(5) unsigned', 'smallint(6)', 'mediumint(9)', 'bigint(20)', 'int(11)'];
				$replace = ['boolean', 'tinyint', 'smallint unsigned', 'smallint', 'mediumint', 'bigint', 'int'];
				$field["Type"] = str_replace($search, $replace, $field["Type"]);

				$fielddata[$field["Field"]]["type"] = $field["Type"];
				if ($field["Null"] == "NO") {
					$fielddata[$field["Field"]]["not null"] = true;
{'fraction_non_alphanumeric': 0.18235995232419547, 'fraction_numerical': 0.002502979737783075, 'mean_word_length': 4.100911854103344, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '638720', 'n_tokens_mistral': 3214, 'n_tokens_neox': 2826, 'n_words': 751}