locale.compare.inc 13.1 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * The API for comparing project translation status with available translation.
 */

use Drupal\Core\Cache;
9
use Drupal\Core\Utility\ProjectInfo;
10 11

/**
12
 * Load common APIs.
13
 */
14 15
// @todo Combine functions differently in files to avoid unnecessary includes.
// Follow-up issue http://drupal.org/node/1834298
16
require_once __DIR__ . '/locale.translation.inc';
17 18 19 20 21

/**
 * Clear the project data table.
 */
function locale_translation_flush_projects() {
22
  // Followup issue: http://drupal.org/node/1842362
23
  // Replace {locale_project} table by \Drupal::state() variable(s).
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  db_truncate('locale_project')->execute();
}

/**
 * Builds list of projects and stores the result in the database.
 *
 * The project data is based on the project list supplied by the Update module.
 * Only the properties required by Locale module is included and additional
 * (custom) modules and translation server data is added.
 *
 * In case the Update module is disabled this function will return an empty
 * array.
 *
 * @return array
 *   Array of project data:
 *   - "name": Project system name.
 *   - "project_type": Project type, e.g. 'module', 'theme'.
 *   - "core": Core release version, e.g. 8.x
 *   - "version": Project release version, e.g. 8.x-1.0
43 44
 *     See http://drupalcode.org/project/drupalorg.git/blob/refs/heads/7.x-3.x:/drupalorg_project/plugins/release_packager/DrupalorgProjectPackageRelease.class.php#l219
 *     for how the version strings are created.
45 46 47 48 49
 *   - "server_pattern": Translation server po file pattern.
 *   - "status": Project status, 1 = enabled.
 */
function locale_translation_build_projects() {
  // This function depends on Update module. We degrade gracefully.
50
  if (!\Drupal::moduleHandler()->moduleExists('update')) {
51 52 53
    return array();
  }

54
  // Get the project list based on .info.yml files.
55 56 57 58 59 60 61 62 63 64 65
  $projects = locale_translation_project_list();

  // Mark all previous projects as disabled and store new project data.
  db_update('locale_project')
    ->fields(array(
      'status' => 0,
    ))
    ->execute();

  $default_server = locale_translation_default_translation_server();

66
  // If project is a dev release, or core, find the latest available release.
67 68 69 70 71 72 73 74 75
  $project_updates = update_get_available(TRUE);
  foreach ($projects as $name => $data) {
    if (isset($project_updates[$name]['releases']) && $project_updates[$name]['project_status'] != 'not-fetched') {
      // Find out if a dev version is installed.
      if (preg_match("/^[0-9]+\.x-([0-9]+)\..*-dev$/", $data['info']['version'], $matches)) {
        // Find a suitable release to use as alternative translation.
        foreach ($project_updates[$name]['releases'] as $project_release) {
          // The first release with the same major release number which is not a
          // dev release is the one. Releases are sorted the most recent first.
76
          // @todo http://drupal.org/node/1774024 Make a helper function.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
          if ($project_release['version_major'] == $matches[1] &&
              (!isset($project_release['version_extra']) || $project_release['version_extra'] != 'dev')) {
            $release = $project_release;
            break;
          }
        }
      }

      if (!empty($release['version'])) {
        $data['info']['version'] = $release['version'];
      }

      unset($release);
    }

92
    // For every project store information.
93 94
    $data += array(
      'version' => isset($data['info']['version']) ? $data['info']['version'] : '',
95
      'core' => isset($data['info']['core']) ? $data['info']['core'] : \Drupal::CORE_COMPATIBILITY,
96 97
      // A project can provide the path and filename pattern to download the
      // gettext file. Use the default if not.
98
      'server_pattern' => isset($data['info']['interface translation server pattern']) && $data['info']['interface translation server pattern'] ? $data['info']['interface translation server pattern'] : $default_server['pattern'],
99
      'status' => !empty($data['project_status']) ? 1 : 0,
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    );
    $project = (object) $data;
    $projects[$name] = $project;

    // Create or update the project record.
    db_merge('locale_project')
      ->key(array('name' => $project->name))
      ->fields(array(
        'name' => $project->name,
        'project_type' => $project->project_type,
        'core' => $project->core,
        'version' => $project->version,
        'server_pattern' => $project->server_pattern,
        'status' => $project->status,
      ))
      ->execute();
116 117 118

      // Invalidate the cache of translatable projects.
      locale_translation_clear_cache_projects();
119 120 121 122 123 124 125 126
  }
  return $projects;
}

/**
 * Fetch an array of projects for translation update.
 *
 * @return array
127
 *   Array of project data including .info.yml file data.
128 129 130 131 132
 */
function locale_translation_project_list() {
  $projects = &drupal_static(__FUNCTION__, array());
  if (empty($projects)) {
    module_load_include('compare.inc', 'update');
133
    $config = \Drupal::config('locale.settings');
134 135 136 137 138 139 140 141
    $projects = array();

    $additional_whitelist = array(
      'interface translation project',
      'interface translation server pattern',
    );
    $module_data = _locale_translation_prepare_project_list(system_rebuild_module_data(), 'module');
    $theme_data = _locale_translation_prepare_project_list(system_rebuild_theme_data(), 'theme');
142 143 144
    $project_info = new ProjectInfo();
    $project_info->processInfoList($projects, $module_data, 'module', TRUE, $additional_whitelist);
    $project_info->processInfoList($projects, $theme_data, 'theme', TRUE, $additional_whitelist);
145
    if ($config->get('translation.check_disabled_modules')) {
146 147
      $project_info->processInfoList($projects, $module_data, 'module', FALSE, $additional_whitelist);
      $project_info->processInfoList($projects, $theme_data, 'theme', FALSE, $additional_whitelist);
148 149 150 151 152 153 154 155 156 157 158
    }

    // Allow other modules to alter projects before fetching and comparing.
    drupal_alter('locale_translation_projects', $projects);
  }
  return $projects;
}

/**
 * Prepare module and theme data.
 *
159 160 161 162
 * Modify .info.yml file data before it is processed by
 * \Drupal\Core\Utility\ProjectInfo->processInfoList(). In order for
 * \Drupal\Core\Utility\ProjectInfo->processInfoList() to recognize a project,
 * it requires the 'project' parameter in the .info.yml file data.
163
 *
164 165
 * Custom modules or themes can bring their own gettext translation file. To
 * enable import of this file the module or theme defines "interface translation
166
 * project = myproject" in its .info.yml file. This function will add a project
167 168 169
 * "myproject" to the info data.
 *
 * @param array $data
170
 *   Array of .info.yml file data.
171 172 173 174
 * @param string $type
 *   The project type. i.e. module, theme.
 *
 * @return array
175
 *   Array of .info.yml file data.
176 177 178
 */
function _locale_translation_prepare_project_list($data, $type) {
  foreach ($data as $name => $file) {
179
    // Include interface translation projects. To allow
180 181 182
    // \Drupal\Core\Utility\ProjectInfo->processInfoList() to identify this as
    // a project the 'project' property is filled with the
    // 'interface translation project' value.
183 184 185 186 187 188 189 190 191 192 193 194
    if (isset($file->info['interface translation project'])) {
      $data[$name]->info['project'] = $file->info['interface translation project'];
    }
  }
  return $data;
}

/**
 * Retrieve data for default server.
 *
 * @return array
 *   Array of server parameters:
195
 *   - "server_pattern": URI containing po file pattern.
196 197
 */
function locale_translation_default_translation_server() {
198
  $pattern = \Drupal::config('locale.settings')->get('translation.default_server_pattern');
199
  // An additional check is required here. During the upgrade process
200
  // \Drupal::config()->get() returns NULL. We use the defined value as fallback.
201 202
  $pattern  = $pattern ? $pattern : LOCALE_TRANSLATION_DEFAULT_SERVER_PATTERN;

203
  return array(
204
    'pattern' => $pattern,
205 206 207
  );
}

208 209 210 211
/**
 * Check for the latest release of project translations.
 *
 * @param array $projects
212
 *   Array of project names to check. Defaults to all translatable projects.
213
 * @param string $langcodes
214
 *   Array of language codes. Defaults to all translatable languages.
215 216 217 218
 *
 * @return array
 *   Available sources indexed by project and language.
 */
219
// @todo Return batch or NULL
220 221
function locale_translation_check_projects($projects = array(), $langcodes = array()) {
  if (locale_translation_use_remote_source()) {
222 223 224 225 226 227 228
    // Retrieve the status of both remote and local translation sources by
    // using a batch process.
    locale_translation_check_projects_batch($projects, $langcodes);
  }
  else {
    // Retrieve and save the status of local translations only.
    locale_translation_check_projects_local($projects, $langcodes);
229
    \Drupal::state()->set('locale.translation_last_checked', REQUEST_TIME);
230 231 232 233 234 235 236 237 238
  }
}

/**
 * Gets and stores the status and timestamp of remote po files.
 *
 * A batch process is used to check for po files at remote locations and (when
 * configured) to check for po files in the local file system. The most recent
 * translation source states are stored in the state variable
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
 * 'locale.translation_status'.
 *
 * @param array $projects
 *   Array of project names to check. Defaults to all translatable projects.
 * @param string $langcodes
 *   Array of language codes. Defaults to all translatable languages.
 */
function locale_translation_check_projects_batch($projects = array(), $langcodes = array()) {
  // Build and set the batch process.
  $batch = locale_translation_batch_status_build($projects, $langcodes);
  batch_set($batch);
}

/**
 * Builds a batch to get the status of remote and local translation files.
254
 *
255
 * The batch process fetches the state of both local and (if configured) remote
256 257 258 259 260 261 262 263 264 265 266 267 268
 * translation files. The data of the most recent translation is stored per
 * per project and per language. This data is stored in a state variable
 * 'locale.translation_status'. The timestamp it was last updated is stored
 * in the state variable 'locale.translation_last_checked'.
 *
 * @param array $projects
 *   Array of project names for which to check the state of translation files.
 *   Defaults to all translatable projects.
 * @param array $langcodes
 *   Array of language codes. Defaults to all translatable languages.
 *
 * @return array
 *   Batch definition array.
269
 */
270 271
function locale_translation_batch_status_build($projects = array(), $langcodes = array()) {
  $projects = $projects ? $projects : array_keys(locale_translation_get_projects());
272
  $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
273
  $options = _locale_translation_default_update_options();
274

275
  $operations = _locale_translation_batch_status_operations($projects, $langcodes, $options);
276 277 278

  $batch = array(
    'operations' => $operations,
279
    'title' => t('Checking translations'),
280
    'progress_message' => '',
281
    'finished' => 'locale_translation_batch_status_finished',
282
    'error_message' => t('Error checking translation updates.'),
283 284 285 286 287 288 289 290 291
    'file' => drupal_get_path('module', 'locale') . '/locale.batch.inc',
  );
  return $batch;
}

/**
 * Helper function to construct batch operations checking remote translation
 * status.
 *
292
 * @param array $projects
293
 *   Array of project names to be processed.
294
 * @param array $langcodes
295
 *   Array of language codes.
296 297
 * @param array $options
 *   Batch processing options.
298 299 300 301
 *
 * @return array
 *   Array of batch operations.
 */
302
function _locale_translation_batch_status_operations($projects, $langcodes, $options = array()) {
303 304
  $operations = array();

305 306 307 308
  foreach ($projects as $project) {
    foreach ($langcodes as $langcode) {
      // Check status of local and remote translation sources.
      $operations[] = array('locale_translation_batch_status_check', array($project, $langcode, $options));
309 310 311
    }
  }

312
  return $operations;
313 314 315 316 317 318
}

/**
 * Check and store the status and timestamp of local po files.
 *
 * Only po files in the local file system are checked. Any remote translation
319
 * files will be ignored.
320 321 322 323 324
 *
 * Projects may contain a server_pattern option containing a pattern of the
 * path to the po source files. If no server_pattern is defined the default
 * translation directory is checked for the po file. When a server_pattern is
 * defined the specified location is checked. The server_pattern can be set in
325 326
 * the module's .info.yml file or by using
 * hook_locale_translation_projects_alter().
327
 *
328 329 330 331 332
 * @param array $projects
 *   Array of project names for which to check the state of translation files.
 *   Defaults to all translatable projects.
 * @param array $langcodes
 *   Array of language codes. Defaults to all translatable languages.
333
 */
334 335
function locale_translation_check_projects_local($projects = array(), $langcodes = array()) {
  $projects = locale_translation_get_projects($projects);
336 337 338 339 340 341 342 343
  $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());

  // For each project and each language we check if a local po file is
  // available. When found the source object is updated with the appropriate
  // type and timestamp of the po file.
  foreach ($projects as $name => $project) {
    foreach ($langcodes as $langcode) {
      $source = locale_translation_source_build($project, $langcode);
344 345
      $file = locale_translation_source_check_file($source);
      locale_translation_status_save($name, $langcode, LOCALE_TRANSLATION_LOCAL, $file);
346 347 348
    }
  }
}