Blame view

src/InputOutput/IHMImpl/Tools/IHMUserWSManagerClass.php 6.71 KB
574d7ed3   Benjamin Renard   First step to aut...
1
2
3
4
5
6
7
8
9
<?php

/**
 * @class IHMUserWSManagerClass
 * @brief Manager for IHM user workspace
 * @details
 */
class IHMUserWSManagerClass
{
5581ea41   Benjamin Renard   First implementat...
10
	private static $WS_VERSION = 3;
574d7ed3   Benjamin Renard   First step to aut...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

	protected $wsInfo      = null;

	public function init()
	{
		//check user workspace
		if (IHMConfigClass::getUserName() == "" || !is_dir(IHMConfigClass::getUserPath()))
			throw new Exception('Cannot find user workspace.');

		//Load info about WS
		if (!$this->loadWSInfoFile())
			throw new Exception('Error to load workspace info file.');

		//Update WS if need
		if (!$this->update())
			throw new Exception('Error during user workspace conversion.');

		return TRUE;
	}

	public function update()
	{
		while (IHMUserWSManagerClass::$WS_VERSION > $this->wsInfo['version']) {
			$updateMethod = "updateFromVersion".$this->wsInfo['version'];
			if (method_exists($this,$updateMethod)) {
				if (!$this->{$updateMethod}()) {
					return FALSE;
				}
			}
			$this->wsInfo['version'] += 1;
			if (!$this->saveWSInfoFile()) {
				return FALSE;
			}
		}
		return TRUE;
	}

	private function loadWSInfoFile()
	{
		if (file_exists(IHMConfigClass::getUserWSInfoFilePath())) {
			$infoContent = file_get_contents(IHMConfigClass::getUserWSInfoFilePath());
			if (empty($infoContent)) {
				return FALSE;
			}
			$this->wsInfo = json_decode($infoContent, TRUE);
			if (empty($this->wsInfo)) {
				return FALSE;
			}
307aa386   Benjamin Renard   Split plot requests
59

574d7ed3   Benjamin Renard   First step to aut...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
			return TRUE;
		}

		//Init info file
		$this->wsInfo = array(
			"version" => 0,
		);

		return $this->saveWSInfoFile();
	}

