function node_import_autodetect in Node import 6
Return an autodetected file format and file options for given file.
This checks which file format options cause the file to contain the most fields. The file options tested are the ones used by the user (last 5) and the builtin ones.
Parameters
$filepath: String. Path to file.
Return value
Array of file options.
Related topics
1 call to node_import_autodetect()
File
- ./
node_import.inc, line 1723 - Public API of the Node import module.
Code
function node_import_autodetect($filepath) {
global $user;
if (user_access('administer imports')) {
$result = db_query("SELECT DISTINCT file_options FROM {node_import_tasks} ORDER BY created DESC LIMIT 5");
}
else {
$result = db_query("SELECT DISTINCT file_options FROM {node_import_tasks} WHERE uid = %d ORDER BY created DESC LIMIT 5");
}
$file_formats = node_import_format_options('file formats');
while ($used_format = db_fetch_array($result)) {
$file_formats[] = unserialize($used_format['file_options']);
}
$format = 'csv';
$max_cols = 0;
foreach ($file_formats as $file_format => $file_options) {
$file_offset = 0;
$num_rows = variable_get('node_import:detect:row_count', 5);
$num_cols = 0;
$fields = array();
while ($num_rows > 0 && is_array($fields)) {
list($new_offset, $fields) = node_import_read_from_file($filepath, $file_offset, $file_options);
$num_rows--;
if (is_array($fields) && count($fields) > $num_cols) {
$num_cols = count($fields);
}
}
if ($num_cols > $max_cols) {
$format = $file_format;
$max_cols = $num_cols;
}
}
$file_options = $file_formats[$format];
$file_options['file_format'] = is_int($format) ? '' : $format;
return $file_options;
}