db_xml = $db_xml; $this->namespace = $namespace; $this->head_tag = $head_tag; $hl = new hierarchy_loader(); $this->cur_group = $hl->create_group_structure($hierarchy_file, $db_xml); $this->tables = $this->get_table_names($this->cur_group); if($mode == $this->CREATE_NEW_DATABASE) { $this->cur_group->init_ids(); $tables = array(); $this->create_sql_tables($this->cur_group,$tables); } else { $this->cur_group->ids = $this->get_ids(); } } function get_table_names($group) { $group_names = $group->get_group_names(); $tables = array(); foreach($group_names as $name) { $tables[] = $this->namespace."_".$name; } return $tables; } function unhtmlentities ($string) { $trans_tbl = get_html_translation_table (HTML_ENTITIES); $trans_tbl = array_flip ($trans_tbl); $ret = strtr ($string, $trans_tbl); return preg_replace('/\&\#([0-9]+)\;/me', "chr('\\1')",$ret); } function get_ids() { $ids = array(); foreach($this->tables as $name) { $id_field = $name."_id"; $query = "SELECT max($id_field) FROM $name"; $query_result = mysql_query($query); $row = mysql_fetch_assoc($query_result); $ids[$name] = $row["max($id_field)"]+1; } return $ids; } function get_ids_fill() { $ids = array(); foreach($this->tables as $name) { $id_field = $name."_id"; $query = "SELECT $id_field FROM $name ORDER BY $id_field"; $query_result = mysql_query($query); $count = 1; while($row = mysql_fetch_assoc($query_result)) { $id = $row[$id_field]; if($id != $count) { break; } $count++; } $ids[$name] = $count; } return $ids; } function create_sql_tables($cur_group,$made_tables) { $table_name = $this->namespace."_".$cur_group->name; $table = "CREATE TABLE $table_name \n("; if($cur_group->name != $this->head_tag) { $table .= $this->head_tag."_id int unsigned,\n"; $table .= "parent_id text,\n"; } $table .= $cur_group->name."_id int unsigned not null auto_increment primary key,\n"; if($cur_group->name == $this->head_tag) { $table .= "template text,\n"; } foreach($cur_group->fields as $field=>$num) { for($i=0;$i<$num;$i++) { $table .= $field."_$i text,\n"; } } $table = substr(trim($table),0,-1).")"; mysql_query("DROP TABLE $table_name"); mysql_query($table); $made_tables[] = $cur_group->name; foreach($cur_group->groups as $group) { if(!in_array($group->name,$made_tables)) { $subgroup_made_tables = $this->create_sql_tables($group,$made_tables); foreach($subgroup_made_tables as $t) { if(!in_array($t, $made_tables)) { $made_tables[] = $t; } } } } return $made_tables; } function startHandler($xp, $element, $attribs) { //echo "START: $element
"; $element = strtolower($element); if($element == $this->head_tag) { $this->cur_group->destroy_subgroups(); $this->cur_group->assign_ids(Group::$ids[$this->head_tag],Group::$ids[$this->head_tag], Group::$ids[$this->head_tag]); $this->template = ""; $this->get_entry = true; } else if($this->get_entry) { if($element == $this->cur_group->name."_id") { $this->delete_entry = true; $this->cur_tag = $element; } else { $this->template .= "$element "; if(!array_key_exists($element,$this->cur_group->fields)) { if(!array_key_exists($element,$this->cur_group->groups)) { echo "ERROR: $element not in ".$this->cur_group->name." line ".xml_get_current_line_number($this->parser)."
"; die("ERROR: XML does not match schema"); } //echo "moving down from ".$this->cur_group->name." to "; $new_group = clone $this->cur_group->groups[$element]; $new_group->assign_ids($this->cur_group->main_id, $this->cur_group->id, $this->cur_group->name); $this->cur_group->group_vals[$new_group->name][] = $new_group; $new_group->parent = $this->cur_group; $this->cur_group = $new_group; unset($new_group); //echo $this->cur_group->name."\n"; } else { $this->cur_tag = $element; } } } } function endHandler($xp, $element) { //echo "END: $element
"; $element = strtolower($element); if($this->head_tag == $element) { $this->count++; $ref = $this->cur_group->field_vals["ref"][0]; echo nl2br("$this->count : ref : $ref \n"); $this->cur_group->template = trim($this->template); $this->head_tag_ids[] = $this->cur_group->id; $result = $this->cur_group->to_ssv($this->head_tag); foreach($result as $table=>$rows) { foreach($rows as $row) { $this->results[$table][] = $row; } if(count($this->results[$table]) == 200) { echo "Writing $table data to file...\n"; foreach($this->results[$table] as $row) { $s .= trim($row)."\n"; } $out = fopen($this->namespace."_".$table.".sql","a"); fwrite($out,$s); fclose($out); unset($this->results[$table]); } } unset($result); $this->get_entry = false; } else if($element == $this->cur_group->name) { $this->template .= "/$element "; $child = $this->cur_group; //echo "moving up from $child->name to "; $this->cur_group = $child->parent; unset($child); //echo $this->cur_group->name." \n"; } else if($element != $this->cur_group->name."_id"){ $this->cur_group->add_value($this->cur_tag, $this->cur_data); $this->cur_tag = ""; $this->cur_data = ""; } } function cDataHandler($xp, $data) { $data = trim($data); if($data != "") { if($this->delete_entry) { $this->cur_group->id = $data; if($this->cur_tag == $this->head_tag."_id") { $this->cur_group->main_id = $data; } $this->delete_entry($this->cur_group->name,$this->cur_group->name."_id",$data); $this->delete_entry = false; } else { $data = ereg_replace("&","&",$data); $data = ereg_replace("<","<",$data); $data = ereg_replace(">",">",$data); $this->cur_data .= $data; } } } function parse() { $in = fopen($this->db_xml,"r"); $HEAD_TAG_START = "<".$this->head_tag.">"; $this->parser = xml_parser_create(); xml_set_object($this->parser,$this); xml_set_element_handler($this->parser,"startHandler","endHandler"); xml_set_character_data_handler($this->parser,"cDataHandler"); while($line = fgets($in)) { $line = ereg_replace("&","&",$line); $good_parse = xml_parse($this->parser,$line,false); if(!$good_parse) { echo "there was an error
"; echo "line: ".xml_get_current_line_number($this->parser)."
"; } } foreach($this->results as $table=>$rows) { foreach($rows as $row) { $s = ""; $s .= trim($row)."\n"; $out = fopen($this->namespace."_".$table.".sql","a"); fwrite($out,$s); fclose($out); } } xml_parser_free($this->parser); } function xml_to_tabledata() { $this->parse(); foreach($this->tables as $table) { //echo "searching for $table ...
"; $path = realpath($table.".sql"); $path = str_replace("\\","\\\\",$path); if(file_exists($table.".sql")) { $query = "LOAD DATA INFILE '$path' INTO TABLE $table FIELDS TERMINATED BY ' ' ENCLOSED BY '^'"; //$query = "LOAD DATA LOCAL INFILE '/home/cluster1/data/o/d/1121861/html/php/$table.sql' INTO TABLE $table FIELDS TERMINATED BY ' ' ENCLOSED BY '^'"; mysql_query($query); if(mysql_error()) { echo mysql_error()."
"; } unlink($table.".sql"); } } return $this->head_tag_ids; } function xml_to_insert() { $this->parse(); foreach($this->tables as $table) { $in = fopen($table.".sql","r"); while($line = fgets($in)) { $line = str_replace("^","'",$line); $line = str_replace(" ",",",$line); $line = "(".$line.")"; $query = "INSERT INTO $table () VALUES $line"; mysql_query($query); echo mysql_error()."
"; } //unlink($table.".sql"); } return $this->head_tag_ids; } function delete_entry($table, $field, $value) { $query = "DELETE FROM $table WHERE $field='$value'"; mysql_query($query); echo mysql_error(); } } //$dl = new db_loader(1,"test.xml","new_hierarchy.xml","nahuatl","refgroup"); //$dl->xml_to_tabledata(); ?>