Tryag File Manager
Home
-
Turbo Force
Current Path :
/
home
/
cluster1
/
data
/
bu01
/
1121861
/
html
/
old_jlex_versions
/
php
/
Upload File :
New :
File
Dir
/home/cluster1/data/bu01/1121861/html/old_jlex_versions/php/db_loader.php4
<? include_once("group.php4"); include_once("hierarchy_loader.php4"); include_once("db_connection.php4"); class db_loader { var $CREATE_NEW_DATABASE = 1; var $parser; var $head_tag; var $cur_group; var $cur_tag; var $cur_data; var $db_xml; var $table_data; var $tables; var $template; var $get_entry = false; var $delete_entry_ = false; var $head_tag_ids = array(); function db_loader($mode, $db_xml, $hierarchy_file, $head_tag) { $this->db_xml = $db_xml; $this->head_tag = $head_tag; $hl = new hierarchy_loader(); $this->cur_group = $hl->create_group_structure($hierarchy_file, $db_xml); $this->tables = $this->cur_group->get_group_names(); if($mode == $this->CREATE_NEW_DATABASE) { $this->cur_group->init_ids(); $this->create_sql_tables($this->cur_group,$head_tag); } else { $this->cur_group->ids = $this->get_ids(); } } 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() { $group_names = $this->cur_group->get_group_names(); $ids = array(); foreach($group_names 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() { $group_names = $this->cur_group->get_group_names(); $ids = array(); foreach($group_names 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 startHandler($xp, $element, $attribs) { //echo "START: $element <BR>"; $element = strtolower($element); if($element == $this->head_tag) { $this->cur_group->reset_values(); $this->cur_group->main_id = $this->cur_group->ids[$this->head_tag]; $this->cur_group->parent_id = $this->cur_group->ids[$this->head_tag]; $this->cur_group->id = $this->cur_group->ids[$this->head_tag]; $this->cur_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)."<BR>"; die("ERROR: XML does not match schema"); } //echo "moving down from ".$this->cur_group->name." to "; $new_group = $this->cur_group->groups[$element]; $new_group->assign_ids($this->cur_group->main_id, $this->cur_group->id, $this->cur_group->name); $parent = &$this->cur_group; unset($this->cur_group); $new_group->parent = &$parent; $parent->group_vals[$new_group->name][] = &$new_group; $this->cur_group = &$new_group; //echo $this->cur_group->name."<BR>"; } else { $this->cur_tag = $element; } } } } function endHandler($xp, $element) { //echo "END: $element <BR>"; $element = strtolower($element); if($this->head_tag == $element) { $this->cur_group->template = trim($this->template); $this->head_tag_ids[] = $this->cur_group->id; $this->cur_group->to_ssv($this->head_tag); $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 "; unset($this->cur_group); $this->cur_group = &$child->parent; //echo $this->cur_group->name." <BR>"; } 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); //echo $line; $good_parse = xml_parse($this->parser,$line,false); if(!$good_parse) { echo "there was an error <BR>"; } } xml_parser_free($this->parser); } function create_sql_tables($main_group,$head_tag) { $path = "sql_data"; $tables = array(); $tables = $main_group->create_sql_table($head_tag); foreach($tables as $name=>$create_table_sql) { mysql_query("DROP TABLE $name"); mysql_query($tables[$name]); } } function delete_entry($table, $field, $value) { $query = "DELETE FROM $table WHERE $field='$value'"; mysql_query($query); echo mysql_error(); } function xml_to_tabledata() { $this->parse(); foreach($this->tables as $table) { //$query = "LOAD DATA INFILE 'C:\\\\Program Files\\\\Apache Group\\\\Apache2\\\\htdocs\\\\xml_to_mysql\\\\sql_data\\\\$table.sql' INTO TABLE $table FIELDS TERMINATED BY ' ' ENCLOSED BY '%'"; $query = "LOAD DATA LOCAL INFILE '/home/cluster1/data/o/d/1121861/html/php/sql_data/$table.sql' INTO TABLE $table FIELDS TERMINATED BY ' ' ENCLOSED BY '^'"; mysql_query($query); echo mysql_error()."<BR>"; unlink("sql_data/".$table.".sql"); } return $this->head_tag_ids; } function xml_to_insert() { $this->parse(); foreach($this->tables as $table) { $in = fopen("sql_data/".$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("sql_data/".$table.".sql"); } return $this->head_tag_ids; } } ?>