Tryag File Manager
Home
-
Turbo Force
Current Path :
/
home
/
cluster1
/
data
/
bu01
/
1121861
/
html
/
old_jlex_versions
/
beta
/
2005-08-04 php4
/
Upload File :
New :
File
Dir
/home/cluster1/data/bu01/1121861/html/old_jlex_versions/beta/2005-08-04 php4/mysql_to_xml.php4
<? include_once("group.php4"); include_once("schema_loader.php4"); include_once("char_classes_parser.php4"); include_once("db_connection.php4"); class mysql_to_xml { var $tables; var $form_vars; var $num_conditions; var $head_tag; var $cur_group; var $max_results = 50; var $database; function mysql_to_xml($form_vars) { $this->form_vars = $form_vars; $this->database = $form_vars["database"]; $schema = trim($form_vars["schema"]); if($schema == "") { $schema = "../$this->database/schema.xml"; } else { $schema = "../$this->database/$schema"; } $sl = new schema_loader(); $this->cur_group = $sl->create_group_structure($schema,""); $this->head_tag = $this->cur_group->name; } function get_table_names($group) { $group_names = $group->get_group_names(); $tables = array(); foreach($group_names as $name) { $tables[] = $this->database."_".$name; } return $tables; } function neutralize_vowels($s) { $s= preg_replace("/([aeiou]):/","$1",$s); $s= preg_replace("/([aeiou])/","$1:?",$s); return $s; } function combine_subqueries($sub_queries) { $s = "("; foreach($sub_queries as $sub_query) { $s .= $sub_query." OR "; } $s = substr($s,0,-4); $s .= ")"; return $s; } /** This function returns a list of all the main_ids of records in the database which match the search criteria. There are two built in functions. 1. The first allows the user to search a field in the database for an exact match of a word. In this case, function="word_search", field="X", word="Y". 2. The second allows the user to search a field by regular expression. In this case, function="reg_exp", field="X", reg_exp="Y". If no user function is provided, the function builds a valid query based on user supplied data. All forms must contain the following variables: 1. phrases_and_fields : This provides a list of the fields in the database to be searched. 2. reg_exp : the regular expression being used to search the database. 3. string : the user supplied value being searched for. 4. num_conditions: the number of conditions in the form. The form consists of one or more conditions allowing the user to do multi_value queries. In each condition of the form, each variable will be named variable_i where i is the number of the condition. For example, in condition 0, the variables will be: phrases_and_fields_0, reg_exp_0, and string_0. This allows flexibility for the linguist who may want to have many or few allowable conditions. As long as the number of conditions provided, the program will cycle through checking to see if the web-user provided values for each condition. It should be noted that a web-user must use each condition in order. If he supplies data in the 0th and 2nd conditions, only data from condition 0 will be processed. The linguist may choose to include character classes. A character class consists of a symbol naming the class, and a set of letters which the class contains. If the linguist uses this option, he will provide a variable in the form called "character_classes" which has as a value the name of the xml file containg the relevant information. */ function unhtmlentities ($string) { $trans_tbl = get_html_translation_table (HTML_ENTITIES); $trans_tbl = array_flip ($trans_tbl); $length = strlen($string); for($i=0;$i<$length;$i++) { $char = $string[$i]; } $ret = strtr ($string, $trans_tbl); return preg_replace('/&#(\d+);/me', "chr('\\1')",$ret); } function advanced_query() { $ids = array(); /* $char_classes = trim($this->form_vars["char_classes"]); if($char_classes != "") { $ccp = new char_classes_parser($this->form_vars["char_classes"]); $char_classes = $ccp->get_char_classes(); } else { $char_classes = array(); } */ $sort_fields = strtolower($this->form_vars["sort_order"]); $sort_fields = explode(",",$sort_fields); $sort_order = ""; foreach($sort_fields as $field) { $field = trim($field); if(array_key_exists($field,$this->cur_group->fields)) { $num_fields = $this->cur_group->fields[$field]; for($i=0;$i<$num_fields;$i++) { $sort_order .= $field."_".$i.","; } } } $sort_order = substr($sort_order,0,-1); $subqueries = ""; $num_conditions = $this->form_vars["num_conditions"]; if($num_conditions == "") { $num_conditions = 1; } for($i=0; $i<$num_conditions; $i++) { $connective_var = "connectives_".$i; $connective = trim($this->form_vars[$connective_var]); $subqueries .= $connective; $string_var = "string_".$i; $string = trim($this->form_vars[$string_var]); $string = ereg_replace("[+]","\\\+",$string); $neutralize_var = "neutralize_".$i; $fields_var = "phrases_and_fields_".$i; $fields = trim($this->form_vars[$fields_var]); $reg_exp_var = "reg_exps_".$i; $reg_exp = stripslashes($this->form_vars[$reg_exp_var]); if(($reg_exp == "equals") OR ($reg_exp == "")) { $reg_exp = "regexp '^&$'"; } if($string != "" || ereg("<>", $reg_exp)) { $subqueries .= "("; if(array_key_exists($neutralize_var,$this->form_vars)) { if(trim($this->form_vars[$neutralize_var]) == "on") { $string = $this->neutralize_vowels($string); } } /* foreach($char_classes as $key=>$value) { $string = ereg_replace($key,$value,$string); } */ if(eregi("not",$reg_exp)) { $inner_connective = "AND"; } else { $inner_connective = "OR"; } $regexp_phrase = ereg_replace("&", $string, $reg_exp); $fields = explode(" ", $fields); foreach($fields as $field) { $group_names = $this->cur_group->find_group($field); foreach($group_names as $group=>$count) { $table = $this->database."_".$group; $subquery = "("; for($j=0;$j<$count;$j++) { $phrase = ereg_replace("field",$table.".".$field."_$j",$regexp_phrase); $subquery .= "$phrase $inner_connective "; } $subquery = substr($subquery,0,-(strlen($inner_connective)+2)).")"; $subqueries .= " $subquery $inner_connective "; } } $length = strlen($inner_connective)+1; $subqueries = substr($subqueries,0,(-1*$length)); $subqueries .= ")"; } } $tables = $this->get_table_names($this->cur_group); foreach($tables as $table) { $t .= $table.", "; } $t = substr($t,0,-2); $id = $this->database."_".$this->cur_group->name.".".$this->head_tag."_id"; $query = "SELECT DISTINCT $id FROM $t WHERE $subqueries"; if(($sort_order != "") && ($table == $this->head_tag)){ $query .= "ORDER BY $sort_order"; } //echo "<BR>query: $query <BR><BR>"; $query_result = mysql_query($query); if(mysql_error()) { echo mysql_error()."<BR>"; } while($row = mysql_fetch_assoc($query_result)) { $main_ids[] = $row[$this->head_tag."_id"]; } $main_ids = array_unique($main_ids); return $main_ids; } /**********************************************************************************************************/ function query_database() { $database = $this->form_vars["database"]; if(array_key_exists("function",$this->form_vars)) { $function = trim($this->form_vars["function"]); } else { $function = ""; } if($function != "") { if($function == "word_search") { $val = $this->form_vars["word"]; $accents = "αινσϊ"; $no_accents = "aeiou"; $vowels = array("a","e","i","o","u"); $neutralized = array("a:?","e:?","i:?","o:?","u:?"); $val = str_replace($vowels,$neutralized,$val); /* for($i=0;$i<5;$i++) { $val = ereg_replace($no_accents[$i],$accents[$i],$val); } */ $operator = " REGEXP "; } else if($function = "reg_exp") { $val = $this->form_vars["reg_exp"]; $operator = " regexp "; } $val = urldecode($val); $val = $this->unhtmlentities($val); $fields = $this->form_vars["fields"]; $fields = ereg_replace(" ","",$fields); $fields = explode(",",$fields); $s = ""; foreach($fields as $field) { if(array_key_exists($field, $this->cur_group->fields)) { $count = $this->cur_group->fields[$field]; for($i=0;$i<$count;$i++) { $s .= $field."_".$i." $operator '$val' OR "; } } } $s = substr($s,0,-4); $query = "SELECT ".$this->head_tag."_id FROM ".$database."_$this->head_tag WHERE $s ORDER BY alpha_0"; $query_result = mysql_query($query); while($row = mysql_fetch_assoc($query_result)) { $main_ids[] = $row[$this->head_tag."_id"]; } } else { $ids = array(); $search_stripped_fields = trim($this->form_vars["search_stripped_fields"]); $char_classes = trim($this->form_vars["char_classes"]); if($char_classes != "") { $ccp = new char_classes_parser(); $char_classes = $ccp->get_char_classes($char_classes); } else { $char_classes = array(); } $sort_fields = strtolower($this->form_vars["sort_order"]); $sort_fields = explode(",",$sort_fields); $sort_order = ""; foreach($sort_fields as $field) { $field = trim($field); if(array_key_exists($field,$this->cur_group->fields)) { $num_fields = $this->cur_group->fields[$field]; for($i=0;$i<$num_fields;$i++) { $sort_order .= $field."_".$i.","; } } } $sort_order = substr($sort_order,0,-1); $subquery_set = array(); $prev_connective = ""; $AND_ids = array(); $OR_ids = array(); $num_conditions = $this->form_vars["num_conditions"]; if($num_conditions == "") { $num_conditions = 1; } $has_and_condition = false; for($i=0; $i<$num_conditions; $i++) { $string_var = "string_".$i; $string = trim($this->form_vars[$string_var]); $string = ereg_replace("[+]","\\\+",$string); $neutralize_var = "neutralize_".$i; $fields_var = "phrases_and_fields_".$i; $fields = trim($this->form_vars[$fields_var]); $reg_exp_var = "reg_exps_".$i; $reg_exp = stripslashes($this->form_vars[$reg_exp_var]); if(($reg_exp == "equals") OR ($reg_exp == "")) { $reg_exp = "regexp '^&$'"; } $connective_var = "connectives_".$i; $connective = trim($this->form_vars[$connective_var]); if($string != "" || ereg("<>", $reg_exp)) { if(array_key_exists($neutralize_var,$this->form_vars)) { if(trim($this->form_vars[$neutralize_var]) == "on") { $string = $this->neutralize_vowels($string); } } foreach($char_classes as $key=>$value) { $string = ereg_replace($key,$value,$string); } if(eregi("not",$reg_exp)) { $inner_connective = "AND"; } else { $inner_connective = "OR"; } $cur_subquery_set = array(); $cur_ids = array(); $regexp_phrase = ereg_replace("&", $string, $reg_exp); $fields = explode(" ", $fields); if($search_stripped_fields == "true") { foreach($fields as $field) { if($this->cur_group->contains_field($field."_s")) { $fields[] = $field."_s"; } } } foreach($fields as $field) { $group_names = $this->cur_group->find_group($field); foreach($group_names as $table=>$count) { $subquery = "("; for($j=0;$j<$count;$j++) { $phrase = ereg_replace("field",$field."_$j",$regexp_phrase); $subquery .= "$phrase $inner_connective "; } $subquery = substr($subquery,0,-(strlen($inner_connective)+2)).")"; $cur_subquery_set[$table][] = $subquery; } } if($prev_connective == "AND") { $has_and_condition = true; $keys = array_keys($cur_subquery_set); foreach($keys as $table) { $q = $this->combine_subqueries($cur_subquery_set[$table]); $query = "SELECT DISTINCT ".$this->head_tag."_id FROM ".$database."_$table WHERE $q"; if($sort_order != "") { $query .= " ORDER BY $sort_order"; } //echo "<BR>QUERY: $query <BR>"; $query_result = mysql_query($query); while($row = mysql_fetch_assoc($query_result)) { $cur_ids[] = $row[$this->head_tag."_id"]; } if(count($AND_ids) > 0) { $AND_ids = array_intersect($AND_ids, $cur_ids); $temp = array(); foreach($AND_ids as $id) { $temp[] = $id; } $AND_ids = $temp; } else { $AND_ids = $cur_ids; } } } else { $keys = array_keys($cur_subquery_set); foreach($keys as $table) { if(!array_key_exists($table,$subquery_set)) { $subquery_set[$table] = $cur_subquery_set[$table]; } else { //echo "BEFORE: ".count($sub_queries[$table]); $subquery_set[$table] = array_merge($subquery_set[$table], $cur_subquery_set[$table]); //echo "BEFORE: ".count($sub_queries[$table]); } } } } $prev_connective = $connective; } $tables = array_keys($subquery_set); foreach($tables as $table) { $q = $this->combine_subqueries($subquery_set[$table]); $query = "SELECT DISTINCT ".$this->head_tag."_id FROM ".$database."_$table WHERE $q"; if(($sort_order != "") && ($table == $this->head_tag)){ $query .= " ORDER BY $sort_order"; } //echo "<BR>query: $query <BR><BR>"; $query_result = mysql_query($query); while($row = mysql_fetch_assoc($query_result)) { $OR_ids[] = $row[$this->head_tag."_id"]; } } if($has_and_condition) { $main_ids = array_intersect($OR_ids, $AND_ids); $temp = array(); foreach($main_ids as $id) { $temp[] = $id; } $main_ids = $temp; } else { $main_ids = $OR_ids; } $main_ids = array_unique($main_ids); } $main_ids = array_unique($main_ids); /* foreach($main_ids as $count=>$id) { echo "($count)ID: $id <BR>"; } echo "num_ids: ".count($main_ids)."<BR>"; */ return $main_ids; } /* The linguist indicates the corresponding XSLT file using the variable, $stylesheet. If no $stylesheet is provided, the data is returned in xml form. */ function convert_resultset_to_xml($main_ids, $page, $stylesheet) { $xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"; if($stylesheet != "") { $xml .= "<?xml-stylesheet href='$stylesheet' type='text/xsl'?>"; } $strings = ""; for($i=0;$i<$this->form_vars["num_conditions"];$i++) { $string = $this->form_vars["string_".$i]; if($string != "") { $string = ereg_replace("<","<",$string); $string = ereg_replace(">",">",$string); $strings .= "<search_string>$string</search_string>"; } else { break; } } $xml .= "<resultset num_results='".count($main_ids)."' page='$page'>"; $xml .= $strings; if(count($main_ids) > 0) { $reg_exp = ""; $last = $page*$this->max_results; if($last > count($main_ids)) { $last = count($main_ids); } for($j=($page*$this->max_results-$this->max_results);$j<$last;$j++) { $ids[] = $main_ids[$j]; } foreach($ids as $main_id) { $main_id = trim($main_id); if($main_id != "") { $reg_exp .= $this->head_tag."_id = $main_id OR "; } } $reg_exp = substr($reg_exp,0,-4); $tables = $this->get_table_names($this->cur_group); foreach($tables as $table) { $group = substr($table,strpos($table,"_")+1); $query = "SELECT * FROM $table WHERE $reg_exp"; //echo "query: $query <BR>"; $query_result = mysql_query($query); if($group == $this->head_tag) { $id_field = $this->head_tag."_id"; } else { $id_field = "parent_id"; } while($row = mysql_fetch_assoc($query_result)) { $data[$row[$id_field]."_$group"][] = $row; } } foreach($ids as $index=>$main_id) { if($main_id != "") { $this->cur_group->destroy_subgroups(); $this->cur_group->main_id = $main_id; $this->cur_group->id = $main_id; $this->cur_group->parent = false; $row = $data[$main_id."_".$this->cur_group->name][0]; //echo "row: <BR>".var_dump($row)."<BR>"; $this->cur_group->load_query_result($row,$data); //$this->cur_group->print_group_values()."<BR><BR>"; $template = explode(" ",trim($this->cur_group->template)); //echo $this->cur_group->print_group_values(); $cur_xml = $this->cur_group->to_xml_3($template,false); //echo $cur_xml; $xml .= $cur_xml; } } } $xml .= "</resultset>"; return $xml; } function load_resultset_into_group($main_id) { $reg_exp = ""; $main_id = trim($main_id); if($main_id != "") { $reg_exp = $this->head_tag."_id = $main_id"; } $tables = $this->get_table_names($this->cur_group); foreach($tables as $table) { $group = substr($table,strpos($table,"_")+1); $query = "SELECT * FROM $table WHERE $reg_exp"; //echo "query: $query <BR>"; $query_result = mysql_query($query); if($group == $this->head_tag) { $id_field = $this->head_tag."_id"; } else { $id_field = "parent_id"; } while($row = mysql_fetch_assoc($query_result)) { $data[$row[$id_field]."_$group"][] = $row; } } $this->cur_group->destroy_subgroups(); $this->cur_group->main_id = $main_id; $this->cur_group->id = $main_id; $this->cur_group->parent = false; $row = $data[$main_id."_".$this->cur_group->name][0]; $this->cur_group->load_query_result($row,$data); $template = explode(" ",trim($this->cur_group->template)); return $this->cur_group; } } ?>