PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/lib-uoguelph-ca/ocs
PHP | 166 lines | 105 code | 29 blank | 32 comment | 24 complexity | fd0bf9ee22d3a6c4410dfc7b23b6fed1 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/cliTool/XmlToSqlTool.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class XmlToSqlTool
  9. * @ingroup tools
  10. *
  11. * @brief CLI tool to output the SQL statements corresponding to an XML database schema.
  12. */
  13. // $Id$
  14. import('db.DBDataXMLParser');
  15. class XmlToSqlTool extends CommandLineTool {
  16. /** @var string type of file to parse (schema or data) */
  17. var $type;
  18. /** @var string command to execute (print|execute|upgrade) */
  19. var $command;
  20. /** @var string XML file to parse */
  21. var $inputFile;
  22. /** @var string file to save SQL statements in */
  23. var $outputFile;
  24. /**
  25. * Constructor.
  26. * @param $argv array command-line arguments
  27. * If specified, the first argument should be the file to parse
  28. */
  29. function XmlToSqlTool($argv = array()) {
  30. parent::CommandLineTool($argv);
  31. if (isset($this->argv[0]) && in_array($this->argv[0], array('-schema', '-data'))) {
  32. $this->type = substr($this->argv[0], 1);
  33. $argOffset = 1;
  34. } else {
  35. $this->type = 'schema';
  36. $argOffset = 0;
  37. }
  38. if (!isset($this->argv[$argOffset]) || !in_array($this->argv[$argOffset], array('print', 'save', 'print_upgrade', 'save_upgrade', 'execute'))) {
  39. $this->usage();
  40. exit(1);
  41. }
  42. $this->command = $this->argv[$argOffset];
  43. $file = isset($this->argv[$argOffset+1]) ? $this->argv[$argOffset+1] : DATABASE_XML_FILE;
  44. if (!file_exists($file) && !file_exists(($file2 = PWD . '/' . $file))) {
  45. printf("Input file \"%s\" does not exist!\n", $file);
  46. exit(1);
  47. }
  48. $this->inputFile = isset($file2) ? $file2 : $file;
  49. $this->outputFile = isset($this->argv[$argOffset+2]) ? PWD . '/' . $this->argv[$argOffset+2] : null;
  50. 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)))) {
  51. printf("Invalid output file \"%s\"!\n", $this->outputFile);
  52. exit(1);
  53. }
  54. }
  55. /**
  56. * Print command usage information.
  57. */
  58. function usage() {
  59. echo "Script to convert and execute XML-formatted database schema and data files\n"
  60. . "Usage: {$this->scriptName} [-data|-schema] command [input_file] [output_file]\n"
  61. . "Supported commands:\n"
  62. . " print - print SQL statements\n"
  63. . " save - save SQL statements to output_file\n"
  64. . " print_upgrade - print upgrade SQL statements for current database\n"
  65. . " save_upgrade - save upgrade SQL statements to output_file\n"
  66. . " execute - execute SQL statements on current database\n";
  67. }
  68. /**
  69. * Parse an XML database file and output the corresponding SQL statements.
  70. * See lib/pkp/dtd/xmlSchema.dtd for the format of the XML files.
  71. */
  72. function execute() {
  73. require_once('adodb-xmlschema.inc.php');
  74. if (in_array($this->command, array('print', 'save'))) {
  75. // Don't connect to actual database (so parser won't build upgrade XML)
  76. $conn = new DBConnection(
  77. Config::getVar('database', 'driver'),
  78. null,
  79. null,
  80. null,
  81. null,
  82. true,
  83. Config::getVar('i18n', 'connection_charset')
  84. );
  85. $dbconn = $conn->getDBConn();
  86. } else {
  87. // Create or upgrade existing database
  88. $dbconn =& DBConnection::getConn();
  89. }
  90. $schema = new adoSchema($dbconn);
  91. $dict =& $schema->dict;
  92. $dict->SetCharSet(Config::getVar('i18n', 'database_charset'));
  93. if ($this->type == 'schema') {
  94. // Parse XML schema files
  95. $sql = $schema->parseSchema($this->inputFile);
  96. switch ($this->command) {
  97. case 'execute':
  98. $schema->ExecuteSchema();
  99. break;
  100. case 'save':
  101. case 'save_upgrade':
  102. $schema->SaveSQL($this->outputFile);
  103. break;
  104. case 'print':
  105. case 'print_upgrade':
  106. default:
  107. echo @$schema->PrintSQL('TEXT') . "\n";
  108. break;
  109. }
  110. } else if ($this->type == 'data') {
  111. // Parse XML data files
  112. $dataXMLParser = new DBDataXMLParser();
  113. $dataXMLParser->setDBConn($dbconn);
  114. $sql = $dataXMLParser->parseData($this->inputFile);
  115. switch ($this->command) {
  116. case 'execute':
  117. $schema->addSQL($sql);
  118. $schema->ExecuteSchema();
  119. break;
  120. case 'save':
  121. case 'save_upgrade':
  122. $schema->addSQL($sql);
  123. $schema->SaveSQL($this->outputFile);
  124. break;
  125. case 'print':
  126. case 'print_upgrade':
  127. default:
  128. $schema->addSQL($sql);
  129. echo @$schema->PrintSQL('TEXT') . "\n";
  130. break;
  131. }
  132. $schema->destroy();
  133. $dataXMLParser->destroy();
  134. }
  135. }
  136. }
  137. ?>