Ensemble Attribute Export Utility

"; echo "ATTEMPTED QUERY: ".$query."
"; echo "RECEIVED ERROR MESSAGE: ".$error_msg."
"; } function interpret_fieldtype($type) { // interprets the mysql type and returns a type string // that can be interpreted by the GetSQLValueString function if(strncmp($type,"int",3) == 0) return "int"; elseif(strncmp($type,"varchar",7) == 0) return "text"; elseif(strcmp($type,"date") == 0) return "date"; elseif(strcmp($type,"time") == 0) return "time"; elseif(strncmp($type,"enum",4) == 0) return "text"; elseif(strcmp($type,"float") == 0) return "float"; elseif(strcmp($type,"double") == 0) return "double"; else return "text"; } function dump_table() { $numargs = func_num_args(); $arglist = func_get_args(); for ($iarg = 0; $iarg < $numargs; $iarg += 2) { switch($arglist[$iarg]) { case 'table': $table_name = $arglist[$iarg+1]; break; case 'key_fld': $key_fld = $arglist[$iarg+1]; break; case 'key_val': $key_val = $arglist[$iarg+1]; break; case 'get_vals': $get_val_fieldname_list = explode(',',$arglist[$iarg+1]); break; default: echo sprintf("function dump_table: unrecognized tag '%s'",$arglist[$iarg+1]); } } $sql_get_fieldnames = sprintf('describe `%s`',$table_name); $result_get_fieldnames = mysql_query($sql_get_fieldnames) or die(mysql_error()); while($row_get_fieldnames = mysql_fetch_assoc($result_get_fieldnames)) { $fld = $row_get_fieldnames['Field']; $fieldnames[] = $fld; $quoted_fieldnames[] = '`' . $fld . '`'; $types[$fld] = interpret_fieldtype($row_get_fieldnames['Type']); } if(isset($key_fld)) $sql_get_records = sprintf('select * from `%s` where `%s` in (%s)',$table_name,$key_fld,$key_val); else $sql_get_records = sprintf('select * from `%s`',$table_name); $result_records = mysql_query($sql_get_records) or echo_sql_and_die($sql_get_records,mysql_error()); while($next_row = mysql_fetch_assoc($result_records)) { unset($vals); //we could just assume that the fields are going to be in the same order as listed in $fieldnames, but //it's better to be safe about this. foreach($fieldnames as $field) { $new_col = addslashes($next_row[$field]); $vals[] = GetSQLValueString($new_col,$types[$field]); } //$insert_vals contains the values of all the fields for a single record $insert_vals[] = '(' . implode(',',$vals) . ')'; if(isset($get_val_fieldname_list)) { foreach($get_val_fieldname_list as $get_val_fieldname) { $new_val = GetSQLValueString($next_row[$get_val_fieldname],$types[$get_val_fieldname]); if(!isset($get_val_array[$get_val_fieldname]) || !in_array($new_val,$get_val_array[$get_val_fieldname])) $get_val_array[$get_val_fieldname][] = $new_val; } } } // using insert ignore here, so that if a duplicate key exists, it won't be overwritten, but the query will still proceed and a warning will be issued instead if(!empty($insert_vals)) //using a tag here to indicate a carriage return. Inserting a \n in a string doesn't work. The will need to be replaced by actual linefeeds later $sql_insert = 'insert ignore into `' . $table_name . '` (' . implode(',',$quoted_fieldnames) . ') values ' . implode(',',$insert_vals) . ';'; else $sql_insert = ''; if(isset($get_val_fieldname_list)) { $idx = 0; foreach($get_val_fieldname_list as $get_val_fieldname) { $get_val_array[$get_val_fieldname] = array_unique($get_val_array[$get_val_fieldname]); $return_vals[$idx] = implode(',',$get_val_array[$get_val_fieldname]); $idx++; } $return_array = array_merge(array($sql_insert),$return_vals); return $return_array; } else return $sql_insert; } //end function dump_table() //unless "Export" button was pressed, send the HTTP headers if(!isset($_POST['action']) || ($_POST['action'] != "Submit")) { ?> Export Attributes

Ensemble Attribute Export Utility

This utility exports one or more Ensemble attributes, and optionally, trials and stimuli associated with the attributes. The attributes, trials, and stimuli will be exported to a file containing sql insert statements. The file can then be used to import the records to another Ensemble database with phpMyAdmin or an equivalent utility. Note that primary keys are not remapped using this method, so record importing is best performed on a new Ensemble installation. Usage of this utility is simple:

  1. Select one or more attributes that you wish to export in the menu.
  2. If you wish to export associated trials, select "Export Trials."
  3. If you wish to export stimuli that are associated with the attribute either through the trials, or through the stimulus_x_attribute table, select "Export Stimuli."
  4. Optionally, enter a filestub. If omitted, a default filestub will be used.
  5. Click Submit. The SQL file should download immediately.
