';
$xml = new sxml();
$kb = $xml->parse(file_get_contents('../packages/database/contents.xml'));
$struct = $opt = $data = array();
$structc = $dcnt = $optcnt = $datacnt = 0;
$tables = array();
//iterate through contents.xml and populate a structure list
foreach($kb['kb3']['table'] as $idx => $tbl)
{
$table = $tbl['name'];
$files = array();
$dir = opendir('../packages/database/'.$table);
$xml = new sxml();
$st = $xml->parse(file_get_contents('../packages/database/'.$table.'/table.xml'));
$struct[$table] = $st['kb3']['structure'];
$kb['kb3']['table'][$idx]['rows'] = $st['kb3']['rows'];
$structc++;
//check various aspects of the directory contents per file / directory
while ($file = readdir($dir))
{
//is the file non-path directive or part of the subversion repository?
if ($file == '.' || $file == '..' || $file == '.svn')
{
continue;
}
if (strpos($file, '_opt_')) //is it an optional package?
{
$dcnt++;
$optcnt++;
$opt[$table][] = '../packages/database/'.$table.'/'.$file;
asort($opt[$table]);
}
elseif (!strpos($file, 'xml')) //with xml it is probably the table definition file...
{
$dcnt++;
$datacnt++;
$data[$table][] = '../packages/database/'.$table.'/'.$file;
asort($data[$table]);
}
}
}
//start a new db connection with stored session info
$db = new mysqli($_SESSION['sql']['host'], $_SESSION['sql']['user'], $_SESSION['sql']['pass'], $_SESSION['sql']['db']);
$db->set_charset('utf8');
$result = $db->query('SHOW TABLES');
//compare the listed tables to the structure list's and remove if they are the same
while ($row = $result->fetch_assoc())
{
$table = reset($row);
unset($struct[$table]);
}
//if structure creation action has been set, create the missing tables
if (isset($_REQUEST['sub']) && $_REQUEST['sub'] == 'struct')
{
foreach ($struct as $table => $structure)
{
echo 'Creating table '.$table.'...';
$query = $struct[$table];
if ($_SESSION['sql']['engine'] == "InnoDB")
{
$query = preg_replace('/MyISAM/', 'InnoDB', $query);
}
$id = $db->query($query);
if ($id)
{
echo 'done
';
}
else
{
echo 'Error: '.$db->error.'
';
}
unset($struct[$table]);
}
}
//forget progress in insertion which should force it back to step4 as if it started fresh
if (!empty($_REQUEST['do']) && $_REQUEST['do'] == 'reset')
{
unset($_SESSION['sqlinsert']);
unset($_SESSION['doopt']);
}
//advance one screen in the data insertion process
if (!empty($_REQUEST['sub']) && $_REQUEST['sub'] == 'data')
{
if (!isset($_SESSION['sqlinsert']))
{
$_SESSION['sqlinsert'] = 1;
if (isset($_POST['opt']))
{
$_SESSION['useopt'] = array();
foreach ($_POST['opt'] as $table => $value)
{
$_SESSION['useopt'][] = $table;
}
}
}
$i = 0;
$did = false;
$errors = false;
if (!isset($_SESSION['doopt']))
{
@$db->query("ALTER DATABASE ".$_SESSION['sql']['db']." CHARACTER SET utf8 COLLATE utf8_general_ci");
foreach ($data as $table => $files)
{
foreach ($files as $file)
{
$i++;
if ($_SESSION['sqlinsert'] > $i)
{
continue;
}
echo 'Inserting data ('.$i.'/'.$datacnt.') into '.$table.'
using file '.$file.'...
';
$error = '';
$fp = gzopen($file, 'r');
$lines = 0;
$errors = 0;
$text = '';
$query_count = 0;
$db->query("START TRANSACTION");
while ($query = gzgets($fp, 131072))
{
$text .= $query;
if (substr(trim($query), -1, 1) != ';')
{
continue;
}
$query = $text;
$text = '';
$lines++;
if (trim($query))
{
$query = trim($query);
if (substr($query, -1, 1) == ';')
{
$query = substr($query, 0, -1);
}
if (strpos($query, 'TRUNCATE') !== FALSE)
{
$db->query("COMMIT");
}
$query_count++;
$id = $db->query($query);
if (strpos($query, 'TRUNCATE') !== FALSE)
{
$db->query("START TRANSACTION");
}
#echo $query;
if (!$id)
{
$error .= 'error: '.$db->error.'
';
$errors++;
}
}
}
$db->query("COMMIT");
echo '
File '.$file.' had '.$lines.' lines with '.$query_count.' queries.
'.$errors.' queries failed.
';
if (!$error)
{
echo '
Finished importing this file.
';
echo '';
echo 'Automatic reload in 1s for next chunk. Manual Link
';
$_SESSION['sqlinsert']++;
}
else
{
echo $error;
echo '';
echo 'Automatic reload in 20s for next chunk because an error occurred. Manual Link
';
}
$did = true;
break 2;
}
}
}
if (!$did)
{
$stoppage = false;
$failed = 0;
echo 'All tables have imported. Now checking the tables for the correct data...
';
foreach ($kb['kb3']['table'] as $line)
{
$table = $line['name'];
$count = $line['rows'];
echo 'Checking table '.$table.': ';
$result = $db->query('SELECT count(*) AS cnt FROM '.$table);
$test = $result->fetch_array();
if ($test['cnt'] != $count && $count != 0)
{
echo $test['cnt'].'/'.$count.' - FAILED';
$failed++;
}
else
{
echo $test['cnt'].'/'.$count.' - PASSED';
}
echo '
';
}
if ($stoppage)
{
echo 'An error has occured with one of the tables. Please reset and try again.
';
}
else
{
echo '
All tables have passed.
';
echo 'You can now create or search for your corporation/alliance: Next Step -->
';
}
}
echo '
Use reset to step back to the optional package selection.
';
}
?>
Please select optional SQL data to be inserted into the database: