mirror of https://github.com/pixelfed/pixelfed.git
Merge pull request #1610 from pixelfed/staging
Update Import Cities command, shorten country names
This commit is contained in:
commit
a413d5035e
|
@ -8,6 +8,29 @@ use Illuminate\Support\Str;
|
||||||
|
|
||||||
class ImportCities extends Command
|
class ImportCities extends Command
|
||||||
{
|
{
|
||||||
|
protected $countries = [
|
||||||
|
'AE' => 'UAE',
|
||||||
|
'BA' => 'Bosnia-Herzegovina',
|
||||||
|
'BO' => 'Bolivia',
|
||||||
|
'CD' => 'Democratic Republic of Congo',
|
||||||
|
'CG' => 'Republic of Congo',
|
||||||
|
'FM' => 'Micronesia',
|
||||||
|
'GB' => 'United Kingdom',
|
||||||
|
'IR' => 'Iran',
|
||||||
|
'KP' => 'DRPK',
|
||||||
|
'KR' => 'South Korea',
|
||||||
|
'LA' => 'Laos',
|
||||||
|
'MD' => 'Moldova',
|
||||||
|
'PS' => 'Palestine',
|
||||||
|
'RU' => 'Russia',
|
||||||
|
'SH' => 'Saint Helena',
|
||||||
|
'SY' => 'Syria',
|
||||||
|
'TW' => 'Taiwan',
|
||||||
|
'TZ' => 'Tanzania',
|
||||||
|
'US' => 'USA',
|
||||||
|
'VE' => 'Venezuela',
|
||||||
|
'XK' => 'Kosovo'
|
||||||
|
];
|
||||||
/**
|
/**
|
||||||
* The name and signature of the console command.
|
* The name and signature of the console command.
|
||||||
*
|
*
|
||||||
|
@ -37,13 +60,21 @@ class ImportCities extends Command
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$path = storage_path('app/cities.json');
|
$path = storage_path('app/cities.json');
|
||||||
|
|
||||||
|
if(hash_file('sha512', $path) !== 'e203c0247538788b2a91166c7cf4b95f58291d998f514e9306d315aa72b09e48bfd3ddf310bf737afc4eefadca9083b8ff796c67796c6bd8e882a3d268bd16af') {
|
||||||
|
$this->error('Invalid or corrupt storage/app/cities.json data.');
|
||||||
|
$this->line('');
|
||||||
|
$this->info('Run the following command to fix:');
|
||||||
|
$this->info('git checkout storage/app/cities.json');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_file($path)) {
|
if (!is_file($path)) {
|
||||||
$this->error('Missing storage/app/cities.json file!');
|
$this->error('Missing storage/app/cities.json file!');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Place::count() > 10) {
|
if (Place::count() > 0) {
|
||||||
$this->error('Cities already imported, aborting operation...');
|
DB::table('places')->truncate();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
$this->info('Importing city data into database ...');
|
$this->info('Importing city data into database ...');
|
||||||
$cities = file_get_contents($path);
|
$cities = file_get_contents($path);
|
||||||
|
@ -55,7 +86,7 @@ class ImportCities extends Command
|
||||||
$buffer = [];
|
$buffer = [];
|
||||||
$count = 0;
|
$count = 0;
|
||||||
foreach ($cities as $city) {
|
foreach ($cities as $city) {
|
||||||
$country = $city->country == 'XK' ? 'Kosovo' : (new \League\ISO3166\ISO3166)->alpha2($city->country)['name'];
|
$country = $this->codeToCountry($city->country);
|
||||||
$buffer[] = ["name" => $city->name, "slug" => Str::slug($city->name), "country" => $country, "lat" => $city->lat, "long" => $city->lng];
|
$buffer[] = ["name" => $city->name, "slug" => Str::slug($city->name), "country" => $country, "lat" => $city->lat, "long" => $city->lng];
|
||||||
$count++;
|
$count++;
|
||||||
if ($count % $this->argument('chunk') == 0) {
|
if ($count % $this->argument('chunk') == 0) {
|
||||||
|
@ -75,4 +106,16 @@ class ImportCities extends Command
|
||||||
{
|
{
|
||||||
DB::table('places')->insert($buffer);
|
DB::table('places')->insert($buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function codeToCountry($code)
|
||||||
|
{
|
||||||
|
$countries = $this->countries;
|
||||||
|
if(isset($countries[$code])) {
|
||||||
|
return $countries[$code];
|
||||||
|
}
|
||||||
|
|
||||||
|
$country = (new \League\ISO3166\ISO3166)->alpha2($code);
|
||||||
|
$this->countries[$code] = $country['name'];
|
||||||
|
return $this->countries[$code];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue