commit 93b898d9a8523245b5745f99dba839aa45470047 Author: Erik Stielstra Date: Thu May 30 22:03:25 2013 +0200 #17 diff --git a/core/modules/locale/locale.compare.inc b/core/modules/locale/locale.compare.inc index 1cb4d67..380078d 100644 --- a/core/modules/locale/locale.compare.inc +++ b/core/modules/locale/locale.compare.inc @@ -361,74 +361,3 @@ function locale_translation_check_projects_local($projects = array(), $langcodes } } } - -/** - * Check updates for active projects and languages. - * - * @param $count - * Number of package translations to check. - * @param $last - * Unix timestamp, check only updates that haven't been checked for this time. - * @param $limit - * Maximum number of updates to do. We check $count translations - * but we stop after we do $limit updates. - * @return array - */ -function locale_translation_update_cron($count, $last) { - $projects_to_update = $languages_to_update = array(); - - $languages = locale_translatable_language_list(); - if (empty($languages)) { - return; - } - - // Select active projects x languages ordered by last checked time - $query = db_select('locale_project', 'p'); - $query->leftJoin('locale_file', 'f', 'p.name = f.project'); - $query->condition('p.status', 1); - $query->condition('f.last_checked', $last, '<'); - $query->condition('f.langcode', array_keys($languages)); - $query->fields('f', array('project')); - $query->range(0, $count); - $query->orderBy('last_checked'); - $projects = $query->execute()->fetchAllKeyed(0, 0); -watchdog('debug', 'Projects to update: ' . print_r($projects, 1)); - - if ($projects) { - module_load_include('fetch.inc', 'locale'); - - // Get status of translations. - // @todo This kicks off a batch, but it does not work in Cron. - // Need to convert the batch exection to a queue process. -// locale_translation_check_projects($projects); - $status = locale_translation_get_status(); - $status = array_intersect_key($status, $projects); - - // Mark 'last_checked' for up to date translations. - foreach ($status as $project => $languages) { - foreach ($languages as $langcode => $source) { - if (!isset($source->type) || $source->type == LOCALE_TRANSLATION_CURRENT) { - // The translation is up-to-date update the last_updated timestamp in {locale_file}. -watchdog('debug', "Mark up-to-date: $project : $langcode"); - } - else { - $projects_to_update[$project] = $project; - $languages_to_update[$langcode] = $langcode; - } - } - } - if ($projects_to_update && $languages_to_update) { -watchdog('debug', "Update translations for " . print_r($projects_to_update, 1) . ' in ' . print_r($languages_to_update, 1) . "."); - // Update the remaining projects/languages. - $options = _locale_translation_default_update_options(); - // @todo This kicks off a batch, but it does not work in Cron. - // Need to convert the batch exection to a queue process. -// if ($batch = locale_translation_batch_fetch_build($projects_to_update, $languages_to_update, $options)) { -// batch_set($batch); -// batch_process(); -// } - return count($projects_to_update) * count($languages_to_update); - } - } - return FALSE; -} diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index 35341a7..4022809 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -517,45 +517,56 @@ function locale_cron() { * Implements hook_queue_info(). */ function locale_queue_info() { - $queues['locale_translation_update'] = array( + $queues['locale_translation'] = array( 'title' => t('Update translations'), - 'worker callback' => 'locale_translation_update_worker', + 'worker callback' => 'locale_translation_worker', 'cron' => array( - 'time' => 60, + 'time' => 30, ), ); return $queues; } /** + * @todo Function title + * * Performs check, download and import of project translations. * May be called multiple times if the import is not completed. */ -function locale_translation_update_worker($data) { +function locale_translation_worker($data) { module_load_include('batch.inc', 'locale'); list($function, $args) = $data; - // @todo Make the batch operation functions working here by emulating - // a $batch_context variable. - - // Build the 'context' array if not already present and execute the function - // call. - if (!isset($args[count($args) - 1]['finished'])) { + // We execute batch operation functions here to import the translation files. + // All batch functions use a context variable as last argument, the context + // is passed by reference. When a batch operation is called for the first time + // a default batch context is set. When called iterative (usually the batch + // import function) the batch context is passed through via the queue and is + // part of the $data. The $batch_context is be passed by reference to the + // batch operation function. + $last = count($args) - 1; + if (!is_array($args[$last]) || !isset($args[$last]['finished'])) { $batch_context = array( 'sandbox' => array(), 'results' => array(), 'finished' => 1, 'message' => '', ); - $args = array_merge($args, array(&$batch_context)); } + else { + $batch_context = $args[$last]; + unset ($args[$last]); + } + $args = array_merge($args, array(&$batch_context)); + + // Call the batch operation function. call_user_func_array($function, $args); // If the batch operation is not yet finished we create a new queue task - // to continue the works. Most probably this is the translation import task. + // to continue the works. This probably is the translation import task. if ($batch_context['finished'] < 1) { - $batch_context['sandbox'] = array(); + $batch_context['strings'] = array(); $queue = Drupal::queue('locale_translation', TRUE); $queue->createItem(array($function, $args)); } @@ -964,7 +975,7 @@ function locale_translation_get_file_history() { if (empty($history)) { // Get file history from the database. - $result = db_query('SELECT project, langcode, filename, version, uri, timestamp, last_checked, queued FROM {locale_file}'); + $result = db_query('SELECT project, langcode, filename, version, uri, timestamp, last_checked FROM {locale_file}'); foreach ($result as $file) { $file->type = LOCALE_TRANSLATION_CURRENT; $history[$file->project][$file->langcode] = $file;