changes to prior commit
[openemr.git] / sql_upgrade.php
blobc84afe4562ed7b0a215993d4f8acf1ef53b73567
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 // Checks if the server's PHP version is compatible with OpenEMR:
14 require_once(dirname(__FILE__) . "/common/compatibility/Checker.php");
16 $response = OpenEMR\Checker::checkPhpVersion();
17 if ($response !== true) {
18 die($response);
21 // Disable PHP timeout. This will not work in safe mode.
22 ini_set('max_execution_time', '0');
24 $ignoreAuth = true; // no login required
26 require_once('interface/globals.php');
27 require_once('library/sql_upgrade_fx.php');
29 $versionService = new \services\VersionService();
31 // Fetching current version because it was updated by the sql_upgrade_fx
32 // script and this script will further modify it.
33 $currentVersion = $versionService->fetch();
35 $desiredVersion = $currentVersion;
36 $desiredVersion->setDatabase($v_database);
37 $desiredVersion->setTag($v_tag);
38 $desiredVersion->setRealPatch($v_realpatch);
39 $desiredVersion->setPatch($v_patch);
40 $desiredVersion->setMinor($v_minor);
41 $desiredVersion->setMajor($v_major);
43 // Force logging off
44 $GLOBALS["enable_auditlog"]=0;
46 $versions = array();
47 $sqldir = "$webserver_root/sql";
48 $dh = opendir($sqldir);
49 if (! $dh) die("Cannot read $sqldir");
50 while (false !== ($sfname = readdir($dh))) {
51 if (substr($sfname, 0, 1) == '.') continue;
52 if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
53 $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
54 $versions[$version] = $sfname;
57 closedir($dh);
58 ksort($versions);
60 <html>
61 <head>
62 <title>OpenEMR Database Upgrade</title>
63 <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
64 <link rel="shortcut icon" href="public/images/favicon.ico" />
65 </head>
66 <body>
67 <center>
68 <span class='title'>OpenEMR Database Upgrade</span>
69 <br>
70 </center>
71 <?php
72 if (!empty($_POST['form_submit'])) {
73 $form_old_version = $_POST['form_old_version'];
75 foreach ($versions as $version => $filename) {
76 if (strcmp($version, $form_old_version) < 0) continue;
77 upgradeFromSqlFile($filename);
80 if (!empty($GLOBALS['ippf_specific'])) {
81 // Upgrade custom stuff for IPPF.
82 upgradeFromSqlFile('ippf_upgrade.sql');
85 if ( (!empty($v_realpatch)) && ($v_realpatch != "") && ($v_realpatch > 0) ) {
86 // This release contains a patch file, so process it.
87 upgradeFromSqlFile('patch.sql');
90 flush();
92 echo "<font color='green'>Updating global configuration defaults...</font><br />\n";
93 require_once("library/globals.inc.php");
94 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
95 foreach ($grparr as $fldid => $fldarr) {
96 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
97 if ( is_array($fldtype) || (substr($fldtype, 0, 2) !== 'm_') ) {
98 $row = sqlQuery("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
99 if (empty($row['count'])) {
100 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
101 "VALUES ( '$fldid', '0', '$flddef' )");
107 echo "<font color='green'>Updating Access Controls...</font><br />\n";
108 require("acl_upgrade.php");
109 echo "<br />\n";
111 $canRealPatchBeApplied = $versionService->canRealPatchBeApplied($desiredVersion);
112 $line = "Updating version indicators";
114 if ($canRealPatchBeApplied) {
115 $line = $line . ". Patch was also installed, updating version patch indicator";
118 echo "<font color='green'>" . $line . "...</font><br />\n";
119 $result = $versionService->update($desiredVersion);
121 if (!$result) {
122 echo "<font color='red'>Version could not be updated</font><br />\n";
123 exit();
126 echo "<p><font color='green'>Database and Access Control upgrade finished.</font></p>\n";
127 echo "</body></html>\n";
128 exit();
132 <center>
133 <form method='post' action='sql_upgrade.php'>
134 <p>Please select the prior release you are converting from:
135 <select name='form_old_version'>
136 <?php
137 foreach ($versions as $version => $filename) {
138 echo " <option value='$version'";
139 // Defaulting to most recent version, which is now 5.0.0.
140 if ($version === '5.0.0') echo " selected";
141 echo ">$version</option>\n";
144 </select>
145 </p>
146 <p>If you are unsure or were using a development version between two
147 releases, then choose the older of possible releases.</p>
148 <p style="color:red">If you are upgrading from a version below 5.0.0 to version 5.0.0 or greater, do note that this upgrade can take anywhere from several minutes to several hours (you will only see a whitescreen until it is complete; do not stop the script before it is complete or you risk corrupting your data).</p>
149 <p><input type='submit' name='form_submit' value='Upgrade Database' /></p>
150 </form>
151 </center>
152 </body>
153 </html>