update-iso-3166.sh

Updates ISO-3166 codes in standard.inc to latest data.

We rely on the Debian ISO code repository, because it is easily accessible, scriptable, in the right human-readable format, and all changes went through sufficient FOSS community discussion already.

File

drupal/core/scripts/update-iso-3166.sh
View source
  1. #!/bin/php
  2. /**
  3. * @file
  4. * Updates ISO-3166 codes in standard.inc to latest data.
  5. *
  6. * We rely on the Debian ISO code repository, because it is easily accessible,
  7. * scriptable, in the right human-readable format, and all changes went through
  8. * sufficient FOSS community discussion already.
  9. */
  10. // Determine DRUPAL_ROOT.
  11. $cwd = $dir = dirname(__FILE__);
  12. while (!defined('DRUPAL_ROOT')) {
  13. if (is_dir($dir . '/core')) {
  14. define('DRUPAL_ROOT', $dir);
  15. }
  16. $dir = dirname($dir);
  17. }
  18. // Determine source data file URI to process.
  19. $uri = $cwd . '/iso_3166.xml';
  20. // Despite its actual file name, at least Firefox merges and converts the path
  21. // and filename into a combined filename.
  22. if (!file_exists($uri)) {
  23. $uri = $cwd . '/iso_3166_iso_3166.xml';
  24. }
  25. // Fall back and default to original Debian source.
  26. if (!file_exists($uri)) {
  27. $uri = 'http://anonscm.debian.org/gitweb/?p=iso-codes/iso-codes.git;a=blob_plain;f=iso_3166/iso_3166.xml;hb=master';
  28. }
  29. // Read in existing codes.
  30. // @todo Allow to remove previously existing country codes.
  31. // @see http://drupal.org/node/1436754
  32. require_once DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManagerInterface.php';
  33. require_once DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php';
  34. $existing_countries = \Drupal\Core\Locale\CountryManager::getStandardList();
  35. $countries = $existing_countries;
  36. // Parse the source data into an array.
  37. $data = simplexml_load_file($uri);
  38. foreach ($data->iso_3166_entry as $entry) {
  39. // Ignore every territory that doesn't have a alpha-2 code.
  40. if (!isset($entry['alpha_2_code'])) {
  41. continue;
  42. }
  43. $name = isset($entry['name']) ? (string) $entry['name'] : (string) $entry['official_name'];
  44. $countries[(string) $entry['alpha_2_code']] = $name;
  45. }
  46. if (empty($countries)) {
  47. echo 'ERROR: Did not find expected alpha_2_code country names.' . PHP_EOL;
  48. exit;
  49. }
  50. // Sort by country code (to minimize diffs).
  51. ksort($countries);
  52. // Produce PHP code.
  53. $out = '';
  54. foreach ($countries as $code => $name) {
  55. // For .po translation file's sake, use double-quotes instead of escaped
  56. // single-quotes.
  57. $name = (strpos($name, '\'') !== FALSE ? '"' . $name . '"' : "'" . $name . "'");
  58. $out .= ' ' . var_export($code, TRUE) . ' => $t(' . $name . '),' . "\n";
  59. }
  60. // Replace the actual PHP code in standard.inc.
  61. $file = DRUPAL_ROOT . '/core/includes/standard.inc';
  62. $content = file_get_contents($file);
  63. $content = preg_replace('/(\$countries = array\(\n)(.+?)(^\s+\);)/ms', '$1' . $out . '$3', $content);
  64. file_put_contents($file, $content);
  65. /**
  66. * No-op script helper.
  67. */
  68. function get_t() {
  69. return function ($string) {
  70. return $string;
  71. };
  72. }