PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/sql_upgrade.php

https://github.com/md-tech/openemr
PHP | 283 lines | 233 code | 28 blank | 22 comment | 71 complexity | 0ef7768b8abf283ace6d75425d805cef MD5 | raw file
  1. <?php
  2. // Copyright (C) 2008-2010 Rod Roark <rod@sunsetsystems.com>
  3. //
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU General Public License
  6. // as published by the Free Software Foundation; either version 2
  7. // of the License, or (at your option) any later version.
  8. //
  9. // This may be run after an upgraded OpenEMR has been installed.
  10. // Its purpose is to upgrade the MySQL OpenEMR database as needed
  11. // for the new release.
  12. // Disable PHP timeout. This will not work in safe mode.
  13. ini_set('max_execution_time', '0');
  14. $ignoreAuth = true; // no login required
  15. require_once('interface/globals.php');
  16. require_once('library/sql.inc');
  17. // Force logging off
  18. $GLOBALS["enable_auditlog"]=0;
  19. function tableExists($tblname) {
  20. $row = sqlQuery("SHOW TABLES LIKE '$tblname'");
  21. if (empty($row)) return false;
  22. return true;
  23. }
  24. function columnExists($tblname, $colname) {
  25. $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
  26. if (empty($row)) return false;
  27. return true;
  28. }
  29. function columnHasType($tblname, $colname, $coltype) {
  30. $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
  31. if (empty($row)) return true;
  32. return (strcasecmp($row['Type'], $coltype) == 0);
  33. }
  34. function tableHasRow($tblname, $colname, $value) {
  35. $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
  36. "$colname LIKE '$value'");
  37. return $row['count'] ? true : false;
  38. }
  39. function tableHasRow2D($tblname, $colname, $value, $colname2, $value2) {
  40. $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
  41. "$colname LIKE '$value' AND $colname2 LIKE '$value2'");
  42. return $row['count'] ? true : false;
  43. }
  44. function tableHasRow3D($tblname, $colname, $value, $colname2, $value2, $colname3, $value3) {
  45. $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
  46. "$colname LIKE '$value' AND $colname2 LIKE '$value2' AND $colname3 LIKE '$value3'");
  47. return $row['count'] ? true : false;
  48. }
  49. function tableHasRow4D($tblname, $colname, $value, $colname2, $value2, $colname3, $value3, $colname4, $value4) {
  50. $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
  51. "$colname LIKE '$value' AND $colname2 LIKE '$value2' AND $colname3 LIKE '$value3' AND $colname4 LIKE '$value4'");
  52. return $row['count'] ? true : false;
  53. }
  54. function upgradeFromSqlFile($filename) {
  55. global $webserver_root;
  56. flush();
  57. echo "<font color='green'>Processing $filename ...</font><br />\n";
  58. $fullname = "$webserver_root/sql/$filename";
  59. $fd = fopen($fullname, 'r');
  60. if ($fd == FALSE) {
  61. echo "ERROR. Could not open '$fullname'.\n";
  62. flush();
  63. break;
  64. }
  65. $query = "";
  66. $line = "";
  67. $skipping = false;
  68. while (!feof ($fd)){
  69. $line = fgets($fd, 2048);
  70. $line = rtrim($line);
  71. if (preg_match('/^\s*--/', $line)) continue;
  72. if ($line == "") continue;
  73. if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
  74. $skipping = tableExists($matches[1]);
  75. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  76. }
  77. else if (preg_match('/^#IfTable\s+(\S+)/', $line, $matches)) {
  78. $skipping = ! tableExists($matches[1]);
  79. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  80. }
  81. else if (preg_match('/^#IfMissingColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
  82. if (tableExists($matches[1])) {
  83. $skipping = columnExists($matches[1], $matches[2]);
  84. }
  85. else {
  86. // If no such table then the column is deemed not "missing".
  87. $skipping = true;
  88. }
  89. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  90. }
  91. else if (preg_match('/^#IfNotColumnType\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
  92. if (tableExists($matches[1])) {
  93. $skipping = columnHasType($matches[1], $matches[2], $matches[3]);
  94. }
  95. else {
  96. // If no such table then the column type is deemed not "missing".
  97. $skipping = true;
  98. }
  99. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  100. }
  101. else if (preg_match('/^#IfNotRow\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
  102. if (tableExists($matches[1])) {
  103. $skipping = tableHasRow($matches[1], $matches[2], $matches[3]);
  104. }
  105. else {
  106. // If no such table then the row is deemed not "missing".
  107. $skipping = true;
  108. }
  109. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  110. }
  111. else if (preg_match('/^#IfNotRow2D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
  112. if (tableExists($matches[1])) {
  113. $skipping = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
  114. }
  115. else {
  116. // If no such table then the row is deemed not "missing".
  117. $skipping = true;
  118. }
  119. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  120. }
  121. else if (preg_match('/^#IfNotRow3D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
  122. if (tableExists($matches[1])) {
  123. $skipping = tableHasRow3D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7]);
  124. }
  125. else {
  126. // If no such table then the row is deemed not "missing".
  127. $skipping = true;
  128. }
  129. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  130. }
  131. else if (preg_match('/^#IfNotRow4D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
  132. if (tableExists($matches[1])) {
  133. $skipping = tableHasRow4D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7], $matches[8], $matches[9]);
  134. }
  135. else {
  136. // If no such table then the row is deemed not "missing".
  137. $skipping = true;
  138. }
  139. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  140. }
  141. else if (preg_match('/^#IfNotRow2Dx2\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
  142. if (tableExists($matches[1])) {
  143. // If either check exist, then will skip
  144. $firstCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
  145. $secondCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[6], $matches[7]);
  146. if ($firstCheck || $secondCheck) {
  147. $skipping = true;
  148. }
  149. else {
  150. $skipping = false;
  151. }
  152. }
  153. else {
  154. // If no such table then the row is deemed not "missing".
  155. $skipping = true;
  156. }
  157. if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
  158. }
  159. else if (preg_match('/^#EndIf/', $line)) {
  160. $skipping = false;
  161. }
  162. if (preg_match('/^\s*#/', $line)) continue;
  163. if ($skipping) continue;
  164. $query = $query . $line;
  165. if (substr($query, -1) == ';') {
  166. $query = rtrim($query, ';');
  167. echo "$query<br />\n";
  168. if (!sqlStatement($query)) {
  169. echo "<font color='red'>The above statement failed: " .
  170. mysql_error() . "<br />Upgrading will continue.<br /></font>\n";
  171. }
  172. $query = '';
  173. }
  174. }
  175. flush();
  176. } // end function
  177. $versions = array();
  178. $sqldir = "$webserver_root/sql";
  179. $dh = opendir($sqldir);
  180. if (! $dh) die("Cannot read $sqldir");
  181. while (false !== ($sfname = readdir($dh))) {
  182. if (substr($sfname, 0, 1) == '.') continue;
  183. if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
  184. $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
  185. $versions[$version] = $sfname;
  186. }
  187. }
  188. closedir($dh);
  189. ksort($versions);
  190. ?>
  191. <html>
  192. <head>
  193. <title>OpenEMR Database Upgrade</title>
  194. <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
  195. </head>
  196. <body>
  197. <center>
  198. <span class='title'>OpenEMR Database Upgrade</span>
  199. <br>
  200. </center>
  201. <?php
  202. if (!empty($_POST['form_submit'])) {
  203. $form_old_version = $_POST['form_old_version'];
  204. foreach ($versions as $version => $filename) {
  205. if (strcmp($version, $form_old_version) < 0) continue;
  206. upgradeFromSqlFile($filename);
  207. }
  208. if (!empty($GLOBALS['ippf_specific'])) {
  209. // Upgrade custom stuff for IPPF.
  210. upgradeFromSqlFile('ippf_upgrade.sql');
  211. }
  212. flush();
  213. echo "<font color='green'>Updating global configuration defaults...</font><br />\n";
  214. require_once("library/globals.inc.php");
  215. foreach ($GLOBALS_METADATA as $grpname => $grparr) {
  216. foreach ($grparr as $fldid => $fldarr) {
  217. list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
  218. if (substr($fldtype, 0, 2) !== 'm_') {
  219. $row = sqlQuery("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
  220. if (empty($row['count'])) {
  221. sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
  222. "VALUES ( '$fldid', '0', '$flddef' )");
  223. }
  224. }
  225. }
  226. }
  227. echo "<font color='green'>Updating version indicators...</font><br />\n";
  228. sqlStatement("UPDATE version SET v_major = '$v_major', v_minor = '$v_minor', " .
  229. "v_patch = '$v_patch', v_tag = '$v_tag', v_database = '$v_database'");
  230. echo "<p><font color='green'>Database upgrade finished.</font></p>\n";
  231. echo "</body></html>\n";
  232. exit();
  233. }
  234. ?>
  235. <center>
  236. <form method='post' action='sql_upgrade.php'>
  237. <p>Please select the prior release you are converting from:
  238. <select name='form_old_version'>
  239. <?php
  240. foreach ($versions as $version => $filename) {
  241. echo " <option value='$version'";
  242. // Defaulting to most recent version, which is now 4.0.0.
  243. if ($version === '4.1.0') echo " selected";
  244. echo ">$version</option>\n";
  245. }
  246. ?>
  247. </select>
  248. </p>
  249. <p>If you are unsure or were using a development version between two
  250. releases, then choose the older of possible releases.</p>
  251. <p><input type='submit' name='form_submit' value='Upgrade Database' /></p>
  252. </form>
  253. </center>
  254. </body>
  255. </html>