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
class BaseManager 
{
	public $xp, $xslt;
	public $baseDom; 
	private $fileDomName, $baseDomName;

	function __construct() 
	{  
57f8eda0   Benjamin Renard   Fix some errors d...
18
19
		if (!defined("DELTA"))
			define("DELTA", 120);   // minumum file size in secs 
bf74fc2d   Elena.Budnik   IMPEX
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
		
		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
44
45
46
47

/*
*   Provide an unique ID
*/
bf74fc2d   Elena.Budnik   IMPEX
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	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
71
    
bf74fc2d   Elena.Budnik   IMPEX
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
   /*
   *  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
101

bf74fc2d   Elena.Budnik   IMPEX
102
103
104
105
106
	/*
	*   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...
107
108
	  
	  // standard file upload : start / stop get from file
bf74fc2d   Elena.Budnik   IMPEX
109
110
111
112
113
114
		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...
115
			$stop = $file->getAttribute('stop');			
bf74fc2d   Elena.Budnik   IMPEX
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
		}
		
		$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
153
154
155
/*
*        File was deleted
*/
bf74fc2d   Elena.Budnik   IMPEX
156
157
	public function delFile($fileObj) 
	{                            
bf74fc2d   Elena.Budnik   IMPEX
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
		$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
177

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

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

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

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

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

bf74fc2d   Elena.Budnik   IMPEX
205
206
		return  max($stops);
	}
16035364   Benjamin Renard   First commit
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
     
/*
*     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
239
240
           $newVi->setAttribute("start", min($starts));
           $newVi->setAttribute("stop", max($stops));
16035364   Benjamin Renard   First commit
241
242
243
244
245
246
        }                          
      }

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

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

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

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

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

bf74fc2d   Elena.Budnik   IMPEX
307
308
		return -1;
	}
16035364   Benjamin Renard   First commit
309
     
bf74fc2d   Elena.Budnik   IMPEX
310
311
312
313
314
315
316
317
	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
318
   
bf74fc2d   Elena.Budnik   IMPEX
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
   /*
   * 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
372
		}
bf74fc2d   Elena.Budnik   IMPEX
373
		return array('start' => $start, 'stop' => $stop);
16035364   Benjamin Renard   First commit
374
	}
bf74fc2d   Elena.Budnik   IMPEX
375
376
377
378
379
380
381
382
383
384
385
386
	
   /*
	* 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...
387
			return "nodata";
16035364   Benjamin Renard   First commit
388

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

bf74fc2d   Elena.Budnik   IMPEX
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
		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
425
426
}
?>