Blame view

php/classes/BaseManager.php 10.9 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
<?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 $
*    
*/
 
bf74fc2d   Elena.Budnik   IMPEX
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
class BaseManager 
{
	public $xp, $xslt;
	public $baseDom; 
	private $fileDomName, $baseDomName;

	function __construct() 
	{  
		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);
	}
16035364   Benjamin Renard   First commit
43
44
45
46

/*
*   Provide an unique ID
*/
bf74fc2d   Elena.Budnik   IMPEX
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	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;
	}
16035364   Benjamin Renard   First commit
70
    
bf74fc2d   Elena.Budnik   IMPEX
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
   /*
   *  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;
	}
16035364   Benjamin Renard   First commit
100

bf74fc2d   Elena.Budnik   IMPEX
101
102
103
104
105
	/*
	*   New file is uploaded -> check if it should be added into MASK 
	*/
	public function addFile($fileName, $start = null, $stop = null) 
	{
0a18b1cf   Elena.Budnik   bug while IMPEX a...
106
107
	  
	  // standard file upload : start / stop get from file
bf74fc2d   Elena.Budnik   IMPEX
108
109
110
111
112
113
		if (!$start)
		{
			$fileDom = new DomDocument("1.0");
			$fileDom->load($this->fileDomName);       
			$file = $fileDom->getElementById($fileName);
			$start = $file->getAttribute('start');
0a18b1cf   Elena.Budnik   bug while IMPEX a...
114
			$stop = $file->getAttribute('stop');			
bf74fc2d   Elena.Budnik   IMPEX
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
		}
		
		$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;           
	}
	
16035364   Benjamin Renard   First commit
152
153
154
/*
*        File was deleted
*/
bf74fc2d   Elena.Budnik   IMPEX
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
	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);
	}
16035364   Benjamin Renard   First commit
178

bf74fc2d   Elena.Budnik   IMPEX
179
180
181
182
183
184
185
	/*
	*
	*/      
	public function updateStart($vi) 
	{        
		$files = $vi->getElementsByTagName("file");
		if ($files->length == 0) return "EMPTY";
16035364   Benjamin Renard   First commit
186

bf74fc2d   Elena.Budnik   IMPEX
187
188
189
		$starts = Array();
		foreach ($files as $file) 
			$starts[] =  $file->getAttribute("start");
16035364   Benjamin Renard   First commit
190

bf74fc2d   Elena.Budnik   IMPEX
191
192
		return  min($starts);
	}
16035364   Benjamin Renard   First commit
193
194
195
196

/*
*
*/   
bf74fc2d   Elena.Budnik   IMPEX
197
198
199
200
	public function updateStop($vi) 
	{    
		$files = $vi->getElementsByTagName("file");
		if ($files->length == 0) return "EMPTY";
16035364   Benjamin Renard   First commit
201

bf74fc2d   Elena.Budnik   IMPEX
202
203
204
		$stops = Array();
		foreach ($files as $file) 
					$stops[] =  $file->getAttribute("stop");
16035364   Benjamin Renard   First commit
205

bf74fc2d   Elena.Budnik   IMPEX
206
207
		return  max($stops);
	}
16035364   Benjamin Renard   First commit
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
     
/*
*     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;
                } 
            } 

bf74fc2d   Elena.Budnik   IMPEX
240
241
           $newVi->setAttribute("start", min($starts));
           $newVi->setAttribute("stop", max($stops));
16035364   Benjamin Renard   First commit
242
243
244
245
246
247
        }                          
      }

/*
*       
*/
bf74fc2d   Elena.Budnik   IMPEX
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	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;
	}
16035364   Benjamin Renard   First commit
269
270
271
/*
*       
*/
bf74fc2d   Elena.Budnik   IMPEX
272
273
274
275
276
277
278
279
280
281
282
283
	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);
		
0a18b1cf   Elena.Budnik   bug while IMPEX a...
284
285
286
287
		$this->baseDom->documentElement->appendChild($newVi);
		
		 /* SORT files by Start Time */         
		$this->xslt->transformToDoc($this->baseDom)->save($this->baseDomName); 
bf74fc2d   Elena.Budnik   IMPEX
288
289
290
291
292
293
		return $newId;            
	}
 
	public function delVI($fileMask) 
	{          
		$vi = $this->xp->query("//vi[mask='".$fileMask."']");
16035364   Benjamin Renard   First commit
294

bf74fc2d   Elena.Budnik   IMPEX
295
296
		if ($vi->length > 0) 
					$vi->item(0)->parentNode->removeChild($vi->item(0));
16035364   Benjamin Renard   First commit
297

bf74fc2d   Elena.Budnik   IMPEX
298
299
		$this->baseDom->save($this->baseDomName); 
	} 
16035364   Benjamin Renard   First commit
300

bf74fc2d   Elena.Budnik   IMPEX
301
302
303
	public function getVI($fileMask) 
	{
		$vi = $this->xp->query("//vi[mask='".$fileMask."']");
16035364   Benjamin Renard   First commit
304

bf74fc2d   Elena.Budnik   IMPEX
305
306
		if ($vi->length > 0) 
						return $vi->item(0)->getAttribute("id");
16035364   Benjamin Renard   First commit
307

bf74fc2d   Elena.Budnik   IMPEX
308
309
		return -1;
	}
16035364   Benjamin Renard   First commit
310
     
bf74fc2d   Elena.Budnik   IMPEX
311
312
313
314
315
316
317
318
	public function existsVi($mask)
	{
		$vi = $this->xp->query("//vi[mask='".$mask."']");
		if ($vi->length > 0) 
						return true;
		else 
						return false;
	}
16035364   Benjamin Renard   First commit
319
   
bf74fc2d   Elena.Budnik   IMPEX
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
   /*
   * 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;         
						}
				}
			}
16035364   Benjamin Renard   First commit
373
		}
bf74fc2d   Elena.Budnik   IMPEX
374
		return array('start' => $start, 'stop' => $stop);
16035364   Benjamin Renard   First commit
375
	}
bf74fc2d   Elena.Budnik   IMPEX
376
377
378
379
380
381
382
383
384
385
386
387
	
   /*
	* 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) 
469237a5   Elena.Budnik   base.xml xml:id w...
388
			return "nodata";
16035364   Benjamin Renard   First commit
389

bf74fc2d   Elena.Budnik   IMPEX
390
391
392
393
		$vi = $vis->item(0);
				
		$start = $vi->getAttribute("start"); 
		$stop = $vi->getAttribute("stop");
16035364   Benjamin Renard   First commit
394

bf74fc2d   Elena.Budnik   IMPEX
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
		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);		
		} 
	} 
		
16035364   Benjamin Renard   First commit
426
427
}
?>