From 90097c594d47da903a81fe4b2239f4db917658f5 Mon Sep 17 00:00:00 2001
From: Benjamin Renard <benjamin.renard@akka.eu>
Date: Thu, 7 May 2020 14:01:36 +0200
Subject: [PATCH] Create a backup before WS migration

---
 src/InputOutput/IHMImpl/Config/IHMConfigClass.php       | 16 ++++++++++++++++
 src/InputOutput/IHMImpl/Tools/IHMUserWSManagerClass.php | 33 ++++++++++++++++++++++-----------
 2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/src/InputOutput/IHMImpl/Config/IHMConfigClass.php b/src/InputOutput/IHMImpl/Config/IHMConfigClass.php
index f0cf4b0..96adc9b 100644
--- a/src/InputOutput/IHMImpl/Config/IHMConfigClass.php
+++ b/src/InputOutput/IHMImpl/Config/IHMConfigClass.php
@@ -49,6 +49,8 @@ class IHMConfigClass
 
 	private static $processMgrFile = "processManager.xml";
 
+	private static $migrationBackupDir = "MIGRATION_BACKUP";
+
 	private static $jobsMgrFile = "jobs.xml";
 	
 	private static $userParamMgrFile = "WsParams.xml";
@@ -112,6 +114,20 @@ class IHMConfigClass
 	{
 		return self::getDataDir().self::$processMgrFile;
 	}
+
+	public static function getMigrationBackupPath($version)
+	{
+		$backupPath = self::getDataDir().self::$migrationBackupDir;
+		if (!is_dir($backupPath))
+			mkdir($backupPath);
+		$backupPath .= ("".$version."/");
+		if (!is_dir($backupPath))
+			mkdir($backupPath);
+		$backupPath .= (self::$userName.'/');
+		if (!is_dir($backupPath))
+			mkdir($backupPath);
+		return mkdir($backupPath);
+	}
 	
 	public static function getGenericDataPath()
 	{
diff --git a/src/InputOutput/IHMImpl/Tools/IHMUserWSManagerClass.php b/src/InputOutput/IHMImpl/Tools/IHMUserWSManagerClass.php
index 9f5a152..30caed4 100644
--- a/src/InputOutput/IHMImpl/Tools/IHMUserWSManagerClass.php
+++ b/src/InputOutput/IHMImpl/Tools/IHMUserWSManagerClass.php
@@ -56,7 +56,6 @@ class IHMUserWSManagerClass
 			if (empty($this->wsInfo)) {
 				return FALSE;
 			}
-			$this->wsInfo["version"] = 0;// DEBUG - To Remove!!!
 
 			return TRUE;
 		}
@@ -161,20 +160,32 @@ class IHMUserWSManagerClass
 				}
 			}
 		}
+
+		if (!empty($split_plot)) {
+			//Backup
+			$backupPath = IHMConfigClass::getMigrationBackupPath($this->wsInfo['version']);
+			copy($req_mgr_file_path, $backupPath.basename($req_mgr_file_path));
+			foreach ($split_plot as $old_key => $split) {
+				$req_data_path = IHMConfigClass::getStoredRequestPath() . $old_key;
+				copy($req_data_path, $backupPath.basename($req_data_path));
+			}
 		
-		//Apply split in user requests file
-		foreach ($split_plot as $old_key => $split) {
-			$parentNode = $plot_requests[$old_key]->parentNode;
-			$parentNode->removeChild($plot_requests[$old_key]);
-			foreach ($split as $new_key => $new_name) {
-				$new_node = $dom->createElement("request");
-				$new_node->setAttribute("xml:id", $new_key);
-				$new_node->setAttribute("name", $new_name);
-				$parentNode->appendChild($new_node);
+			//Apply split in user requests file + cleanup
+			foreach ($split_plot as $old_key => $split) {
+				$parentNode = $plot_requests[$old_key]->parentNode;
+				$parentNode->removeChild($plot_requests[$old_key]);
+				unlink(IHMConfigClass::getStoredRequestPath() . $old_key);
+				foreach ($split as $new_key => $new_name) {
+					$new_node = $dom->createElement("request");
+					$new_node->setAttribute("xml:id", $new_key);
+					$new_node->setAttribute("name", $new_name);
+					$parentNode->appendChild($new_node);
+				}
 			}
+
+			$dom->save($req_mgr_file_path);
 		}
 
-		$dom->save($req_mgr_file_path);
 		return TRUE;
 	}
 }
--
libgit2 0.21.2