Blame view

php/classes/BaseManager.php 17.6 KB
16035364   Benjamin Renard   First commit
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
<?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 (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
0a089287   Benjamin Renard   Add table definit...
48
        $vis = $this->xp->query("//vi/@id");
16035364   Benjamin Renard   First commit
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
 
        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;
    }
    
    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) 
    {
        $fileDom = new DomDocument("1.0");
        $fileDom->load($this->fileDomName);       
        $file = $fileDom->getElementById($fileName); 
        $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);			
                    $start = $file->getAttribute('start');
                    $stop = $file->getAttribute('stop');
                    $newFile->setAttribute('start', $start);
                    $newFile->setAttribute('stop', $stop);

                    $vi = $mask->parentNode;
                    $vi->appendChild($newFile);

                    $vi_start = $vi->getAttribute('start');
                    if ($vi_start > $start) $vi->setAttribute('start',$start);
                    $vi_stop = $vi->getAttribute('stop');
                    if ($vi_stop < $stop) $vi->setAttribute('stop',$stop);                          
                } 
        } 

        $this->xslt->transformToDoc($this->baseDom)->save($this->baseDomName);
                              
        return $lastMask;           
      }

/*
*       special add method for "simulation" files
*/      
    public function addWsFile($fileName, $start, $stop) 
    {
        // add file description to base.xml file
        $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 > $start) $vi->setAttribute('start',$start);
                    $vi_stop = $vi->getAttribute('stop');
                    if ($vi_stop < $stop) $vi->setAttribute('stop',$stop);                          
                } 
        }
 
        $this->xslt->transformToDoc($this->baseDom)->save($this->baseDomName);
                              
        return $lastMask;           
    }

/*
*        File was deleted
*/
    public function delFile($fileObj) 
    {                            
            $start = $fileObj->getAttribute($start);
            $stop = $fileObj->getAttribute($stop);
            $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);
      }

/*
*      Delete simu VI and simu Download Files
*/
    public function delSimuFiles($data)
    {
        if (file_exists(USERWSDIR.'WsParams.xml')) 
        {
            $simuVIs = $this->xp->query("//vi[@moreData = 'true']"); 
            $dom = new DomDocument("1.0");
            $dom->load(USERWSDIR.'WsParams.xml'); 
            $xp = new domxpath($dom); 
	    $dataList = $data.'List';
            $mysimudataList = $dom->getElementsByTagName($dataList);
            foreach ($simuVIs as $simuVI)
                {
                    $files = $simuVI->getElementsByTagName('file');
                    foreach ($files as $file) 
                    {	 
                        $fileName = $file->getAttribute('name');
                        if (file_exists(USERDATADIR.$fileName)) unlink(USERDATADIR.$fileName);
                    } 
0a089287   Benjamin Renard   Add table definit...
201
                    $vi =  $simuVI->getAttribute("id");
16035364   Benjamin Renard   First commit
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
		    $mask = $simuVI->getElementsByTagName('mask')->item(0)->nodeValue; 
		    if ((strpos($mask, 'CLWEB_') !== false && $data == "mywsrdata") || (strpos($mask, 'CLWEB_') === false && $data == "mysimudata")) { 
		    $mysimdata = $xp->query("//".$data."[@mask='".$mask."']/@*[namespace-uri()='http://www.w3.org/XML/1998/namespace' and local-name()='id']");
		    $sumuParamName = $mysimdata->item(0)->nodeValue;
                    $base = $simuVI->parentNode;   
                    $base->removeChild($simuVI);
 
                    if (file_exists(USERWSDIR.$sumuParamName.'.xml'))
                                    unlink(USERWSDIR.$sumuParamName.'.xml');
		    }
                }

                $mysimudata = $xp->query("//".$data.""); 

                if ($mysimudata->length > 0)
                {
                    foreach($mysimudata as $simudata)
                                    $mysimudataList->item(0)->removeChild($simudata);
                } 
                
                $dom->save(USERWSDIR.'WsParams.xml');
                $this->baseDom->save($this->baseDomName);
        }

      return;
     }

/*
*
*/      
    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;
    }

/*
*       
*/
0a089287   Benjamin Renard   Add table definit...
323
     public function addVI($fileMask, $format) 
16035364   Benjamin Renard   First commit
324
325
326
    {                                                             
        $newVi = $this->baseDom->createElement("vi");
        $newId = $this->getNewId();
0a089287   Benjamin Renard   Add table definit...
327
        $newVi->setAttribute('id', $newId);
7bd75a2a   Elena.Budnik   format = ASCII in...
328
        if ($format == 'txt') $format = 'ascii';
0a089287   Benjamin Renard   Add table definit...
329
        $newVi->setAttribute('format', strtoupper($format));
16035364   Benjamin Renard   First commit
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
        $newVi->appendChild($this->baseDom->createElement("mask",$fileMask)); 
        $this->appendFiles($newVi, $fileMask);
        
        $this->baseDom->getElementsByTagName("base")->item(0)->appendChild($newVi); 
        /* SORT files by Start Time */         
        $this->xslt->transformToDoc($this->baseDom)->save($this->baseDomName); 

        return $newId;            
      }

/*
*       
*/      
    public function addSimuVI($p)
    {
            $newVi = $this->baseDom->createElement("vi");
            $newId = $this->getNewId();
0a089287   Benjamin Renard   Add table definit...
347
            $newVi->setAttribute('id', $newId);
16035364   Benjamin Renard   First commit
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
            $newVi->appendChild($this->baseDom->createElement("mask",$p->mask)); 
            $newVi->appendChild($this->baseDom->createElement("param",$p->name)); 
            $newVi->setAttribute("start", $p->start);
            $newVi->setAttribute("stop", $p->stop);
            $newVi->setAttribute("moreData", "true");
            
//               $this->appendFiles($newVi, $fileMask);
            $newFile = $this->baseDom->createElement("file");
            $newFile->setAttribute('name', $p->file);
            $newFile->setAttribute('start', $p->start);
            $newFile->setAttribute('stop', $p->stop);
            $this->baseDom->getElementsByTagName("base")->item(0)->appendChild($newVi);

            if (strpos($p->mask,'___CCMC_')=== FALSE){
                $arguments = $this->baseDom->createElement("arguments");
                
                foreach ($p->args[$p->name] AS $key => $val) {
                  $arguments->appendChild($this->baseDom->createElement($key,$val)); 
                }
                $newVi->appendChild($arguments);
              } 
             
            $newVi->appendChild($newFile);
//          $this->baseDom->getElementsByTagName("base")->item(0)->appendChild($newFile); 
            /* 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) 
0a089287   Benjamin Renard   Add table definit...
392
                    return $vi->item(0)->getAttribute("id");
16035364   Benjamin Renard   First commit
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510

        return -1;
     }
     
    public function existsSimuMask($start,$stop,$mask)
    {
        $vi = $this->xp->query("//vi[mask='".$mask."' and @start<=".$start." and @stop>=".$stop."]");

        if ($vi->length > 0) 
                    return true;
        else 
                    return false;
    }
    
    public function existsWsrMask($start,$stop,$mask)
    {
        $vi = $this->xp->query("//vi[mask='".$mask."' and @start<=".$start." and @stop>=".$stop."]");

        if ($vi->length > 0) 
                    return true;
        else 
                    return false;
    }

    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();

        $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);
    }

     public function getStartStopParam ($start,$stop,$mask) {
        $vi = $this->xp->query("//vi[mask='".$mask."']");
        $isData = $this->xp->query("//vi[mask='".$mask."' and @start<=".$start." and @stop>=".$stop."]");
        
        if ($isData->length > 0) 
		    return true;
        else {
        if ($vi->length > 0) {
	    $viStart = $vi->item(0)->getAttribute('start');
	    $viStop  = $vi->item(0)->getAttribute('stop');
	    if ($start < $viStart){
	      if (($viStart - $start) > 60){
		  $newStart = $start; 
		  $newStop  = $viStart;
		}
	    }
	    elseif ($start >= $viStop){
	      $newStart = $start; 
	      $newStop  = $stop;
	    }
	    elseif (($start = $viStart) && ($stop > $viStop)){
	      $newStart = $viStop;
	      $newStop  = $stop;
	    }
	    else {
	      if ($stop <= $viStart){
		$newStart = $start; 
		$newStop  = $stop;
		}
	      elseif ($stop > $viStop){
		$newStart = $viStop; 
		$newStop  = $stop;
	      }
	    }
	  }
	   return  array('start' => $newStart, 'stop' => $newStop);
	}
                   
      }
    
    public function getStartStop($id) 
    {  
469237a5   Elena.Budnik   base.xml xml:id w...
511
512
513
514
515
516
517
518
519
520
       $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);
             
16035364   Benjamin Renard   First commit
521
522
523
        $start = $vi->getAttribute("start"); 
        $stop = $vi->getAttribute("stop");

469237a5   Elena.Budnik   base.xml xml:id w...
524
        return date("Y-m-d", $start)."T".date("H:i:s", $start)."-".date("Y-m-d", $stop)."T".date("H:i:s", $stop);
16035364   Benjamin Renard   First commit
525
526
527
528
529
530
531
532
533
534
    } 
 

    public function getViDesc($vi)
    { 
        return $this->getStartStop($vi);   
    }
     
}
?>