Weno eRx feature build, take 5.
[openemr.git] / sql_upgrade.php
blob9603db4e80129bcd0ae1032a30e2ba249a850817
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 use OpenEMR\Services\VersionService;
31 $versionService = new VersionService();
33 // Fetching current version because it was updated by the sql_upgrade_fx
34 // script and this script will further modify it.
35 $currentVersion = $versionService->fetch();
37 $desiredVersion = $currentVersion;
38 $desiredVersion->setDatabase($v_database);
39 $desiredVersion->setTag($v_tag);
40 $desiredVersion->setRealPatch($v_realpatch);
41 $desiredVersion->setPatch($v_patch);
42 $desiredVersion->setMinor($v_minor);
43 $desiredVersion->setMajor($v_major);
45 // Force logging off
46 $GLOBALS["enable_auditlog"]=0;
48 $versions = array();
49 $sqldir = "$webserver_root/sql";
50 $dh = opendir($sqldir);
51 if (! $dh) {
52 die("Cannot read $sqldir");
55 while (false !== ($sfname = readdir($dh))) {
56 if (substr($sfname, 0, 1) == '.') {
57 continue;
60 if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
61 $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
62 $versions[$version] = $sfname;
66 closedir($dh);
67 ksort($versions);
69 <html>
70 <head>
71 <title>OpenEMR Database Upgrade</title>
72 <link rel='STYLESHEET' href='interface/themes/style_blue.css'>
73 <link rel="shortcut icon" href="public/images/favicon.ico" />
74 </head>
75 <body>
76 <center>
77 <span class='title'>OpenEMR Database Upgrade</span>
78 <br>
79 </center>
80 <?php
81 if (!empty($_POST['form_submit'])) {
82 $form_old_version = $_POST['form_old_version'];
84 foreach ($versions as $version => $filename) {
85 if (strcmp($version, $form_old_version) < 0) {
86 continue;
89 upgradeFromSqlFile($filename);
92 if (!empty($GLOBALS['ippf_specific'])) {
93 // Upgrade custom stuff for IPPF.
94 upgradeFromSqlFile('ippf_upgrade.sql');
97 if ((!empty($v_realpatch)) && ($v_realpatch != "") && ($v_realpatch > 0)) {
98 // This release contains a patch file, so process it.
99 upgradeFromSqlFile('patch.sql');
102 flush();
104 echo "<font color='green'>Updating global configuration defaults...</font><br />\n";
105 require_once("library/globals.inc.php");
106 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
107 foreach ($grparr as $fldid => $fldarr) {
108 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
109 if (is_array($fldtype) || (substr($fldtype, 0, 2) !== 'm_')) {
110 $row = sqlQuery("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
111 if (empty($row['count'])) {
112 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
113 "VALUES ( '$fldid', '0', '$flddef' )");
119 echo "<font color='green'>Updating Access Controls...</font><br />\n";
120 require("acl_upgrade.php");
121 echo "<br />\n";
123 $canRealPatchBeApplied = $versionService->canRealPatchBeApplied($desiredVersion);
124 $line = "Updating version indicators";
126 if ($canRealPatchBeApplied) {
127 $line = $line . ". Patch was also installed, updating version patch indicator";
130 echo "<font color='green'>" . $line . "...</font><br />\n";
131 $result = $versionService->update($desiredVersion);
133 if (!$result) {
134 echo "<font color='red'>Version could not be updated</font><br />\n";
135 exit();
138 echo "<p><font color='green'>Database and Access Control upgrade finished.</font></p>\n";
139 echo "</body></html>\n";
140 exit();
144 <center>
145 <form method='post' action='sql_upgrade.php'>
146 <p>Please select the prior release you are converting from:
147 <select name='form_old_version'>
148 <?php
149 foreach ($versions as $version => $filename) {
150 echo " <option value='$version'";
151 // Defaulting to most recent version, which is now 5.0.0.
152 if ($version === '5.0.0') {
153 echo " selected";
156 echo ">$version</option>\n";
159 </select>
160 </p>
161 <p>If you are unsure or were using a development version between two
162 releases, then choose the older of possible releases.</p>
163 <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>
164 <p><input type='submit' name='form_submit' value='Upgrade Database' /></p>
165 </form>
166 </center>
167 </body>
168 </html>