Commit a52d11df60b6953b24237681645b14d006dd2ed2

Authored by Hacene SI HADJ MOHAND
2 parents d906f187 5446b8f0

Merge branch 'master' of https://gitlab.irap.omp.eu/CDPP/AMDA_IHM

php/classes/CatalogCacheMgr.php
... ... @@ -3,14 +3,14 @@
3 3 /**
4 4 * @class CatalogCacheMgr
5 5 */
6   -
7   -
  6 +
  7 +
8 8 class CatIntervalCacheObject extends IntervalCacheObject
9 9 {
10   - // for catalog
  10 + // for catalog
11 11 private $params = array();
12   -
13   - public function toArray()
  12 +
  13 + public function toArray()
14 14 {
15 15 $result = array(
16 16 "cacheId" => $this->id,
... ... @@ -19,54 +19,54 @@ class CatIntervalCacheObject extends IntervalCacheObject
19 19 );
20 20 if ($this->isNew)
21 21 $result["isNew"] = true;
22   -
  22 +
23 23 if ($this->isModified)
24 24 $result["isModified"] = true;
25   -
26   - for ($i = 0; $i < count($this->params); $i++)
  25 +
  26 + for ($i = 0; $i < count($this->params); $i++)
27 27 {
28 28 $paramObject = array();
29 29 $index = 'param'.sprintf("%d",$i+2);
30   - $result[$index] = $this->params[$i];
31   - }
32   - return $result;
  30 + $result[$index] = $this->params[$i];
  31 + }
  32 + return $result;
33 33 }
34 34  
35   - // for catalog
36   - public function setParams($params)
  35 + // for catalog
  36 + public function setParams($params)
37 37 {
38   - $this->params = $params;
  38 + $this->params = $params;
39 39 }
40   -
41   - public function getParams()
42   - {
43   - return $this->params;
  40 +
  41 + public function getParams()
  42 + {
  43 + return $this->params;
44 44 }
45   -
46   - public function writeBin($handle, $paramsNumber, $paramsSizes, $paramsTypes)
  45 +
  46 + public function writeBin($handle, $paramsNumber, $paramsSizes, $paramsTypes)
47 47 {
48 48 fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified));
49   - for ($i = 0; $i < $paramsNumber; $i++)
  49 + for ($i = 0; $i < $paramsNumber; $i++)
50 50 {
51 51 if ($paramsTypes[$i] == '2') // string
52 52 {
53 53 fwrite($handle,pack('d', strlen($this->params[$i])));
54 54 fwrite($handle, $this->params[$i],strlen($this->params[$i]));
55 55 }
56   - else
  56 + else
57 57 {
58   - if ($paramsTypes[$i] == '1')
59   - $paramString = CacheTools::iso2stamp($this->params[$i]);
  58 + if ($paramsTypes[$i] == '1')
  59 + $paramString = TimeUtils::iso2stamp($this->params[$i]);
60 60 else
61 61 $paramString = $this->params[$i];
62   -
  62 +
63 63 $paramArray = explode(',',$paramString);
64 64 for ($j = 0; $j < $paramsSizes[$i]; $j++) fwrite($handle,pack('d', $paramArray[$j]));
65 65 }
66   - }
  66 + }
67 67 }
68   -
69   - public function loadBin($handle, $paramsNumber, $paramsSizes, $paramsTypes)
  68 +
  69 + public function loadBin($handle, $paramsNumber, $paramsSizes, $paramsTypes)
70 70 {
71 71 $array = unpack('L6int',fread($handle,6*4));
72 72 $this->id = $array['int1'];
... ... @@ -75,45 +75,45 @@ class CatIntervalCacheObject extends IntervalCacheObject
75 75 $this->stop = $array['int4'];
76 76 $this->isNew = $array['int5'];
77 77 $this->isModified = $array['int6'];
78   -
  78 +
79 79 for ($i = 0; $i < $paramsNumber; $i++) {
80 80 $this->params[$i] = null;
81   -
82   - for ($j = 0; $j < $paramsSizes[$i]; $j++)
  81 +
  82 + for ($j = 0; $j < $paramsSizes[$i]; $j++)
83 83 {
84 84 $val = unpack('dval',fread($handle,8));
85   -
  85 +
86 86 if ($paramsTypes[$i] == '2') // string
87 87 {
88 88 $this->params[$i] = fread($handle,$val['val']);
89 89 }
90   - else
  90 + else
91 91 {
92 92 if ($paramsTypes[$i] == '1')
93   - $this->params[$i] .= CacheTools::stamp2iso($val['val']);
94   - else
  93 + $this->params[$i] .= TimeUtils::stamp2iso($val['val']);
  94 + else
95 95 $this->params[$i] .= $val['val'];
96   -
  96 +
97 97 if ($j != $paramsSizes[$i] - 1) $this->params[$i] .= ',';
98 98 }
99   - }
  99 + }
100 100 }
101   -
  101 +
102 102 }
103   -
104   - public function dump()
  103 +
  104 + public function dump()
105 105 {
106 106 echo " => Interval : id = ".$this->id.", index = ".$this->index.", start = ".$this->start.", stop = ".$this->stop.", isNew = ".$this->isNew.", isModified = ".$this->isModified.PHP_EOL;
107 107 }
108   -}
109   -
  108 +}
  109 +
