<?php

error_reporting(E_ALL);
include_once 'config.php';
include_once 'core/fmt.php';
include_once 'core/struct.php';
include_once 'core/dbsimple/Generic.php';
// including simple conecting for DB
include_once 'core/dbc.class.php';
// including simple conecting for DB
define('BASE_BUILD_CLIENT', 0);
$DB = DbSimple_Generic::connect($db_config['dbc_dns']);
$DB->setErrorHandler("databaseErrorHandler");
// $DB->setIdentPrefix($db_config['db_prefix']);
$dbc = new DBCparser();
define('URL_START', 'https://raw.github.com/mangos/mangos/');
define('URL_DBC_FMT', '/src/game/DBCfmt.h');
define('URL_UPDATE_FIELDS', '/src/game/UpdateFields.h');
define('URL_DBC_STRUCT', '/src/game/DBCStructure.h');
$branch = '400';
function getStructFilesGIT($branch, &$output, &$info)
{
    $url = URL_START . $branch . URL_DBC_FMT;
    $output = '';
    $I = '\\b[A-Z_][A-Z0-9_]*\\b';
    $regex = "^\\w{5}\\s\\w{4}(.*)\\[\\]=\"(.*)\";\$";
    $file = file($url);
    foreach ($file as $line) {
        $line = rtrim($line);
        if (preg_match("#{$regex}#i", $line, $regs)) {
            $regs[1] = str_replace('Entry', '', str_replace('fmt', '', trim($regs[1])));
			print "\nразмер поля string: ".$dbc->sizeString."</pre>";
			break;
		case 'fetchData':
			$file = $_POST['file'];
			$dbc = new DBCparser($file);
			$dbc->writing = true;
			if($dbc->getData())
				print 'OK';
			else
				print $dbc->error;
			break;
		case 'getRows':
			$tbl = strtolower(preg_replace('/(.*).dbc/','dbc_\\1',$_POST['file']));
			$dbc = new DBCparser(strtolower($_POST['file']));
			print $dbc->DB->selectCell("SELECT COUNT(*) FROM ?#",$tbl);
			break;
		case 'truncate':
			$tbl = preg_replace('/(.*).dbc/','dbc_\\1',$_POST['file']);
			$dbc = new DBCparser($_POST['file']);
			print $dbc->DB->query("TRUNCATE ?#",$tbl);
			break;
		case 'support':
			$tbl = preg_replace('/(.*).dbc/','dbc_\\1',$_POST['file']);
			$dbc = new DBCparser($_POST['file']);
			print(!$dbc->isValidFormatFile())? $dbc->error : 'OK';
			break;
		default:
			break;
	}

?>