78 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
function tabDelimToArray($contents, $delimiter = "\t" ) {
|
|
|
4 |
$rows = array();
|
|
|
5 |
$headers = explode( $delimiter, array_shift( $contents ) );
|
|
|
6 |
for ( $line = 0; $line < count( $contents ); $line++ ) {
|
|
|
7 |
if ( $contents[$line] ) {
|
|
|
8 |
$thisLine = explode( $delimiter, $contents[$line] );
|
|
|
9 |
$thisLine = preg_replace( '/^\s+/', '', $thisLine );
|
|
|
10 |
$thisLine = preg_replace( '/\s+$/', '', $thisLine );
|
|
|
11 |
$columns = array();
|
|
|
12 |
for ( $i = 0; $i < count( $headers ); $i++ ) {
|
|
|
13 |
$columns[$headers[$i]] = $thisLine[$i];
|
|
|
14 |
} // for
|
|
|
15 |
$rows[] = $columns;
|
|
|
16 |
} // if
|
|
|
17 |
} // while
|
|
|
18 |
return $rows;
|
|
|
19 |
} // function
|
|
|
20 |
|
|
|
21 |
function quoteString ( $str ) { // don't use this for production
|
|
|
22 |
$str = str_replace( "'", "''", $str );
|
|
|
23 |
return "'$str'";
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
function nullOrEmpty( $str ) {
|
|
|
27 |
return ( !isset( $str) || trim($str) === '');
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
if ( isset( $argv[1] ) ) {
|
|
|
31 |
$filename = $argv[1];
|
|
|
32 |
} else {
|
|
|
33 |
die( "You must tell me what file to process\n" ) ;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
print "$filename\n";
|
|
|
37 |
$data = tabDelimToArray( explode( "\n", file_get_contents( $filename ) ) );
|
|
|
38 |
// get the names of all the units listed
|
|
|
39 |
$keys = array_diff( array_keys( $data[0] ), array( 'device') );
|
|
|
40 |
//print_r( $keys ); die;
|
|
|
41 |
$output = array();
|
|
|
42 |
for ( $i = 0; $i < count($data); $i++ ) {
|
|
|
43 |
$device = quoteString( $data[$i]['device'] );
|
|
|
44 |
foreach ( $keys as $thisKey ) {
|
|
|
45 |
/*
|
|
|
46 |
print "$thisKey\n";
|
|
|
47 |
print_r( $data[$i][$thisKey] ); die;
|
|
|
48 |
continue;
|
|
|
49 |
*/
|
|
|
50 |
if ( ! nullOrEmpty( $data[$i][$thisKey] ) ) {
|
|
|
51 |
$value = quoteString( $data[$i][$thisKey] );
|
|
|
52 |
$keyName = quoteString( $thisKey );
|
|
|
53 |
$output[] = "insert into device_attrib select null,device_id,attrib_id,$value,now(),null from attrib, device where attrib.name = $keyName and device.name = $device";
|
|
|
54 |
} // if
|
|
|
55 |
} // foreach
|
|
|
56 |
} // for
|
|
|
57 |
print implode( ";\n", $output ) . ";\n";
|
|
|
58 |
?>
|