Subversion Repositories computer_asset_manager_v1

Rev

Rev 81 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
80 rodolico 1
<?php
2
 
3
   /*
4
    * This script populates the device_attrib table in CAMP v1 from a tab delimited text file.
5
    * 
6
    * File should have a minimum of four columns and the first line should be headers as follows (case sensitive):
7
    * device    - should have case sensitive full name of something in the device table
8
    * attribute - should have case sensitive full name of something in the attrib table
9
    * value     - What should be placed in the value column of device_attrib
10
    * All other columns are ignored.
11
    *
12
    * If an attribute name is not found, it is added to the attrib table. If device is not found, no entry is made.
13
    *
14
    * Script designed to run from cli.
15
    * 1. Put full path/filename in the $filename variable below
16
    * 2. From cli, run 'php ./bulkLoadAttribs.php > out.sql
17
    * 3. (optional) open out.sql in text editor to review
18
    * 4. From cli, run 'mysql -u root -p camp < out.sql'
19
    *
20
    * If you're positive about the data you are importing, you can combine steps 2-4 as
21
    * php ./bulkLoadAttribs.php | mysql -u root -p camp
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
56
   $filename = '/home/rodolico/www/web/computer_asset_manager_v1/device_attributes.csv';
57
   // get data from import file and load it into $data. Read loadCSV
58
   $data = loadCSV( $filename );
59
 
60
   // we'll put our SQL into an array, then dump it.
61
   $sql = array();
62
   $attributes = array(); // uniquely store our attributes here
63
   for ( $i = 0; $i < count( $data ); $i++ ) {  // go through each line and grab fields we need
64
      $device = escapeforDB( $data[$i]['device'] );
65
      $attrib = escapeforDB( $data[$i]['attribute'] );
66
      $value = escapeforDB( $data[$i]['value'] );
67
 
68
      // track attributes we have used so we can make sure they're in there
69
      $attributes[$attrib] = true;
70
      // standard SQL that does an insert if the value doesn't already exist.
71
      $sql[] =
72
         "insert into device_attrib ( device_id, attrib_id,value, added_date )
73
            select device_id,attrib_id, $value, now()
74
            from device join attrib
75
            where
76
               device.name = $device
77
               and attrib.name = $attrib
78
               and not exists (
79
                  select * from device_attrib join device using (device_id) join attrib using (attrib_id) where device.name = $device and attrib.name = $attrib
80
                  );";
81
   }
82
 
83
   // we have our distinct attributes, so let's insert anything which does not already exist
84
   // we'll just print them to STDOUT (first, so $sql can use any new values)
85
   foreach ( $attributes as $key => $value ) {
86
      print "insert into attrib( name,added_date ) select $key, now() from dual where not exists (select * from attrib where name = $key);\n";
87
   }
88
   // our actual load
89
   print implode( "\n", $sql ) . "\n";
90
 
91
 
92
?>