Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117863059
D1606.1775320292.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
15 KB
Referenced Files
None
Subscribers
None
D1606.1775320292.diff
View Options
diff --git a/src/app/Console/Commands/DataCountries.php b/src/app/Console/Commands/DataCountries.php
--- a/src/app/Console/Commands/DataCountries.php
+++ b/src/app/Console/Commands/DataCountries.php
@@ -6,6 +6,11 @@
class DataCountries extends Command
{
+ private $currency_fixes = [
+ // Country code => currency
+ 'LT' => 'EUR',
+ ];
+
/**
* The name and signature of the console command.
*
@@ -29,111 +34,61 @@
{
$countries = [];
$currencies = [];
- $currencies_url = 'http://en.wikipedia.org/wiki/ISO_4217';
- $countries_url = 'http://en.wikipedia.org/wiki/ISO_3166-1';
+ $currencies_url = 'http://country.io/currency.json';
+ $countries_url = 'http://country.io/names.json';
$this->info("Fetching currencies from $currencies_url...");
// fetch currency table and create an index by country page url
- $page = file_get_contents($currencies_url);
+ $currencies_json = file_get_contents($currencies_url);
- if (!$page) {
+ if (!$currencies_json) {
$this->error("Failed to fetch currencies");
return;
}
- $table_regexp = '!<table class="(wikitable|prettytable) sortable".+</table>!ims';
- if (preg_match_all($table_regexp, $page, $matches, PREG_PATTERN_ORDER)) {
- foreach ($matches[0] as $currency_table) {
- preg_match_all('!<tr>\s*<td>(.+)</td>\s*</tr>!Ums', $currency_table, $rows);
-
- foreach ($rows[1] as $row) {
- $cells = preg_split('!</td>\s*<td[^>]*>!', $row);
-
- if (count($cells) == 5) {
- // actual currency table
- $currency = preg_match('/([A-Z]{3})/', $cells[0], $m) ? $m[1] : '';
-
- if (preg_match('/(\d+)/', $cells[1], $m)) {
- $isocode = $m[1];
- $currencies[$m[1]] = $currency;
- }
-
- preg_match_all('!<a[^>]+href="(/wiki/[^"]+)"[^>]*>!', $cells[4], $links, PREG_PATTERN_ORDER);
-
- foreach ($links[1] as $link) {
- $currencies[strtolower($link)] = $currency;
- }
- } elseif (count($cells) == 7) {
- // replacements table
- $currency = preg_match('/([A-Z]{3})/', $cells[6], $m) ? $m[1] : '';
-
- if (preg_match('/(\d+)/', $cells[1], $m)) {
- $currencies[$m[1]] = $currency;
- }
- }
- }
- }
- }
-
- $namecol = 0;
- $codecol = 1;
- $numcol = 3;
- $lang = 'en';
-
$this->info("Fetching countries from $countries_url...");
- $page = file_get_contents($countries_url);
+ $countries_json = file_get_contents($countries_url);
- if (!$page) {
+ if (!$countries_json) {
$this->error("Failed to fetch countries");
return;
}
- if (preg_match($table_regexp, $page, $matches)) {
- preg_match_all('!<tr>\s*<td>(.+)</td>\s*</tr>!Ums', $matches[0], $rows);
-
- foreach ($rows[1] as $row) {
- $cells = preg_split('!</td>\s*<td[^>]*>!', $row);
-
- if (count($cells) < 5) {
- continue;
- }
-
- $regexp = '!<a[^>]+href="(/wiki/[^"]+)"[^>]*>([^>]+)</a>!i';
- $content = preg_match($regexp, $cells[$namecol], $m) ? $m : null;
-
- if (preg_match('/>([A-Z]{2})</', $cells[$codecol], $m)) {
- $code = $m[1];
- } elseif (preg_match('/^([A-Z]{2})/', $cells[$codecol], $m)) {
- $code = $m[1];
- } else {
- continue;
- }
-
- if ($content) {
- $isocode = preg_match('/(\d+)/', $cells[$numcol], $m) ? $m[1] : '';
- list(, $link, $name) = $content;
- $countries[$code][$lang] = $name;
-
- if (!empty($currencies[$isocode])) {
- $countries[$code]['currency'] = $currencies[$isocode];
- } elseif (!empty($currencies[strtolower($link)])) {
- $countries[$code]['currency'] = $currencies[strtolower($link)];
- }
- }
- }
+ $currencies = json_decode($currencies_json, true);
+ $countries = json_decode($countries_json, true);
+
+ if (!is_array($countries) || empty($countries)) {
+ $this->error("Invalid countries data");
+ return;
+ }
+
+ if (!is_array($currencies) || empty($currencies)) {
+ $this->error("Invalid currencies data");
+ return;
}
$file = resource_path('countries.php');
$this->info("Generating resource file $file...");
+ asort($countries);
+
$out = "<?php return [\n";
- foreach ($countries as $code => $names) {
- if (!empty($names['en']) && !empty($names['currency'])) {
- $out .= sprintf(" '%s' => ['%s','%s'],\n", $code, $names['currency'], addslashes($names['en']));
+ foreach ($countries as $code => $name) {
+ $currency = $currencies[$code] ?? null;
+
+ if (!empty($this->currency_fixes[$code])) {
+ $currency = $this->currency_fixes[$code];
+ }
+
+ if (!$currency) {
+ $this->error("Unknown currency for {$name} ({$code}). Skipped.");
+ continue;
}
+
+ $out .= sprintf(" '%s' => ['%s','%s'],\n", $code, $currency, addslashes($name));
}
$out .= "];\n";
diff --git a/src/resources/countries.php b/src/resources/countries.php
--- a/src/resources/countries.php
+++ b/src/resources/countries.php
@@ -1,6 +1,6 @@
<?php return [
'AF' => ['AFN','Afghanistan'],
- 'AX' => ['EUR','Åland Islands'],
+ 'AX' => ['EUR','Aland Islands'],
'AL' => ['ALL','Albania'],
'DZ' => ['DZD','Algeria'],
'AS' => ['USD','American Samoa'],
@@ -14,60 +14,62 @@
'AU' => ['AUD','Australia'],
'AT' => ['EUR','Austria'],
'AZ' => ['AZN','Azerbaijan'],
+ 'BS' => ['BSD','Bahamas'],
'BH' => ['BHD','Bahrain'],
'BD' => ['BDT','Bangladesh'],
- 'BB' => ['USD','Barbados'],
- 'BY' => ['BYN','Belarus'],
+ 'BB' => ['BBD','Barbados'],
+ 'BY' => ['BYR','Belarus'],
'BE' => ['EUR','Belgium'],
'BZ' => ['BZD','Belize'],
'BJ' => ['XOF','Benin'],
- 'BM' => ['USD','Bermuda'],
- 'BT' => ['INR','Bhutan'],
- 'BO' => ['BOV','Bolivia (Plurinational State of)'],
- 'BQ' => ['USD','Bonaire, Sint Eustatius and Saba'],
+ 'BM' => ['BMD','Bermuda'],
+ 'BT' => ['BTN','Bhutan'],
+ 'BO' => ['BOB','Bolivia'],
+ 'BQ' => ['USD','Bonaire, Saint Eustatius and Saba '],
'BA' => ['BAM','Bosnia and Herzegovina'],
'BW' => ['BWP','Botswana'],
'BV' => ['NOK','Bouvet Island'],
'BR' => ['BRL','Brazil'],
'IO' => ['USD','British Indian Ocean Territory'],
+ 'VG' => ['USD','British Virgin Islands'],
+ 'BN' => ['BND','Brunei'],
'BG' => ['BGN','Bulgaria'],
'BF' => ['XOF','Burkina Faso'],
'BI' => ['BIF','Burundi'],
- 'KH' => ['USD','Cambodia'],
+ 'KH' => ['KHR','Cambodia'],
'CM' => ['XAF','Cameroon'],
'CA' => ['CAD','Canada'],
+ 'CV' => ['CVE','Cape Verde'],
'KY' => ['KYD','Cayman Islands'],
'CF' => ['XAF','Central African Republic'],
'TD' => ['XAF','Chad'],
'CL' => ['CLP','Chile'],
'CN' => ['CNY','China'],
'CX' => ['AUD','Christmas Island'],
- 'CC' => ['AUD','Cocos (Keeling) Islands'],
- 'CO' => ['COU','Colombia'],
+ 'CC' => ['AUD','Cocos Islands'],
+ 'CO' => ['COP','Colombia'],
'KM' => ['KMF','Comoros'],
- 'CG' => ['XAF','Congo'],
- 'CD' => ['CDF','Congo, Democratic Republic of the'],
'CK' => ['NZD','Cook Islands'],
'CR' => ['CRC','Costa Rica'],
- 'CI' => ['XOF','Côte d\'Ivoire'],
'HR' => ['HRK','Croatia'],
'CU' => ['CUP','Cuba'],
- 'CW' => ['ANG','Curaçao'],
+ 'CW' => ['ANG','Curacao'],
'CY' => ['EUR','Cyprus'],
- 'CZ' => ['CZK','Czechia'],
+ 'CZ' => ['CZK','Czech Republic'],
+ 'CD' => ['CDF','Democratic Republic of the Congo'],
'DK' => ['DKK','Denmark'],
'DJ' => ['DJF','Djibouti'],
'DM' => ['XCD','Dominica'],
'DO' => ['DOP','Dominican Republic'],
+ 'TL' => ['USD','East Timor'],
'EC' => ['USD','Ecuador'],
'EG' => ['EGP','Egypt'],
'SV' => ['USD','El Salvador'],
'GQ' => ['XAF','Equatorial Guinea'],
'ER' => ['ERN','Eritrea'],
'EE' => ['EUR','Estonia'],
- 'SZ' => ['SZL','Eswatini'],
'ET' => ['ETB','Ethiopia'],
- 'FK' => ['FKP','Falkland Islands (Malvinas)'],
+ 'FK' => ['FKP','Falkland Islands'],
'FO' => ['DKK','Faroe Islands'],
'FJ' => ['FJD','Fiji'],
'FI' => ['EUR','Finland'],
@@ -76,6 +78,7 @@
'PF' => ['XPF','French Polynesia'],
'TF' => ['EUR','French Southern Territories'],
'GA' => ['XAF','Gabon'],
+ 'GM' => ['GMD','Gambia'],
'GE' => ['GEL','Georgia'],
'DE' => ['EUR','Germany'],
'GH' => ['GHS','Ghana'],
@@ -86,10 +89,11 @@
'GP' => ['EUR','Guadeloupe'],
'GU' => ['USD','Guam'],
'GT' => ['GTQ','Guatemala'],
+ 'GG' => ['GBP','Guernsey'],
'GN' => ['GNF','Guinea'],
'GW' => ['XOF','Guinea-Bissau'],
'GY' => ['GYD','Guyana'],
- 'HT' => ['USD','Haiti'],
+ 'HT' => ['HTG','Haiti'],
'HM' => ['AUD','Heard Island and McDonald Islands'],
'HN' => ['HNL','Honduras'],
'HK' => ['HKD','Hong Kong'],
@@ -97,12 +101,13 @@
'IS' => ['ISK','Iceland'],
'IN' => ['INR','India'],
'ID' => ['IDR','Indonesia'],
- 'IR' => ['IRR','Iran (Islamic Republic of)'],
+ 'IR' => ['IRR','Iran'],
'IQ' => ['IQD','Iraq'],
'IE' => ['EUR','Ireland'],
'IM' => ['GBP','Isle of Man'],
'IL' => ['ILS','Israel'],
'IT' => ['EUR','Italy'],
+ 'CI' => ['XOF','Ivory Coast'],
'JM' => ['JMD','Jamaica'],
'JP' => ['JPY','Japan'],
'JE' => ['GBP','Jersey'],
@@ -110,20 +115,20 @@
'KZ' => ['KZT','Kazakhstan'],
'KE' => ['KES','Kenya'],
'KI' => ['AUD','Kiribati'],
- 'KP' => ['KPW','Korea (Democratic People\'s Republic of)'],
- 'KR' => ['KRW','Korea, Republic of'],
+ 'XK' => ['EUR','Kosovo'],
'KW' => ['KWD','Kuwait'],
'KG' => ['KGS','Kyrgyzstan'],
- 'LA' => ['LAK','Lao People\'s Democratic Republic'],
+ 'LA' => ['LAK','Laos'],
'LV' => ['EUR','Latvia'],
'LB' => ['LBP','Lebanon'],
- 'LS' => ['ZAR','Lesotho'],
+ 'LS' => ['LSL','Lesotho'],
'LR' => ['LRD','Liberia'],
'LY' => ['LYD','Libya'],
'LI' => ['CHF','Liechtenstein'],
'LT' => ['EUR','Lithuania'],
'LU' => ['EUR','Luxembourg'],
'MO' => ['MOP','Macao'],
+ 'MK' => ['MKD','Macedonia'],
'MG' => ['MGA','Madagascar'],
'MW' => ['MWK','Malawi'],
'MY' => ['MYR','Malaysia'],
@@ -132,12 +137,12 @@
'MT' => ['EUR','Malta'],
'MH' => ['USD','Marshall Islands'],
'MQ' => ['EUR','Martinique'],
- 'MR' => ['MRU','Mauritania'],
+ 'MR' => ['MRO','Mauritania'],
'MU' => ['MUR','Mauritius'],
'YT' => ['EUR','Mayotte'],
- 'MX' => ['MXV','Mexico'],
- 'FM' => ['USD','Micronesia (Federated States of)'],
- 'MD' => ['MDL','Moldova, Republic of'],
+ 'MX' => ['MXN','Mexico'],
+ 'FM' => ['USD','Micronesia'],
+ 'MD' => ['MDL','Moldova'],
'MC' => ['EUR','Monaco'],
'MN' => ['MNT','Mongolia'],
'ME' => ['EUR','Montenegro'],
@@ -145,9 +150,10 @@
'MA' => ['MAD','Morocco'],
'MZ' => ['MZN','Mozambique'],
'MM' => ['MMK','Myanmar'],
- 'NA' => ['ZAR','Namibia'],
+ 'NA' => ['NAD','Namibia'],
'NR' => ['AUD','Nauru'],
'NP' => ['NPR','Nepal'],
+ 'NL' => ['EUR','Netherlands'],
'NC' => ['XPF','New Caledonia'],
'NZ' => ['NZD','New Zealand'],
'NI' => ['NIO','Nicaragua'],
@@ -155,14 +161,14 @@
'NG' => ['NGN','Nigeria'],
'NU' => ['NZD','Niue'],
'NF' => ['AUD','Norfolk Island'],
- 'MK' => ['MKD','North Macedonia'],
+ 'KP' => ['KPW','North Korea'],
'MP' => ['USD','Northern Mariana Islands'],
- 'NL' => ['EUR','The Netherlands'],
'NO' => ['NOK','Norway'],
'OM' => ['OMR','Oman'],
'PK' => ['PKR','Pakistan'],
'PW' => ['USD','Palau'],
- 'PA' => ['USD','Panama'],
+ 'PS' => ['ILS','Palestinian Territory'],
+ 'PA' => ['PAB','Panama'],
'PG' => ['PGK','Papua New Guinea'],
'PY' => ['PYG','Paraguay'],
'PE' => ['PEN','Peru'],
@@ -172,42 +178,49 @@
'PT' => ['EUR','Portugal'],
'PR' => ['USD','Puerto Rico'],
'QA' => ['QAR','Qatar'],
- 'RE' => ['EUR','Réunion'],
+ 'CG' => ['XAF','Republic of the Congo'],
+ 'RE' => ['EUR','Reunion'],
'RO' => ['RON','Romania'],
- 'RU' => ['RUB','Russian Federation'],
+ 'RU' => ['RUB','Russia'],
'RW' => ['RWF','Rwanda'],
- 'BL' => ['EUR','Saint Barthélemy'],
+ 'BL' => ['EUR','Saint Barthelemy'],
+ 'SH' => ['SHP','Saint Helena'],
'KN' => ['XCD','Saint Kitts and Nevis'],
'LC' => ['XCD','Saint Lucia'],
- 'MF' => ['EUR','Saint Martin (French part)'],
+ 'MF' => ['EUR','Saint Martin'],
'PM' => ['EUR','Saint Pierre and Miquelon'],
'VC' => ['XCD','Saint Vincent and the Grenadines'],
'WS' => ['WST','Samoa'],
'SM' => ['EUR','San Marino'],
+ 'ST' => ['STD','Sao Tome and Principe'],
'SA' => ['SAR','Saudi Arabia'],
'SN' => ['XOF','Senegal'],
'RS' => ['RSD','Serbia'],
'SC' => ['SCR','Seychelles'],
'SL' => ['SLL','Sierra Leone'],
'SG' => ['SGD','Singapore'],
- 'SX' => ['ANG','Sint Maarten (Dutch part)'],
+ 'SX' => ['ANG','Sint Maarten'],
'SK' => ['EUR','Slovakia'],
'SI' => ['EUR','Slovenia'],
'SB' => ['SBD','Solomon Islands'],
'SO' => ['SOS','Somalia'],
'ZA' => ['ZAR','South Africa'],
+ 'GS' => ['GBP','South Georgia and the South Sandwich Islands'],
+ 'KR' => ['KRW','South Korea'],
'SS' => ['SSP','South Sudan'],
'ES' => ['EUR','Spain'],
'LK' => ['LKR','Sri Lanka'],
'SD' => ['SDG','Sudan'],
'SR' => ['SRD','Suriname'],
+ 'SJ' => ['NOK','Svalbard and Jan Mayen'],
+ 'SZ' => ['SZL','Swaziland'],
'SE' => ['SEK','Sweden'],
- 'CH' => ['CHW','Switzerland'],
- 'SY' => ['SYP','Syrian Arab Republic'],
+ 'CH' => ['CHF','Switzerland'],
+ 'SY' => ['SYP','Syria'],
+ 'TW' => ['TWD','Taiwan'],
'TJ' => ['TJS','Tajikistan'],
- 'TZ' => ['TZS','Tanzania, United Republic of'],
+ 'TZ' => ['TZS','Tanzania'],
'TH' => ['THB','Thailand'],
- 'TL' => ['USD','Timor-Leste'],
'TG' => ['XOF','Togo'],
'TK' => ['NZD','Tokelau'],
'TO' => ['TOP','Tonga'],
@@ -217,22 +230,22 @@
'TM' => ['TMT','Turkmenistan'],
'TC' => ['USD','Turks and Caicos Islands'],
'TV' => ['AUD','Tuvalu'],
+ 'VI' => ['USD','U.S. Virgin Islands'],
'UG' => ['UGX','Uganda'],
'UA' => ['UAH','Ukraine'],
'AE' => ['AED','United Arab Emirates'],
- 'GB' => ['GBP','United Kingdom of Great Britain and Northern Ireland'],
- 'US' => ['USN','United States of America'],
+ 'GB' => ['GBP','United Kingdom'],
+ 'US' => ['USD','United States'],
'UM' => ['USD','United States Minor Outlying Islands'],
- 'UY' => ['UYW','Uruguay'],
+ 'UY' => ['UYU','Uruguay'],
'UZ' => ['UZS','Uzbekistan'],
'VU' => ['VUV','Vanuatu'],
- 'VE' => ['VES','Venezuela (Bolivarian Republic of)'],
- 'VN' => ['VND','Viet Nam'],
- 'VG' => ['USD','Virgin Islands (British)'],
- 'VI' => ['USD','Virgin Islands (U.S.)'],
+ 'VA' => ['EUR','Vatican'],
+ 'VE' => ['VEF','Venezuela'],
+ 'VN' => ['VND','Vietnam'],
'WF' => ['XPF','Wallis and Futuna'],
'EH' => ['MAD','Western Sahara'],
'YE' => ['YER','Yemen'],
- 'ZM' => ['ZMW','Zambia'],
+ 'ZM' => ['ZMK','Zambia'],
'ZW' => ['ZWL','Zimbabwe'],
];
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Apr 4, 4:31 PM (1 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18826565
Default Alt Text
D1606.1775320292.diff (15 KB)
Attached To
Mode
D1606: Fix data:countries, Update countries.php file.
Attached
Detach File
Event Timeline