$this->id, "start" => $this->getStartToISO(), "stop" => $this->getStopToISO() ); if ($this->isNew) $result["isNew"] = true; if ($this->isModified) $result["isModified"] = true; for ($i = 0; $i < count($this->params); $i++) { $paramObject = array(); $index = 'param'.sprintf("%d",$i+2); $result[$index] = $this->params[$i]; } return $result; } // for catalog public function setParams($params) { $this->params = $params; } public function getParams() { return $this->params; } 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++) { if ($paramsTypes[$i] == '2') // string { fwrite($handle,pack('d', strlen($this->params[$i]))); fwrite($handle, $this->params[$i],strlen($this->params[$i])); } else { if ($paramsTypes[$i] == '1') $paramString = CacheTools::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) { $array = unpack('L6int',fread($handle,6*4)); $this->id = $array['int1']; $this->index = $array['int2']; $this->start = $array['int3']; $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++) { $val = unpack('dval',fread($handle,8)); if ($paramsTypes[$i] == '2') // string { $this->params[$i] = fread($handle,$val['val']); } else { if ($paramsTypes[$i] == '1') $this->params[$i] .= CacheTools::stamp2iso($val['val']); else $this->params[$i] .= $val['val']; if ($j != $paramsSizes[$i] - 1) $this->params[$i] .= ','; } } } } 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) { $interval = new CatIntervalCacheObject($this->lastId, count($this->intervals)); ++$this->lastId; $interval->setStartFromISO($startIso); $interval->setStopFromISO($stopIso); // for catalog $interval->setParams($params); $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']; } public function setParamsTypes($params) { for ($i = 0; $i < $this->paramsNumber; $i++) $this->paramsTypes[$i] = $params[$i]['type']; } 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++) fwrite($handle,pack('L',$this->paramsSizes[$i])); //Params Types 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; if (($res['version'] != TimeTableCacheObject::$format_version)) return false; //Token $token = ""; for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i) { if (!$res = unpack('Ctoken',fread($handle,1))) return false; $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))) return false; $this->paramsSizes[$i] = $res['size']; } //ParamsTypes for ($i = 0; $i < $this->paramsNumber; $i++) { if (!$res = unpack('Ltype',fread($handle,4))) return false; $this->paramsTypes[$i] = $res['type']; } //Intervals $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); array_push($this->intervals, $interval); } //Indexes $res = unpack('Ldata',fread($handle,4)); $nbIndexes = $res['data']; for ($i = 0; $i < $nbIndexes; ++$i) { $res = unpack('Lindex',fread($handle,4)); array_push($this->indexes, $res['index']); } return true; } public function modifyIntervalFromId($obj) { foreach ($this->intervals as $interval) { if ($interval->getId() == $obj->cacheId) { foreach((array)$obj as $key => $val) { if ($key == 'start') $interval->setStartFromISO($val); else if ($key == 'stop') $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->setIsModified(true); $this->isModified = true; return true; } } return false; } } 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 $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'); if (array_key_exists('intervals', $intervals_res)) { foreach ($intervals_res['intervals'] as $interval) { //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); //Update cache $this->cache->updateIndexes(); //Save cache file return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(), 'status'=>$this->cache->getStatus(), 'parameters'=>$paramHeaders); } public function initFromUploadedFile($name, $format) { //Create new cache $this->cache = new CatalogCacheObject(); //Load intervals from uploaded file $intervals_res = $this->ttMgr->getUploadedObject($name, $format); if (!isset($intervals_res)) return array('success' => false, 'message' => 'Cannot get Uploaded Object'); if (array_key_exists('intervals', $intervals_res)) { foreach ($intervals_res['intervals'] as $interval) { //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); //Update cache $this->cache->updateIndexes(); //Save cache file return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(), 'status'=>$this->cache->getStatus(), 'parameters'=>$paramHeaders); } 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; foreach ($intervals_res['intervals'] as $interval) { //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 $this->cache->updateIndexes(); //Save cache file return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $paramHeaders); } protected function loadFromFile() { if (!file_exists($this->getCacheFilePath())) return false; $this->cache = new CatalogCacheObject(); $handle = fopen($this->getCacheFilePath(), 'rb'); $result = false; if (flock($handle, LOCK_SH)) { $this->cache->loadBin($handle); flock( $handle, LOCK_UN ); $result = true; } fclose($handle); return $result; } protected function getCacheFilePath() { return USERTTDIR.(self::$cache_file); } 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'); if (!isset($index)) $index = 0; if (!isset($start)) $start = date('Y-m-d\TH:i:s'); if (!isset($stop)) $stop = date('Y-m-d\TH:i:s'); if (!isset($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); //Save cache file return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(), 'parameters' => $paramHeaders); } } ?>