00001 <?php
00002
00015
00016
00017
00018
00019 require(dirname(__FILE__) . '/includes/cliTool.inc.php');
00020
00022 define('DATABASE_XML_FILE', 'dbscripts/xml/ojs_schema.xml');
00023
00024 import('db.DBDataXMLParser');
00025
00026 class dbXMLtoSQL extends CommandLineTool {
00027
00029 var $type;
00030
00032 var $command;
00033
00035 var $inputFile;
00036
00038 var $outputFile;
00039
00045 function dbXMLtoSQL($argv = array()) {
00046 parent::CommandLineTool($argv);
00047
00048 if (isset($this->argv[0]) && in_array($this->argv[0], array('-schema', '-data'))) {
00049 $this->type = substr($this->argv[0], 1);
00050 $argOffset = 1;
00051 } else {
00052 $this->type = 'schema';
00053 $argOffset = 0;
00054 }
00055
00056 if (!isset($this->argv[$argOffset]) || !in_array($this->argv[$argOffset], array('print', 'save', 'print_upgrade', 'save_upgrade', 'execute'))) {
00057 $this->usage();
00058 exit(1);
00059 }
00060
00061 $this->command = $this->argv[$argOffset];
00062
00063 $file = isset($this->argv[$argOffset+1]) ? $this->argv[$argOffset+1] : DATABASE_XML_FILE;
00064
00065 if (!file_exists($file) && !file_exists(($file2 = PWD . '/' . $file))) {
00066 printf("Input file \"%s\" does not exist!\n", $file);
00067 exit(1);
00068 }
00069
00070 $this->inputFile = isset($file2) ? $file2 : $file;
00071
00072 $this->outputFile = isset($this->argv[$argOffset+2]) ? PWD . '/' . $this->argv[$argOffset+2] : null;
00073 if (in_array($this->command, array('save', 'save_upgrade')) && ($this->outputFile == null || (file_exists($this->outputFile) && (is_dir($this->outputFile) || !is_writeable($this->outputFile))) || !is_writable(dirname($this->outputFile)))) {
00074 printf("Invalid output file \"%s\"!\n", $this->outputFile);
00075 exit(1);
00076 }
00077 }
00078
00082 function usage() {
00083 echo "Script to convert and execute XML-formatted database schema and data files\n"
00084 . "Usage: {$this->scriptName} [-data|-schema] command [input_file] [output_file]\n"
00085 . "Supported commands:\n"
00086 . " print - print SQL statements\n"
00087 . " save - save SQL statements to output_file\n"
00088 . " print_upgrade - print upgrade SQL statements for current database\n"
00089 . " save_upgrade - save upgrade SQL statements to output_file\n"
00090 . " execute - execute SQL statements on current database\n";
00091 }
00092
00097 function execute() {
00098 require('adodb/adodb-xmlschema.inc.php');
00099
00100 if (in_array($this->command, array('print', 'save'))) {
00101
00102 $conn = &new DBConnection(
00103 Config::getVar('database', 'driver'),
00104 null,
00105 null,
00106 null,
00107 null,
00108 true,
00109 Config::getVar('i18n', 'connection_charset')
00110 );
00111 $dbconn = $conn->getDBConn();
00112
00113 } else {
00114
00115 $dbconn = &DBConnection::getConn();
00116 }
00117
00118 $schema = &new adoSchema($dbconn, Config::getVar('i18n', 'database_charset'));
00119
00120 if ($this->type == 'schema') {
00121
00122 $sql = $schema->parseSchema($this->inputFile);
00123
00124 switch ($this->command) {
00125 case 'execute':
00126 $schema->ExecuteSchema();
00127 break;
00128 case 'save':
00129 case 'save_upgrade':
00130 $schema->SaveSQL($this->outputFile);
00131 break;
00132 case 'print':
00133 case 'print_upgrade':
00134 default:
00135 echo @$schema->PrintSQL('TEXT') . "\n";
00136 break;
00137 }
00138
00139 } else if ($this->type == 'data') {
00140
00141 $dataXMLParser = &new DBDataXMLParser();
00142 $dataXMLParser->setDBConn($dbconn);
00143 $sql = $dataXMLParser->parseData($this->inputFile);
00144
00145 switch ($this->command) {
00146 case 'execute':
00147 $schema->addSQL($sql);
00148 $schema->ExecuteSchema();
00149 break;
00150 case 'save':
00151 case 'save_upgrade':
00152 $schema->addSQL($sql);
00153 $schema->SaveSQL($this->outputFile);
00154 break;
00155 case 'print':
00156 case 'print_upgrade':
00157 default:
00158 $schema->addSQL($sql);
00159 echo @$schema->PrintSQL('TEXT') . "\n";
00160 break;
00161 }
00162
00163 $schema->destroy();
00164
00165 $dataXMLParser->destroy();
00166 }
00167 }
00168
00169 }
00170
00171 $tool = &new dbXMLtoSQL(isset($argv) ? $argv : array());
00172 $tool->execute();
00173 ?>