I have a custom module to import nodes from a .txt file. (Drupal 7). My problem is I have a lot of nodes to import and this function takes so much time.
$operations = array();
$items = array();
$limit = 100;
$i = 0;
$regs = db_query("SELECT * FROM {table_import_apunte} WHERE field_processed = :processed", array(':processed' => 0));
foreach ($regs as $row) {
$row = (array) $row;
if ($i < $limit) {
$items[] = $row;
$i += 1;
} else {
$operations[] = array('csvImporter_create_nodes', array($items, 'apunte'));
$items = array();
$items[] = $row;
$i = 1;
}
}
if (!empty($items)) {
$operations[] = array('csvImporter_create_nodes', array($items, 'apunte'));
$batch = array(
'title' => t('Importing %name table', array('%name' => drupal_strtoupper('apunte'))),
'init_message' => t('Starting Import...'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('There was a problem importing the file.'),
'operations' => $operations,
'finished' => 'csvImporter_batch_finished',
);
batch_set($batch);
}
csvImporter_create_nodes
use save_node to create nodes. In this foreach
, I divide $regs
(which contain my data to import) in package of 100 elements and call the csvImporter_create_nodes
function. I need to make this foreach
faster and more efficient.
I have a version in Drupal 6 which is faster. This is the extract code:
while (($row = db_fetch_array($regs)) !== FALSE) {
if ($i < $limit) {
$items[] = $row;
$i += 1;
} else {
$operations[] = array('_importer_batch_create_nodes', array($items, $table));
$items = array();
$items[] = $row;
$i = 1;
}
}
but it uses db_fetch_array
and I can't use it in Drupal 7.