Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / locale / locale.install
1 <?php
2
3 /**
4  * @file
5  * Install, update, and uninstall functions for the Locale module.
6  */
7
8 use Drupal\Core\Url;
9
10 /**
11  * Implements hook_install().
12  */
13 function locale_install() {
14   // Create the interface translations directory and ensure it's writable.
15   if (!$directory = \Drupal::config('locale.settings')->get('translation.path')) {
16     $site_path = \Drupal::service('site.path');
17     $directory = $site_path . '/files/translations';
18     \Drupal::configFactory()->getEditable('locale.settings')->set('translation.path', $directory)->save();
19   }
20   file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
21 }
22
23 /**
24  * Implements hook_uninstall().
25  */
26 function locale_uninstall() {
27   $config = \Drupal::config('locale.settings');
28   // Delete all JavaScript translation files.
29   $locale_js_directory = 'public://' . $config->get('javascript.directory');
30
31   if (is_dir($locale_js_directory)) {
32     $locale_javascripts = \Drupal::state()->get('locale.translation.javascript') ?: [];
33     foreach ($locale_javascripts as $langcode => $file_suffix) {
34       if (!empty($file_suffix)) {
35         file_unmanaged_delete($locale_js_directory . '/' . $langcode . '_' . $file_suffix . '.js');
36       }
37     }
38     // Delete the JavaScript translations directory if empty.
39     if (!file_scan_directory($locale_js_directory, '/.*/')) {
40       drupal_rmdir($locale_js_directory);
41     }
42   }
43
44   // Clear variables.
45   \Drupal::state()->delete('system.javascript_parsed');
46   \Drupal::state()->delete('locale.translation.plurals');
47   \Drupal::state()->delete('locale.translation.javascript');
48 }
49
50 /**
51  * Implements hook_schema().
52  */
53 function locale_schema() {
54   $schema['locales_source'] = [
55     'description' => 'List of English source strings.',
56     'fields' => [
57       'lid' => [
58         'type' => 'serial',
59         'not null' => TRUE,
60         'description' => 'Unique identifier of this string.',
61       ],
62       'source' => [
63         'type' => 'text',
64         'mysql_type' => 'blob',
65         'not null' => TRUE,
66         'description' => 'The original string in English.',
67       ],
68       'context' => [
69         'type' => 'varchar_ascii',
70         'length' => 255,
71         'not null' => TRUE,
72         'default' => '',
73         'description' => 'The context this string applies to.',
74       ],
75       'version' => [
76         'type' => 'varchar_ascii',
77         'length' => 20,
78         'not null' => TRUE,
79         'default' => 'none',
80         'description' => 'Version of Drupal where the string was last used (for locales optimization).',
81       ],
82     ],
83     'primary key' => ['lid'],
84     'indexes' => [
85       'source_context' => [['source', 30], 'context'],
86     ],
87   ];
88
89   $schema['locales_target'] = [
90     'description' => 'Stores translated versions of strings.',
91     'fields' => [
92       'lid' => [
93         'type' => 'int',
94         'not null' => TRUE,
95         'default' => 0,
96         'description' => 'Source string ID. References {locales_source}.lid.',
97       ],
98       'translation' => [
99         'type' => 'text',
100         'mysql_type' => 'blob',
101         'not null' => TRUE,
102         'description' => 'Translation string value in this language.',
103       ],
104       'language' => [
105         'type' => 'varchar_ascii',
106         'length' => 12,
107         'not null' => TRUE,
108         'default' => '',
109         'description' => 'Language code. References {language}.langcode.',
110       ],
111       'customized' => [
112         'type' => 'int',
113         'not null' => TRUE,
114         // LOCALE_NOT_CUSTOMIZED
115         'default' => 0,
116         'description' => 'Boolean indicating whether the translation is custom to this site.',
117       ],
118     ],
119     'primary key' => ['language', 'lid'],
120     'foreign keys' => [
121       'locales_source' => [
122         'table' => 'locales_source',
123         'columns' => ['lid' => 'lid'],
124       ],
125     ],
126     'indexes' => [
127       'lid' => ['lid'],
128     ],
129   ];
130
131   $schema['locales_location'] = [
132     'description' => 'Location information for source strings.',
133     'fields' => [
134       'lid' => [
135         'type' => 'serial',
136         'not null' => TRUE,
137         'description' => 'Unique identifier of this location.',
138       ],
139       'sid' => [
140         'type' => 'int',
141         'not null' => TRUE,
142         'description' => 'Unique identifier of this string.',
143       ],
144       'type' => [
145         'type' => 'varchar_ascii',
146         'length' => 50,
147         'not null' => TRUE,
148         'default' => '',
149         'description' => 'The location type (file, config, path, etc).',
150       ],
151       'name' => [
152         'type' => 'varchar',
153         'length' => 255,
154         'not null' => TRUE,
155         'default' => '',
156         'description' => 'Type dependent location information (file name, path, etc).',
157       ],
158       'version' => [
159         'type' => 'varchar_ascii',
160         'length' => 20,
161         'not null' => TRUE,
162         'default' => 'none',
163         'description' => 'Version of Drupal where the location was found.',
164       ],
165     ],
166     'primary key' => ['lid'],
167     'foreign keys' => [
168       'locales_source' => [
169         'table' => 'locales_source',
170         'columns' => ['sid' => 'lid'],
171       ],
172     ],
173     'indexes' => [
174       'string_id' => ['sid'],
175       'string_type' => ['sid', 'type'],
176     ],
177   ];
178
179   $schema['locale_file'] = [
180     'description' => 'File import status information for interface translation files.',
181     'fields' => [
182       'project' => [
183         'type' => 'varchar_ascii',
184         'length' => '255',
185         'not null' => TRUE,
186         'default' => '',
187         'description' => 'A unique short name to identify the project the file belongs to.',
188       ],
189       'langcode' => [
190         'type' => 'varchar_ascii',
191         'length' => '12',
192         'not null' => TRUE,
193         'default' => '',
194         'description' => 'Language code of this translation. References {language}.langcode.',
195       ],
196       'filename' => [
197         'type' => 'varchar',
198         'length' => 255,
199         'not null' => TRUE,
200         'default' => '',
201         'description' => 'Filename of the imported file.',
202       ],
203       'version' => [
204         'type' => 'varchar',
205         'length' => '128',
206         'not null' => TRUE,
207         'default' => '',
208         'description' => 'Version tag of the imported file.',
209       ],
210       'uri' => [
211         'type' => 'varchar',
212         'length' => 255,
213         'not null' => TRUE,
214         'default' => '',
215         'description' => 'URI of the remote file, the resulting local file or the locally imported file.',
216       ],
217       'timestamp' => [
218         'type' => 'int',
219         'not null' => FALSE,
220         'default' => 0,
221         'description' => 'Unix timestamp of the imported file.',
222       ],
223       'last_checked' => [
224         'type' => 'int',
225         'not null' => FALSE,
226         'default' => 0,
227         'description' => 'Unix timestamp of the last time this translation was confirmed to be the most recent release available.',
228       ],
229     ],
230     'primary key' => ['project', 'langcode'],
231   ];
232   return $schema;
233 }
234
235 /**
236  * Implements hook_requirements().
237  */
238 function locale_requirements($phase) {
239   $requirements = [];
240   if ($phase == 'runtime') {
241     $available_updates = [];
242     $untranslated = [];
243     $languages = locale_translatable_language_list();
244
245     if ($languages) {
246       // Determine the status of the translation updates per language.
247       $status = locale_translation_get_status();
248       if ($status) {
249         foreach ($status as $project) {
250           foreach ($project as $langcode => $project_info) {
251             if (empty($project_info->type)) {
252               $untranslated[$langcode] = $languages[$langcode]->getName();
253             }
254             elseif ($project_info->type == LOCALE_TRANSLATION_LOCAL || $project_info->type == LOCALE_TRANSLATION_REMOTE) {
255               $available_updates[$langcode] = $languages[$langcode]->getName();
256             }
257           }
258         }
259
260         if ($available_updates || $untranslated) {
261           if ($available_updates) {
262             $requirements['locale_translation'] = [
263               'title' => 'Translation update status',
264               'value' => \Drupal::l(t('Updates available'), new Url('locale.translate_status')),
265               'severity' => REQUIREMENT_WARNING,
266               'description' => t('Updates available for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', ['@languages' => implode(', ', $available_updates), ':updates' => \Drupal::url('locale.translate_status')]),
267             ];
268           }
269           else {
270             $requirements['locale_translation'] = [
271               'title' => 'Translation update status',
272               'value' => t('Missing translations'),
273               'severity' => REQUIREMENT_INFO,
274               'description' => t('Missing translations for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', ['@languages' => implode(', ', $untranslated), ':updates' => \Drupal::url('locale.translate_status')]),
275             ];
276           }
277         }
278         else {
279           $requirements['locale_translation'] = [
280             'title' => 'Translation update status',
281             'value' => t('Up to date'),
282             'severity' => REQUIREMENT_OK,
283           ];
284         }
285       }
286       else {
287         $requirements['locale_translation'] = [
288           'title' => 'Translation update status',
289           'value' => \Drupal::l(t('Can not determine status'), new Url('locale.translate_status')),
290           'severity' => REQUIREMENT_WARNING,
291           'description' => t('No translation status is available. See the <a href=":updates">Available translation updates</a> page for more information.', [':updates' => \Drupal::url('locale.translate_status')]),
292         ];
293       }
294     }
295   }
296   return $requirements;
297 }
298
299 /**
300  * Delete translation status data in state.
301  */
302 function locale_update_8300() {
303   // Delete the old translation status data, it will be rebuilt and stored in
304   // the new key value collection.
305   \Drupal::state()->delete('locale.translation_status');
306 }