110 110 class CatalogCacheObject extends TimeTableCacheObject
111 111 {
112 112 private $paramsNumber;
113 113 private $paramsSizes = array();
114 114 private $paramsTypes = array();
115   -
116   - public function addInterval($startIso, $stopIso, $params, $isNew = false, $index = -1)
  115 +
  116 + public function addInterval($startIso, $stopIso, $params, $isNew = false, $index = -1)
117 117 {
118 118 $interval = new CatIntervalCacheObject($this->lastId, count($this->intervals));
119 119 ++$this->lastId;
... ... @@ -124,88 +124,88 @@ class CatalogCacheObject extends TimeTableCacheObject
124 124  
125 125 $interval->setIsNew($isNew);
126 126 array_push($this->intervals, $interval);
127   -
  127 +
128 128 if ($index < 0)
129 129 array_push($this->indexes, count($this->intervals) - 1);
130 130 else
131 131 array_splice($this->indexes, $index, 0, array(count($this->intervals) - 1));
132   -
  132 +
133 133 if ($isNew)
134 134 $this->isModified = true;
135   -
  135 +
136 136 return $interval;
137 137 }
138   -
  138 +
139 139 public function setParamsNumber($number)
140 140 {
141 141 $this->paramsNumber = $number;
142 142 }
143   -
  143 +
144 144 public function setParamsSizes($params)
145 145 {
146   - for ($i = 0; $i < $this->paramsNumber; $i++)
147   - $this->paramsSizes[$i] = $params[$i]['size'];
  146 + for ($i = 0; $i < $this->paramsNumber; $i++)
  147 + $this->paramsSizes[$i] = $params[$i]['size'];
148 148 }
149   -
  149 +
150 150 public function setParamsTypes($params)
151 151 {
152   - for ($i = 0; $i < $this->paramsNumber; $i++)
153   - $this->paramsTypes[$i] = $params[$i]['type'];
  152 + for ($i = 0; $i < $this->paramsNumber; $i++)
  153 + $this->paramsTypes[$i] = $params[$i]['type'];
154 154 }
155   -
156   - public function writeBin($handle)
  155 +
  156 + public function writeBin($handle)
157 157 {
158 158 //Magic key ("TTC")
159 159 fwrite($handle,pack('C3',ord('T'),ord('T'),ord('C')));
160   -
  160 +
161 161 //Version
162 162 fwrite($handle,pack('L',TimeTableCacheObject::$format_version));
163   -
  163 +
164 164 //Token
165 165 for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i)
166 166 fwrite($handle,pack('C',ord($this->token[$i])));
167   -
  167 +
168 168 //Modified
169 169 fwrite($handle,pack('L',$this->isModified));
170   -
  170 +
171 171 //Filter
172 172 $this->filter->writeBin($handle);
173   -
  173 +
174 174 //Sort
175 175 $this->sort->writeBin($handle);
176   -
  176 +
177 177 //Params Number
178 178 fwrite($handle,pack('L',$this->paramsNumber));
179   -
  179 +
180 180 //Params Sizes
181   - for ($i = 0; $i < $this->paramsNumber; $i++)
  181 + for ($i = 0; $i < $this->paramsNumber; $i++)
182 182 fwrite($handle,pack('L',$this->paramsSizes[$i]));
183   -
  183 +
184 184 //Params Types
185   - for ($i = 0; $i < $this->paramsNumber; $i++)
  185 + for ($i = 0; $i < $this->paramsNumber; $i++)
186 186 fwrite($handle,pack('L',$this->paramsTypes[$i]));
187   -
  187 +
188 188 //Intervals
189 189 fwrite($handle,pack('L2', count($this->intervals), $this->lastId));
190   -
  190 +
191 191  
192 192 foreach($this->intervals as $interval)
193 193 $interval->writeBin($handle,$this->paramsNumber,$this->paramsSizes, $this->paramsTypes);
194   -
  194 +
195 195 //Indexes
196 196 fwrite($handle,pack('L',count($this->indexes)));
197 197 foreach($this->indexes as $index)
198 198 fwrite($handle,pack('L',$index));
199 199 }
200   -
  200 +
201 201 public function loadBin($handle) {
202 202 //Magic key ("TTC")
203 203 if (!$res = unpack('C3key',fread($handle,3)))
204 204 return false;
205   -
  205 +
206 206 if (($res['key1'] != ord('T')) || ($res['key2'] != ord('T')) || ($res['key3'] != ord('C')))
207 207 return false;
208   -
  208 +
209 209 //Version
210 210 if (!$res = unpack('Lversion',fread($handle,4)))
211 211 return false;
... ... @@ -221,23 +221,23 @@ class CatalogCacheObject extends TimeTableCacheObject
221 221 $token .= chr($res['token']);
222 222 }
223 223 $this->token = $token;
224   -
  224 +
225 225 //Modified
226 226 if (!$res = unpack('Lmodified',fread($handle,4)))
227 227 return false;
228 228 $this->isModified = $res['modified'];
229   -
  229 +
230 230 //Filter
231 231 $this->filter->loadBin($handle);
232   -
  232 +
233 233 //Sort
234 234 $this->sort->loadBin($handle);
235   -
  235 +
236 236 //ParamsNumber
237 237 if (!$res = unpack('Lnumber',fread($handle,4)))
238 238 return false;
239 239 $this->paramsNumber = $res['number'];
240   -
  240 +