	private function saveWSInfoFile()
	{
		if (empty($this->wsInfo)) {
			return FALSE;
		}

		$json_data = json_encode($this->wsInfo);

		if ($json_data === FALSE) {
			return FALSE;
		}


		return file_put_contents(IHMConfigClass::getUserWSInfoFilePath(),$json_data);
	}

5581ea41   Benjamin Renard   First implementat...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	private function updateFromVersion2() {
		// Update WS for save statistic requests

		// Load user requests file
		$req_mgr_file_path = IHMConfigClass::getUserRequestManagerFilePath();
		if (!file_exists($req_mgr_file_path)) {
			return TRUE;
		}

		$dom = new DOMDocument();
		if (!$dom->load($req_mgr_file_path)) {
			return FALSE;
		}

		$statisticNodeTag = 'statisticList';
		$statisticNodes = $dom->documentElement->getElementsByTagName($statisticNodeTag);
		// Id statistic root node already exists, nothing to do
		if ($statisticNodes->length > 0) {
			return TRUE;
		}

		// Create statistic root node
		$statisticNode = $dom->createElement($statisticNodeTag);
		$statisticNode->setAttribute('xml:id', 'statistic-treeRootNode');
		$dom->documentElement->appendChild($statisticNode);

		return $dom->save($req_mgr_file_path);
	}

c907012c   Benjamin Renard   Migrate user WS t...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
	private function updateFromVersion1() {
		// Update WS for save download requests

		// Load user requests file
		$req_mgr_file_path = IHMConfigClass::getUserRequestManagerFilePath();
		if (!file_exists($req_mgr_file_path)) {
			return TRUE;
		}

		$dom = new DOMDocument();
		if (!$dom->load($req_mgr_file_path)) {
			return FALSE;
		}

		$downloadNodeTag = 'downloadList';
		$downloadNodes = $dom->documentElement->getElementsByTagName($downloadNodeTag);
		// Id download root node already exists, nothing to do
		if ($downloadNodes->length > 0) {
			return TRUE;
		}

		// Create download root node
		$downloadNode = $dom->createElement($downloadNodeTag);
		$downloadNode->setAttribute('xml:id', 'download-treeRootNode');
		$dom->documentElement->appendChild($downloadNode);
		
		return $dom->save($req_mgr_file_path);
	}

574d7ed3   Benjamin Renard   First step to aut...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
	private function updateFromVersion0() {
		// This update split plot requests

		// Load user requests file
		$req_mgr_file_path = IHMConfigClass::getUserRequestManagerFilePath();
		if (!file_exists($req_mgr_file_path)) {
			return TRUE;
		}

		$dom = new DOMDocument();
		if (!$dom->load($req_mgr_file_path)) {
			return FALSE;
		}

		// Retrieve all plot requests and last request id
		$plot_requests = array();
		$last_id = 0;
		foreach ($dom->getElementsByTagName('request') as $requestNode) {
			$request_id = $requestNode->getAttribute("xml:id");
			$plot_requests[$request_id] = $requestNode;
			sscanf($request_id, "req_%d", $crt_id);
			if ($crt_id > $last_id) {
				$last_id = $crt_id;
			}
		}

		if (empty($plot_requests)) {
			//No plot requests
			return TRUE;
		}

		// Split requests
307aa386   Benjamin Renard   Split plot requests
177
		$split_plot = array();
574d7ed3   Benjamin Renard   First step to aut...
178
179
180
181
182
183
184
185
186
		foreach ($plot_requests as $id => $node) {
			$req_data_path = IHMConfigClass::getStoredRequestPath() . $id;
			if (!file_exists($req_data_path)) {
				continue;
			}
			$req_data_content = file_get_contents($req_data_path);
			if (!$req_data_content) {
				continue;
			}
307aa386   Benjamin Renard   Split plot requests
187
			$req_data_json = json_decode($req_data_content,TRUE);
574d7ed3   Benjamin Renard   First step to aut...
188
189
190
			if (!$req_data_json) {
				continue;
			}
307aa386   Benjamin Renard   Split plot requests
191
192
193
194
195
196
			if (array_key_exists("tabs", $req_data_json)) {
				$split_plot[$id] = array();
				$tab_index = 1;
				foreach ($req_data_json["tabs"] as $tab) {
					$new_splitted_request = array_merge(array(), $tab);
					foreach (array_keys($req_data_json) as $key) {
74fd500a   Hacene SI HADJ MOHAND   us #9874 ms zoo...
197
						if (in_array($key, array('tabs', 'startDate', 'stopDate', 'durationDay', 'durationHour', 'durationMin', 'durationSec', 'durationMs'))) {
307aa386   Benjamin Renard   Split plot requests
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
							continue;
						}
						$new_splitted_request[$key] = $req_data_json[$key];
					}
					if (count($req_data_json["tabs"]) > 1) {
						if (!empty($new_splitted_request['tab-name'])) {
							$new_splitted_request['name'] .= ('_' . $new_splitted_request['tab-name']);
						}
						else {
							$new_splitted_request['name'] .= ('_Plot' . $tab_index);
						}
					}
					foreach (array("tab-name", "last-plotted-tab", "active-tab-id", "last-tab-id", "multi-plot-linked") as $key) {
						unset($new_splitted_request[$key]);
					}
					++$last_id;
					$new_splitted_request['id'] = "req_".$last_id;
					file_put_contents(IHMConfigClass::getStoredRequestPath() . $new_splitted_request['id'], json_encode($new_splitted_request));
					$split_plot[$id][$new_splitted_request['id']] = $new_splitted_request['name'];
					++$tab_index;
				}
			}
574d7ed3   Benjamin Renard   First step to aut...
220
		}
90097c59   Benjamin Renard   Create a backup b...
221
222
223
224
225
226
227
228
229

		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));
			}
574d7ed3   Benjamin Renard   First step to aut...
230
		
90097c59   Benjamin Renard   Create a backup b...
231
232
233
234
235
236
237
238
239
240
241
			//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);
				}
307aa386   Benjamin Renard   Split plot requests
242
			}
90097c59   Benjamin Renard   Create a backup b...
243
244

			$dom->save($req_mgr_file_path);
307aa386   Benjamin Renard   Split plot requests
245
		}
574d7ed3   Benjamin Renard   First step to aut...
246

574d7ed3   Benjamin Renard   First step to aut...
247
248
249
		return TRUE;
	}
}