diff --git a/src/app/Console/Commands/DataCountries.php b/src/app/Console/Commands/DataCountries.php new file mode 100644 index 00000000..3c6bbd60 --- /dev/null +++ b/src/app/Console/Commands/DataCountries.php @@ -0,0 +1,142 @@ +info("Fetching currencies from $currencies_url..."); + + // fetch currency table and create an index by country page url + $page = file_get_contents($currencies_url); + + if (!$page) { + $this->error("Failed to fetch currencies"); + return; + } + + $table_regexp = '!!ims'; + if (preg_match_all($table_regexp, $page, $matches, PREG_PATTERN_ORDER)) { + foreach ($matches[0] as $currency_table) { + preg_match_all('!\s*\s*!Ums', $currency_table, $rows); + + foreach ($rows[1] as $row) { + $cells = preg_split('!\s*]*>!', $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('!]+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); + + if (!$page) { + $this->error("Failed to fetch countries"); + return; + } + + if (preg_match($table_regexp, $page, $matches)) { + preg_match_all('!\s*\s*!Ums', $matches[0], $rows); + + foreach ($rows[1] as $row) { + $cells = preg_split('!\s*]*>!', $row); + + if (count($cells) < 5) { + continue; + } + + $regexp = '!]+href="(/wiki/[^"]+)"[^>]*>([^>]+)!i'; + $content = preg_match($regexp, $cells[$namecol], $m) ? $m : null; + + if (preg_match('/>([A-Z]{2})info("Generating resource file $file..."); + + $out = " $names) { + if (!empty($names['en']) && !empty($names['currency'])) { + $out .= sprintf(" '%s' => ['%s','%s'],\n", $code, $names['currency'], addslashes($names['en'])); + } + } + $out .= "];\n"; + + file_put_contents($file, $out); + } +}
(.+)
(.+)