241 241 //ParamsSizes
242 242 for ($i = 0; $i < $this->paramsNumber; $i++) {
243 243 if (!$res = unpack('Lsize',fread($handle,4)))
... ... @@ -254,14 +254,14 @@ class CatalogCacheObject extends TimeTableCacheObject
254 254 $res = unpack('L2data',fread($handle,2*4));
255 255 $nbIntervals = $res['data1'];
256 256 $this->lastId = $res['data2'];
257   -
  257 +
258 258 for ($i = 0; $i < $nbIntervals; ++$i)
259 259 {
260 260 $interval = new CatIntervalCacheObject(-1);
261   - $interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes);
  261 + $interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes);
262 262 array_push($this->intervals, $interval);
263 263 }
264   -
  264 +
265 265 //Indexes
266 266 $res = unpack('Ldata',fread($handle,4));
267 267 $nbIndexes = $res['data'];
... ... @@ -270,29 +270,29 @@ class CatalogCacheObject extends TimeTableCacheObject
270 270 $res = unpack('Lindex',fread($handle,4));
271 271 array_push($this->indexes, $res['index']);
272 272 }
273   -
  273 +
274 274 return true;
275 275 }
276   -
277   - public function modifyIntervalFromId($obj) {
278   -
  276 +
  277 + public function modifyIntervalFromId($obj) {
  278 +
279 279 foreach ($this->intervals as $interval)
280 280 {
281 281 if ($interval->getId() == $obj->cacheId)
282   - {
283   - foreach((array)$obj as $key => $val) {
284   -
  282 + {
  283 + foreach((array)$obj as $key => $val) {
  284 +
285 285 if ($key == 'start')
286 286 $interval->setStartFromISO($val);
287 287 else if ($key == 'stop')
288   - $interval->setStopFromISO($val);
  288 + $interval->setStopFromISO($val);
289 289 else {
290 290 if (strpos($key, 'param') === false)
291 291 continue;
292 292 $params = $interval->getParams();
293 293 $paramIndex = (int)substr($key,5);
294 294 $params[$paramIndex-2] = $val;
295   - $interval->setParams($params);
  295 + $interval->setParams($params);
296 296 }
297 297 }
298 298 $interval->setIsModified(true);
... ... @@ -300,33 +300,33 @@ class CatalogCacheObject extends TimeTableCacheObject
300 300 return true;
301 301 }
302 302 }
303   -
  303 +
304 304 return false;
305 305 }
306 306 }
307 307  
308   -class CatalogCacheMgr extends TimeTableCacheMgr
  308 +class CatalogCacheMgr extends TimeTableCacheMgr
309 309 {
310   -
  310 +
311 311 protected static $cache_file = "cacheCat";
312   -
  312 +
313 313 protected $ttMgr = null;
314 314 protected $cache = null;
315 315  
316 316 function __construct() {
317   -
  317 +
318 318 $this->ttMgr = new CatalogMgr();
319 319 }
320   -
321   -
  320 +
  321 +
322 322 public function initFromTmpObject($folderId, $name) {
323   -
324   - //Create new cache
  323 +
  324 + //Create new cache
325 325 $this->cache = new CatalogCacheObject();
326 326  
327 327 //Load intervals from TmpObject file (Statistics Module)
328 328 $intervals_res = $this->ttMgr->getTmpObject($folderId, $name);
329   -
  329 +
330 330 if (!isset($intervals_res))
331 331 return array('success' => false, 'message' => 'Cannot get Tmp Object');
332 332  
... ... @@ -336,28 +336,28 @@ class CatalogCacheMgr extends TimeTableCacheMgr
336 336 {
337 337 //Add interval
338 338 $this->cache->addInterval($interval['start'], $interval['stop'], $interval['paramTable']);
339   -
  339 +
340 340 }
341 341 }
342 342  
343 343 $this->cache->setIsModified(true);
344   -
  344 +
345 345 $paramHeaders = $intervals_res['parameters'];
346   -
  346 +
347 347 $this->cache->setParamsNumber(count($paramHeaders));
348 348 $this->cache->setParamsSizes($paramHeaders);
349 349 $this->cache->setParamsTypes($paramHeaders);
350   -
351   - unset($intervals_res);
352   -
  350 +
  351 + unset($intervals_res);
  352 +
353 353 //Update cache
354 354 $this->cache->updateIndexes();
355 355  
356 356 //Save cache file
357   - return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(),
  357 + return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(),
358 358 'status'=>$this->cache->getStatus(), 'parameters'=>$paramHeaders);
359 359 }
360   -
  360 +
361 361 public function initFromUploadedFile($name, $format)
362 362 {
363 363 //Create new cache
... ... @@ -380,29 +380,29 @@ class CatalogCacheMgr extends TimeTableCacheMgr
380 380  
381 381 $this->cache->setIsModified(true);
382 382 $paramHeaders = $intervals_res['parameters'];
383   -
  383 +
384 384 $this->cache->setParamsNumber(count($paramHeaders));
385 385 $this->cache->setParamsSizes($paramHeaders);
386 386 $this->cache->setParamsTypes($paramHeaders);
387   -
  387 +
388 388 unset($intervals_res);
389   -
  389 +
390 390 //Update cache
391 391 $this->cache->updateIndexes();
392 392  
393 393 //Save cache file
394   - return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(),
  394 + return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(),
395 395 'status'=>$this->cache->getStatus(), 'parameters'=>$paramHeaders);
396 396 }
397   -
398   - public function initFromTT($id, $typeTT)
  397 +
  398 + public function initFromTT($id, $typeTT)
