removed call time *pass by reference* and made sure pass by reference specified in...
[openemr.git] / sql_upgrade.php
blobb561f1cb1e4596031f655b6f16dd8879252dc3bd
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.
13 // Disable PHP timeout. This will not work in safe mode.
14 ini_set('max_execution_time', '0');
16 $ignoreAuth = true; // no login required
18 require_once('interface/globals.php');
19 require_once('library/sql.inc');
21 // Force logging off
22 $GLOBALS["enable_auditlog"]=0;
24 function tableExists($tblname) {
25 $row = sqlQuery("SHOW TABLES LIKE '$tblname'");
26 if (empty($row)) return false;
27 return true;
30 function columnExists($tblname, $colname) {
31 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
32 if (empty($row)) return false;
33 return true;
36 function columnHasType($tblname, $colname, $coltype) {
37 $row = sqlQuery("SHOW COLUMNS FROM $tblname LIKE '$colname'");
38 if (empty($row)) return true;
39 return (strcasecmp($row['Type'], $coltype) == 0);
42 function tableHasRow($tblname, $colname, $value) {
43 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
44 "$colname LIKE '$value'");
45 return $row['count'] ? true : false;
48 function tableHasRow2D($tblname, $colname, $value, $colname2, $value2) {
49 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
50 "$colname LIKE '$value' AND $colname2 LIKE '$value2'");
51 return $row['count'] ? true : false;
54 function tableHasRow3D($tblname, $colname, $value, $colname2, $value2, $colname3, $value3) {
55 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
56 "$colname LIKE '$value' AND $colname2 LIKE '$value2' AND $colname3 LIKE '$value3'");
57 return $row['count'] ? true : false;
60 function tableHasRow4D($tblname, $colname, $value, $colname2, $value2, $colname3, $value3, $colname4, $value4) {
61 $row = sqlQuery("SELECT COUNT(*) AS count FROM $tblname WHERE " .
62 "$colname LIKE '$value' AND $colname2 LIKE '$value2' AND $colname3 LIKE '$value3' AND $colname4 LIKE '$value4'");
63 return $row['count'] ? true : false;
66 function upgradeFromSqlFile($filename) {
67 global $webserver_root;
69 flush();
70 echo "<font color='green'>Processing $filename ...</font><br />\n";
72 $fullname = "$webserver_root/sql/$filename";
74 $fd = fopen($fullname, 'r');
75 if ($fd == FALSE) {
76 echo "ERROR. Could not open '$fullname'.\n";
77 flush();
78 break;
81 $query = "";
82 $line = "";
83 $skipping = false;
85 while (!feof ($fd)){
86 $line = fgets($fd, 2048);
87 $line = rtrim($line);
89 if (preg_match('/^\s*--/', $line)) continue;
90 if ($line == "") continue;
92 if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
93 $skipping = tableExists($matches[1]);
94 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
96 else if (preg_match('/^#IfTable\s+(\S+)/', $line, $matches)) {
97 $skipping = ! tableExists($matches[1]);
98 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
100 else if (preg_match('/^#IfMissingColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
101 if (tableExists($matches[1])) {
102 $skipping = columnExists($matches[1], $matches[2]);
104 else {
105 // If no such table then the column is deemed not "missing".
106 $skipping = true;
108 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
110 else if (preg_match('/^#IfNotColumnType\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
111 if (tableExists($matches[1])) {
112 $skipping = columnHasType($matches[1], $matches[2], $matches[3]);
114 else {
115 // If no such table then the column type is deemed not "missing".
116 $skipping = true;
118 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
120 else if (preg_match('/^#IfNotRow\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
121 if (tableExists($matches[1])) {
122 $skipping = tableHasRow($matches[1], $matches[2], $matches[3]);
124 else {
125 // If no such table then the row is deemed not "missing".
126 $skipping = true;
128 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
130 else if (preg_match('/^#IfNotRow2D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
131 if (tableExists($matches[1])) {
132 $skipping = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
134 else {
135 // If no such table then the row is deemed not "missing".
136 $skipping = true;
138 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
140 else if (preg_match('/^#IfNotRow3D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
141 if (tableExists($matches[1])) {
142 $skipping = tableHasRow3D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7]);
144 else {
145 // If no such table then the row is deemed not "missing".
146 $skipping = true;
148 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
150 else if (preg_match('/^#IfNotRow4D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
151 if (tableExists($matches[1])) {
152 $skipping = tableHasRow4D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5], $matches[6], $matches[7], $matches[8], $matches[9]);
154 else {
155 // If no such table then the row is deemed not "missing".
156 $skipping = true;
158 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
160 else if (preg_match('/^#IfNotRow2Dx2\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
161 if (tableExists($matches[1])) {
162 // If either check exist, then will skip
163 $firstCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
164 $secondCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[6], $matches[7]);
165 if ($firstCheck || $secondCheck) {
166 $skipping = true;
168 else {
169 $skipping = false;
172 else {
173 // If no such table then the row is deemed not "missing".
174 $skipping = true;
176 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
178 else if (preg_match('/^#EndIf/', $line)) {
179 $skipping = false;
182 if (preg_match('/^\s*#/', $line)) continue;
183 if ($skipping) continue;
185 $query = $query . $line;
186 if (substr($query, -1) == ';') {
187 $query = rtrim($query, ';');
188 echo "$query<br />\n";
189 if (!sqlStatement($query)) {
190 echo "<font color='red'>The above statement failed: " .
191 mysql_error() . "<br />Upgrading will continue.<br /></font>\n";
193 $query = '';
196 flush();
197 } // end function
199 $versions = array();
200 $sqldir = "$webserver_root/sql";
201 $dh = opendir($sqldir);
202 if (! $dh) die("Cannot read $sqldir");
203 while (false !== ($sfname = readdir($dh))) {
204 if (substr($sfname, 0, 1) == '.') continue;
205 if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
206 $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
207 $versions[$version] = $sfname;
210 closedir($dh);
211 ksort($versions);
213 <html>
214 <head>
215 <title>OpenEMR Database Upgrade</title>
216 <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
217 </head>
218 <body>
219 <center>
220 <span class='title'>OpenEMR Database Upgrade</span>
221 <br>
222 </center>
223 <?php
224 if (!empty($_POST['form_submit'])) {
225 $form_old_version = $_POST['form_old_version'];
227 foreach ($versions as $version => $filename) {
228 if (strcmp($version, $form_old_version) < 0) continue;
229 upgradeFromSqlFile($filename);
232 if (!empty($GLOBALS['ippf_specific'])) {
233 // Upgrade custom stuff for IPPF.
234 upgradeFromSqlFile('ippf_upgrade.sql');
237 flush();
238 echo "<font color='green'>Updating global configuration defaults...</font><br />\n";
239 require_once("library/globals.inc.php");
240 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
241 foreach ($grparr as $fldid => $fldarr) {
242 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
243 if (substr($fldtype, 0, 2) !== 'm_') {
244 $row = sqlQuery("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
245 if (empty($row['count'])) {
246 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
247 "VALUES ( '$fldid', '0', '$flddef' )");
253 echo "<font color='green'>Updating version indicators...</font><br />\n";
254 sqlStatement("UPDATE version SET v_major = '$v_major', v_minor = '$v_minor', " .
255 "v_patch = '$v_patch', v_tag = '$v_tag', v_database = '$v_database'");
257 echo "<p><font color='green'>Database upgrade finished.</font></p>\n";
258 echo "</body></html>\n";
259 exit();
263 <center>
264 <form method='post' action='sql_upgrade.php'>
265 <p>Please select the prior release you are converting from:
266 <select name='form_old_version'>
267 <?php
268 foreach ($versions as $version => $filename) {
269 echo " <option value='$version'";
270 // Defaulting to most recent version, which is now 4.0.0.
271 if ($version === '4.0.0') echo " selected";
272 echo ">$version</option>\n";
275 </select>
276 </p>
277 <p>If you are unsure or were using a development version between two
278 releases, then choose the older of possible releases.</p>
279 <p><input type='submit' name='form_submit' value='Upgrade Database' /></p>
280 </form>
281 </center>
282 </body>
283 </html>