From 5446b8f017bc0d68955adf6851df5ebdb1a20004 Mon Sep 17 00:00:00 2001 From: Benjamin Renard <benjamin.renard@akka.eu> Date: Thu, 18 Jul 2019 09:00:12 +0200 Subject: [PATCH] Move CacheTools in TimeUtils --- php/classes/CatalogCacheMgr.php | 280 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------- php/classes/IntervalCacheObject.php | 34 +++++++++++++++++----------------- php/classes/TimeTableCacheMgr.php | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------- php/classes/TimeUtils.php | 22 ++++++++++++++++++++++ 4 files changed, 264 insertions(+), 260 deletions(-) create mode 100644 php/classes/TimeUtils.php diff --git a/php/classes/CatalogCacheMgr.php b/php/classes/CatalogCacheMgr.php index 1da3037..670972d 100644 --- a/php/classes/CatalogCacheMgr.php +++ b/php/classes/CatalogCacheMgr.php @@ -3,14 +3,14 @@ /** * @class CatalogCacheMgr */ - - + + class CatIntervalCacheObject extends IntervalCacheObject { - // for catalog + // for catalog private $params = array(); - - public function toArray() + + public function toArray() { $result = array( "cacheId" => $this->id, @@ -19,54 +19,54 @@ class CatIntervalCacheObject extends IntervalCacheObject ); if ($this->isNew) $result["isNew"] = true; - + if ($this->isModified) $result["isModified"] = true; - - for ($i = 0; $i < count($this->params); $i++) + + for ($i = 0; $i < count($this->params); $i++) { $paramObject = array(); $index = 'param'.sprintf("%d",$i+2); - $result[$index] = $this->params[$i]; - } - return $result; + $result[$index] = $this->params[$i]; + } + return $result; } - // for catalog - public function setParams($params) + // for catalog + public function setParams($params) { - $this->params = $params; + $this->params = $params; } - - public function getParams() - { - return $this->params; + + public function getParams() + { + return $this->params; } - - public function writeBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) + + public function writeBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) { fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified)); - for ($i = 0; $i < $paramsNumber; $i++) + for ($i = 0; $i < $paramsNumber; $i++) { if ($paramsTypes[$i] == '2') // string { fwrite($handle,pack('d', strlen($this->params[$i]))); fwrite($handle, $this->params[$i],strlen($this->params[$i])); } - else + else { - if ($paramsTypes[$i] == '1') - $paramString = CacheTools::iso2stamp($this->params[$i]); + if ($paramsTypes[$i] == '1') + $paramString = TimeUtils::iso2stamp($this->params[$i]); else $paramString = $this->params[$i]; - + $paramArray = explode(',',$paramString); for ($j = 0; $j < $paramsSizes[$i]; $j++) fwrite($handle,pack('d', $paramArray[$j])); } - } + } } - - public function loadBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) + + public function loadBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) { $array = unpack('L6int',fread($handle,6*4)); $this->id = $array['int1']; @@ -75,45 +75,45 @@ class CatIntervalCacheObject extends IntervalCacheObject $this->stop = $array['int4']; $this->isNew = $array['int5']; $this->isModified = $array['int6']; - + for ($i = 0; $i < $paramsNumber; $i++) { $this->params[$i] = null; - - for ($j = 0; $j < $paramsSizes[$i]; $j++) + + for ($j = 0; $j < $paramsSizes[$i]; $j++) { $val = unpack('dval',fread($handle,8)); - + if ($paramsTypes[$i] == '2') // string { $this->params[$i] = fread($handle,$val['val']); } - else + else { if ($paramsTypes[$i] == '1') - $this->params[$i] .= CacheTools::stamp2iso($val['val']); - else + $this->params[$i] .= TimeUtils::stamp2iso($val['val']); + else $this->params[$i] .= $val['val']; - + if ($j != $paramsSizes[$i] - 1) $this->params[$i] .= ','; } - } + } } - + } - - public function dump() + + public function dump() { echo " => Interval : id = ".$this->id.", index = ".$this->index.", start = ".$this->start.", stop = ".$this->stop.", isNew = ".$this->isNew.", isModified = ".$this->isModified.PHP_EOL; } -} - +} + class CatalogCacheObject extends TimeTableCacheObject { private $paramsNumber; private $paramsSizes = array(); private $paramsTypes = array(); - - public function addInterval($startIso, $stopIso, $params, $isNew = false, $index = -1) + + public function addInterval($startIso, $stopIso, $params, $isNew = false, $index = -1) { $interval = new CatIntervalCacheObject($this->lastId, count($this->intervals)); ++$this->lastId; @@ -124,88 +124,88 @@ class CatalogCacheObject extends TimeTableCacheObject $interval->setIsNew($isNew); array_push($this->intervals, $interval); - + if ($index < 0) array_push($this->indexes, count($this->intervals) - 1); else array_splice($this->indexes, $index, 0, array(count($this->intervals) - 1)); - + if ($isNew) $this->isModified = true; - + return $interval; } - + public function setParamsNumber($number) { $this->paramsNumber = $number; } - + public function setParamsSizes($params) { - for ($i = 0; $i < $this->paramsNumber; $i++) - $this->paramsSizes[$i] = $params[$i]['size']; + for ($i = 0; $i < $this->paramsNumber; $i++) + $this->paramsSizes[$i] = $params[$i]['size']; } - + public function setParamsTypes($params) { - for ($i = 0; $i < $this->paramsNumber; $i++) - $this->paramsTypes[$i] = $params[$i]['type']; + for ($i = 0; $i < $this->paramsNumber; $i++) + $this->paramsTypes[$i] = $params[$i]['type']; } - - public function writeBin($handle) + + public function writeBin($handle) { //Magic key ("TTC") fwrite($handle,pack('C3',ord('T'),ord('T'),ord('C'))); - + //Version fwrite($handle,pack('L',TimeTableCacheObject::$format_version)); - + //Token for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i) fwrite($handle,pack('C',ord($this->token[$i]))); - + //Modified fwrite($handle,pack('L',$this->isModified)); - + //Filter $this->filter->writeBin($handle); - + //Sort $this->sort->writeBin($handle); - + //Params Number fwrite($handle,pack('L',$this->paramsNumber)); - + //Params Sizes - for ($i = 0; $i < $this->paramsNumber; $i++) + for ($i = 0; $i < $this->paramsNumber; $i++) fwrite($handle,pack('L',$this->paramsSizes[$i])); - + //Params Types - for ($i = 0; $i < $this->paramsNumber; $i++) + for ($i = 0; $i < $this->paramsNumber; $i++) fwrite($handle,pack('L',$this->paramsTypes[$i])); - + //Intervals fwrite($handle,pack('L2', count($this->intervals), $this->lastId)); - + foreach($this->intervals as $interval) $interval->writeBin($handle,$this->paramsNumber,$this->paramsSizes, $this->paramsTypes); - + //Indexes fwrite($handle,pack('L',count($this->indexes))); foreach($this->indexes as $index) fwrite($handle,pack('L',$index)); } - + public function loadBin($handle) { //Magic key ("TTC") if (!$res = unpack('C3key',fread($handle,3))) return false; - + if (($res['key1'] != ord('T')) || ($res['key2'] != ord('T')) || ($res['key3'] != ord('C'))) return false; - + //Version if (!$res = unpack('Lversion',fread($handle,4))) return false; @@ -221,23 +221,23 @@ class CatalogCacheObject extends TimeTableCacheObject $token .= chr($res['token']); } $this->token = $token; - + //Modified if (!$res = unpack('Lmodified',fread($handle,4))) return false; $this->isModified = $res['modified']; - + //Filter $this->filter->loadBin($handle); - + //Sort $this->sort->loadBin($handle); - + //ParamsNumber if (!$res = unpack('Lnumber',fread($handle,4))) return false; $this->paramsNumber = $res['number']; - + //ParamsSizes for ($i = 0; $i < $this->paramsNumber; $i++) { if (!$res = unpack('Lsize',fread($handle,4))) @@ -254,14 +254,14 @@ class CatalogCacheObject extends TimeTableCacheObject $res = unpack('L2data',fread($handle,2*4)); $nbIntervals = $res['data1']; $this->lastId = $res['data2']; - + for ($i = 0; $i < $nbIntervals; ++$i) { $interval = new CatIntervalCacheObject(-1); - $interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes); + $interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes); array_push($this->intervals, $interval); } - + //Indexes $res = unpack('Ldata',fread($handle,4)); $nbIndexes = $res['data']; @@ -270,29 +270,29 @@ class CatalogCacheObject extends TimeTableCacheObject $res = unpack('Lindex',fread($handle,4)); array_push($this->indexes, $res['index']); } - + return true; } - - public function modifyIntervalFromId($obj) { - + + public function modifyIntervalFromId($obj) { + foreach ($this->intervals as $interval) { if ($interval->getId() == $obj->cacheId) - { - foreach((array)$obj as $key => $val) { - + { + foreach((array)$obj as $key => $val) { + if ($key == 'start') $interval->setStartFromISO($val); else if ($key == 'stop') - $interval->setStopFromISO($val); + $interval->setStopFromISO($val); else { if (strpos($key, 'param') === false) continue; $params = $interval->getParams(); $paramIndex = (int)substr($key,5); $params[$paramIndex-2] = $val; - $interval->setParams($params); + $interval->setParams($params); } } $interval->setIsModified(true); @@ -300,33 +300,33 @@ class CatalogCacheObject extends TimeTableCacheObject return true; } } - + return false; } } -class CatalogCacheMgr extends TimeTableCacheMgr +class CatalogCacheMgr extends TimeTableCacheMgr { - + protected static $cache_file = "cacheCat"; - + protected $ttMgr = null; protected $cache = null; function __construct() { - + $this->ttMgr = new CatalogMgr(); } - - + + public function initFromTmpObject($folderId, $name) { - - //Create new cache + + //Create new cache $this->cache = new CatalogCacheObject(); //Load intervals from TmpObject file (Statistics Module) $intervals_res = $this->ttMgr->getTmpObject($folderId, $name); - + if (!isset($intervals_res)) return array('success' => false, 'message' => 'Cannot get Tmp Object'); @@ -336,28 +336,28 @@ class CatalogCacheMgr extends TimeTableCacheMgr { //Add interval $this->cache->addInterval($interval['start'], $interval['stop'], $interval['paramTable']); - + } } $this->cache->setIsModified(true); - + $paramHeaders = $intervals_res['parameters']; - + $this->cache->setParamsNumber(count($paramHeaders)); $this->cache->setParamsSizes($paramHeaders); $this->cache->setParamsTypes($paramHeaders); - - unset($intervals_res); - + + unset($intervals_res); + //Update cache $this->cache->updateIndexes(); //Save cache file - return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(), + return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(), 'status'=>$this->cache->getStatus(), 'parameters'=>$paramHeaders); } - + public function initFromUploadedFile($name, $format) { //Create new cache @@ -380,29 +380,29 @@ class CatalogCacheMgr extends TimeTableCacheMgr $this->cache->setIsModified(true); $paramHeaders = $intervals_res['parameters']; - + $this->cache->setParamsNumber(count($paramHeaders)); $this->cache->setParamsSizes($paramHeaders); $this->cache->setParamsTypes($paramHeaders); - + unset($intervals_res); - + //Update cache $this->cache->updateIndexes(); //Save cache file - return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(), + return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(), 'status'=>$this->cache->getStatus(), 'parameters'=>$paramHeaders); } - - public function initFromTT($id, $typeTT) + + public function initFromTT($id, $typeTT) { //Create new cache $this->cache = new CatalogCacheObject(); //Load intervals from catalog file and add to cache $intervals_res = $this->ttMgr->loadIntervalsFromTT($id,$typeTT); - + if (!$intervals_res['success']) return $intervals_res; @@ -411,13 +411,13 @@ class CatalogCacheMgr extends TimeTableCacheMgr //Add interval $this->cache->addInterval($interval['start'], $interval['stop'], $interval['paramTable']); } - + $paramHeaders = $intervals_res['parameters']; - + $this->cache->setParamsNumber(count($paramHeaders)); $this->cache->setParamsSizes($paramHeaders); $this->cache->setParamsTypes($paramHeaders); - + unset($intervals_res); //Update cache @@ -429,8 +429,8 @@ class CatalogCacheMgr extends TimeTableCacheMgr } - protected function loadFromFile() - { + protected function loadFromFile() + { if (!file_exists($this->getCacheFilePath())) return false; $this->cache = new CatalogCacheObject(); @@ -445,30 +445,30 @@ class CatalogCacheMgr extends TimeTableCacheMgr fclose($handle); return $result; } - - protected function getCacheFilePath() + + protected function getCacheFilePath() { return USERTTDIR.(self::$cache_file); } - public function saveInTT($id, $action, $token) - { + public function saveInTT($id, $action, $token) + { if (!$this->loadFromFile()) return array('success' => false, 'message' => 'Cannot load cache file'); if ($token != $this->cache->getToken()) return array('success' => false, 'message' => 'Cache token check error'); - + $this->cache->updateIndexes(); $this->saveToFile(); - + $intervals = $this->cache->getIntervalsArray(NULL,NULL,true); - + $this->cache->reset(); return $this->ttMgr->saveIntervals($id, $intervals, $action); } - + public function addInterval($index, $start, $stop, $params) { if (!$this->loadFromFile()) return array('success' => false, 'message' => 'Cannot load cache file'); @@ -481,42 +481,42 @@ class CatalogCacheMgr extends TimeTableCacheMgr if (!isset($stop)) $stop = date('Y-m-d\TH:i:s'); - + if (!isset($params)) - $params = []; - + $params = []; + $this->cache->addInterval($start, $stop, $params, true, $index); - + //$this->cache->updateIndexes(); - + $this->saveToFile(); - + return array('success' => true, 'index' => $index, 'status' => $this->cache->getStatus()); } - + public function modifyIntervalFromId($obj) { if (!$this->loadFromFile()) return array('success' => false, 'message' => 'Cannot load cache file'); $this->cache->modifyIntervalFromId($obj); - + $this->saveToFile(); - + return array('success' => true, 'status' => $this->cache->getStatus()); } - + public function initTTCache($nparams) { //Create new cache $this->cache = new CatalogCacheObject(); $this->cache->setParamsNumber((int)$nparams); $paramHeaders = array(); - + for ($i = 0; $i < (int)$nparams; $i++) { $paramHeaders[$i]['id'] = 'id_'.(string)($i+1); $paramHeaders[$i]['name'] = 'param_'.(string)($i+1); $paramHeaders[$i]['size'] = 1; $paramHeaders[$i]['type'] = 'Float'; - + } $this->cache->setParamsSizes($paramHeaders); $this->cache->setParamsTypes($paramHeaders); @@ -525,4 +525,4 @@ class CatalogCacheMgr extends TimeTableCacheMgr 'status' => $this->cache->getStatus(), 'parameters' => $paramHeaders); } } -?> \ No newline at end of file +?> diff --git a/php/classes/IntervalCacheObject.php b/php/classes/IntervalCacheObject.php index c565e3a..b715093 100644 --- a/php/classes/IntervalCacheObject.php +++ b/php/classes/IntervalCacheObject.php @@ -16,11 +16,11 @@ class IntervalCacheObject public function getId() { return $this->id; } - + public function getIndex() { return $this->index; } - + public function setIndex($index) { $this->index = $index; } @@ -28,35 +28,35 @@ class IntervalCacheObject public function getStartToStamp() { return $this->start; } - + public function getStartToISO() { - return CacheTools::stamp2iso($this->start); + return TimeUtils::stamp2iso($this->start); } public function setStartFromStamp($stamp) { $this->start = $stamp; } - + public function setStartFromISO($iso) { - $this->start = CacheTools::iso2stamp($iso); + $this->start = TimeUtils::iso2stamp($iso); } public function getStopToStamp() { return $this->stop; } - + public function getStopToISO() { - return CacheTools::stamp2iso($this->stop); + return TimeUtils::stamp2iso($this->stop); } public function setStopFromStamp($stamp) { $this->stop = $stamp; } - + public function setStopFromISO($iso) { - $this->stop = CacheTools::iso2stamp($iso); + $this->stop = TimeUtils::iso2stamp($iso); } - + public function getDuration() { return ($this->stop-$this->start); } @@ -76,7 +76,7 @@ class IntervalCacheObject public function setIsNew($isNew) { $this->isNew = $isNew; } - + public function toArray() { $result = array( "cacheId" => $this->id, @@ -87,16 +87,16 @@ class IntervalCacheObject $result["isNew"] = true; if ($this->isModified) $result["isModified"] = true; - + return $result; } - - + + public function writeBin($handle) { fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified)); } - + public function loadBin($handle) { $array = unpack('L6int',fread($handle,6*4)); $this->id = $array['int1']; @@ -106,7 +106,7 @@ class IntervalCacheObject $this->isNew = $array['int5']; $this->isModified = $array['int6']; } - + public function dump() { echo " => Interval : id = ".$this->id.", index = ".$this->index.", start = ".$this->start.", stop = ".$this->stop.", isNew = ".$this->isNew.", isModified = ".$this->isModified.PHP_EOL; } diff --git a/php/classes/TimeTableCacheMgr.php b/php/classes/TimeTableCacheMgr.php index 3bc1f2d..af9b262 100644 --- a/php/classes/TimeTableCacheMgr.php +++ b/php/classes/TimeTableCacheMgr.php @@ -1,22 +1,4 @@ <?php -class CacheTools -{ - public static function iso2stamp($iso) { - try { - $time = new DateTime($iso); - } - catch (Exception $e) { - $time = new DateTime('1970-01-01T00:00:00Z'); - } - $stamp = $time->format('U'); - unset($time); - return $stamp; - } - - public static function stamp2iso($stamp) { - return date('Y-m-d\TH:i:s',$stamp); - } -} class SortPartCacheObject { @@ -26,31 +8,31 @@ class SortPartCacheObject public static $TYPE_DURATION_SEC = 3; public static $TYPE_DURATION_MIN = 4; public static $TYPE_DURATION_HOUR = 5; - + public static $DIRECTION_UNKNOWN = 0; public static $DIRECTION_ASC = 1; public static $DIRECTION_DES = 2; - + protected $type; protected $dir; - + function __construct() { $this->type = self::$TYPE_UNKNOWN; $this->dir = self::$DIRECTION_UNKNOWN; } - + public function getType() { return $this->type; } - + public function getDir() { return $this->dir; } - + public function isSame($part) { return (($this->type == $part->getType()) && ($this->dir == $part->getDir())); } - + public function compare($interval_a, $interval_b) { switch ($this->type) { case self::$TYPE_START : @@ -59,7 +41,7 @@ class SortPartCacheObject case self::$DIRECTION_ASC : return ($interval_b->getStartToStamp() - $interval_a->getStartToStamp()); default : - return ($interval_a->getStartToStamp() - $interval_b->getStartToStamp()); + return ($interval_a->getStartToStamp() - $interval_b->getStartToStamp()); } } break; @@ -90,7 +72,7 @@ class SortPartCacheObject } return 0; } - + public function loadFromObject($part_obj) { switch ($part_obj->property) { @@ -112,7 +94,7 @@ class SortPartCacheObject default: $this->type = self::$TYPE_UNKNOWN; } - + switch ($part_obj->direction) { case 'ASC' : @@ -125,17 +107,17 @@ class SortPartCacheObject $this->dir = self::$DIRECTION_UNKNOWN; } } - + public function writeBin($handle) { fwrite($handle,pack('L2',$this->type,$this->dir)); } - + public function loadBin($handle) { $res = unpack('L2data',fread($handle,4*2)); $this->type = $res['data1']; $this->dir = $res['data2']; } - + public function dump() { echo " => SortPartCacheObject : type = "; switch ($this->type) @@ -202,7 +184,7 @@ class SortCacheObject array_push($this->parts, $part); } } - + public function isSameFromObject($sort_obj) { $sort = new SortCacheObject(); $sort->loadFromObject($sort_obj); @@ -221,23 +203,23 @@ class SortCacheObject return false; } } - + return true; } public function apply($intervals) { $sorted_indexes = array(); - + global $global_parts, $global_intervals; $global_parts = $this->parts; $global_intervals = $intervals; - + foreach ($intervals as $interval) array_push($sorted_indexes, $interval->getIndex()); - + if (count($global_parts) == 0) return $sorted_indexes; - + usort($sorted_indexes, function ($index_a, $index_b) { global $global_parts, $global_intervals; foreach ($global_parts as $part) @@ -284,38 +266,38 @@ class FilterPartCacheObject public static $TYPE_DURATION_SEC = 3; public static $TYPE_DURATION_MIN = 4; public static $TYPE_DURATION_HOUR = 5; - + public static $OPERATION_UNKNOWN = 0; public static $OPERATION_LT = 1; public static $OPERATION_GT = 2; public static $OPERATION_EQ = 3; - + protected $type; protected $op; protected $value; - + function __construct() { $this->type = self::$TYPE_UNKNOWN; $this->op = self::$OPERATION_UNKNOWN; $this->value = 0.; } - + public function getType() { return $this->type; } - + public function getOp() { return $this->op; } - + public function getValue() { return $this->value; } - + public function isSame($part) { return (($this->type == $part->getType()) && ($this->op == $part->getOp()) && ($this->value == $part->getValue())); } - + public function toFiltered($interval) { switch ($this->type) { case self::$TYPE_START : @@ -371,17 +353,17 @@ class FilterPartCacheObject return false; } } - + public function loadFromObject($part_obj) { $this->value = 0.; switch ($part_obj->field) { case 'start' : - $this->value = CacheTools::iso2stamp($part_obj->value); + $this->value = TimeUtils::iso2stamp($part_obj->value); $this->type = self::$TYPE_START; break; case 'stop' : - $this->value = CacheTools::iso2stamp($part_obj->value); + $this->value = TimeUtils::iso2stamp($part_obj->value); $this->type = self::$TYPE_STOP; break; case 'durationMin' : @@ -400,7 +382,7 @@ class FilterPartCacheObject $this->value = 0.; $this->type = self::$TYPE_UNKNOWN; } - + switch ($part_obj->comparison) { case 'lt' : @@ -416,21 +398,21 @@ class FilterPartCacheObject $this->op = self::$OPERATION_UNKNOWN; } } - + public function writeBin($handle) { fwrite($handle,pack('L2',$this->type,$this->op)); fwrite($handle,pack('f',$this->value)); } - + public function loadBin($handle) { $res = unpack('L2data',fread($handle,4*2)); $this->type = $res['data1']; $this->op = $res['data2']; - + $res = unpack('fvalue',fread($handle,4)); $this->value = $res['value']; } - + public function dump() { echo " => FilterPartCacheObject : type = "; switch ($this->type) @@ -475,27 +457,27 @@ class FilterPartCacheObject class FilterCacheObject { protected $parts = array(); - + function __construct() { - + } - + public function getParts() { return $this->parts; } - + public function reset() { $this->parts = array(); } - + public function isEmpty() { return (count($this->parts) == 0); } - + public function loadFromJSON($filter_json) { $this->reset(); $filter_obj = json_decode($filter_json); - + foreach ($filter_obj as $filter_part) { $part = new FilterPartCacheObject(); @@ -503,11 +485,11 @@ class FilterCacheObject array_push($this->parts, $part); } } - + public function isSame($filter) { if (count($this->parts) != count($filter->getParts())) return false; - + $identique = true; for ($i = 0; $i < count($this->parts); ++$i) { @@ -516,16 +498,16 @@ class FilterCacheObject return false; } } - + return true; } - + public function isSameFromJSON($filter_json) { $filter = new FilterCacheObject(); $filter->loadFromJSON($filter_json); return $this->isSame($filter); } - + public function toFiltered($interval) { foreach ($this->parts as $part) { @@ -534,13 +516,13 @@ class FilterCacheObject } return false; } - + public function writeBin($handle) { fwrite($handle,pack('L',count($this->parts))); foreach ($this->parts as $part) $part->writeBin($handle); } - + public function loadBin($handle) { $this->reset(); $res = unpack('Lcount',fread($handle,4)); @@ -551,7 +533,7 @@ class FilterCacheObject array_push($this->parts, $part); } } - + public function dump() { echo " => FilterCacheObject : number of parts = ".count($this->parts).PHP_EOL; foreach ($this->parts as $part) @@ -562,7 +544,7 @@ class FilterCacheObject class TimeTableCacheMgr { protected static $cache_file = "cacheTT"; - + protected $ttMgr = null; protected $cache = null; @@ -574,7 +556,7 @@ class FilterCacheObject public function initTTCache() { //Create new cache $this->cache = new TimeTableCacheObject(); - + //Save cache file return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus()); } @@ -585,7 +567,7 @@ class FilterCacheObject //Load intervals from TT file and add to cache $intervals_res = $this->ttMgr->loadIntervalsFromTT($id,$typeTT); - + if (!$intervals_res['success']) return $intervals_res; @@ -594,7 +576,7 @@ class FilterCacheObject //Add interval $this->cache->addInterval($interval['start'], $interval['stop']); } - + unset($intervals_res); //Update cache @@ -650,9 +632,9 @@ class FilterCacheObject } $this->cache->setIsModified(true); - + unset($intervals_res); - + //Update cache $this->cache->updateIndexes(); @@ -680,9 +662,9 @@ class FilterCacheObject } $this->cache->setIsModified(true); - + unset($intervals_res); - + //Update cache $this->cache->updateIndexes(); @@ -696,14 +678,14 @@ class FilterCacheObject if ($token != $this->cache->getToken()) return array('success' => false, 'message' => 'Cache token check error'); - + $this->cache->updateIndexes(); $this->saveToFile(); - + $intervals = $this->cache->getIntervalsArray(NULL,NULL,true); - + $this->cache->reset(); - + $res_intervals = array(); foreach ($intervals as $interval) { @@ -717,7 +699,7 @@ class FilterCacheObject } unset($intervals); - + return $this->ttMgr->saveIntervals($id, $res_intervals, $action); } @@ -742,7 +724,7 @@ class FilterCacheObject $this->cache->getFilter()->reset(); } } - + if (isset($sort_obj)) { if (!$this->cache->getSort()->isSameFromObject($sort_obj)) @@ -759,7 +741,7 @@ class FilterCacheObject $this->cache->getSort()->reset(); } } - + if ($needToUpdate) { $this->cache->updateIndexes(); @@ -789,13 +771,13 @@ class FilterCacheObject if (!isset($stop)) $stop = date('Y-m-d\TH:i:s'); - + $this->cache->addInterval($start, $stop, true, $index); - + //$this->cache->updateIndexes(); - + $this->saveToFile(); - + return array('success' => true, 'index' => $index, 'status' => $this->cache->getStatus()); } @@ -804,7 +786,7 @@ class FilterCacheObject return array('success' => false, 'message' => 'Cannot load cache file'); $this->cache->removeIntervalFromId($id); - + $this->cache->updateIndexes(); $this->saveToFile(); @@ -817,38 +799,38 @@ class FilterCacheObject return array('success' => false, 'message' => 'Cannot load cache file'); $this->cache->modifyIntervalFromId($id, $start, $stop); - + $this->saveToFile(); - + return array('success' => true, 'status' => $this->cache->getStatus()); } - + public function operationIntervals($extendTime, $shiftTime) { if (!$this->loadFromFile()) return array('success' => false, 'message' => 'Cannot load cache file'); - + $this->cache->operationIntervals($extendTime, $shiftTime); - + $this->saveToFile(); - + return array('success' => true, 'status' => $this->cache->getStatus()); } - + public function mergeIntervals() { if (!$this->loadFromFile()) return array('success' => false, 'message' => 'Cannot load cache file'); - + $this->cache->mergeIntervals(); - + $this->saveToFile(); - + return array('success' => true, 'status' => $this->cache->getStatus()); } - + public function getStatistics() { if (!$this->loadFromFile()) return array('success' => false, 'message' => 'Cannot load cache file'); - + return array('success' => true, "result" => $this->cache->getStatistics(), 'status' => $this->cache->getStatus()); } @@ -860,11 +842,11 @@ class FilterCacheObject } $this->cache->dump(); } - + protected function getCacheFilePath() { return USERTTDIR.(self::$cache_file); } - + protected function saveToFile() { if (!isset($this->cache)) return false; @@ -879,7 +861,7 @@ class FilterCacheObject fclose($handle); return $result; } - + protected function loadFromFile() { if (!file_exists($this->getCacheFilePath())) return false; diff --git a/php/classes/TimeUtils.php b/php/classes/TimeUtils.php new file mode 100644 index 0000000..5bc32c4 --- /dev/null +++ b/php/classes/TimeUtils.php @@ -0,0 +1,22 @@ +<?php + +class TimeUtils +{ + public static function iso2stamp($iso) { + try { + $time = new DateTime($iso); + } + catch (Exception $e) { + $time = new DateTime('1970-01-01T00:00:00Z'); + } + $stamp = $time->format('U'); + unset($time); + return $stamp; + } + + public static function stamp2iso($stamp) { + return date('Y-m-d\TH:i:s',$stamp); + } +} + +?> -- libgit2 0.21.2