ocs /lib/pkp/classes/cliTool/XmlToSqlTool.inc.php

Language PHP Lines 167
MD5 Hash fd0bf9ee22d3a6c4410dfc7b23b6fed1
Repository https://github.com/lib-uoguelph-ca/ocs.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

/**
 * @file classes/cliTool/XmlToSqlTool.inc.php
 *
 * Copyright (c) 2000-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class XmlToSqlTool
 * @ingroup tools
 *
 * @brief CLI tool to output the SQL statements corresponding to an XML database schema.
 */

// $Id$


import('db.DBDataXMLParser');

class XmlToSqlTool extends CommandLineTool {

	/** @var string type of file to parse (schema or data) */
	var $type;

	/** @var string command to execute (print|execute|upgrade) */
	var $command;

	/** @var string XML file to parse */
	var $inputFile;

	/** @var string file to save SQL statements in */
	var $outputFile;

	/**
	 * Constructor.
	 * @param $argv array command-line arguments
	 * 	If specified, the first argument should be the file to parse
	 */
	function XmlToSqlTool($argv = array()) {
		parent::CommandLineTool($argv);

		if (isset($this->argv[0]) && in_array($this->argv[0], array('-schema', '-data'))) {
			$this->type = substr($this->argv[0], 1);
			$argOffset = 1;
		} else {
			$this->type = 'schema';
			$argOffset = 0;
		}

		if (!isset($this->argv[$argOffset]) || !in_array($this->argv[$argOffset], array('print', 'save', 'print_upgrade', 'save_upgrade', 'execute'))) {
			$this->usage();
			exit(1);
		}

		$this->command = $this->argv[$argOffset];

		$file = isset($this->argv[$argOffset+1]) ? $this->argv[$argOffset+1] : DATABASE_XML_FILE;

		if (!file_exists($file) && !file_exists(($file2 = PWD . '/' . $file))) {
			printf("Input file \"%s\" does not exist!\n", $file);
			exit(1);
		}

		$this->inputFile = isset($file2) ? $file2 : $file;

		$this->outputFile = isset($this->argv[$argOffset+2]) ? PWD . '/' . $this->argv[$argOffset+2] : null;
		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)))) {
			printf("Invalid output file \"%s\"!\n", $this->outputFile);
			exit(1);
		}
	}

	/**
	 * Print command usage information.
	 */
	function usage() {
		echo "Script to convert and execute XML-formatted database schema and data files\n"
			. "Usage: {$this->scriptName} [-data|-schema] command [input_file] [output_file]\n"
			. "Supported commands:\n"
			. "    print - print SQL statements\n"
			. "    save - save SQL statements to output_file\n"
			. "    print_upgrade - print upgrade SQL statements for current database\n"
			. "    save_upgrade - save upgrade SQL statements to output_file\n"
			. "    execute - execute SQL statements on current database\n";
	}

	/**
	 * Parse an XML database file and output the corresponding SQL statements.
	 * See lib/pkp/dtd/xmlSchema.dtd for the format of the XML files.
	 */
	function execute() {
		require_once('adodb-xmlschema.inc.php');

		if (in_array($this->command, array('print', 'save'))) {
			// Don't connect to actual database (so parser won't build upgrade XML)
			$conn = new DBConnection(
				Config::getVar('database', 'driver'),
				null,
				null,
				null,
				null,
				true,
				Config::getVar('i18n', 'connection_charset')
			);
			$dbconn = $conn->getDBConn();

		} else {
			// Create or upgrade existing database
			$dbconn =& DBConnection::getConn();
		}

		$schema = new adoSchema($dbconn);
		$dict =& $schema->dict;
		$dict->SetCharSet(Config::getVar('i18n', 'database_charset'));

		if ($this->type == 'schema') {
			// Parse XML schema files
			$sql = $schema->parseSchema($this->inputFile);

			switch ($this->command) {
				case 'execute':
					$schema->ExecuteSchema();
					break;
				case 'save':
				case 'save_upgrade':
					$schema->SaveSQL($this->outputFile);
					break;
				case 'print':
				case 'print_upgrade':
				default:
					echo @$schema->PrintSQL('TEXT') . "\n";
					break;
			}

		} else if ($this->type == 'data') {
			// Parse XML data files
			$dataXMLParser = new DBDataXMLParser();
			$dataXMLParser->setDBConn($dbconn);
			$sql = $dataXMLParser->parseData($this->inputFile);

			switch ($this->command) {
				case 'execute':
					$schema->addSQL($sql);
					$schema->ExecuteSchema();
					break;
				case 'save':
				case 'save_upgrade':
					$schema->addSQL($sql);
					$schema->SaveSQL($this->outputFile);
					break;
				case 'print':
				case 'print_upgrade':
				default:
					$schema->addSQL($sql);
					echo @$schema->PrintSQL('TEXT') . "\n";
					break;
			}

			$schema->destroy();

			$dataXMLParser->destroy();
		}
	}
}

?>
Back to Top