implementations sgbd dans env/sgbd
[mtweb] / mw / env / sgbd / xml / mw_xml_data.php
diff --git a/mw/env/sgbd/xml/mw_xml_data.php b/mw/env/sgbd/xml/mw_xml_data.php
new file mode 100644 (file)
index 0000000..1148da9
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+
+  class mw_xml_data{
+
+    public $host;
+    public $base;
+    public $sxml;
+    public $cache;
+    public $buffer;
+
+    public function mw_xml_data($host, $base){
+      $this->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 = "<tuple>\n";
+      foreach($data as $key => $value){
+        if(isset($value)) $this->buffer .= "  <".$key."><![CDATA[".$value."]]></".$key.">\n";
+      }
+      $this->buffer .= "</tuple>\n";
+      return $this->buffer;
+    }
+
+  }