| 1 |
<?php
|
| 2 |
// $Id: active_translation.batch.inc,v 1.7 2009/07/12 15:47:48 drewish Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Build a batch to rebuild all the translation information.
|
| 6 |
*/
|
| 7 |
function active_translation_build_batch_all() {
|
| 8 |
$batch = array(
|
| 9 |
'title' => t('Rebuilding the active translation table'),
|
| 10 |
'operations' => array(
|
| 11 |
array('_active_translation_batch_operation', array()),
|
| 12 |
),
|
| 13 |
'finished' => '_active_translation_batch_finished',
|
| 14 |
'file' => drupal_get_path('module', 'active_translation') .'/active_translation.batch.inc',
|
| 15 |
);
|
| 16 |
|
| 17 |
return $batch;
|
| 18 |
}
|
| 19 |
|
| 20 |
function _active_translation_batch_operation(&$context) {
|
| 21 |
if (empty($context['sandbox'])) {
|
| 22 |
watchdog('active_translati', 'Starting to rebuild the active translation table.');
|
| 23 |
|
| 24 |
// Initiate multistep processing. Call language_list() and reset its cache.
|
| 25 |
$context['sandbox']['languages'] = array_keys(language_list('language', TRUE));
|
| 26 |
$context['sandbox']['progress'] = 0;
|
| 27 |
$context['sandbox']['current_translation'] = 0;
|
| 28 |
$context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT tnid) FROM {node} WHERE tnid IS NOT NULL AND tnid > 0'));
|
| 29 |
|
| 30 |
// Drop and recreate the database table to ensure the columns match the
|
| 31 |
// current list of languages. Clear the cache to get rid of any cached
|
| 32 |
// schema info.
|
| 33 |
drupal_uninstall_schema('active_translation');
|
| 34 |
drupal_get_schema('active_translation', TRUE);
|
| 35 |
drupal_install_schema('active_translation');
|
| 36 |
|
| 37 |
// Insert records for language neutral and untranslated nodes.
|
| 38 |
$fields = $values = array();
|
| 39 |
// We need to put the node id into the column for each language.
|
| 40 |
foreach ($context['sandbox']['languages'] as $lang_code) {
|
| 41 |
$fields[] = db_escape_table($lang_code);
|
| 42 |
$values[] = 'nid';
|
| 43 |
}
|
| 44 |
// We can't use db_placeholders() because we're using field names rather
|
| 45 |
// than values.
|
| 46 |
db_query('INSERT INTO {active_translation} (atid, '. implode(', ', $fields) .') SELECT DISTINCT(nid), '. implode(', ', $values) .' FROM {node} WHERE tnid = 0 OR tnid IS NULL');
|
| 47 |
|
| 48 |
watchdog('active_translati', 'Created records for untranslated and language neutral nodes.');
|
| 49 |
}
|
| 50 |
|
| 51 |
// Process the next set of translations.
|
| 52 |
$limit = 20;
|
| 53 |
$result = db_query_range("SELECT DISTINCT tnid FROM {node} WHERE tnid > %d ORDER BY tnid ASC", $context['sandbox']['current_translation'], 0, $limit);
|
| 54 |
while ($row = db_fetch_object($result)) {
|
| 55 |
$node = node_load($row->tnid);
|
| 56 |
if (empty($node->nid)) {
|
| 57 |
watchdog('active_translati', 'Could not load the node %nid.', array('%nid' => $row->tnid), WATCHDOG_ERROR);
|
| 58 |
}
|
| 59 |
else {
|
| 60 |
$translation = active_translation_recompute($node);
|
| 61 |
drupal_write_record('active_translation', $translation);
|
| 62 |
}
|
| 63 |
$context['sandbox']['progress']++;
|
| 64 |
$context['sandbox']['current_translation'] = $row->tnid;
|
| 65 |
}
|
| 66 |
|
| 67 |
// Multistep processing: report progress.
|
| 68 |
if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
|
| 69 |
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
function _active_translation_batch_finished($success, $results, $operations) {
|
| 74 |
if ($success) {
|
| 75 |
watchdog('active_translati', 'Sucessfully rebuilt the active translation table.');
|
| 76 |
drupal_set_message(t('The active translation table has rebuilt.'));
|
| 77 |
}
|
| 78 |
else {
|
| 79 |
watchdog('active_translati', 'There was an error that prevented the active translation table from being properly rebuilt.', array(), WATCHDOG_ERROR);
|
| 80 |
drupal_set_message(t('The active translation table has not been properly rebuilt.'), 'error');
|
| 81 |
}
|
| 82 |
cache_clear_all();
|
| 83 |
}
|
| 84 |
|