From 724c57d309272ca8bb4586dc2cfe4ab6167c60de Mon Sep 17 00:00:00 2001
From: OpenXE <>
Date: Tue, 13 Dec 2022 18:23:37 +0000
Subject: [PATCH] upgrade improved folder handling, git init

---
 upgrade/upgrade.php           | 121 ++++++++++++++++++----------------
 www/pages/content/upgrade.tpl |   6 +-
 www/pages/upgrade.php         |  14 ++--
 3 files changed, 76 insertions(+), 65 deletions(-)

diff --git a/upgrade/upgrade.php b/upgrade/upgrade.php
index 3872de06..bf996bc6 100644
--- a/upgrade/upgrade.php
+++ b/upgrade/upgrade.php
@@ -48,25 +48,22 @@ function git(string $command, &$output, bool $show_command, bool $show_output, s
             echo_output($output);
         }
     }
+    if ($retval != 0) {
+        echo_out($error_text."\n");
+    }
     return($retval);
 }
 
 // -------------------------------- START
 
 // Check for correct call method
-$directory = "";
 if (php_sapi_name() == "cli") {
-    $cli = true;
-    $directory = ".";
-} else if (basename(getcwd()) != 'upgrade') {
-    // Started from "www"
-    $directory = "../upgrade";
-} else {
-    abort("Must be executed from 'upgrade' directory.");
-    return(-1);
-}
-
-if ($cli) {
+ 
+    $directory = getcwd();
+    if (basename($directory) != 'upgrade') {
+        abort("Must be executed from 'upgrade' directory.");
+        return(-1);
+    }
 
     $check_git = false;
     $do_git = false;
@@ -137,39 +134,8 @@ if ($cli) {
 // -------------------------------- END
 
 function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do_git, bool $export_db, bool $check_db, bool $do_db, bool $force, bool $connection) {  
-
-    if ($connection) {
-        $connection_file_name = $directory."/data/connection.json";
-        $connection_file_contents = file_get_contents($connection_file_name);
-        if (!$connection_file_contents) {
-            abort("Unable to load $connection_file_name");
-            return(-1);
-        } 
-        $connection_info = json_decode($connection_file_contents, true);
-
-        $host = $connection_info['host'];
-        $user = $connection_info['user'];
-        $passwd = $connection_info['passwd'];
-        $schema = $connection_info['schema'];
-
-    } else {
-
-        class DatabaseConnectionInfo {
-            function __construct($dir) {
-                require($dir."/../conf/user.inc.php");
-            }
-        }
-
-        $dbci = new DatabaseConnectionInfo($directory);
-
-        $host = $dbci->WFdbhost;
-        $user = $dbci->WFdbuser;
-        $passwd = $dbci->WFdbpass;
-        $schema = $dbci->WFdbname;
-    }
-
-    require_once($directory.'/../vendor/mustal/mustal_mysql_upgrade_tool.php');
-
+  
+    $mainfolder = dirname($directory);
     $datafolder = $directory."/data";
     $lockfile_name = $datafolder."/.in_progress.flag";
     $remote_file_name = $datafolder."/remote.json";
@@ -185,22 +151,18 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do
         abort("Unable to load $remote_file_name");
         return(-1);
     } 
-    $remote_info = json_decode($remote_info_contents, true);
-
-    $modified_files = false;
+    $remote_info = json_decode($remote_info_contents, true);    
 
     if ($check_git || $do_git) {
-        // Get changed files on system -> Should be empty
-        $output = array();
-        $retval = git("ls-files -m ..", $output,$verbose,false,"Git not initialized.");
-        if (!empty($output)) {
-            $modified_files = true;
-            echo_out("There are modified files:\n");
-            echo_output($output);
-        }
 
+        $retval = git("log HEAD", $output,$verbose,false,"");
         // Not a git repository -> Create it and then go ahead
-        if ($retval == 128) { 
+        if ($retval == 128) {         
+            if (!$do_git) {
+                abort("Git not initialized, use -do to initialize.");
+                return(-1);               
+            }
+
             echo_out("Setting up git...");
             $retval = git("init ..", $output,$verbose,$verbose,"Error while initializing git!");
             if ($retval != 0) {
@@ -227,7 +189,17 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do
             abort("Error while executing git!");
             return(-1);
         }
-
+    
+        // Get changed files on system -> Should be empty
+        $modified_files = false;
+        $output = array();
+        $retval = git("ls-files -m ..", $output,$verbose,false,"Error while checking Git status.");
+        if (!empty($output)) {
+            $modified_files = true;
+            echo_out("There are modified files:\n");
+            echo_output($output);
+        }
+ 
         if ($verbose) {
             echo_out("--------------- Upgrade history ---------------\n");
             $retval = git("log --date=short-local --pretty=\"%cd (%h): %s\" HEAD --not HEAD~5",$output,$verbose,$verbose,"Error while showing history!");
@@ -302,6 +274,39 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do
     } // $check_git
 
     if ($check_db || $do_db || $export_db) {
+
+        if ($connection) {
+            $connection_file_name = $directory."/data/connection.json";
+            $connection_file_contents = file_get_contents($connection_file_name);
+            if (!$connection_file_contents) {
+                abort("Unable to load $connection_file_name");
+                return(-1);
+            } 
+            $connection_info = json_decode($connection_file_contents, true);
+
+            $host = $connection_info['host'];
+            $user = $connection_info['user'];
+            $passwd = $connection_info['passwd'];
+            $schema = $connection_info['schema'];
+
+        } else {
+
+            class DatabaseConnectionInfo {
+                function __construct($dir) {
+                    require($dir."/../conf/user.inc.php");
+                }
+            }
+
+            $dbci = new DatabaseConnectionInfo($directory);
+
+            $host = $dbci->WFdbhost;
+            $user = $dbci->WFdbuser;
+            $passwd = $dbci->WFdbpass;
+            $schema = $dbci->WFdbname;
+        }
+
+        require_once($directory.'/../vendor/mustal/mustal_mysql_upgrade_tool.php');
+
         echo_out("--------------- Loading from database '$schema@$host'... ---------------\n");
         $db_def = mustal_load_tables_from_db($host, $schema, $user, $passwd, $mustal_replacers);
 
diff --git a/www/pages/content/upgrade.tpl b/www/pages/content/upgrade.tpl
index 58368878..0bdca245 100644
--- a/www/pages/content/upgrade.tpl
+++ b/www/pages/content/upgrade.tpl
@@ -53,11 +53,11 @@
                                 <table width="100%" border="0" class="mkTableFormular">
                                     <tr><td colspan=2><button name="submit" value="refresh" class="ui-button-icon" style="width:100%;">Anzeige auffrischen</button></td></tr>
                                     <tr><td colspan=2><button name="submit" value="check_upgrade" class="ui-button-icon" style="width:100%;">Upgrades pr&uuml;fen</button></td></tr>
-                                    <tr><td style="width:100%;">{|Upgrade-Details anzeigen|}:</td><td><input type="checkbox" name="details_anzeigen" value=1 size="20"></td></tr>
+                                    <tr><td style="width:100%;">{|Upgrade-Details anzeigen|}:</td><td><input type="checkbox" name="details_anzeigen" value=1 [DETAILS_ANZEIGEN] size="20"></td></tr>
                                     <tr [UPGRADE_VISIBLE]><td colspan=2><button name="submit" value="do_upgrade" class="ui-button-icon" style="width:100%;">UPGRADE</button></td></tr>
-                                    <tr [UPGRADE_VISIBLE]><td style="width:100%;">{|Erzwingen (-f)|}:</td><td><input type="checkbox" name="erzwingen" value=1 size="20"></td></tr>
+                                    <tr [UPGRADE_VISIBLE]><td style="width:100%;">{|Erzwingen (-f)|}:</td><td><input type="checkbox" name="erzwingen" value=1 [ERZWINGEN] size="20"></td></tr>
                                     <tr><td colspan=2><button name="submit" value="check_db" class="ui-button-icon" style="width:100%;">Datenbank pr&uuml;fen</button></td></tr>
-                                    <tr><td style="width:100%;">{|Datenbank-Details anzeigen|}:</td><td><input type="checkbox" name="db_details_anzeigen" value=1 size="20"></td></tr>
+                                    <tr><td style="width:100%;">{|Datenbank-Details anzeigen|}:</td><td><input type="checkbox" name="db_details_anzeigen" value=1 [DB_DETAILS_ANZEIGEN] size="20"></td></tr>
                                     <tr [UPGRADE_DB_VISIBLE]><td colspan=2><button name="submit" value="do_db_upgrade" class="ui-button-icon" style="width:100%;">Datenbank UPGRADE</button></td></tr>
                                 </table>
                             </fieldset>            
diff --git a/www/pages/upgrade.php b/www/pages/upgrade.php
index 2f64ead4..dd76180b 100644
--- a/www/pages/upgrade.php
+++ b/www/pages/upgrade.php
@@ -30,6 +30,9 @@ class upgrade {
         $db_verbose = $this->app->Secure->GetPOST('db_details_anzeigen') === '1';
         $force = $this->app->Secure->GetPOST('erzwingen') === '1';
 
+      	$this->app->Tpl->Set('DETAILS_ANZEIGEN', $verbose?"checked":"");
+      	$this->app->Tpl->Set('DB_DETAILS_ANZEIGEN', $db_verbose?"checked":"");
+
         include("../upgrade/upgrade.php");
 
         $logfile = "../upgrade/data/upgrade.log";
@@ -40,25 +43,27 @@ class upgrade {
 
         //function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do_git, bool $export_db, bool $check_db, bool $do_db, bool $force, bool   $connection)
 
+        $directory = dirname(getcwd())."/upgrade";
+
         switch ($submit) {
             case 'check_upgrade':
                 $this->app->Tpl->Set('UPGRADE_VISIBLE', "");
                 unlink($logfile);
-                upgrade_main("../upgrade",$verbose,true,false,false,true,false,$force,false);
+                upgrade_main($directory,$verbose,true,false,false,true,false,$force,false);
             break;
             case 'do_upgrade':
                 unlink($logfile);
-                upgrade_main("../upgrade",$verbose,true,true,false,true,true,$force,false);  
+                upgrade_main($directory,$verbose,true,true,false,true,true,$force,false);  
             break;    
             case 'check_db':
                 $this->app->Tpl->Set('UPGRADE_DB_VISIBLE', "");
                 unlink($logfile);
-                upgrade_main("../upgrade",$db_verbose,false,false,false,true,false,$force,false);  
+                upgrade_main($directory,$db_verbose,false,false,false,true,false,$force,false);  
             break;    
             case 'do_db_upgrade':
                 $this->app->Tpl->Set('UPGRADE_DB_VISIBLE', "");
                 unlink($logfile);
-                upgrade_main("../upgrade",$db_verbose,false,false,false,true,true,$force,false);  
+                upgrade_main($directory,$db_verbose,false,false,false,true,true,$force,false);  
             break;    
             case 'refresh':
             break;
@@ -73,3 +78,4 @@ class upgrade {
     
 
 }
+