"; die(); } printf("

Help

",$_SERVER['PHP_SELF']); } if(isset($_POST['action']) && ($_POST['action'] == "Submit")) { $export_attribute_ids = implode(',',$_POST['export_attribute_ids']); $dump_result[] = "-- EXPORTING FROM ATTRIBUTE TABLE"; $dump_result[] = sprintf("-- DATABASE %s",$DATABASE_NAME); $dump_result[] = "-- ".date("F d, Y; g:i a"); $dump_result[] = dump_table('table','attribute','key_fld','attribute_id','key_val',$export_attribute_ids); $do_export_trials = (isset($_POST['do_export_trials']) && ($_POST['do_export_trials'] == 'true')); $do_export_stimuli = (isset($_POST['do_export_stimuli']) && ($_POST['do_export_stimuli'] == 'true')); if($do_export_trials) { list($trial_x_attribute_dump,$trial_ids) = dump_table('table','trial_x_attribute','key_fld','attribute_id','key_val',$export_attribute_ids,'get_vals','trial_id'); if(!empty($trial_ids)) { $dump_result[] = "-- EXPORTING FROM TRIAL_X_ATTRIBUTE TABLE"; $dump_result[] = $trial_x_attribute_dump; $dump_result[] = "-- EXPORTING FROM TRIAL TABLE"; list($dump_result[], $stimulus_id1, $stimulus_id2) = dump_table('table','trial','key_fld','trial_id','key_val',$trial_ids,'get_vals','stimulus_id1,stimulus_id2'); } else $dump_result[] = "-- NO TRIALS WERE LINKED TO THE SELECTED ATTRIBUTES THROUGH THE TRIAL_X_ATTRIBUTE TABLE"; //export any stimuli linked by stimulus_id1 or stimulus_id2 in the trial table if($do_export_stimuli && (strcmp($stimulus_id1,'NULL') != 0) && !empty($stimulus_id1)) { $dump_result[] = "-- EXPORTING STIMULI LINKED THROUGH THE STIMULUS_ID1 FIELD IN THE TRIAL TABLE"; $dump_result[] = dump_table('table','stimulus','key_fld','stimulus_id','key_val',$stimulus_id1); } elseif($do_export_stimuli && !empty($trial_ids)) //only output this message if there were trial_ids $dump_result[] = "-- NO STIMULI WERE LINKED TO THE STIMULUS_ID1 FIELD IN THE TRIAL TABLE"; if($do_export_stimuli && (strcmp($stimulus_id2,'NULL') != 0) && !empty($stimulus_id2)) { $dump_result[] = "-- EXPORTING STIMULI LINKED THROUGH THE STIMULUS_ID2 FIELD IN THE TRIAL TABLE"; $dump_result[] = dump_table('table','stimulus','key_fld','stimulus_id','key_val',$stimulus_id2); } elseif($do_export_stimuli && !empty($trial_ids)) //only output this message if there were trial_ids $dump_result[] = "-- NO STIMULI WERE LINKED TO THE STIMULUS_ID2 FIELD IN THE TRIAL TABLE"; } //export any stimuli linked by stimulus_x_attribute table if($do_export_stimuli) { list($stimulus_x_attribute_dump,$att_stimIDs) = dump_table('table','stimulus_x_attribute','key_fld','attribute_id','key_val',$export_attribute_ids,'get_vals','stimulus_id'); if(!empty($stimulus_x_attribute_dump)) { $dump_result[] = "-- EXPORTING RECORDS FROM THE STIMULUS_X_ATTRIBUTE TABLE"; $dump_result[] = $stimulus_x_attribute_dump; $dump_result[] = "-- EXPORTING STIMULI LINKED THROUGH THE STIMULUS_X_ATTRIBUTE TABLE"; $dump_result[] = dump_table('table','stimulus','key_fld','stimulus_id','key_val',$att_stimIDs); } } $filename = ($_POST['filename'] == "")? "attribute_export.sql" : $_POST['filename'].".sql"; text_download_header($filename); foreach($dump_result as $value) { $value = explode('',$value); foreach($value as $valueLine) printf("$valueLine\n"); print "\n"; } } else { $sql_get_attributes = sprintf("select * from attribute"); $attribute_names = mysql_query($sql_get_attributes) or die(mysql_error()); printf("
\n",$_SERVER['PHP_SELF']); printf("
\n"); printf("Select One or More Attributes
\n"); printf("

\n"); printf("
\n"); printf("Export Trials
"); printf("
\n"); printf("Export Stimuli
"); printf("
\n"); printf("Filestub: "); printf(""); printf("
\n"); printf("
\n"); } ?>