399 399 {
400 400 //Create new cache
401 401 $this->cache = new CatalogCacheObject();
402 402  
403 403 //Load intervals from catalog file and add to cache
404 404 $intervals_res = $this->ttMgr->loadIntervalsFromTT($id,$typeTT);
405   -
  405 +
406 406 if (!$intervals_res['success'])
407 407 return $intervals_res;
408 408  
... ... @@ -411,13 +411,13 @@ class CatalogCacheMgr extends TimeTableCacheMgr
411 411 //Add interval
412 412 $this->cache->addInterval($interval['start'], $interval['stop'], $interval['paramTable']);
413 413 }
414   -
  414 +
415 415 $paramHeaders = $intervals_res['parameters'];
416   -
  416 +
417 417 $this->cache->setParamsNumber(count($paramHeaders));
418 418 $this->cache->setParamsSizes($paramHeaders);
419 419 $this->cache->setParamsTypes($paramHeaders);
420   -
  420 +
421 421 unset($intervals_res);
422 422  
423 423 //Update cache
... ... @@ -429,8 +429,8 @@ class CatalogCacheMgr extends TimeTableCacheMgr
429 429 }
430 430  
431 431  
432   - protected function loadFromFile()
433   - {
  432 + protected function loadFromFile()
  433 + {
434 434 if (!file_exists($this->getCacheFilePath()))
435 435 return false;
436 436 $this->cache = new CatalogCacheObject();
... ... @@ -445,30 +445,30 @@ class CatalogCacheMgr extends TimeTableCacheMgr
445 445 fclose($handle);
446 446 return $result;
447 447 }
448   -
449   - protected function getCacheFilePath()
  448 +
  449 + protected function getCacheFilePath()
450 450 {
451 451 return USERTTDIR.(self::$cache_file);
452 452 }
453 453  
454   - public function saveInTT($id, $action, $token)
455   - {
  454 + public function saveInTT($id, $action, $token)
  455 + {
456 456 if (!$this->loadFromFile())
457 457 return array('success' => false, 'message' => 'Cannot load cache file');
458 458  
459 459 if ($token != $this->cache->getToken())
460 460 return array('success' => false, 'message' => 'Cache token check error');
461   -
  461 +
462 462 $this->cache->updateIndexes();
463 463 $this->saveToFile();
464   -
  464 +
465 465 $intervals = $this->cache->getIntervalsArray(NULL,NULL,true);
466   -
  466 +
467 467 $this->cache->reset();
468 468  
469 469 return $this->ttMgr->saveIntervals($id, $intervals, $action);
470 470 }
471   -
  471 +
472 472 public function addInterval($index, $start, $stop, $params) {
473 473 if (!$this->loadFromFile())
474 474 return array('success' => false, 'message' => 'Cannot load cache file');
... ... @@ -481,42 +481,42 @@ class CatalogCacheMgr extends TimeTableCacheMgr
481 481  
482 482 if (!isset($stop))
483 483 $stop = date('Y-m-d\TH:i:s');
484   -
  484 +
485 485 if (!isset($params))
486   - $params = [];
487   -
  486 + $params = [];
  487 +
488 488 $this->cache->addInterval($start, $stop, $params, true, $index);
489   -
  489 +
490 490 //$this->cache->updateIndexes();
491   -
  491 +
492 492 $this->saveToFile();
493   -
  493 +
494 494 return array('success' => true, 'index' => $index, 'status' => $this->cache->getStatus());
495 495 }
496   -
  496 +
497 497 public function modifyIntervalFromId($obj) {
498 498 if (!$this->loadFromFile())
499 499 return array('success' => false, 'message' => 'Cannot load cache file');
500 500  
501 501 $this->cache->modifyIntervalFromId($obj);
502   -
  502 +
503 503 $this->saveToFile();
504   -
  504 +
505 505 return array('success' => true, 'status' => $this->cache->getStatus());
506 506 }
507   -
  507 +
508 508 public function initTTCache($nparams) {
509 509 //Create new cache
510 510 $this->cache = new CatalogCacheObject();
511 511 $this->cache->setParamsNumber((int)$nparams);
512 512 $paramHeaders = array();
513   -
  513 +
514 514 for ($i = 0; $i < (int)$nparams; $i++) {
515 515 $paramHeaders[$i]['id'] = 'id_'.(string)($i+1);
516 516 $paramHeaders[$i]['name'] = 'param_'.(string)($i+1);
517 517 $paramHeaders[$i]['size'] = 1;
518 518 $paramHeaders[$i]['type'] = 'Float';
519   -
  519 +
520 520 }
521 521 $this->cache->setParamsSizes($paramHeaders);
522 522 $this->cache->setParamsTypes($paramHeaders);
... ... @@ -525,4 +525,4 @@ class CatalogCacheMgr extends TimeTableCacheMgr
525 525 'status' => $this->cache->getStatus(), 'parameters' => $paramHeaders);
526 526 }
527 527 }
528   -?>
529 528 \ No newline at end of file
  529 +?>
