<?php
function locale_translation_batch_status_build($sources) {
$operations = array();
foreach ($sources as $source) {
$operations[] = array(
'locale_translation_batch_status_fetch_remote',
array(
$source,
),
);
}
$operations[] = array(
'locale_translation_batch_status_fetch_local',
array(
$sources,
),
);
$operations[] = array(
'locale_translation_batch_status_compare',
array(),
);
$batch = array(
'operations' => $operations,
'title' => t('Checking available translations'),
'finished' => 'locale_translation_batch_status_finished',
'error_message' => t('Error checking available interface translation updates.'),
'file' => drupal_get_path('module', 'locale') . '/locale.batch.inc',
);
return $batch;
}
function locale_translation_batch_status_fetch_remote($source, &$context) {
if (isset($source->files['remote'])) {
$remote_file = $source->files['remote'];
$result = locale_translation_http_check($remote_file->url);
if ($result && !empty($result->updated)) {
$remote_file->url = isset($result->redirect_url) ? $result->redirect_url : $remote_file->url;
$remote_file->timestamp = $result->updated;
$source->files['remote'] = $remote_file;
}
$context['results'][$source->name][$source->language] = $source;
}
}
function locale_translation_batch_status_fetch_local($sources, &$context) {
module_load_include('compare.inc', 'locale');
foreach ($sources as $source) {
if (isset($source->files['local'])) {
locale_translation_source_check_file($source);
if (isset($context['results'][$source->name][$source->language])) {
$source->files['remote'] = $context['results'][$source->name][$source->language]->files['remote'];
}
$context['results'][$source->name][$source->language] = $source;
}
}
}
function locale_translation_batch_status_compare(&$context) {
module_load_include('compare.inc', 'locale');
$results = array();
foreach ($context['results'] as $project => $langcodes) {
foreach ($langcodes as $langcode => $source) {
$local = isset($source->files['local']) ? $source->files['local'] : NULL;
$remote = isset($source->files['remote']) ? $source->files['remote'] : NULL;
$file = _locale_translation_source_compare($local, $remote) == LOCALE_TRANSLATION_SOURCE_COMPARE_LT ? $remote : $local;
if (isset($file->timestamp)) {
$source->type = $file->type;
$source->timestamp = $file->timestamp;
$results[$project][$langcode] = $source;
}
}
}
state()
->set('locale_translation_status', $results);
state()
->set('locale_translation_status_last_update', REQUEST_TIME);
}
function locale_translation_batch_status_finished($success, $results) {
$t = get_t();
if ($success) {
if ($results) {
drupal_set_message(format_plural(count($results), 'Checked available interface translation updates for one project.', 'Checked available interface translation updates for @count projects.'));
}
}
else {
drupal_set_message($t('An error occurred trying to check available interface translation updates.'), 'error');
}
}
function locale_translation_http_check($url, $headers = array()) {
$result = drupal_http_request($url, array(
'headers' => $headers,
'method' => 'HEAD',
));
if ($result && $result->code == '200') {
$result->updated = isset($result->headers['last-modified']) ? strtotime($result->headers['last-modified']) : 0;
}
return $result;
}