l10n_update.install 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <?php
  2. /**
  3. * @file
  4. * Install file for l10n remote updates.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function l10n_update_schema() {
  10. $schema['l10n_update_project'] = array(
  11. 'description' => 'Update information for project translations.',
  12. 'fields' => array(
  13. 'name' => array(
  14. 'description' => 'A unique short name to identify the project.',
  15. 'type' => 'varchar',
  16. 'length' => '255',
  17. 'not null' => TRUE,
  18. ),
  19. 'project_type' => array(
  20. 'description' => 'Project type, may be core, module, theme',
  21. 'type' => 'varchar',
  22. 'length' => '50',
  23. 'not null' => TRUE,
  24. ),
  25. 'core' => array(
  26. 'description' => 'Core compatibility string for this project.',
  27. 'type' => 'varchar',
  28. 'length' => '128',
  29. 'not null' => TRUE,
  30. 'default' => '',
  31. ),
  32. 'version' => array(
  33. 'description' => 'Human readable name for project used on the interface.',
  34. 'type' => 'varchar',
  35. 'length' => '128',
  36. 'not null' => TRUE,
  37. 'default' => '',
  38. ),
  39. 'l10n_path' => array(
  40. 'description' => 'Server path this project updates.',
  41. 'type' => 'varchar',
  42. 'length' => '255',
  43. 'not null' => TRUE,
  44. 'default' => '',
  45. ),
  46. 'status' => array(
  47. 'description' => 'Status flag. If TRUE, translations of this module will be updated.',
  48. 'type' => 'int',
  49. 'not null' => TRUE,
  50. 'default' => 1,
  51. ),
  52. ),
  53. 'primary key' => array('name'),
  54. );
  55. $schema['l10n_update_file'] = array(
  56. 'description' => 'File and download information for project translations.',
  57. 'fields' => array(
  58. 'project' => array(
  59. 'description' => 'A unique short name to identify the project.',
  60. 'type' => 'varchar',
  61. 'length' => '255',
  62. 'not null' => TRUE,
  63. ),
  64. 'language' => array(
  65. 'description' => 'Reference to the {languages}.language for this translation.',
  66. 'type' => 'varchar',
  67. 'length' => '12',
  68. 'not null' => TRUE,
  69. ),
  70. 'type' => array(
  71. 'description' => 'File origin: download or localfile',
  72. 'type' => 'varchar',
  73. 'length' => '50',
  74. 'not null' => TRUE,
  75. 'default' => '',
  76. ),
  77. 'filename' => array(
  78. 'description' => 'Link to translation file for download.',
  79. 'type' => 'varchar',
  80. 'length' => 255,
  81. 'not null' => TRUE,
  82. 'default' => '',
  83. ),
  84. 'fileurl' => array(
  85. 'description' => 'Link to translation file for download.',
  86. 'type' => 'varchar',
  87. 'length' => 255,
  88. 'not null' => TRUE,
  89. 'default' => '',
  90. ),
  91. 'uri' => array(
  92. 'description' => 'File system path for importing the file.',
  93. 'type' => 'varchar',
  94. 'length' => 255,
  95. 'not null' => TRUE,
  96. 'default' => '',
  97. ),
  98. 'timestamp' => array(
  99. 'description' => 'Unix timestamp of the time the file was downloaded or saved to disk. Zero if not yet downloaded',
  100. 'type' => 'int',
  101. 'not null' => FALSE,
  102. 'disp-width' => '11',
  103. 'default' => 0,
  104. ),
  105. 'version' => array(
  106. 'description' => 'Version tag of the downloaded file.',
  107. 'type' => 'varchar',
  108. 'length' => '128',
  109. 'not null' => TRUE,
  110. 'default' => '',
  111. ),
  112. 'status' => array(
  113. 'description' => 'Status flag. TBD',
  114. 'type' => 'int',
  115. 'not null' => TRUE,
  116. 'default' => 1,
  117. ),
  118. 'last_checked' => array(
  119. 'description' => 'Unix timestamp of the last time this translation was downloaded from or checked at remote server and confirmed to be the most recent release available.',
  120. 'type' => 'int',
  121. 'not null' => FALSE,
  122. 'disp-width' => '11',
  123. 'default' => 0,
  124. ),
  125. ),
  126. 'primary key' => array('project', 'language'),
  127. );
  128. return $schema;
  129. }
  130. /**
  131. * Implements hook_schema_alter().
  132. */
  133. function l10n_update_schema_alter(&$schema) {
  134. $schema['locales_target']['fields']['l10n_status'] = array(
  135. 'type' => 'int',
  136. 'not null' => TRUE,
  137. 'default' => 0,
  138. 'description' => 'Boolean indicating whether the translation is custom to this site.',
  139. );
  140. }
  141. /**
  142. * Implements hook_install().
  143. */
  144. function l10n_update_install() {
  145. db_add_field('locales_target', 'l10n_status', array(
  146. 'type' => 'int',
  147. 'not null' => TRUE,
  148. 'default' => 0,
  149. ));
  150. variable_set('l10n_update_rebuild_projects', 1);
  151. // Create the translation directory. We try different alternative paths as the
  152. // default may not always be writable.
  153. $directories = array(
  154. variable_get('l10n_update_download_store', L10N_UPDATE_DEFAULT_TRANSLATION_PATH),
  155. variable_get('file_public_path', conf_path() . '/files') . '/translations',
  156. 'sites/default/files/translations',
  157. );
  158. foreach ($directories as $directory) {
  159. if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  160. variable_set('l10n_update_download_store', $directory);
  161. return;
  162. }
  163. }
  164. watchdog('l10n_update', 'The directory %directory does not exist or is not writable.', array('%directory' => $directories[0]), WATCHDOG_ERROR);
  165. drupal_set_message(t('The directory %directory does not exist or is not writable.', array('%directory' => $directories[0])), 'error');
  166. }
  167. /**
  168. * Implements hook_uninstall().
  169. */
  170. function l10n_update_uninstall() {
  171. db_drop_field('locales_target', 'l10n_status');
  172. variable_del('l10n_update_check_disabled');
  173. variable_del('l10n_update_default_filename');
  174. variable_del('l10n_update_default_update_url');
  175. variable_del('l10n_update_import_enabled');
  176. variable_del('l10n_update_import_mode');
  177. variable_del('l10n_update_check_frequency');
  178. variable_del('l10n_update_last_check');
  179. variable_del('l10n_update_download_store');
  180. variable_del('l10n_update_check_mode');
  181. }
  182. /**
  183. * Implements hook_requirements().
  184. */
  185. function l10n_update_requirements($phase) {
  186. $requirements = array();
  187. if ($phase == 'runtime') {
  188. $available_updates = array();
  189. $untranslated = array();
  190. $languages = l10n_update_translatable_language_list();
  191. if ($languages) {
  192. // Determine the status of the translation updates per language.
  193. $status = l10n_update_get_status();
  194. if ($status) {
  195. foreach ($status as $project) {
  196. foreach ($project as $langcode => $project_info) {
  197. if (empty($project_info->type)) {
  198. $untranslated[$langcode] = $languages[$langcode];
  199. }
  200. elseif ($project_info->type == L10N_UPDATE_LOCAL || $project_info->type == L10N_UPDATE_REMOTE) {
  201. $available_updates[$langcode] = $languages[$langcode];
  202. }
  203. }
  204. }
  205. if ($available_updates || $untranslated) {
  206. if ($available_updates) {
  207. $requirements['l10n_update'] = array(
  208. 'title' => 'Translation update status',
  209. 'value' => l(t('Updates available'), 'admin/config/regional/translate/update'),
  210. 'severity' => REQUIREMENT_WARNING,
  211. 'description' => t('Updates available for: @languages. See the <a href="!updates">Translate interface update</a> page for more information.', array(
  212. '@languages' => implode(', ', $available_updates),
  213. '!updates' => url('admin/config/regional/translate/update'),
  214. )),
  215. );
  216. }
  217. else {
  218. $requirements['l10n_update'] = array(
  219. 'title' => 'Translation update status',
  220. 'value' => t('Missing translations'),
  221. 'severity' => REQUIREMENT_INFO,
  222. 'description' => t('Missing translations for: @languages. See the <a href="!updates">Translate interface update</a> page for more information.', array(
  223. '@languages' => implode(', ', $untranslated),
  224. '!updates' => url('admin/config/regional/translate/update'),
  225. )),
  226. );
  227. }
  228. }
  229. else {
  230. $requirements['l10n_update'] = array(
  231. 'title' => 'Translation update status',
  232. 'value' => t('Up to date'),
  233. 'severity' => REQUIREMENT_OK,
  234. );
  235. }
  236. }
  237. else {
  238. $requirements['locale_translation'] = array(
  239. 'title' => 'Translation update status',
  240. 'value' => l(t('Can not determine status'), 'admin/config/regional/translate/update'),
  241. 'severity' => REQUIREMENT_WARNING,
  242. 'description' => t('No translation status is available. See the <a href="!updates">Translate interface update</a> page for more information.', array('!updates' => url('admin/config/regional/translate/update'))),
  243. );
  244. }
  245. }
  246. }
  247. if ($phase == 'update') {
  248. // Make sure the 'translations' stream wrapper class gets registered.
  249. // This is needed when upgrading to 7.x-2.x.
  250. if (!class_exists('TranslationsStreamWrapper')) {
  251. registry_rebuild();
  252. }
  253. }
  254. return $requirements;
  255. }
  256. /**
  257. * Converts D8 style translations strings into D7 format.
  258. *
  259. * Drupal 8 stores plurals (both for source and translation strings) as one
  260. * string, where the plural forms are separated by a special character. By
  261. * mistake this plural handling was also back ported. This function converts any
  262. * D8 style plural into a D7 style plural. It used the following strategy.
  263. * - Any locales_source record that contains the separator character is
  264. * converted.
  265. * - All these records are deleted, except those of which the translation was
  266. * edited.
  267. * - Existing translations will only be overwritten by converted translations
  268. * if the import behaviour setting (admin/config/regional/language/update)
  269. * allows that.
  270. *
  271. * @param int $start
  272. * The number of start.
  273. * @param int $length
  274. * The length number.
  275. * @param array $report
  276. * Array containing import results.
  277. *
  278. * @return bool
  279. * Returns true conversion
  280. * a keyed array of log messages. Available keys:
  281. * - message: Translated message
  282. * - status: Message status. See drush_log() for supported values.
  283. */
  284. function l10n_update_d8_plural_conversion($start = 0, $length = 0, array &$report) {
  285. module_load_include('inc', 'l10n_update', 'l10n_update.translation');
  286. require_once DRUPAL_ROOT . '/includes/locale.inc';
  287. $processed = array();
  288. $plurals = l10n_update_get_d8_plural_strings($start, $length);
  289. $finished = count($plurals) < $length;
  290. if (empty($plurals)) {
  291. return $finished;
  292. }
  293. $mode = variable_get('l10n_update_import_mode', LOCALE_IMPORT_OVERWRITE);
  294. $writer = new PoDatabaseWriter();
  295. $writer->setOptions(array(
  296. 'overwrite_options' => array(
  297. 'not_customized' => $mode != LOCALE_IMPORT_KEEP,
  298. 'customized' => $mode == LOCALE_IMPORT_OVERWRITE,
  299. ),
  300. 'customized' => L10N_UPDATE_NOT_CUSTOMIZED,
  301. ));
  302. $writer_report = isset($report['writer']) ? $report['writer'] : array();
  303. $writer->setReport($writer_report);
  304. foreach ($plurals as $plural) {
  305. // Extract data from source and target strings.
  306. $sources = explode(L10N_UPDATE_PLURAL_DELIMITER, $plural->source);
  307. $translations = explode(L10N_UPDATE_PLURAL_DELIMITER, $plural->translation);
  308. $writer->setLangcode($plural->language);
  309. $item = new PoItem();
  310. $item->setContext($plural->context);
  311. $item->setSource($sources);
  312. $item->setTranslation($translations);
  313. $item->setPlural(TRUE);
  314. $item->setLangcode($plural->language);
  315. $item->setTextgroup($plural->textgroup);
  316. $writer->writeItem($item);
  317. // Collect plurals to be deleted. In the rare case that a translation was
  318. // modified, we will convert it but not delete the custom translation.
  319. if (!$plural->l10n_status) {
  320. $report['results']['lids'][] = $plural->lid;
  321. }
  322. $report['results']['languages'][$plural->language] = $plural->language;
  323. }
  324. $report['writer'] = $writer->getReport();
  325. unset($report['writer']['strings']);
  326. return $finished;
  327. }
  328. /**
  329. * Gets D8 plural style strings.
  330. *
  331. * @param int $start
  332. * Offset of records to load.
  333. * @param int $length
  334. * Maximum number of records to load.
  335. *
  336. * @return array|bool
  337. * An array of data from source and translation strings of which the source
  338. * string contains a Drupal 8 style plural delimiter. Returns false if not
  339. * found.
  340. */
  341. function l10n_update_get_d8_plural_strings($start = 0, $length = 0) {
  342. $translation_strings = FALSE;
  343. $query = db_select('locales_source', 'ls');
  344. $query->fields('ls', array(
  345. 'lid', 'location',
  346. 'textgroup',
  347. 'source',
  348. 'context',
  349. 'version',
  350. ));
  351. $query->fields('lt', array(
  352. 'translation',
  353. 'language',
  354. 'plid',
  355. 'plural',
  356. 'l10n_status',
  357. ));
  358. $query->innerJoin('locales_target', 'lt', 'ls.lid = lt.lid');
  359. $query->condition('source', '%' . db_like(L10N_UPDATE_PLURAL_DELIMITER) . '%', 'LIKE');
  360. if ($length) {
  361. $query->range($start, $length);
  362. }
  363. $results = $query->execute();
  364. if ($results->rowCount()) {
  365. foreach ($results as $result) {
  366. $translation_strings[] = $result;
  367. }
  368. }
  369. return $translation_strings;
  370. }
  371. /**
  372. * Clean-up after plural string conversion.
  373. *
  374. * @param array $results
  375. * Batch results array.
  376. */
  377. function l10n_update_finish_d8_plural_strings($results) {
  378. require_once DRUPAL_ROOT . '/includes/locale.inc';
  379. if ($results) {
  380. // Delete converted D8 style translations.
  381. if (isset($results['lids'])) {
  382. foreach ($results['lids'] as $lids) {
  383. db_delete('locales_source')->condition('lid', $lids)->execute();
  384. db_delete('locales_target')->condition('lid', $lids)->execute();
  385. }
  386. }
  387. // Clear caches if translations are modified.
  388. if (isset($results['languages'])) {
  389. cache_clear_all('locale:', 'cache', TRUE);
  390. foreach ($results['languages'] as $langcode) {
  391. _locale_invalidate_js($langcode);
  392. }
  393. }
  394. }
  395. }
  396. /**
  397. * Rename filepath to uri in {l10n_update_file} table.
  398. */
  399. function l10n_update_update_7001() {
  400. // Only do this update if the field exists from D6.
  401. // If it doesn't, we've got a pure D7 site that doesn't need it.
  402. if (db_field_exists('l10n_update_file', 'filepath')) {
  403. db_change_field('l10n_update_file', 'filepath', 'uri', array(
  404. 'description' => 'File system path for importing the file.',
  405. 'type' => 'varchar',
  406. 'length' => 255,
  407. 'not null' => TRUE,
  408. 'default' => '',
  409. ));
  410. }
  411. }
  412. /**
  413. * Delete 'last_updated' field from {l10n_update_file} table.
  414. */
  415. function l10n_update_update_7002() {
  416. db_drop_field('l10n_update_file', 'last_updated');
  417. }
  418. /**
  419. * Delete 'import_date' field from {l10n_update_file} table.
  420. */
  421. function l10n_update_update_7003() {
  422. db_drop_field('l10n_update_file', 'import_date');
  423. }
  424. /**
  425. * Create {cache_l10n_update} table.
  426. */
  427. function l10n_update_update_7004() {
  428. // Code removed. Table {cache_l10n_update} is no longer used.
  429. }
  430. /**
  431. * Migration to 7.x-2.x branch.
  432. */
  433. function l10n_update_update_7200() {
  434. // Make sure the 'translations' stream wrapper class gets registered.
  435. if (!class_exists('TranslationsStreamWrapper')) {
  436. registry_rebuild();
  437. }
  438. // If no translation directory was set, set one here. We try different
  439. // alternative paths as the default may not always be writable.
  440. if (!variable_get('l10n_update_download_store', '')) {
  441. $directories = array(
  442. variable_get('l10n_update_download_store', L10N_UPDATE_DEFAULT_TRANSLATION_PATH),
  443. variable_get('file_public_path', conf_path() . '/files') . '/translations',
  444. 'sites/default/files/translations',
  445. );
  446. $directory_created = FALSE;
  447. foreach ($directories as $directory) {
  448. if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  449. variable_set('l10n_update_download_store', $directory);
  450. $directory_created = TRUE;
  451. break;
  452. }
  453. }
  454. if (!$directory_created) {
  455. watchdog('l10n_update', 'The directory %directory does not exist or is not writable.', array('%directory' => $directories[0]), WATCHDOG_ERROR);
  456. drupal_set_message(t('The directory %directory does not exist or is not writable.', array('%directory' => $directories[0])), 'error');
  457. }
  458. }
  459. // Translation source 'Remote server only' is no longer supported. Use 'Remote
  460. // and local' instead.
  461. $mode = variable_get('l10n_update_check_mode', 3);
  462. if ($mode == 1) {
  463. variable_set('l10n_update_check_mode', 3);
  464. }
  465. // Daily cron updates are no longer supported. Use weekly instead.
  466. $frequency = variable_get('l10n_update_check_frequency', '0');
  467. if ($frequency == '1') {
  468. variable_set('l10n_update_check_frequency', '7');
  469. }
  470. // Clean up deprecated variables.
  471. variable_del('l10n_update_default_server');
  472. variable_del('l10n_update_default_server_url');
  473. variable_del('l10n_update_rebuild_projects');
  474. }
  475. /**
  476. * Sets the default translation files directory.
  477. */
  478. function l10n_update_update_7201() {
  479. if (!variable_get('l10n_update_download_store', '')) {
  480. variable_set('l10n_update_download_store', 'sites/all/translations');
  481. }
  482. }
  483. /**
  484. * Removes table {cache_l10n_update}.
  485. */
  486. function l10n_update_update_7202() {
  487. if (db_table_exists('cache_l10n_update')) {
  488. db_drop_table('cache_l10n_update');
  489. }
  490. }
  491. /**
  492. * Removes the field 'l10n_server' from table {cache_l10n_update}.
  493. */
  494. function l10n_update_update_7203() {
  495. if (db_field_exists('l10n_update_project', 'l10n_server')) {
  496. db_drop_field('l10n_update_project', 'l10n_server');
  497. }
  498. }
  499. /**
  500. * Increase the length of the name column.
  501. */
  502. function l10n_update_update_7204() {
  503. $schema = l10n_update_schema();
  504. db_change_field('l10n_update_project', 'name', 'name', $schema['l10n_update_project']['fields']['name']);
  505. }
  506. /**
  507. * Increase the length of the name column.
  508. */
  509. function l10n_update_update_7205() {
  510. $schema = l10n_update_schema();
  511. db_change_field('l10n_update_file', 'project', 'project', $schema['l10n_update_file']['fields']['project']);
  512. }
  513. /**
  514. * Remove 'headers' field from cache table.
  515. */
  516. function l10n_update_update_7206() {
  517. if (db_field_exists('cache_l10n_update', 'headers')) {
  518. db_drop_field('cache_l10n_update', 'headers');
  519. }
  520. }
  521. /**
  522. * Migrate D8 style plurals to D7 style.
  523. */
  524. function l10n_update_update_7207(&$sandbox) {
  525. $message = NULL;
  526. $batch_size = 50;
  527. if (!isset($sandbox['progress'])) {
  528. $sandbox['progress'] = 0;
  529. $sandbox['max'] = count(l10n_update_get_d8_plural_strings());
  530. $sandbox['report'] = array();
  531. }
  532. if ($sandbox['max'] == 0) {
  533. $sandbox['#finished'] = 1;
  534. return NULL;
  535. }
  536. $finished = l10n_update_d8_plural_conversion($sandbox['progress'], $batch_size, $sandbox['report']);
  537. $sandbox['progress'] += $batch_size;
  538. // Whether the batch is finished is determined by the result of
  539. // l10n_update_d8_plural_conversion().The progress (percentage) is
  540. // calculated.
  541. if (empty($sandbox['max']) || $finished) {
  542. $sandbox['#finished'] = 1;
  543. }
  544. else {
  545. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  546. }
  547. if ($sandbox['#finished'] == 1) {
  548. l10n_update_finish_d8_plural_strings($sandbox['report']['results']);
  549. if ($sandbox['report']['writer']['skips']) {
  550. $message = t('Some problems were reported during plural string migration. See <a href="/admin/reports/dblog">Recent log messages</a> for details.');
  551. }
  552. }
  553. return $message;
  554. }
  555. /**
  556. * Remove the status variable.
  557. */
  558. function l10n_update_update_7208() {
  559. $status = variable_get('l10n_update_translation_status');
  560. cache_set('l10n_update_status', $status);
  561. variable_del('l10n_update_translation_status');
  562. }
  563. /**
  564. * Update default variable values to use https.
  565. */
  566. function l10n_update_update_7209() {
  567. $server_url = variable_get('l10n_client_server', '');
  568. if ($server_url == 'http://localize.drupal.org') {
  569. variable_set('l10n_client_server', 'https://localize.drupal.org');
  570. }
  571. $update_url = variable_get('l10n_update_default_update_url', '');
  572. if ($update_url == 'http://ftp.drupal.org/files/translations/%core/%project/%project-%release.%language.po') {
  573. variable_set('l10n_update_default_update_url', L10N_UPDATE_DEFAULT_SERVER_PATTERN);
  574. }
  575. }