... ...
php/classes/IntervalCacheObject.php
... ... @@ -16,11 +16,11 @@ class IntervalCacheObject
16 16 public function getId() {
17 17 return $this->id;
18 18 }
19   -
  19 +
20 20 public function getIndex() {
21 21 return $this->index;
22 22 }
23   -
  23 +
24 24 public function setIndex($index) {
25 25 $this->index = $index;
26 26 }
... ... @@ -28,35 +28,35 @@ class IntervalCacheObject
28 28 public function getStartToStamp() {
29 29 return $this->start;
30 30 }
31   -
  31 +
32 32 public function getStartToISO() {
33   - return CacheTools::stamp2iso($this->start);
  33 + return TimeUtils::stamp2iso($this->start);
34 34 }
35 35  
36 36 public function setStartFromStamp($stamp) {
37 37 $this->start = $stamp;
38 38 }
39   -
  39 +
40 40 public function setStartFromISO($iso) {
41   - $this->start = CacheTools::iso2stamp($iso);
  41 + $this->start = TimeUtils::iso2stamp($iso);
42 42 }
43 43  
44 44 public function getStopToStamp() {
45 45 return $this->stop;
46 46 }
47   -
  47 +
48 48 public function getStopToISO() {
49   - return CacheTools::stamp2iso($this->stop);
  49 + return TimeUtils::stamp2iso($this->stop);
50 50 }
51 51  
52 52 public function setStopFromStamp($stamp) {
53 53 $this->stop = $stamp;
54 54 }
55   -
  55 +
56 56 public function setStopFromISO($iso) {
57   - $this->stop = CacheTools::iso2stamp($iso);
  57 + $this->stop = TimeUtils::iso2stamp($iso);
58 58 }
59   -
  59 +
60 60 public function getDuration() {
61 61 return ($this->stop-$this->start);
62 62 }
... ... @@ -76,7 +76,7 @@ class IntervalCacheObject
76 76 public function setIsNew($isNew) {
77 77 $this->isNew = $isNew;
78 78 }
79   -
  79 +
80 80 public function toArray() {
81 81 $result = array(
82 82 "cacheId" => $this->id,
... ... @@ -87,16 +87,16 @@ class IntervalCacheObject
87 87 $result["isNew"] = true;
88 88 if ($this->isModified)
89 89 $result["isModified"] = true;
90   -
  90 +
91 91 return $result;
92 92 }
93 93  
94   -
95   -
  94 +
  95 +
96 96 public function writeBin($handle) {
97 97 fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified));
98 98 }
99   -
  99 +
100 100 public function loadBin($handle) {
101 101 $array = unpack('L6int',fread($handle,6*4));
102 102 $this->id = $array['int1'];
... ... @@ -106,7 +106,7 @@ class IntervalCacheObject
106 106 $this->isNew = $array['int5'];
107 107 $this->isModified = $array['int6'];
108 108 }
109   -
  109 +
110 110 public function dump() {
111 111 echo " => Interval : id = ".$this->id.", index = ".$this->index.", start = ".$this->start.", stop = ".$this->stop.", isNew = ".$this->isNew.", isModified = ".$this->isModified.PHP_EOL;
112 112 }
... ...
php/classes/TimeTableCacheMgr.php
1 1 <?php
2   -class CacheTools
3   -{
4   - public static function iso2stamp($iso) {
5   - try {
6   - $time = new DateTime($iso);
7   - }
8   - catch (Exception $e) {
9   - $time = new DateTime('1970-01-01T00:00:00Z');
10   - }
11   - $stamp = $time->format('U');
12   - unset($time);
13   - return $stamp;
14   - }
15   -
16   - public static function stamp2iso($stamp) {
17   - return date('Y-m-d\TH:i:s',$stamp);
18   - }
19   -}
20 2  
21 3 class SortPartCacheObject
22 4 {
... ... @@ -26,31 +8,31 @@ class SortPartCacheObject
26 8 public static $TYPE_DURATION_SEC = 3;
27 9 public static $TYPE_DURATION_MIN = 4;
28 10 public static $TYPE_DURATION_HOUR = 5;
29   -
  11 +
30 12 public static $DIRECTION_UNKNOWN = 0;
31 13 public static $DIRECTION_ASC = 1;
32 14 public static $DIRECTION_DES = 2;
33   -
  15 +
34 16 protected $type;
35 17 protected $dir;
36   -
  18 +
37 19 function __construct() {
38 20 $this->type = self::$TYPE_UNKNOWN;
39 21 $this->dir = self::$DIRECTION_UNKNOWN;
40 22 }
41   -
  23 +
42 24 public function getType() {
43 25 return $this->type;
44 26 }
45   -
  27 +
46 28 public function getDir() {
47 29 return $this->dir;
48 30 }
49   -
  31 +
50 32 public function isSame($part) {
51 33 return (($this->type == $part->getType()) && ($this->dir == $part->getDir()));
52 34 }
53   -
  35 +
54 36 public function compare($interval_a, $interval_b) {
55 37 switch ($this->type) {
56 38 case self::$TYPE_START :
... ... @@ -59,7 +41,7 @@ class SortPartCacheObject
59 41 case self::$DIRECTION_ASC :
60 42 return ($interval_b->getStartToStamp() - $interval_a->getStartToStamp());
61 43 default :
62   - return ($interval_a->getStartToStamp() - $interval_b->getStartToStamp());
  44 + return ($interval_a->getStartToStamp() - $interval_b->getStartToStamp());
63 45 }
64 46 }
65 47 break;
... ... @@ -90,7 +72,7 @@ class SortPartCacheObject
90 72 }
91 73 return 0;
92 74 }
93   -
  75 +
