2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
15 var $quote_search = array('""', '\\\n', '\\\r');
16 var $quote_replace = array('"', '\n', '\r');
19 function CSVImport() { }
22 // returns the primary_key, or false if there is none, or null if more than 1
23 function getPrimaryFieldName($table_name) {
28 $sql = "SELECT * FROM ".TABLE_PREFIX.$table_name .' WHERE 0';
29 $result = mysql_query($sql, $db);
30 $num_fields = mysql_num_fields($result);
31 for ($i= 0; $i<$num_fields; $i++) {
32 $flags = explode(' ', mysql_field_flags($result, $i));
33 if (in_array('primary_key', $flags)) {
34 if ($field == false) {
35 $field = mysql_field_name($result, $i);
37 // there is more than one primary_key
47 // given a query result returns an array of field types.
48 // possible field types are int, string, datetime, or blob...
49 function detectFieldTypes($table_name) {
52 $field_types = array();
54 $sql = "SELECT * FROM ".TABLE_PREFIX.$table_name .' WHERE 0';
55 $result = @mysql_query($sql, $db);
59 $num_fields = mysql_num_fields($result);
61 for ($i=0; $i< $num_fields; $i++) {
62 $field_types[] = mysql_field_type($result, $i);
68 function translateWhitespace($input) {
69 $input = addslashes($input);
70 $input = str_replace($this->quote_search, $this->quote_replace, $input);
76 function import($tableName, $path, $course_id, $version) {
80 $fn_name = $tableName.'_convert';
83 $lock_sql = 'LOCK TABLES ' . TABLE_PREFIX . $tableName. ', ' . TABLE_PREFIX . 'courses WRITE';
84 $result = mysql_query($lock_sql, $db);
86 // get the field types
87 $field_types = $this->detectFieldTypes($tableName);
92 // get the name of the primary field
93 $primary_key_field_name = $this->getPrimaryFieldName($tableName);
94 // read the rows into an array
95 $fp = @fopen($path . $tableName . '.csv', 'rb');
98 // get the name of the primary ID field and the next index
100 if ($primary_key_field_name) {
101 // get the next primary ID
102 $sql = 'SELECT MAX(' . $primary_key_field_name . ') AS next_id FROM ' . TABLE_PREFIX . $tableName;
103 $result = mysql_query($sql, $db);
104 $next_id = mysql_fetch_assoc($result);
105 $next_id = $next_id['next_id']+1;
109 while ($row = @fgetcsv($fp, 70000)) {
110 if (count($row) && (trim($row[0]) == '')) {
114 if (function_exists($fn_name)) {
115 $row = $fn_name($row, $course_id, $table_id_map, $version);
124 $table_id_map[$tableName][$row[0]] = $next_id;
125 if ($primary_key_field_name != NULL) {
129 $sql = 'REPLACE INTO '.TABLE_PREFIX.$tableName.' VALUES (';
131 foreach($row as $id => $field) {
132 if (($field_types[$id] != 'int') && ($field_types[$id] != 'real')) {
133 $field = $this->translateWhitespace($field);
134 } else if ($field_types[$id] == 'int') {
135 $field = intval($field);
137 $sql .= "'" . $field."',";
139 $sql = substr($sql, 0, -1);
142 $result = mysql_query($sql, $db);
151 $lock_sql = 'UNLOCK TABLES';
152 $result = mysql_query($lock_sql, $db);