Source for file db_updater.php

Documentation is available at db_updater.php

  1. <?php
  2. /**
  3. * @since 9/11/07
  4. * @package harmoni.dbc.mysql
  5. *
  6. * @copyright Copyright &copy; 2007, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: db_updater.php,v 1.2 2007/09/13 18:13:55 adamfranco Exp $
  10. */
  11. ini_set('display_errors', true);
  12.  
  13. require_once(dirname(__FILE__).'/../../harmoni.inc.php');
  14. require_once(dirname(__FILE__).'/MySQL/MySqlUtils.class.php');
  15.  
  16. $types = array(
  17. MYSQL => "MySQL",
  18. POSTGRESQL => "PostgreSQL",
  19. ORACLE => "Oracle",
  20. SQLSERVER => "Microsoft SQL Server"
  21. );
  22.  
  23. $functions = array (
  24. "columnNamesToLowercase" => "Make all column names lowercase.",
  25. "harmoni_0_12_0_update" => "Update the tables from Harmoni-0.11.0 to Harmoni-0.12.0."
  26. );
  27.  
  28. function execute_update (array $types, array $functions) {
  29. if (!array_key_exists($_REQUEST['function'], $functions))
  30. throw new Exception("Unknown function, '".$_REQUEST['function']."'.");
  31. if (!array_key_exists($_REQUEST['db_type'], $types))
  32. throw new Exception("Unknown database type, '".$_REQUEST['db_type']."'.");
  33. switch ($_REQUEST['db_type']) {
  34. case MYSQL:
  35. $utilClass = "MySqlUtils";
  36. break;
  37. default:
  38. throw new Exception($types[$_REQUEST['db_type']]." databases are not currently supported for updates.");
  39. }
  40. if (!class_exists($utilClass))
  41. throw new Exception("The selected database utility class, '".$utilClass."', does not exist.");
  42. if (!method_exists($utilClass, $_REQUEST['function']))
  43. throw new Exception("The selected function, ".$_REQUEST['function']."(), is not currently supported by this database.");
  44. // Now execute the update.
  45. $configuration = new ConfigurationProperties;
  46. $context = new OSIDContext;
  47. Services::startManagerAsService("DatabaseManager", $context, $configuration);
  48. $dbc = Services::getService('DBHandler');
  49. $dbIndex = $dbc->createDatabase($_REQUEST['db_type'], $_REQUEST['db_host'], $_REQUEST['db_name'], $_REQUEST['db_user'], $_REQUEST['db_pass']);
  50. $dbc->connect($dbIndex);
  51. try {
  52. eval($utilClass."::".$_REQUEST['function'].'($dbIndex);');
  53. } catch (Exception $e) {
  54. // Close our connection
  55. $dbc->disconnect($dbIndex);
  56. throw $e;
  57. }
  58. $dbc->disconnect($dbIndex);
  59. }
  60.  
  61.  
  62. ?>
  63. <html>
  64. <head>
  65. <title>Database Updater</title>
  66. <style type='text/css'>
  67. .db_type {
  68. float: left;
  69. margin-right: 20px;
  70. }
  71. h2, h3 {
  72. margin-bottom: 5px;
  73. }
  74. .db_type h3 {
  75. margin-top: 0px;
  76. }
  77. .results {
  78. padding: 10px;
  79. background-color: #CCC;
  80. border: 1px dotted #999;
  81. }
  82. </style>
  83. </head>
  84. <body>
  85. <h1>Database Updater</h1>
  86.  
  87. <?php
  88.  
  89. if (isset($_REQUEST['db_type']) && isset($_REQUEST['db_host'])
  90. && isset($_REQUEST['db_name']) && isset($_REQUEST['db_user'])
  91. && isset($_REQUEST['db_pass']) && isset($_REQUEST['function']))
  92. {
  93. print "\n\t\t<h2>"._("Results")."</h2>";
  94. print "\n\t<div class='results'>";
  95. try {
  96. execute_update($types, $functions);
  97. } catch (Exception $e) {
  98. printExceptionInHtml($e);
  99. }
  100.  
  101. print "\n\t</div>";
  102. }
  103. print "\n\t<form action='".$_SERVER['PHP_SELF']."' method='post'>";
  104. /*******************************************************
  105. * Database Type
  106. *********************************************************/
  107.  
  108. print "\n\t\t<div class='db_type'>";
  109. print "\n\t\t\t<h3>"._("Database Type:")."</h3>";
  110. foreach ($types as $type => $desc) {
  111. print "\n\t\t\t<div>";
  112. print "\n\t\t\t\t<input type='radio' name='db_type' value='".$type."' ".((isset($_REQUEST['db_type']) && $_REQUEST['db_type'] == $type)?" checked='checked'":"")." />".$desc;
  113. print "\n\t\t\t</div>";
  114. }
  115. print "\n\t\t</div>";
  116. /*******************************************************
  117. * Connection Parameters
  118. *********************************************************/
  119.  
  120. print "\n\t\t<div class='connection_params'>";
  121. print "\n\t\t\t<h3>"._("Connection parameters:")."</h3>";
  122. print "\n\t\t\t<div>"._('Database Host: ');
  123. print "\n\t\t\t\t<input type='text' name='db_host' value='".((isset($_REQUEST['db_host']))?$_REQUEST['db_host']:"localhost")."'/>";
  124. print "\n\t\t\t</div>";
  125. print "\n\t\t\t<div>"._('Database Name: ');
  126. print "\n\t\t\t\t<input type='text' name='db_name' value='".((isset($_REQUEST['db_name']))?$_REQUEST['db_name']:"")."'/>";
  127. print "\n\t\t\t</div>";
  128. print "\n\t\t\t<div>"._('Database User: ');
  129. print "\n\t\t\t\t<input type='text' name='db_user' value='".((isset($_REQUEST['db_user']))?$_REQUEST['db_user']:"")."'/>";
  130. print "\n\t\t\t</div>";
  131. print "\n\t\t\t<div>"._('Database Password: ');
  132. print "\n\t\t\t\t<input type='password' name='db_pass' value='".((isset($_REQUEST['db_pass']))?$_REQUEST['db_pass']:"")."'/>";
  133. print "\n\t\t\t</div>";
  134. print "\n\t\t</div>";
  135. /*******************************************************
  136. * Functions
  137. *********************************************************/
  138.  
  139. print "\n\t\t<div class='functions'>";
  140. print "\n\t\t\t<h3>"._("Functions to run:")."</h3>";
  141. foreach ($functions as $function => $desc) {
  142. print "\n\t\t\t<div>";
  143. print "\n\t\t\t\t<input type='radio' name='function' value='".$function."' ".((isset($_REQUEST['function']) && $_REQUEST['function'] == $function)?" checked='checked'":"")." />".$desc;
  144. print "\n\t\t\t</div>";
  145. }
  146. print "\n\t\t</div>";
  147. ?>
  148. <div>
  149. <input type='submit'/>
  150. </div>
  151. </form>
  152. </body>
  153. </html>

Documentation generated on Wed, 19 Sep 2007 10:22:39 -0400 by phpDocumentor 1.3.0RC3