Subversion Repositories computer_asset_manager_v1

Rev

Rev 78 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
78 rodolico 1
<?php
2
 
101 rodolico 3
   /*
4
    * Kludge script that reads a tab delimited text file and generates the SQL that will be used to insert the values
5
    * into CAMP attributes table.
6
    *
7
    * It has a lot of shortcuts, such as automatically creating new attribute keys if they don't exist and assuming
8
    * the device already does exist and is unique.
9
    */
10
 
78 rodolico 11
   function tabDelimToArray($contents, $delimiter = "\t" ) {
12
      $rows = array();
13
      $headers = explode( $delimiter, array_shift( $contents ) );
14
      for  ( $line = 0; $line < count( $contents ); $line++ ) {
15
         if ( $contents[$line] ) {
16
            $thisLine = explode( $delimiter, $contents[$line] );
17
            $thisLine = preg_replace( '/^\s+/', '', $thisLine );
18
            $thisLine = preg_replace( '/\s+$/', '', $thisLine );
19
            $columns = array();
20
            for ( $i = 0; $i < count( $headers ); $i++ ) {
21
               $columns[$headers[$i]] = $thisLine[$i];
22
            } // for
23
            $rows[] = $columns;
24
         } // if
25
      } // while
26
      return $rows;
27
   } // function
28
 
29
   function quoteString ( $str ) { // don't use this for production
30
      $str = str_replace( "'", "''", $str );
31
      return "'$str'";
32
   }
33
 
34
   function nullOrEmpty( $str ) {
35
      return ( !isset( $str) || trim($str) === '');
36
   }
37
 
38
   if ( isset( $argv[1] ) ) {
39
      $filename = $argv[1];
40
   } else {
41
      die( "You must tell me what file to process\n" ) ;
42
   }
43
 
44
   print "$filename\n";
45
   $data = tabDelimToArray( explode( "\n", file_get_contents( $filename ) ) );
46
   // get the names of all the units listed
47
   $keys = array_diff( array_keys( $data[0] ), array( 'device')  );
48
   //print_r( $keys ); die;
49
   $output = array();
50
   for ( $i = 0; $i < count($data); $i++ ) {
51
      $device = quoteString( $data[$i]['device'] );
52
      foreach ( $keys as $thisKey ) {
53
         /*
54
         print "$thisKey\n";
55
         print_r( $data[$i][$thisKey] ); die;
56
         continue;
57
         */
58
         if ( ! nullOrEmpty( $data[$i][$thisKey] ) ) {
59
            $value = quoteString( $data[$i][$thisKey] );
60
            $keyName = quoteString( $thisKey );
101 rodolico 61
            $output[] = "insert into attrib_device select null,device_id,attrib_id,$value,now(),null from attrib, device where attrib.name = $keyName and device.name = $device";
78 rodolico 62
         } // if
63
      } // foreach
64
   } // for
65
   print implode( ";\n", $output ) . ";\n";
66
?>