X-Git-Url: http://git.dj3c1t.com/index.cgi?a=blobdiff_plain;ds=sidebyside;f=mw%2Fenv%2Fsgbd%2Fxml%2Fmw_xml_data.php;fp=mw%2Fenv%2Fsgbd%2Fxml%2Fmw_xml_data.php;h=1148da95458d656708b3fc498ecb10c6b054231e;hb=ad9756b0b72852c82165e824570f21b039fcb359;hp=0000000000000000000000000000000000000000;hpb=8da84cf3aa4d10d91f19b6df06ce4c5e9fcb79da;p=mtweb diff --git a/mw/env/sgbd/xml/mw_xml_data.php b/mw/env/sgbd/xml/mw_xml_data.php new file mode 100644 index 0000000..1148da9 --- /dev/null +++ b/mw/env/sgbd/xml/mw_xml_data.php @@ -0,0 +1,108 @@ +host = $host.(substr($host, -1) != "/" ? "/" : ""); + $this->base = $base.(substr($base, -1) != "/" ? "/" : ""); + $this->cache = array(); + } + + public function host(){ + return $this->host; + } + + public function base(){ + return $this->base; + } + + public function use_cache(){ + return true; + } + + public function set_cache($data_name, $data, $data_id){ + if($this->use_cache()){ + $this->cache[$data_name] = $data; + $this->cache[$data_name]["id"] = $data_id; + } + } + + public function get_data($data_path, $data_id){ + $data_name = $this->data_name($data_path, $data_id); + if(isset($this->cache[$data_name])) return $this->cache[$data_name]; + if($this->buffer = @file_get_contents($data_name)){ + if(($data = $this->parse_data()) !== false){ + $this->set_cache($data_name, $data, $data_id); + $data["id"] = $data_id; + return $data; + } + } + return false; + } + + public function add_data($data_path, $data_id, $data){ + return $this->_set_data($data_path, $data_id, $data); + } + + public function set_data($data_path, $data_id, $data){ + return $this->_set_data($data_path, $data_id, $data); + } + + public function _set_data($data_path, $data_id, $data){ + if($fh = @fopen($this->data_name($data_path, $data_id), "w")){ + $this->buffer = $this->serialize_data($data); + if(@fwrite($fh, $this->buffer) !== false){ + @fclose($fh); + $this->buffer = null; + $data_name = $this->data_name($data_path, $data_id); + $this->set_cache($data_name, $data, $data_id); + return $data_id; + } + else{ + @fclose($fh); + $this->buffer = null; + } + } + return null; + } + + public function del_data($data_path, $data_id){ + $data_name = $this->data_name($data_path, $data_id); + if(isset($this->cache[$data_name])) unset($this->cache[$data_name]); + return @unlink($this->data_name($data_path, $data_id)); + } + + public function data_name($data_path, $data_id){ + return $this->host.$this->base.$data_path.$data_id.".xml"; + } + + public function parse_data(){ + if(!isset($this->sxml)) $this->sxml = new sxml(); + $this->sxml->parse($this->buffer); + if(isset($this->sxml->data["tuple"][0])){ + $this->buffer = $this->sxml->data["tuple"][0]; + $v_rst = array(); + foreach($this->buffer["subs"] as $key => $value){ + $v_rst[$key] = $value[0]["data"]; + } + return $v_rst; + } + return false; + } + + public function serialize_data($data){ + $this->buffer = "\n"; + foreach($data as $key => $value){ + if(isset($value)) $this->buffer .= " <".$key.">\n"; + } + $this->buffer .= "\n"; + return $this->buffer; + } + + }