Internationalization: some more documentation fixes
[openemr.git] / sql_upgrade.php
blobdebe404d170ad9075676d07b6efd3beece55f663
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 upgradeFromSqlFile($filename) {
55 global $webserver_root;
57 flush();
58 echo "<font color='green'>Processing $filename ...</font><br />\n";
60 $fullname = "$webserver_root/sql/$filename";
62 $fd = fopen($fullname, 'r');
63 if ($fd == FALSE) {
64 echo "ERROR. Could not open '$fullname'.\n";
65 flush();
66 break;
69 $query = "";
70 $line = "";
71 $skipping = false;
73 while (!feof ($fd)){
74 $line = fgets($fd, 2048);
75 $line = rtrim($line);
77 if (preg_match('/^\s*--/', $line)) continue;
78 if ($line == "") continue;
80 if (preg_match('/^#IfNotTable\s+(\S+)/', $line, $matches)) {
81 $skipping = tableExists($matches[1]);
82 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
84 else if (preg_match('/^#IfTable\s+(\S+)/', $line, $matches)) {
85 $skipping = ! tableExists($matches[1]);
86 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
88 else if (preg_match('/^#IfMissingColumn\s+(\S+)\s+(\S+)/', $line, $matches)) {
89 if (tableExists($matches[1])) {
90 $skipping = columnExists($matches[1], $matches[2]);
92 else {
93 // If no such table then the column is deemed not "missing".
94 $skipping = true;
96 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
98 else if (preg_match('/^#IfNotColumnType\s+(\S+)\s+(\S+)\s+(\S+)/', $line, $matches)) {
99 if (tableExists($matches[1])) {
100 $skipping = columnHasType($matches[1], $matches[2], $matches[3]);
102 else {
103 // If no such table then the column type is deemed not "missing".
104 $skipping = true;
106 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
108 else if (preg_match('/^#IfNotRow\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
109 if (tableExists($matches[1])) {
110 $skipping = tableHasRow($matches[1], $matches[2], $matches[3]);
112 else {
113 // If no such table then the row is deemed not "missing".
114 $skipping = true;
116 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
118 else if (preg_match('/^#IfNotRow2D\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
119 if (tableExists($matches[1])) {
120 $skipping = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
122 else {
123 // If no such table then the row is deemed not "missing".
124 $skipping = true;
126 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
128 else if (preg_match('/^#IfNotRow2Dx2\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/', $line, $matches)) {
129 if (tableExists($matches[1])) {
130 // If either check exist, then will skip
131 $firstCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
132 $secondCheck = tableHasRow2D($matches[1], $matches[2], $matches[3], $matches[6], $matches[7]);
133 if ($firstCheck || $secondCheck) {
134 $skipping = true;
136 else {
137 $skipping = false;
140 else {
141 // If no such table then the row is deemed not "missing".
142 $skipping = true;
144 if ($skipping) echo "<font color='green'>Skipping section $line</font><br />\n";
146 else if (preg_match('/^#EndIf/', $line)) {
147 $skipping = false;
150 if (preg_match('/^\s*#/', $line)) continue;
151 if ($skipping) continue;
153 $query = $query . $line;
154 if (substr($query, -1) == ';') {
155 $query = rtrim($query, ';');
156 echo "$query<br />\n";
157 if (!sqlStatement($query)) {
158 echo "<font color='red'>The above statement failed: " .
159 mysql_error() . "<br />Upgrading will continue.<br /></font>\n";
161 $query = '';
164 flush();
165 } // end function
167 $versions = array();
168 $sqldir = "$webserver_root/sql";
169 $dh = opendir($sqldir);
170 if (! $dh) die("Cannot read $sqldir");
171 while (false !== ($sfname = readdir($dh))) {
172 if (substr($sfname, 0, 1) == '.') continue;
173 if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
174 $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
175 $versions[$version] = $sfname;
178 closedir($dh);
179 ksort($versions);
181 <html>
182 <head>
183 <title>OpenEMR Database Upgrade</title>
184 <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
185 </head>
186 <body>
187 <center>
188 <span class='title'>OpenEMR Database Upgrade</span>
189 <br>
190 </center>
191 <?php
192 if (!empty($_POST['form_submit'])) {
193 $form_old_version = $_POST['form_old_version'];
195 foreach ($versions as $version => $filename) {
196 if (strcmp($version, $form_old_version) < 0) continue;
197 upgradeFromSqlFile($filename);
200 if (!empty($GLOBALS['ippf_specific'])) {
201 // Upgrade custom stuff for IPPF.
202 upgradeFromSqlFile('ippf_upgrade.sql');
205 flush();
206 echo "<font color='green'>Updating global configuration defaults...</font><br />\n";
207 require_once("library/globals.inc.php");
208 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
209 foreach ($grparr as $fldid => $fldarr) {
210 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
211 if (substr($fldtype, 0, 2) !== 'm_') {
212 $row = sqlQuery("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
213 if (empty($row['count'])) {
214 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
215 "VALUES ( '$fldid', '0', '$flddef' )");
221 echo "<font color='green'>Updating version indicators...</font><br />\n";
222 sqlStatement("UPDATE version SET v_major = '$v_major', v_minor = '$v_minor', " .
223 "v_patch = '$v_patch', v_tag = '$v_tag', v_database = '$v_database'");
225 echo "<p><font color='green'>Database upgrade finished.</font></p>\n";
226 echo "</body></html>\n";
227 exit();
231 <center>
232 <form method='post' action='sql_upgrade.php'>
233 <p>Please select the prior release you are converting from:
234 <select name='form_old_version'>
235 <?php
236 foreach ($versions as $version => $filename) {
237 echo " <option value='$version'";
238 // Defaulting to most recent version, which is now 3.2.0.
239 if ($version === '3.2.0') echo " selected";
240 echo ">$version</option>\n";
243 </select>
244 </p>
245 <p>If you are unsure or were using a development version between two
246 releases, then choose the older of possible releases.</p>
247 <p><input type='submit' name='form_submit' value='Upgrade Database' /></p>
248 </form>
249 </center>
250 </body>
251 </html>