Skip to content

Instantly share code, notes, and snippets.

@lmatteis
Created April 19, 2012 14:15
Show Gist options
  • Save lmatteis/2421227 to your computer and use it in GitHub Desktop.
Save lmatteis/2421227 to your computer and use it in GitHub Desktop.
Some fucked up php code
<?php
require_once('../csv_parser.php');
function do_post_request($url, $fields)
{
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
function csvArray($filename, $rowidx) {
// fucking php sucks shit ass
$csv = new csv_parser($filename);
$header;
$data = array();
foreach($csv->parse() as $idx=>$row) {
if($idx < $rowidx) continue;
if($idx == $rowidx) { // register the header
$header = $row;
} else { // register the data
$temp = array();
foreach($row as $key=>$val) {
$k = $header[$key];
$temp[$k] = $val;
}
array_push($data, $temp);
}
}
return $data;
}
$english = csvArray('english.csv', 6);
$french = csvArray('french.csv', 6);
$data = array();
foreach($english as $idx=>$englishRow) {
$frenchRow = $french[$idx];
$temp = array();
foreach($englishRow as $key=>$val) {
if(trim($val) != trim($frenchRow[$key])) { // need to turn it into a json
$oldval = trim($val);
$val = array();
$val['english'] = $oldval;
$val['french'] = trim($frenchRow[$key]);
}
$temp[$key] = $val;
}
array_push($data, $temp);
}
/// addd it!
$ontology_id = CO_324;
$id = 0;
// create root
$root = array();
$root['id'] = $ontology_id . ':' .str_pad($id, 7, '0', STR_PAD_LEFT);
$root['name'] = 'Sorghum';
$root['ontology_name'] = 'Sorghum';
$root['ontology_id'] = $ontology_id;
$id++;
do_post_request('http://localhost:8080/create-term', array(
'jsonTerm' => json_encode($root)
));
foreach($data as $term) {
unset($term['']);
foreach($term as $k=>$v) {
if(empty($v)) {
unset($term[$k]);
continue;
}
/*
if(!is_array($v)) {
$term[$k] = addcslashes($v);
} else {
foreach($v as $lang=>$transl) {
$v[$lang] = addcslashes($transl);
}
$term[$k] = $v;
}
*/
}
// create trait class
$class = array();
$class['id'] = $ontology_id . ':' . $term['Trait Class']['english'];
$class['name'] = $term['Trait Class'];
$class['ontology_name'] = 'Sorghum';
$class['ontology_id'] = $ontology_id;
$class['parent'] = $root['id'];
$result = do_post_request('http://localhost:8080/create-term', array(
'jsonTerm' => json_encode($class)
));
// calc id
$term['id'] = $ontology_id . ':' .str_pad($id, 7, '0', STR_PAD_LEFT);
$term['name'] = $term['Name of Trait'];
$term['ontology_name'] = 'Sorghum';
$term['ontology_id'] = $ontology_id;
$term['parent'] = $ontology_id . ':' . $term['Trait Class']['english'];
$id++;
$post_data = array(
'jsonTerm'=> json_encode($term)
);
//print_r($post_data);
$result = do_post_request('http://localhost:8080/create-term', $post_data);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment