1<?php
2
3/**
4 * @file
5 * Common API for interface translation.
6 */
7
8use Drupal\Core\StreamWrapper\StreamWrapperManager;
9
10/**
11 * Comparison result of source files timestamps.
12 *
13 * Timestamp of source 1 is less than the timestamp of source 2.
14 *
15 * @see _locale_translation_source_compare()
16 */
17const LOCALE_TRANSLATION_SOURCE_COMPARE_LT = -1;
18
19/**
20 * Comparison result of source files timestamps.
21 *
22 * Timestamp of source 1 is equal to the timestamp of source 2.
23 *
24 * @see _locale_translation_source_compare()
25 */
26const LOCALE_TRANSLATION_SOURCE_COMPARE_EQ = 0;
27
28/**
29 * Comparison result of source files timestamps.
30 *
31 * Timestamp of source 1 is greater than the timestamp of source 2.
32 *
33 * @see _locale_translation_source_compare()
34 */
35const LOCALE_TRANSLATION_SOURCE_COMPARE_GT = 1;
36
37/**
38 * Get array of projects which are available for interface translation.
39 *
40 * This project data contains all projects which will be checked for available
41 * interface translations.
42 *
43 * For full functionality this function depends on Update module.
44 * When Update module is enabled the project data will contain the most recent
45 * module status; both in enabled status as in version. When Update module is
46 * disabled this function will return the last known module state. The status
47 * will only be updated once Update module is enabled.
48 *
49 * @param array $project_names
50 *   Array of names of the projects to get.
51 *
52 * @return array
53 *   Array of project data for translation update.
54 *
55 * @see locale_translation_build_projects()
56 */
57function locale_translation_get_projects(array $project_names = []) {
58  $projects = &drupal_static(__FUNCTION__, []);
59
60  if (empty($projects)) {
61    // Get project data from the database.
62    $row_count = \Drupal::service('locale.project')->countProjects();
63    // https://www.drupal.org/node/1777106 is a follow-up issue to make the
64    // check for possible out-of-date project information more robust.
65    if ($row_count == 0) {
66      module_load_include('compare.inc', 'locale');
67      // At least the core project should be in the database, so we build the
68      // data if none are found.
69      locale_translation_build_projects();
70    }
71    $projects = \Drupal::service('locale.project')->getAll();
72    array_walk($projects, function (&$project) {
73      $project = (object) $project;
74    });
75  }
76
77  // Return the requested project names or all projects.
78  if ($project_names) {
79    return array_intersect_key($projects, array_combine($project_names, $project_names));
80  }
81  return $projects;
82}
83
84/**
85 * Clears the projects cache.
86 */
87function locale_translation_clear_cache_projects() {
88  drupal_static_reset('locale_translation_get_projects');
89}
90
91/**
92 * Loads cached translation sources containing current translation status.
93 *
94 * @param array $projects
95 *   Array of project names. Defaults to all translatable projects.
96 * @param array $langcodes
97 *   Array of language codes. Defaults to all translatable languages.
98 *
99 * @return array
100 *   Array of source objects. Keyed with <project name>:<language code>.
101 *
102 * @see locale_translation_source_build()
103 */
104function locale_translation_load_sources(array $projects = NULL, array $langcodes = NULL) {
105  $sources = [];
106  $projects = $projects ? $projects : array_keys(locale_translation_get_projects());
107  $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
108
109  // Load source data from locale_translation_status cache.
110  $status = locale_translation_get_status();
111
112  // Use only the selected projects and languages for update.
113  foreach ($projects as $project) {
114    foreach ($langcodes as $langcode) {
115      $sources[$project][$langcode] = isset($status[$project][$langcode]) ? $status[$project][$langcode] : NULL;
116    }
117  }
118  return $sources;
119}
120
121/**
122 * Build translation sources.
123 *
124 * @param array $projects
125 *   Array of project names. Defaults to all translatable projects.
126 * @param array $langcodes
127 *   Array of language codes. Defaults to all translatable languages.
128 *
129 * @return array
130 *   Array of source objects. Keyed by project name and language code.
131 *
132 * @see locale_translation_source_build()
133 */
134function locale_translation_build_sources(array $projects = [], array $langcodes = []) {
135  $sources = [];
136  $projects = locale_translation_get_projects($projects);
137  $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
138
139  foreach ($projects as $project) {
140    foreach ($langcodes as $langcode) {
141      $source = locale_translation_source_build($project, $langcode);
142      $sources[$source->name][$source->langcode] = $source;
143    }
144  }
145  return $sources;
146}
147
148/**
149 * Checks whether a po file exists in the local filesystem.
150 *
151 * It will search in the directory set in the translation source. Which defaults
152 * to the "translations://" stream wrapper path. The directory may contain any
153 * valid stream wrapper.
154 *
155 * The "local" files property of the source object contains the definition of a
156 * po file we are looking for. The file name defaults to
157 * %project-%version.%language.po. Per project this value can be overridden
158 * using the server_pattern directive in the module's .info.yml file or by using
159 * hook_locale_translation_projects_alter().
160 *
161 * @param object $source
162 *   Translation source object.
163 *
164 * @return object
165 *   Source file object of the po file, updated with:
166 *   - "uri": File name and path.
167 *   - "timestamp": Last updated time of the po file.
168 *   FALSE if the file is not found.
169 *
170 * @see locale_translation_source_build()
171 */
172function locale_translation_source_check_file($source) {
173  if (isset($source->files[LOCALE_TRANSLATION_LOCAL])) {
174    $source_file = $source->files[LOCALE_TRANSLATION_LOCAL];
175    $directory = $source_file->directory;
176    $filename = '/' . preg_quote($source_file->filename) . '$/';
177
178    if (is_dir($directory)) {
179      if ($files = \Drupal::service('file_system')->scanDirectory($directory, $filename, ['key' => 'name', 'recurse' => FALSE])) {
180        $file = current($files);
181        $source_file->uri = $file->uri;
182        $source_file->timestamp = filemtime($file->uri);
183        return $source_file;
184      }
185    }
186  }
187  return FALSE;
188}
189
190/**
191 * Builds abstract translation source.
192 *
193 * @param object $project
194 *   Project object.
195 * @param string $langcode
196 *   Language code.
197 * @param string $filename
198 *   (optional) File name of translation file. May contain placeholders.
199 *   Defaults to the default translation filename from the settings.
200 *
201 * @return object
202 *   Source object:
203 *   - "project": Project name.
204 *   - "name": Project name (inherited from project).
205 *   - "language": Language code.
206 *   - "core": Core version (inherited from project).
207 *   - "version": Project version (inherited from project).
208 *   - "project_type": Project type (inherited from project).
209 *   - "files": Array of file objects containing properties of local and remote
210 *     translation files.
211 *   Other processes can add the following properties:
212 *   - "type": Most recent translation source found. LOCALE_TRANSLATION_REMOTE
213 *      and LOCALE_TRANSLATION_LOCAL indicate available new translations,
214 *      LOCALE_TRANSLATION_CURRENT indicate that the current translation is them
215 *      most recent. "type" corresponds with a key of the "files" array.
216 *   - "timestamp": The creation time of the "type" translation (file).
217 *   - "last_checked": The time when the "type" translation was last checked.
218 *   The "files" array can hold file objects of type:
219 *   LOCALE_TRANSLATION_LOCAL, LOCALE_TRANSLATION_REMOTE and
220 *   LOCALE_TRANSLATION_CURRENT. Each contains following properties:
221 *   - "type": The object type (LOCALE_TRANSLATION_LOCAL,
222 *     LOCALE_TRANSLATION_REMOTE, etc. see above).
223 *   - "project": Project name.
224 *   - "langcode": Language code.
225 *   - "version": Project version.
226 *   - "uri": Local or remote file path.
227 *   - "directory": Directory of the local po file.
228 *   - "filename": File name.
229 *   - "timestamp": Timestamp of the file.
230 *   - "keep": TRUE to keep the downloaded file.
231 */
232function locale_translation_source_build($project, $langcode, $filename = NULL) {
233  // Follow-up issue: https://www.drupal.org/node/1842380.
234  // Convert $source object to a TranslatableProject class and use a typed class
235  // for $source-file.
236
237  // Create a source object with data of the project object.
238  $source = clone $project;
239  $source->project = $project->name;
240  $source->langcode = $langcode;
241  $source->type = '';
242  $source->timestamp = 0;
243  $source->last_checked = 0;
244
245  $filename = $filename ? $filename : \Drupal::config('locale.settings')->get('translation.default_filename');
246
247  // If the server_pattern contains a remote file path we will check for a
248  // remote file. The local version of this file will only be checked if a
249  // translations directory has been defined. If the server_pattern is a local
250  // file path we will only check for a file in the local file system.
251  $files = [];
252  if (_locale_translation_file_is_remote($source->server_pattern)) {
253    $files[LOCALE_TRANSLATION_REMOTE] = (object) [
254      'project' => $project->name,
255      'langcode' => $langcode,
256      'version' => $project->version,
257      'type' => LOCALE_TRANSLATION_REMOTE,
258      'filename' => locale_translation_build_server_pattern($source, basename($source->server_pattern)),
259      'uri' => locale_translation_build_server_pattern($source, $source->server_pattern),
260    ];
261    $files[LOCALE_TRANSLATION_LOCAL] = (object) [
262      'project' => $project->name,
263      'langcode' => $langcode,
264      'version' => $project->version,
265      'type' => LOCALE_TRANSLATION_LOCAL,
266      'filename' => locale_translation_build_server_pattern($source, $filename),
267      'directory' => 'translations://',
268    ];
269    $files[LOCALE_TRANSLATION_LOCAL]->uri = $files[LOCALE_TRANSLATION_LOCAL]->directory . $files[LOCALE_TRANSLATION_LOCAL]->filename;
270  }
271  else {
272    $files[LOCALE_TRANSLATION_LOCAL] = (object) [
273      'project' => $project->name,
274      'langcode' => $langcode,
275      'version' => $project->version,
276      'type' => LOCALE_TRANSLATION_LOCAL,
277      'filename' => locale_translation_build_server_pattern($source, basename($source->server_pattern)),
278      'directory' => locale_translation_build_server_pattern($source, \Drupal::service('file_system')->dirname($source->server_pattern)),
279    ];
280    $files[LOCALE_TRANSLATION_LOCAL]->uri = $files[LOCALE_TRANSLATION_LOCAL]->directory . '/' . $files[LOCALE_TRANSLATION_LOCAL]->filename;
281  }
282  $source->files = $files;
283
284  // If this project+language is already translated, we add its status and
285  // update the current translation timestamp and last_updated time. If the
286  // project+language is not translated before, create a new record.
287  $history = locale_translation_get_file_history();
288  if (isset($history[$project->name][$langcode]) && $history[$project->name][$langcode]->timestamp) {
289    $source->files[LOCALE_TRANSLATION_CURRENT] = $history[$project->name][$langcode];
290    $source->type = LOCALE_TRANSLATION_CURRENT;
291    $source->timestamp = $history[$project->name][$langcode]->timestamp;
292    $source->last_checked = $history[$project->name][$langcode]->last_checked;
293  }
294  else {
295    locale_translation_update_file_history($source);
296  }
297
298  return $source;
299}
300
301/**
302 * Build path to translation source, out of a server path replacement pattern.
303 *
304 * @param object $project
305 *   Project object containing data to be inserted in the template.
306 * @param string $template
307 *   String containing placeholders. Available placeholders:
308 *   - "%project": Project name.
309 *   - "%version": Project version.
310 *   - "%core": Project core version.
311 *   - "%language": Language code.
312 *
313 * @return string
314 *   String with replaced placeholders.
315 */
316function locale_translation_build_server_pattern($project, $template) {
317  $variables = [
318    '%project' => $project->name,
319    '%version' => $project->version,
320    '%core' => $project->core,
321    '%language' => isset($project->langcode) ? $project->langcode : '%language',
322  ];
323  return strtr($template, $variables);
324}
325
326/**
327 * Populate a queue with project to check for translation updates.
328 */
329function locale_cron_fill_queue() {
330  $updates = [];
331  $config = \Drupal::config('locale.settings');
332
333  // Determine which project+language should be updated.
334  $last = REQUEST_TIME - $config->get('translation.update_interval_days') * 3600 * 24;
335  $projects = \Drupal::service('locale.project')->getAll();
336  $projects = array_filter($projects, function ($project) {
337    return $project['status'] == 1;
338  });
339  $connection = \Drupal::database();
340  $files = $connection->select('locale_file', 'f')
341    ->condition('f.project', array_keys($projects), 'IN')
342    ->condition('f.last_checked', $last, '<')
343    ->fields('f', ['project', 'langcode'])
344    ->execute()->fetchAll();
345  foreach ($files as $file) {
346    $updates[$file->project][] = $file->langcode;
347
348    // Update the last_checked timestamp of the project+language that will
349    // be checked for updates.
350    $connection->update('locale_file')
351      ->fields(['last_checked' => REQUEST_TIME])
352      ->condition('project', $file->project)
353      ->condition('langcode', $file->langcode)
354      ->execute();
355  }
356
357  // For each project+language combination a number of tasks are added to
358  // the queue.
359  if ($updates) {
360    module_load_include('fetch.inc', 'locale');
361    $options = _locale_translation_default_update_options();
362    $queue = \Drupal::queue('locale_translation', TRUE);
363
364    foreach ($updates as $project => $languages) {
365      $batch = locale_translation_batch_update_build([$project], $languages, $options);
366      foreach ($batch['operations'] as $item) {
367        $queue->createItem($item);
368      }
369    }
370  }
371}
372
373/**
374 * Determine if a file is a remote file.
375 *
376 * @param string $uri
377 *   The URI or URI pattern of the file.
378 *
379 * @return bool
380 *   TRUE if the $uri is a remote file.
381 */
382function _locale_translation_file_is_remote($uri) {
383  $scheme = StreamWrapperManager::getScheme($uri);
384  if ($scheme) {
385    return !\Drupal::service('file_system')->realpath($scheme . '://');
386  }
387  return FALSE;
388}
389
390/**
391 * Compare two update sources, looking for the newer one.
392 *
393 * The timestamp property of the source objects are used to determine which is
394 * the newer one.
395 *
396 * @param object $source1
397 *   Source object of the first translation source.
398 * @param object $source2
399 *   Source object of available update.
400 *
401 * @return int
402 *   - "LOCALE_TRANSLATION_SOURCE_COMPARE_LT": $source1 < $source2 OR $source1
403 *     is missing.
404 *   - "LOCALE_TRANSLATION_SOURCE_COMPARE_EQ":  $source1 == $source2 OR both
405 *     $source1 and $source2 are missing.
406 *   - "LOCALE_TRANSLATION_SOURCE_COMPARE_GT":  $source1 > $source2 OR $source2
407 *     is missing.
408 */
409function _locale_translation_source_compare($source1, $source2) {
410  if (isset($source1->timestamp) && isset($source2->timestamp)) {
411    if ($source1->timestamp == $source2->timestamp) {
412      return LOCALE_TRANSLATION_SOURCE_COMPARE_EQ;
413    }
414    else {
415      return $source1->timestamp > $source2->timestamp ? LOCALE_TRANSLATION_SOURCE_COMPARE_GT : LOCALE_TRANSLATION_SOURCE_COMPARE_LT;
416    }
417  }
418  elseif (isset($source1->timestamp) && !isset($source2->timestamp)) {
419    return LOCALE_TRANSLATION_SOURCE_COMPARE_GT;
420  }
421  elseif (!isset($source1->timestamp) && isset($source2->timestamp)) {
422    return LOCALE_TRANSLATION_SOURCE_COMPARE_LT;
423  }
424  else {
425    return LOCALE_TRANSLATION_SOURCE_COMPARE_EQ;
426  }
427}
428
429/**
430 * Returns default import options for translation update.
431 *
432 * @return array
433 *   Array of translation import options.
434 */
435function _locale_translation_default_update_options() {
436  $config = \Drupal::config('locale.settings');
437  return [
438    'customized' => LOCALE_NOT_CUSTOMIZED,
439    'overwrite_options' => [
440      'not_customized' => $config->get('translation.overwrite_not_customized'),
441      'customized' => $config->get('translation.overwrite_customized'),
442    ],
443    'finish_feedback' => TRUE,
444    'use_remote' => locale_translation_use_remote_source(),
445  ];
446}
447