80 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
81 |
rodolico |
4 |
* This script populates the device_attrib table in CAMP v1 from a tab delimited text file. Filename may be passed as the
|
|
|
5 |
* only parameter on the cli, or the system can be used as a filter, ie read from STDIN
|
80 |
rodolico |
6 |
*
|
|
|
7 |
* File should have a minimum of four columns and the first line should be headers as follows (case sensitive):
|
|
|
8 |
* device - should have case sensitive full name of something in the device table
|
|
|
9 |
* attribute - should have case sensitive full name of something in the attrib table
|
|
|
10 |
* value - What should be placed in the value column of device_attrib
|
|
|
11 |
* All other columns are ignored.
|
|
|
12 |
*
|
|
|
13 |
* If an attribute name is not found, it is added to the attrib table. If device is not found, no entry is made.
|
|
|
14 |
*
|
|
|
15 |
* Script designed to run from cli.
|
81 |
rodolico |
16 |
* 1. From cli, run 'php ./bulkLoadAttribs.php tabdelimfile.csv > out.sql
|
|
|
17 |
* 2. (optional) open out.sql in text editor to review
|
|
|
18 |
* 3. From cli, run 'mysql -u root -p camp < out.sql'
|
80 |
rodolico |
19 |
*
|
|
|
20 |
* If you're positive about the data you are importing, you can combine steps 2-4 as
|
81 |
rodolico |
21 |
* php ./bulkLoadAttribs.php tabdelimfile.csv | mysql -u root -p camp
|
80 |
rodolico |
22 |
*
|
|
|
23 |
* Information should be loaded in database.
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
function trimArray( $array ) {
|
|
|
28 |
for( $i = 0; $i < count( $array ); $i++ ) {
|
|
|
29 |
$array[$i] = trim( $array[$i] );
|
|
|
30 |
}
|
|
|
31 |
return $array;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
/*
|
|
|
35 |
* given a csv file with the first line as headers, creates an array of hash where each hash entry has the header
|
|
|
36 |
* as the key and the value as the value
|
|
|
37 |
*/
|
|
|
38 |
function loadCSV( $filename, $delimiter = "\t", $maxLineLength = 1024 ) {
|
|
|
39 |
$allRows = array();
|
|
|
40 |
if ( ( $fh = fopen( $filename, 'r' ) ) !== false ) {
|
|
|
41 |
$header = trimArray( fgetcsv( $fh, $maxLineLength, $delimiter ) );
|
|
|
42 |
while ( $row = fgetcsv( $fh, $maxLineLength, $delimiter ) ) {
|
|
|
43 |
//print "Untrimmed\n" . print_r( $row, true ) . "\nTrimmed\n" . print_r( trimArray( $row ), true ) . "\n" ; die;
|
|
|
44 |
$allRows[] = array_combine( $header, trimArray( $row ) );
|
|
|
45 |
} // while
|
|
|
46 |
}
|
|
|
47 |
return $allRows;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
// don't trust this function for any data not well controlled
|
|
|
51 |
function escapeforDB( $value ) {
|
|
|
52 |
return "'" . mysql_escape_string( $value ) . "'";
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// Name of file to import
|
81 |
rodolico |
56 |
$filename = 'php://stdin';
|
|
|
57 |
if ( isset( $argv[1] ) ) {
|
|
|
58 |
$filename = $argv[1];
|
|
|
59 |
}
|
|
|
60 |
|
80 |
rodolico |
61 |
// get data from import file and load it into $data. Read loadCSV
|
|
|
62 |
$data = loadCSV( $filename );
|
|
|
63 |
|
|
|
64 |
// we'll put our SQL into an array, then dump it.
|
|
|
65 |
$sql = array();
|
|
|
66 |
$attributes = array(); // uniquely store our attributes here
|
|
|
67 |
for ( $i = 0; $i < count( $data ); $i++ ) { // go through each line and grab fields we need
|
|
|
68 |
$device = escapeforDB( $data[$i]['device'] );
|
|
|
69 |
$attrib = escapeforDB( $data[$i]['attribute'] );
|
|
|
70 |
$value = escapeforDB( $data[$i]['value'] );
|
|
|
71 |
|
|
|
72 |
// track attributes we have used so we can make sure they're in there
|
|
|
73 |
$attributes[$attrib] = true;
|
|
|
74 |
// standard SQL that does an insert if the value doesn't already exist.
|
|
|
75 |
$sql[] =
|
|
|
76 |
"insert into device_attrib ( device_id, attrib_id,value, added_date )
|
|
|
77 |
select device_id,attrib_id, $value, now()
|
|
|
78 |
from device join attrib
|
|
|
79 |
where
|
|
|
80 |
device.name = $device
|
|
|
81 |
and attrib.name = $attrib
|
|
|
82 |
and not exists (
|
|
|
83 |
select * from device_attrib join device using (device_id) join attrib using (attrib_id) where device.name = $device and attrib.name = $attrib
|
|
|
84 |
);";
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// we have our distinct attributes, so let's insert anything which does not already exist
|
|
|
88 |
// we'll just print them to STDOUT (first, so $sql can use any new values)
|
|
|
89 |
foreach ( $attributes as $key => $value ) {
|
|
|
90 |
print "insert into attrib( name,added_date ) select $key, now() from dual where not exists (select * from attrib where name = $key);\n";
|
|
|
91 |
}
|
|
|
92 |
// our actual load
|
|
|
93 |
print implode( "\n", $sql ) . "\n";
|
90 |
rodolico |
94 |
print "select
|
|
|
95 |
attrib.name attrib_name,
|
|
|
96 |
device_attrib.value value,
|
|
|
97 |
device_attrib.added_date,
|
|
|
98 |
device.device_id,
|
|
|
99 |
attrib.attrib_id,
|
|
|
100 |
device_attrib_id
|
|
|
101 |
from
|
|
|
102 |
device_attrib
|
|
|
103 |
join attrib using (attrib_id)
|
|
|
104 |
join device using (device_id)
|
|
|
105 |
where
|
|
|
106 |
device.name = $device
|
|
|
107 |
order by
|
|
|
108 |
attrib.name;
|
|
|
109 |
"
|
80 |
rodolico |
110 |
|
|
|
111 |
|
|
|
112 |
?>
|