94 76 public function loadFromObject($part_obj) {
95 77 switch ($part_obj->property)
96 78 {
... ... @@ -112,7 +94,7 @@ class SortPartCacheObject
112 94 default:
113 95 $this->type = self::$TYPE_UNKNOWN;
114 96 }
115   -
  97 +
116 98 switch ($part_obj->direction)
117 99 {
118 100 case 'ASC' :
... ... @@ -125,17 +107,17 @@ class SortPartCacheObject
125 107 $this->dir = self::$DIRECTION_UNKNOWN;
126 108 }
127 109 }
128   -
  110 +
129 111 public function writeBin($handle) {
130 112 fwrite($handle,pack('L2',$this->type,$this->dir));
131 113 }
132   -
  114 +
133 115 public function loadBin($handle) {
134 116 $res = unpack('L2data',fread($handle,4*2));
135 117 $this->type = $res['data1'];
136 118 $this->dir = $res['data2'];
137 119 }
138   -
  120 +
139 121 public function dump() {
140 122 echo " => SortPartCacheObject : type = ";
141 123 switch ($this->type)
... ... @@ -202,7 +184,7 @@ class SortCacheObject
202 184 array_push($this->parts, $part);
203 185 }
204 186 }
205   -
  187 +
206 188 public function isSameFromObject($sort_obj) {
207 189 $sort = new SortCacheObject();
208 190 $sort->loadFromObject($sort_obj);
... ... @@ -221,23 +203,23 @@ class SortCacheObject
221 203 return false;
222 204 }
223 205 }
224   -
  206 +
