array( 'title' => 'Millennium file import', 'page callback' => 'millennium_fileimporter_import', 'type' => MENU_CALLBACK, 'access callback' => TRUE )); } function millennium_fileimporter_import() { $batch = array( 'title' => t('Loading records from mark list.'), 'operations' => array( array('millennium_fileimporter_batch_import', array())), 'finished' => 'millennium_batch_finished'); batch_set($batch); batch_process(''); } function millennium_fileimporter_batch_import(&$context) { if (empty($context['sandbox'])) { $context['sandbox']['progress'] = 0; $context['sandbox']['errors'] = array(); $context['sandbox']['seekpointer'] = 0; } $handle = @fopen("/var/www/musa/sites/default/files/dump.txt", "r"); if (!$handle) { $context['message'] = t('could not open dump file'); $context['finished'] = 1; return; } fseek($handle, $context['sandbox']['seekpointer']); $iteration = 0; while ($iteration < 100 && !feof($handle)) { $buffer = fgets($handle, 4096); // todo: some lines might be longer $bnum = $buffer; $inums = array(); $buffer = fgets($handle, 4096); while (mb_substr($buffer, 0, 6) != "LEADER") { $inum = $buffer; $inums[] = $inum; $buffer = fgets($handle, 4096); } $marc_text = $buffer; while (!feof($handle) && mb_strlen($buffer) > 1) { $buffer = fgets($handle, 4096); $marc_text .= $buffer; } if (mb_strlen($buffer) <= 1) { // currently only the first inum gets saved millennium_update_bibrecord($bnum, $inum[0], true, true, $marc_text); $context['message'] = $context['sandbox']['progress'] . ": " . $bnum; $context['results'][] = $bnum; } $context['sandbox']['seekpointer'] = ftell($handle); $iteration++; $context['sandbox']['progress']++; } if (feof($handle)) { fclose($handle); fclose($outhandle); $context['finished'] = 1; } else { $context['finished'] = 0.5; } } function millennium_batch_finished($success, $results, $operations) { if ($success) { $message = format_plural(count($results), 'One record processed.', '@count records processed'); } else { // An error occurred. // $operations contains the operations that remained unprocessed. $error_operation = reset($operations); $message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE))); } drupal_set_message($message); }