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