225 207 return true;
226 208 }
227 209  
228 210 public function apply($intervals) {
229 211 $sorted_indexes = array();
230   -
  212 +
231 213 global $global_parts, $global_intervals;
232 214 $global_parts = $this->parts;
233 215 $global_intervals = $intervals;
234   -
  216 +
235 217 foreach ($intervals as $interval)
236 218 array_push($sorted_indexes, $interval->getIndex());
237   -
  219 +
238 220 if (count($global_parts) == 0)
239 221 return $sorted_indexes;
240   -
  222 +
241 223 usort($sorted_indexes, function ($index_a, $index_b) {
242 224 global $global_parts, $global_intervals;
243 225 foreach ($global_parts as $part)
... ... @@ -284,38 +266,38 @@ class FilterPartCacheObject
284 266 public static $TYPE_DURATION_SEC = 3;
285 267 public static $TYPE_DURATION_MIN = 4;
286 268 public static $TYPE_DURATION_HOUR = 5;
287   -
  269 +
288 270 public static $OPERATION_UNKNOWN = 0;
289 271 public static $OPERATION_LT = 1;
290 272 public static $OPERATION_GT = 2;
291 273 public static $OPERATION_EQ = 3;
292   -
  274 +
293 275 protected $type;
294 276 protected $op;
295 277 protected $value;
296   -
  278 +
297 279 function __construct() {
298 280 $this->type = self::$TYPE_UNKNOWN;
299 281 $this->op = self::$OPERATION_UNKNOWN;
300 282 $this->value = 0.;
301 283 }
302   -
  284 +
303 285 public function getType() {
304 286 return $this->type;
305 287 }
306   -
  288 +
307 289 public function getOp() {
308 290 return $this->op;
309 291 }
310   -
  292 +
311 293 public function getValue() {
312 294 return $this->value;
313 295 }
314   -
  296 +
315 297 public function isSame($part) {
316 298 return (($this->type == $part->getType()) && ($this->op == $part->getOp()) && ($this->value == $part->getValue()));
317 299 }
318   -
  300 +
319 301 public function toFiltered($interval) {
320 302 switch ($this->type) {
321 303 case self::$TYPE_START :
... ... @@ -371,17 +353,17 @@ class FilterPartCacheObject
371 353 return false;
372 354 }
373 355 }
374   -
  356 +
375 357 public function loadFromObject($part_obj) {
376 358 $this->value = 0.;
377 359 switch ($part_obj->field)
378 360 {
379 361 case 'start' :
380   - $this->value = CacheTools::iso2stamp($part_obj->value);
  362 + $this->value = TimeUtils::iso2stamp($part_obj->value);
381 363 $this->type = self::$TYPE_START;
382 364 break;
383 365 case 'stop' :
384   - $this->value = CacheTools::iso2stamp($part_obj->value);
  366 + $this->value = TimeUtils::iso2stamp($part_obj->value);
385 367 $this->type = self::$TYPE_STOP;
386 368 break;
387 369 case 'durationMin' :
... ... @@ -400,7 +382,7 @@ class FilterPartCacheObject
400 382 $this->value = 0.;
401 383 $this->type = self::$TYPE_UNKNOWN;
402 384 }
403   -
  385 +
404 386 switch ($part_obj->comparison)
405 387 {
406 388 case 'lt' :
... ... @@ -416,21 +398,21 @@ class FilterPartCacheObject
416 398 $this->op = self::$OPERATION_UNKNOWN;
417 399 }
418 400 }
419   -
  401 +
420 402 public function writeBin($handle) {
421 403 fwrite($handle,pack('L2',$this->type,$this->op));
422 404 fwrite($handle,pack('f',$this->value));
423 405 }
424   -
  406 +
425 407 public function loadBin($handle) {
426 408 $res = unpack('L2data',fread($handle,4*2));
427 409 $this->type = $res['data1'];
428 410 $this->op = $res['data2'];
429   -
  411 +
430 412 $res = unpack('fvalue',fread($handle,4));
431 413 $this->value = $res['value'];
432 414 }
433   -
  415 +
434 416 public function dump() {
435 417 echo " => FilterPartCacheObject : type = ";
436 418 switch ($this->type)
... ... @@ -475,27 +457,27 @@ class FilterPartCacheObject
475 457 class FilterCacheObject
476 458 {
477 459 protected $parts = array();
478   -
  460 +
479 461 function __construct() {
480   -
  462 +
481 463 }
482   -
  464 +
483 465 public function getParts() {
484 466 return $this->parts;
485 467 }
486   -
  468 +
487 469 public function reset() {
488 470 $this->parts = array();
489 471 }
490   -
  472 +
491 473 public function isEmpty() {
492 474 return (count($this->parts) == 0);
493 475 }
494   -
  476 +
495 477 public function loadFromJSON($filter_json) {
496 478 $this->reset();
497 479 $filter_obj = json_decode($filter_json);
498   -
  480 +
499 481 foreach ($filter_obj as $filter_part)
500 482 {
501 483 $part = new FilterPartCacheObject();
... ... @@ -503,11 +485,11 @@ class FilterCacheObject
503 485 array_push($this->parts, $part);
504 486 }
505 487 }
506   -
  488 +
507 489 public function isSame($filter) {
508 490 if (count($this->parts) != count($filter->getParts()))
509 491 return false;
510   -
  492 +
511 493 $identique = true;
512 494 for ($i = 0; $i < count($this->parts); ++$i)
513 495 {
... ... @@ -516,16 +498,16 @@ class FilterCacheObject
516 498 return false;
517 499 }
518 500 }
519   -
  501 +
520 502 return true;
521 503 }
522   -
  504 +
523 505 public function isSameFromJSON($filter_json) {
524 506 $filter = new FilterCacheObject();
525 507 $filter->loadFromJSON($filter_json);
526 508 return $this->isSame($filter);
527 509 }
528   -
  510 +
529 511 public function toFiltered($interval) {
530 512 foreach ($this->parts as $part)
531 513 {
... ... @@ -534,13 +516,13 @@ class FilterCacheObject
534 516 }
535 517 return false;
536 518 }
537   -
  519 +
538 520 public function writeBin($handle) {
539 521 fwrite($handle,pack('L',count($this->parts)));
540 522 foreach ($this->parts as $part)
541 523 $part->writeBin($handle);
542 524 }
543   -
  525 +
544 526 public function loadBin($handle) {
545 527 $this->reset();
546 528 $res = unpack('Lcount',fread($handle,4));
... ... @@ -551,7 +533,7 @@ class FilterCacheObject
551 533 array_push($this->parts, $part);
552 534 }
553 535 }
554   -
  536 +
555 537 public function dump() {
556 538 echo " => FilterCacheObject : number of parts = ".count($this->parts).PHP_EOL;
557 539 foreach ($this->parts as $part)
... ... @@ -562,7 +544,7 @@ class FilterCacheObject
562 544 class TimeTableCacheMgr
563 545 {
564 546 protected static $cache_file = "cacheTT";
565   -
  547 +
566 548 protected $ttMgr = null;
567 549 protected $cache = null;
568 550  
... ... @@ -574,7 +556,7 @@ class FilterCacheObject
574 556 public function initTTCache() {
575 557 //Create new cache
576 558 $this->cache = new TimeTableCacheObject();
577   -
  559 +
578 560 //Save cache file
579 561 return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus());
580 562 }
... ... @@ -585,7 +567,7 @@ class FilterCacheObject
585 567  
586 568 //Load intervals from TT file and add to cache
587 569 $intervals_res = $this->ttMgr->loadIntervalsFromTT($id,$typeTT);
588   -
  570 +
589 571 if (!$intervals_res['success'])
590 572 return $intervals_res;
591 573  
... ... @@ -594,7 +576,7 @@ class FilterCacheObject
594 576 //Add interval
595 577 $this->cache->addInterval($interval['start'], $interval['stop']);
596 578 }
597   -
  579 +
598 580 unset($intervals_res);
599 581  
600 582 //Update cache
... ... @@ -650,9 +632,9 @@ class FilterCacheObject
650 632 }
651 633  
652 634 $this->cache->setIsModified(true);
653   -
  635 +
654 636 unset($intervals_res);
655   -
  637 +
656 638 //Update cache
657 639 $this->cache->updateIndexes();
658 640  
... ... @@ -680,9 +662,9 @@ class FilterCacheObject
680 662 }
681 663  
682 664 $this->cache->setIsModified(true);
683   -
  665 +
684 666 unset($intervals_res);
685   -
  667 +
686 668 //Update cache
687 669 $this->cache->updateIndexes();
688 670  
... ... @@ -696,14 +678,14 @@ class FilterCacheObject
696 678  
697 679 if ($token != $this->cache->getToken())
698 680 return array('success' => false, 'message' => 'Cache token check error');
699   -
  681 +
700 682 $this->cache->updateIndexes();
701 683 $this->saveToFile();
702   -
  684 +
703 685 $intervals = $this->cache->getIntervalsArray(NULL,NULL,true);
704   -
  686 +
705 687 $this->cache->reset();
706   -
  688 +
707 689 $res_intervals = array();
708 690 foreach ($intervals as $interval)
709 691 {
... ... @@ -717,7 +699,7 @@ class FilterCacheObject
717 699 }
718 700  
719 701 unset($intervals);
720   -
  702 +
721 703 return $this->ttMgr->saveIntervals($id, $res_intervals, $action);
722 704 }
723 705  
... ... @@ -742,7 +724,7 @@ class FilterCacheObject
742 724 $this->cache->getFilter()->reset();
743 725 }
744 726 }
745   -
  727 +
