function smart_ip_extract_zip in Smart IP 7
Same name and namespace in other branches
- 6.2 includes/smart_ip.utility.inc \smart_ip_extract_zip()
- 6 includes/smart_ip.utility.inc \smart_ip_extract_zip()
- 7.2 includes/smart_ip.utility.inc \smart_ip_extract_zip()
1 string reference to 'smart_ip_extract_zip'
- smart_ip_update_db_batch in includes/
smart_ip.utility.inc - Prepare a batch definition
File
- includes/
smart_ip.utility.inc, line 90 - Utility routines to load the Smart IP database.
Code
function smart_ip_extract_zip(&$context) {
$path = file_stream_wrapper_uri_normalize('private://smart_ip');
if (variable_get('smart_ip_extract_zip_done', FALSE) || variable_get('smart_ip_store_location_csv_done', FALSE)) {
// We are in recovery mode. Using previous extracted csv files.
$context['finished'] = TRUE;
$context['message'] = t('Using previous extracted csv files (recovery mode)');
// Accumulate the previous extracted csv files.
$csv_files = file_scan_directory($path, '/\\.csv$/');
foreach ($csv_files as $csv_file) {
if (strpos($csv_file->filename, SMART_IP_LOCATION_CSV) !== FALSE) {
$context['results']['#location_csv_file'] = $csv_file->uri;
}
elseif (strpos($csv_file->filename, SMART_IP_BLOCKS_CSV) !== FALSE) {
$context['results']['#blocks_csv_file'] = $csv_file->uri;
}
}
return;
}
variable_set('smart_ip_extract_zip_done', FALSE);
// If this update was aborted in a previous step, or has a dependency that
// was aborted in a previous step, go no further.
if (isset($context['results']['#abort'])) {
return;
}
$zip_file = $context['results']['#zip_file'];
$zip = new ZipArchive();
$stat = $zip
->open($zip_file);
$context['finished'] = 50 / 100;
if ($stat === TRUE) {
// Delete existing csv files
$csv_files = file_scan_directory($path, '/\\.csv$/');
foreach ($csv_files as $csv_file) {
file_unmanaged_delete($csv_file->uri);
}
$zip
->extractTo($path);
$zip
->close();
file_unmanaged_delete($zip_file);
unset($context['results']['#zip_file']);
$csv_files = file_scan_directory($path, '/\\.csv$/');
foreach ($csv_files as $csv_file) {
file_unmanaged_move($csv_file->uri, $path . '/' . $csv_file->filename, FILE_EXISTS_REPLACE);
if (strpos($csv_file->filename, SMART_IP_LOCATION_CSV) !== FALSE) {
$context['results']['#location_csv_file'] = drupal_realpath($path . '/' . $csv_file->filename);
}
elseif (strpos($csv_file->filename, SMART_IP_BLOCKS_CSV) !== FALSE) {
$context['results']['#blocks_csv_file'] = drupal_realpath($path . '/' . $csv_file->filename);
}
}
file_unmanaged_delete_recursive(str_replace($csv_file->filename, '', $csv_file->uri));
// Update our progress information.
$context['finished'] = TRUE;
$context['message'] = t('geoip_db.zip extraction done. Starting the process of parsing extracted CSV files...');
// The succeeding process can now be interrupted
variable_set('smart_ip_db_update_busy', FALSE);
// An indicator that is to be used in recovery mode
variable_set('smart_ip_extract_zip_done', TRUE);
}
else {
// Error occured then stop the process
$message = t('Unzip failed (error code: %code).', array(
'%code' => $stat,
));
$context['results']['#abort'] = $message;
$context['message'] = $message;
// Delete the corrupted zip file
file_unmanaged_delete($zip_file);
unset($context['results']['#zip_file']);
// Set download process flag as undone to re-download the database from maxmind
variable_set('smart_ip_get_zip_done', FALSE);
}
}