BaseManager.php
10.9 KB
1
2
3
4
5
6
7
8
9
10
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
59
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
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
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/**
* @class BaseManager
* @brief Management of user uploaded data files in data base
* @author Elena
* @version $Id: BaseManager.php 2914 2015-05-19 10:31:38Z elena $
*
*/
class BaseManager
{
public $xp, $xslt;
public $baseDom;
private $fileDomName, $baseDomName;
function __construct()
{
if (!defined("DELTA"))
define("DELTA", 120); // minumum file size in secs
if (getenv('USER_DATA_PATH') != "")
$this->baseDomName = getenv('USER_DATA_PATH')."base.xml";
else
$this->baseDomName = USERDATADIR."base.xml";
$this->fileDomName = USERWSDIR."Files.xml";
$this->baseDom = new DomDocument("1.0");
if (file_exists($this->baseDomName))
$this->baseDom->load($this->baseDomName);
else {
$baseElement = $this->baseDom->createElement("base");
$this->baseDom->appendChild($baseElement);
}
$this->xp = new domxpath($this->baseDom);
$this->xslt = new XSLTProcessor();
$xsl = new DomDocument("1.0");
$xsl->load(XMLPATH."base.xsl");
$this->xslt->importStylesheet($xsl);
}
/*
* Provide an unique ID
*/
public function getNewId()
{
// Get all ID attributes
$vis = $this->xp->query("//vi/@id");
if ($vis->length > 0) {
$idList = array();
if($vis->length > 0) for ($i = 0; $i < $vis->length; $i++)
$idList[] = $vis->item($i)->nodeValue;
sort($idList);
for ($i = 0; $i < count($idList); $i++) {
if ($idList[$i] > $i) {
$newID = $i;
break;
}
$newID = $i+1;
}
} else
$newID = 0;
return $newID;
}
/*
* Get filename to create IMPEX parameter if VI exists already
*/
public function getFirstFileName($viID)
{
$vis = $this->xp->query("//vi[@id = '".$viID."']");
if ($vis->length > 0)
$vi = $vis->item(0);
else
throw new Exception('No VI '.$viID);
$files = $vi->getElementsByTagName('file');
if ($files->length > 0)
return $files->item(0)->getAttribute('name');
throw new Exception('Empty VI '.$viID);
}
public function fileMask($fileName, $maskName)
{
$mask = explode("*", $maskName);
for ($i = 0; $i < count($mask); $i++)
if ($mask[$i] != null)
if (strpos($fileName, $mask[$i]) === false) return false;
return true;
}
/*
* New file is uploaded -> check if it should be added into MASK
*/
public function addFile($fileName, $start = null, $stop = null)
{
// standard file upload : start / stop get from file
if (!$start)
{
$fileDom = new DomDocument("1.0");
$fileDom->load($this->fileDomName);
$file = $fileDom->getElementById($fileName);
$start = $file->getAttribute('start');
$stop = $file->getAttribute('stop');
}
$lastMask = null;
$masks = $this->baseDom->getElementsByTagName("mask");
if ($masks->length > 0)
{
foreach ($masks as $mask)
if ($this->fileMask($fileName, $mask->nodeValue))
{
$lastMask = $mask->nodeValue;
$newFile = $this->baseDom->createElement("file");
$newFile->setAttribute('name', $fileName);
$newFile->setAttribute('start', $start);
$newFile->setAttribute('stop', $stop);
$vi = $mask->parentNode;
$vi->appendChild($newFile);
$vi_start = $vi->getAttribute('start');
if (!$vi_start || $vi_start > $start)
$vi->setAttribute('start',$start);
$vi_stop = $vi->getAttribute('stop');
if (!$vi_stop || $vi_stop < $stop)
$vi->setAttribute('stop',$stop);
}
}
// sort by time
$this->xslt->transformToDoc($this->baseDom)->save($this->baseDomName);
return $lastMask;
}
/*
* File was deleted
*/
public function delFile($fileObj)
{
$vi = $fileObj->parentNode;
$vi->removeChild($fileObj);
$mask = $vi->getElementsByTagName("mask")->item(0)->nodeValue;
$length = $vi->getElementsByTagName("file")->length;
if ($length == 0)
{
$this->baseDom->getElementsByTagName("base")->item(0)->removeChild($vi);
}
else
{
$vi->setAttribute('start', $this->updateStart($vi));
$vi->setAttribute('stop',$this->updateStop($vi));
}
$this->baseDom->save($this->baseDomName);
return array('mask' => $mask, 'length' => $length);
}
/*
*
*/
public function updateStart($vi)
{
$files = $vi->getElementsByTagName("file");
if ($files->length == 0) return "EMPTY";
$starts = Array();
foreach ($files as $file)
$starts[] = $file->getAttribute("start");
return min($starts);
}
/*
*
*/
public function updateStop($vi)
{
$files = $vi->getElementsByTagName("file");
if ($files->length == 0) return "EMPTY";
$stops = Array();
foreach ($files as $file)
$stops[] = $file->getAttribute("stop");
return max($stops);
}
/*
* New mask was added
*/
public function appendFiles($newVi, $fileMask)
{
$fileDom = new DomDocument("1.0");
$fileDom->load($this->fileDomName);
$files = $fileDom->getElementsByTagName("file");
if ($files->length > 0)
{
$starts = Array();
$stops = Array();
foreach ($files as $file)
{
$fileName = $file->getAttribute("name");
if ($this->fileMask($fileName, $fileMask))
{
$newFile = $this->baseDom->createElement("file");
$newFile->setAttribute('name', $fileName);
$start = $file->getAttribute("start");
$stop = $file->getAttribute("stop");
$newFile->setAttribute('start', $start);
$newFile->setAttribute('stop', $stop);
$newVi->appendChild($newFile);
$starts[] = $start;
$stops[] = $stop;
}
}
$newVi->setAttribute("start", min($starts));
$newVi->setAttribute("stop", max($stops));
}
}
/*
*
*/
public function getOldMask($fileMask)
{
$masks = $this->baseDom->getElementsByTagName("mask");
if ($masks->length > 0)
{
foreach ($masks as $mask) {
// mask exists
if (strcmp($mask->nodeValue, $fileMask) == 0) return null;
if (strncmp($mask->nodeValue, $fileMask, 5) == 0)
{
// less common
if (strlen($mask->nodeValue) <= strlen($fileMask)) return null;
// more common - delete old, create new
return $mask->nodeValue;
}
}
}
return null;
}
/*
*
*/
public function addVI($fileMask, $format)
{
$newVi = $this->baseDom->createElement("vi");
$newId = $this->getNewId();
$newVi->setAttribute('id', $newId);
if ($format == 'txt') $format = 'ascii';
$newVi->setAttribute('format', strtoupper($format));
$newVi->appendChild($this->baseDom->createElement("mask",$fileMask));
$this->appendFiles($newVi, $fileMask);
$this->baseDom->documentElement->appendChild($newVi);
/* SORT files by Start Time */
$this->xslt->transformToDoc($this->baseDom)->save($this->baseDomName);
return $newId;
}
public function delVI($fileMask)
{
$vi = $this->xp->query("//vi[mask='".$fileMask."']");
if ($vi->length > 0)
$vi->item(0)->parentNode->removeChild($vi->item(0));
$this->baseDom->save($this->baseDomName);
}
public function getVI($fileMask)
{
$vi = $this->xp->query("//vi[mask='".$fileMask."']");
if ($vi->length > 0)
return $vi->item(0)->getAttribute("id");
return -1;
}
public function existsVi($mask)
{
$vi = $this->xp->query("//vi[mask='".$mask."']");
if ($vi->length > 0)
return true;
else
return false;
}
/*
* Check intervals to be ordered in the Remote Centers (IMPEX)
*/
public function intervalsToGet($requestStart, $requestStop, $mask)
{
$vis = $this->xp->query("//vi[mask='".$mask."']");
if ($vis->length == 0) return false;
$vi = $vis->item(0);
$files = $vi->getElementsByTagName('file');
$start = array();
$stop = array();
// otherwise - the case of IPIM - only one file
if (strpos($mask, "*") > 0)
{
$globalStart = (int)$vi->getAttribute("start");
$globalStop = (int)$vi->getAttribute("stop");
if ($requestStart > $globalStop || $requestStop < $globalStart)
{
$start[] = $requestStart;
$stop[] = $requestStop;
}
else
{
if ($requestStart < $globalStart - DELTA ) {
$start[] = $requestStart;
$stop[] = $globalStart;
}
if ($requestStop > $globalStop + DELTA ) {
$start[] = $globalStop;
$stop[] = $requestStop;
}
for ($i = 0; $i < $files->length; $i++)
if ((int)$files->item($i)->getAttribute("stop") >= $requestStart) break;
for ($j = $i; $j < $files->length; $j++)
if ((int)$files->item($j)->getAttribute("start") >= $requestStop) break;
$d = $j+1 < $files->length -1 ? $j+1 : $files->length - 1;
for ($k = $i; $k < $d ; $k++) {
$stopT = (int)$files->item($k)->getAttribute("stop");
$startT = (int)$files->item($k+1)->getAttribute("start");
if ($startT - $stopT > DELTA) {
$start[] = $stopT;
$stop[] = $startT;
}
}
}
}
return array('start' => $start, 'stop' => $stop);
}
/*
* Get VI Start - Stop
*/
public function getStartStop($id)
{
$this->baseDom->load($this->baseDomName);
$xpath = new DOMXpath($this->baseDom);
$vis = $xpath->query("//vi[@id=".$id."]");
if ($vis->length == 0)
return "nodata";
$vi = $vis->item(0);
$start = $vi->getAttribute("start");
$stop = $vi->getAttribute("stop");
return date("Y-m-d", $start)."T".date("H:i:s", $start)."-".date("Y-m-d", $stop)."T".date("H:i:s", $stop);
}
/*
* Return VI Description (Start - Stop)
*/
public function getViDesc($vi)
{
return $this->getStartStop($vi);
}
/*
* Delete Vi in base.xml and delete corresponding files in User DATA dir
*/
public function deleteViTotal($viId)
{
$vi = $this->xp->query("//vi[@id=".$viId."]");
if ($vi->length > 0) {
$files = $vi->item(0)->getElementsByTagName('file');
foreach ($files as $file)
{
$fileName = USERDATADIR.$file->getAttribute('name');
if (file_exists($fileName)) unlink($fileName);
}
$vi->item(0)->parentNode->removeChild($vi->item(0));
$this->baseDom->save($this->baseDomName);
}
}
}
?>