potx_exportables.module in Potx exportables 7
This module will ease deployment for translated strings found in PO files generated by the POTx module.
File
potx_exportables.moduleView source
<?php
/**
* @file
* This module will ease deployment for translated
* strings found in PO files generated by the POTx module.
*/
/**
* Implements hook_menu().
*/
function potx_exportables_menu() {
$items['admin/config/regional/translate/update-from-code'] = array(
'title' => 'Update from code',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'_potx_exportables_import_form',
),
'access arguments' => array(
'translate interface',
),
'weight' => 250,
'type' => MENU_LOCAL_TASK,
'file' => 'potx_exportables.admin.inc',
);
return $items;
}
/**
* Implements hook_cron_queue_info().
*/
function potx_exportables_cron_queue_info() {
$queues['potx_exportables_tasks'] = array(
'worker callback' => '_potx_exportables_import_task',
'time' => 60,
'skip on cron' => TRUE,
);
return $queues;
}
/**
* This function is in charge of performing the import
* process.
*/
function _potx_exportables_import_task($file) {
_locale_import_po($file, $file->language, LOCALE_IMPORT_OVERWRITE, 'default');
}
/**
* The function that does all the magic. Finds new PO files, adds them to
* the queue if not yet processed and finally saves the registry in the table.
*/
function _potx_exportables_import_data() {
// Gather the list of modules that implement our hooks.
$module_info = array();
foreach (module_implements('potx_file_location_info') as $module) {
$info = module_invoke($module, 'potx_file_location_info');
$module_info += $info;
}
drupal_alter('potx_file_location_info', $module_info);
// Find the paths for all files and collect the IDs.
$regex = '/(.*?\\.)([a-z\\-]{2,11})\\.po$/';
$files_found = array();
foreach ($module_info as $module_key => $module_data) {
$path = $module_data['path'];
$found = file_scan_directory($path, $regex);
if ($found) {
foreach ($found as $file) {
preg_match($regex, $file->filename, $result);
if (isset($result[1])) {
$file->language = $result[2];
$files_found[$module_key . '__' . $result[0]] = $file;
}
}
}
}
if (!$files_found) {
_potx_exportables_display_message(t('No po files were found.'));
return;
}
// Find the files not in Database. (Those that have not been processed yet)
$in_files = array_keys($files_found);
$in_database = db_select('potx_exportables_log_entry', 'log')
->fields('log', array(
'name',
))
->condition('log.name', $in_files, 'IN')
->execute()
->fetchCol();
$not_in_database = array_diff($in_files, $in_database);
// Those that were found in database may still get a chance to be processed
// if we find they have changed since the last time they were processed.
if ($in_database) {
$file_hashes = db_select('potx_exportables_log_entry', 'log')
->fields('log', array(
'name',
'hash',
))
->condition('log.name', $in_database, 'IN')
->execute()
->fetchAllKeyed();
foreach ($file_hashes as $id => $old_hash) {
$data = file_get_contents($files_found[$id]->uri);
$sha1 = sha1($data);
if ($sha1 != $old_hash) {
$not_in_database[] = $id;
}
}
}
if (!$not_in_database) {
_potx_exportables_display_message(t('No pending po files were found.'));
return;
}
// Begin processing the new files and create the list of tasks.
$tasks_queue = DrupalQueue::get('potx_exportables_tasks');
foreach ($not_in_database as $id) {
$file = array();
$data = file_get_contents($files_found[$id]->uri);
if (!$data) {
continue;
}
$sha1 = sha1($data);
db_merge('potx_exportables_log_entry')
->key(array(
'name' => $id,
))
->fields(array(
'hash' => $sha1,
))
->execute();
$tasks_queue
->createItem($files_found[$id]);
}
// Kick off batch.
if ($tasks_queue
->numberOfItems()) {
$batch = array();
$queue_info = module_invoke('potx_exportables', 'cron_queue_info');
$queue_info = $queue_info['potx_exportables_tasks'];
$function = $queue_info['worker callback'];
while ($item = $tasks_queue
->claimItem()) {
$tasks_queue
->deleteItem($item);
if (!drupal_is_cli()) {
$batch['operations'][] = array(
$function,
array(
$item->data,
),
);
}
else {
$function($item->data);
}
}
if ($batch['operations']) {
batch_set($batch);
}
}
_potx_exportables_display_message(t('po files import has been successful.'));
}
/**
* Function uses drush_log or drupal_set_message to display a message
* when appropiate.
*/
function _potx_exportables_display_message($message, $type = 'success') {
if (!empty($message)) {
if (drupal_is_cli()) {
drush_log($message, $type);
}
else {
if ($type == 'success') {
$type = 'status';
}
drupal_set_message($message, $type);
}
}
}
Functions
Name![]() |
Description |
---|---|
potx_exportables_cron_queue_info | Implements hook_cron_queue_info(). |
potx_exportables_menu | Implements hook_menu(). |
_potx_exportables_display_message | Function uses drush_log or drupal_set_message to display a message when appropiate. |
_potx_exportables_import_data | The function that does all the magic. Finds new PO files, adds them to the queue if not yet processed and finally saves the registry in the table. |
_potx_exportables_import_task | This function is in charge of performing the import process. |