This script is only meant to run at the command line."); } $no_http_headers = true; include(dirname(__FILE__) . "/../include/global.php"); /* process calling arguments */ $parms = $_SERVER["argv"]; array_shift($parms); $table = ''; $create = true; foreach($parms as $parameter) { @list($arg, $value) = @explode("=", $parameter); switch ($arg) { case '--table': $table = trim(sql_clean($value)); break; case '--update': $create = false; break; default: } } if ($table == '') { } else { print sqltable_to_php($table, $create); } function sqltable_to_php ($table, $create) { global $config, $database_default; include_once($config['library_path'] . '/database.php'); $result = db_fetch_assoc('SHOW tables FROM `' . $database_default . '`') or die (mysql_error()); $tables = array(); $text = ''; foreach($result as $index => $arr) { foreach ($arr as $t) { $tables[] = $t; } } if (in_array($table, $tables)) { $cols = array(); $result = db_fetch_assoc("SHOW columns FROM $table") or die ('ERROR: Can not display columns!'); $pri = array(); $keys = array(); $text = "\n\$data = array();\n"; foreach ($result as $r) { $text .= "\$data['columns'][] = array("; $text .= "'name' => '" . $r['Field'] . "'"; if (strpos(strtolower($r['Type']), ' unsigned') !== FALSE) { $r['Type'] = str_ireplace(' unsigned', '', $r['Type']); $text .= ", 'unsigned' => true"; } $text .= ", 'type' => \"" . $r['Type'] . "\""; $text .= ", 'NULL' => " . (strtolower($r['Null']) == 'no' ? 'false' : 'true'); if (trim($r['Default']) != '') { $text .= ", 'default' => '" . $r['Default'] . "'"; } if (trim($r['Extra']) != '') { if (strtolower($r['Extra']) == 'auto_increment') { $text .= ", 'auto_increment' => true"; } } $text .= ");\n"; } $result = db_fetch_assoc("SHOW INDEX FROM $table") or die ('ERROR: Can not display Indexes!'); foreach ($result as $r) { if ($r['Key_name'] == 'PRIMARY') { $pri[] = $r['Column_name']; } else { $keys[$r['Key_name']][$r['Seq_in_index']] = $r['Column_name']; } } if (!empty($pri)) { $text .= "\$data['primary'] = array('" . implode("','", $pri) . "');\n"; } if (!empty($keys)) { foreach ($keys as $n => $k) { $text .= "\$data['keys'][] = array('name' => '$n', 'columns' => array('" . implode("','", $k) . "'));\n"; } } $result = db_fetch_row("SELECT ENGINE, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_NAME = '$table'"); $text .= "\$data['type'] = '" . $result['ENGINE'] . "';\n"; $text .= "\$data['comment'] = '" . $result['TABLE_COMMENT'] . "';\n"; if ($create) { $text .= "db_table_create ('$table', \$data);\n"; } else { $text .= "db_update_table ('$table', \$data, false);\n"; } } return $text; } function sql_clean($text) { $text = str_replace(array("\\", '/', "'", '"', '|'), '', $text); return $text; }