746 728 if (isset($sort_obj))
747 729 {
748 730 if (!$this->cache->getSort()->isSameFromObject($sort_obj))
... ... @@ -759,7 +741,7 @@ class FilterCacheObject
759 741 $this->cache->getSort()->reset();
760 742 }
761 743 }
762   -
  744 +
763 745 if ($needToUpdate)
764 746 {
765 747 $this->cache->updateIndexes();
... ... @@ -789,13 +771,13 @@ class FilterCacheObject
789 771  
790 772 if (!isset($stop))
791 773 $stop = date('Y-m-d\TH:i:s');
792   -
  774 +
793 775 $this->cache->addInterval($start, $stop, true, $index);
794   -
  776 +
795 777 //$this->cache->updateIndexes();
796   -
  778 +
797 779 $this->saveToFile();
798   -
  780 +
799 781 return array('success' => true, 'index' => $index, 'status' => $this->cache->getStatus());
800 782 }
801 783  
... ... @@ -804,7 +786,7 @@ class FilterCacheObject
804 786 return array('success' => false, 'message' => 'Cannot load cache file');
805 787  
806 788 $this->cache->removeIntervalFromId($id);
807   -
  789 +
808 790 $this->cache->updateIndexes();
809 791  
810 792 $this->saveToFile();
... ... @@ -817,38 +799,38 @@ class FilterCacheObject
817 799 return array('success' => false, 'message' => 'Cannot load cache file');
818 800  
819 801 $this->cache->modifyIntervalFromId($id, $start, $stop);
820   -
  802 +
821 803 $this->saveToFile();
822   -
  804 +
823 805 return array('success' => true, 'status' => $this->cache->getStatus());
824 806 }
825   -
  807 +
826 808 public function operationIntervals($extendTime, $shiftTime) {
827 809 if (!$this->loadFromFile())
828 810 return array('success' => false, 'message' => 'Cannot load cache file');
829   -
  811 +
830 812 $this->cache->operationIntervals($extendTime, $shiftTime);
831   -
  813 +
832 814 $this->saveToFile();
833   -
  815 +
834 816 return array('success' => true, 'status' => $this->cache->getStatus());
835 817 }
836   -
  818 +
837 819 public function mergeIntervals() {
838 820 if (!$this->loadFromFile())
839 821 return array('success' => false, 'message' => 'Cannot load cache file');
840   -
  822 +
841 823 $this->cache->mergeIntervals();
842   -
  824 +
843 825 $this->saveToFile();
844   -
  826 +
845 827 return array('success' => true, 'status' => $this->cache->getStatus());
846 828 }
847   -
  829 +
848 830 public function getStatistics() {
849 831 if (!$this->loadFromFile())
850 832 return array('success' => false, 'message' => 'Cannot load cache file');
851   -
  833 +
852 834 return array('success' => true, "result" => $this->cache->getStatistics(), 'status' => $this->cache->getStatus());
853 835 }
854 836  
... ... @@ -860,11 +842,11 @@ class FilterCacheObject
860 842 }
861 843 $this->cache->dump();
862 844 }
863   -
  845 +
864 846 protected function getCacheFilePath() {
865 847 return USERTTDIR.(self::$cache_file);
866 848 }
867   -
  849 +
868 850 protected function saveToFile() {
869 851 if (!isset($this->cache))
870 852 return false;
... ... @@ -879,7 +861,7 @@ class FilterCacheObject
879 861 fclose($handle);
880 862 return $result;
881 863 }
882   -
  864 +
883 865 protected function loadFromFile() {
884 866 if (!file_exists($this->getCacheFilePath()))
885 867 return false;
... ...
php/classes/TimeUtils.php 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +<?php
  2 +
  3 +class TimeUtils
  4 +{
  5 + public static function iso2stamp($iso) {
  6 + try {
  7 + $time = new DateTime($iso);
  8 + }
  9 + catch (Exception $e) {
  10 + $time = new DateTime('1970-01-01T00:00:00Z');
  11 + }
  12 + $stamp = $time->format('U');
  13 + unset($time);
  14 + return $stamp;
  15 + }
  16 +
  17 + public static function stamp2iso($stamp) {
  18 + return date('Y-m-d\TH:i:s',$stamp);
  19 + }
  20 +}
  21 +
  22 +?>
... ...