| 1 |
<?php |
<?php |
| 2 |
// $Id: tw_import_delimited.module,v 1.1.2.9 2009/04/17 19:31:52 mikeryan Exp $ |
// $Id: tw_import_delimited.module,v 1.1.2.10 2009/04/30 21:20:22 mikeryan Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 8 |
|
|
| 9 |
/** |
/** |
| 10 |
* Implementation of hook_tw_form(). |
* Implementation of hook_tw_form(). |
|
* |
|
|
* @return unknown |
|
| 11 |
*/ |
*/ |
| 12 |
function tw_import_delimited_tw_form() { |
function tw_import_delimited_tw_form() { |
| 13 |
//////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////// |
| 46 |
'#value' => t('Import new file'), |
'#value' => t('Import new file'), |
| 47 |
), |
), |
| 48 |
); |
); |
| 49 |
|
|
| 50 |
return $fieldsets; |
return $fieldsets; |
| 51 |
} |
} |
| 52 |
|
|
| 53 |
/** |
/** |
| 54 |
* Implementation of hook_tw_form_submit |
* Implementation of hook_tw_form_submit(). |
|
* |
|
|
* @param unknown_type $values |
|
|
* @return unknown |
|
| 55 |
*/ |
*/ |
| 56 |
function tw_import_delimited_tw_form_submit_delimited($values) { |
function tw_import_delimited_tw_form_submit_delimited($values) { |
| 57 |
$file = file_save_upload('delimited'); |
$file = file_save_upload('delimited'); |
| 60 |
$dest = _tw_delimited_file_name($file->filename); |
$dest = _tw_delimited_file_name($file->filename); |
| 61 |
if ($dest) { |
if ($dest) { |
| 62 |
file_move($file, $dest, FILE_EXISTS_REPLACE); |
file_move($file, $dest, FILE_EXISTS_REPLACE); |
| 63 |
} |
} |
| 64 |
else { |
else { |
| 65 |
drupal_set_message(t('Could not upload !filename', |
drupal_set_message(t('Could not upload !filename', |
| 66 |
array('!filename' => $file->filename))); |
array('!filename' => $file->filename))); |
| 67 |
return NULL; |
return NULL; |
| 68 |
} |
} |
| 73 |
if ($headers === FALSE) { |
if ($headers === FALSE) { |
| 74 |
return; |
return; |
| 75 |
} |
} |
| 76 |
|
|
| 77 |
// If an explicit table name was not provided... |
// If an explicit table name was not provided... |
| 78 |
if ($values['tablename']) { |
if ($values['tablename']) { |
| 79 |
$tablename = trim($values['tablename']); |
$tablename = trim($values['tablename']); |
| 80 |
} else { |
} |
| 81 |
|
else { |
| 82 |
// ... Derive one from the filename (minus extension) |
// ... Derive one from the filename (minus extension) |
| 83 |
$pieces = explode('.', $file->filename); |
$pieces = explode('.', $file->filename); |
| 84 |
$tablename = trim($pieces[0]); |
$tablename = trim($pieces[0]); |
| 85 |
} |
} |
| 86 |
|
|
| 87 |
if (!$tablename) { |
if (!$tablename) { |
| 88 |
drupal_set_message(t('Could not derive a table name for !filename', |
drupal_set_message(t('Could not derive a table name for !filename', |
| 89 |
array('!filename' => $file->filename))); |
array('!filename' => $file->filename))); |
| 90 |
} |
} |
| 91 |
|
|
| 92 |
// Lowercase table names make views integration much easier |
// Lowercase table names make views integration much easier |
| 93 |
$tablename = strtolower(preg_replace('/[^a-z0-9]/i', '_', $tablename)); |
$tablename = drupal_strtolower(preg_replace('/[^a-z0-9]/i', '_', $tablename)); |
| 94 |
// Truncate to 63 characters (valid for either MySQL or Postgres) |
// Truncate to 63 characters (valid for either MySQL or Postgres) |
| 95 |
$tablename = substr($tablename, 0, 63); |
$tablename = drupal_substr($tablename, 0, 63); |
| 96 |
|
|
| 97 |
// Build a schema definition for the table |
// Build a schema definition for the table |
| 98 |
$schema = array( |
$schema = array( |
| 99 |
'description' => t('Uploaded data file'), |
'description' => t('Uploaded data file'), |
| 106 |
// Field names must be unique, generate variations if necessary |
// Field names must be unique, generate variations if necessary |
| 107 |
if (!isset($seen[$coltitle])) { |
if (!isset($seen[$coltitle])) { |
| 108 |
$seen[$coltitle] = 1; |
$seen[$coltitle] = 1; |
| 109 |
} |
} |
| 110 |
else { |
else { |
| 111 |
$seen[$coltitle]++; |
$seen[$coltitle]++; |
| 112 |
} |
} |
| 126 |
if (db_table_exists($tablename)) { |
if (db_table_exists($tablename)) { |
| 127 |
db_query('TRUNCATE TABLE {' . $tablename . '}'); |
db_query('TRUNCATE TABLE {' . $tablename . '}'); |
| 128 |
$prevexists = TRUE; |
$prevexists = TRUE; |
| 129 |
} |
} |
| 130 |
else { |
else { |
| 131 |
// Note that when using table prefixes, a prefix is prepended to the table name |
// Note that when using table prefixes, a prefix is prepended to the table name |
| 132 |
db_create_table($ret, $tablename, $schema); |
db_create_table($ret, $tablename, $schema); |
| 145 |
$srcinfo->success_msg = t('%filename has been imported into table %tablename, replacing |
$srcinfo->success_msg = t('%filename has been imported into table %tablename, replacing |
| 146 |
the original data in that table', |
the original data in that table', |
| 147 |
array('%filename' => $file->filename, '%tablename' => $tablename)); |
array('%filename' => $file->filename, '%tablename' => $tablename)); |
| 148 |
} else { |
} |
| 149 |
|
else { |
| 150 |
$srcinfo->success_msg = t('%filename has been imported into new table %tablename. All fields |
$srcinfo->success_msg = t('%filename has been imported into new table %tablename. All fields |
| 151 |
have initially been defined as text, with no indexing - it is important to set appropriate |
have initially been defined as text, with no indexing - it is important to set appropriate |
| 152 |
data types for each column, and to define a single (unique, non-null) column as the |
data types for each column, and to define a single (unique, non-null) column as the |
| 168 |
|
|
| 169 |
// Pass the table name back to Table Wizard |
// Pass the table name back to Table Wizard |
| 170 |
return array($tablename); |
return array($tablename); |
| 171 |
} |
} |
| 172 |
else { |
else { |
| 173 |
drupal_set_message(t('Failed to upload delimited file')); |
drupal_set_message(t('Failed to upload delimited file')); |
| 174 |
} |
} |
| 177 |
/** |
/** |
| 178 |
* Generate the Drupal-relative path for storing imported files (creating the tw_delimited |
* Generate the Drupal-relative path for storing imported files (creating the tw_delimited |
| 179 |
* directory if necessary). |
* directory if necessary). |
|
* |
|
|
* @param unknown_type $file |
|
|
* @return unknown |
|
| 180 |
*/ |
*/ |
| 181 |
function _tw_delimited_file_name($file) { |
function _tw_delimited_file_name($file) { |
| 182 |
$dir = str_replace('\\', '/', getcwd()).'/'.file_directory_path() . '/tw_delimited'; |
$dir = str_replace('\\', '/', getcwd()) . '/' . file_directory_path() . '/tw_delimited'; |
| 183 |
if (file_check_directory($dir, TRUE)) { |
if (file_check_directory($dir, TRUE)) { |
| 184 |
return $dir . '/' . $file; |
return $dir . '/' . $file; |
| 185 |
} |
} |
| 186 |
else { |
else { |
| 187 |
return FALSE; |
return FALSE; |
| 188 |
} |
} |
| 207 |
$sample = fgets($handle); |
$sample = fgets($handle); |
| 208 |
if (strpos($sample, "\t") === FALSE) { |
if (strpos($sample, "\t") === FALSE) { |
| 209 |
$separator = ','; |
$separator = ','; |
| 210 |
} else { |
} |
| 211 |
|
else { |
| 212 |
$separator = "\t"; |
$separator = "\t"; |
| 213 |
} |
} |
| 214 |
fseek($handle, $offset); |
fseek($handle, $offset); |
| 220 |
$handle = NULL; |
$handle = NULL; |
| 221 |
$currpath = NULL; |
$currpath = NULL; |
| 222 |
$separator = NULL; |
$separator = NULL; |
| 223 |
} else { |
} |
| 224 |
|
else { |
| 225 |
$offset = ftell($handle); |
$offset = ftell($handle); |
| 226 |
} |
} |
| 227 |
|
|
| 228 |
return $result; |
return $result; |
| 229 |
} |
} |
| 230 |
|
|
| 248 |
|
|
| 249 |
if ($context['sandbox']['total'] > 0) { |
if ($context['sandbox']['total'] > 0) { |
| 250 |
$context['finished'] = $context['sandbox']['done'] / $context['sandbox']['total']; |
$context['finished'] = $context['sandbox']['done'] / $context['sandbox']['total']; |
| 251 |
} else { |
} |
| 252 |
|
else { |
| 253 |
$context['finished'] = 1; |
$context['finished'] = 1; |
| 254 |
} |
} |
| 255 |
$context['message'] = t('%done of %total bytes imported<br />%rows rows inserted into %tablename', |
$context['message'] = t('%done of %total bytes imported<br />%rows rows inserted into %tablename', |
| 256 |
array('%done' => format_size($context['sandbox']['done']), |
array('%done' => format_size($context['sandbox']['done']), |
| 257 |
'%total' => format_size($context['sandbox']['total']), |
'%total' => format_size($context['sandbox']['total']), |
| 258 |
'%rows' => $context['sandbox']['rows'], |
'%rows' => $context['sandbox']['rows'], |