|
@@ -8,6 +8,31 @@ use Illuminate\Support\Str;
|
|
|
|
|
|
class ImportCities extends Command
|
|
class ImportCities extends Command
|
|
{
|
|
{
|
|
|
|
+ /**
|
|
|
|
+ * The name and signature of the console command.
|
|
|
|
+ *
|
|
|
|
+ * @var string
|
|
|
|
+ */
|
|
|
|
+ protected $signature = 'import:cities {chunk=1000}';
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * The console command description.
|
|
|
|
+ *
|
|
|
|
+ * @var string
|
|
|
|
+ */
|
|
|
|
+ protected $description = 'Import Cities to database';
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Checksum of city dataset.
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+ const CHECKSUM = 'e203c0247538788b2a91166c7cf4b95f58291d998f514e9306d315aa72b09e48bfd3ddf310bf737afc4eefadca9083b8ff796c67796c6bd8e882a3d268bd16af';
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * List of shortened countries.
|
|
|
|
+ *
|
|
|
|
+ * @var array
|
|
|
|
+ */
|
|
protected $countries = [
|
|
protected $countries = [
|
|
'AE' => 'UAE',
|
|
'AE' => 'UAE',
|
|
'BA' => 'Bosnia-Herzegovina',
|
|
'BA' => 'Bosnia-Herzegovina',
|
|
@@ -31,18 +56,7 @@ class ImportCities extends Command
|
|
'VE' => 'Venezuela',
|
|
'VE' => 'Venezuela',
|
|
'XK' => 'Kosovo'
|
|
'XK' => 'Kosovo'
|
|
];
|
|
];
|
|
- /**
|
|
|
|
- * The name and signature of the console command.
|
|
|
|
- *
|
|
|
|
- * @var string
|
|
|
|
- */
|
|
|
|
- protected $signature = 'import:cities {chunk=1000}';
|
|
|
|
- /**
|
|
|
|
- * The console command description.
|
|
|
|
- *
|
|
|
|
- * @var string
|
|
|
|
- */
|
|
|
|
- protected $description = 'Import Cities to database';
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Create a new command instance.
|
|
* Create a new command instance.
|
|
*
|
|
*
|
|
@@ -52,6 +66,7 @@ class ImportCities extends Command
|
|
{
|
|
{
|
|
parent::__construct();
|
|
parent::__construct();
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Execute the console command.
|
|
* Execute the console command.
|
|
*
|
|
*
|
|
@@ -61,7 +76,7 @@ class ImportCities extends Command
|
|
{
|
|
{
|
|
$path = storage_path('app/cities.json');
|
|
$path = storage_path('app/cities.json');
|
|
|
|
|
|
- if(hash_file('sha512', $path) !== 'e203c0247538788b2a91166c7cf4b95f58291d998f514e9306d315aa72b09e48bfd3ddf310bf737afc4eefadca9083b8ff796c67796c6bd8e882a3d268bd16af') {
|
|
|
|
|
|
+ if(hash_file('sha512', $path) !== self::CHECKSUM) {
|
|
$this->error('Invalid or corrupt storage/app/cities.json data.');
|
|
$this->error('Invalid or corrupt storage/app/cities.json data.');
|
|
$this->line('');
|
|
$this->line('');
|
|
$this->info('Run the following command to fix:');
|
|
$this->info('Run the following command to fix:');
|
|
@@ -73,36 +88,57 @@ class ImportCities extends Command
|
|
$this->error('Missing storage/app/cities.json file!');
|
|
$this->error('Missing storage/app/cities.json file!');
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+
|
|
if (Place::count() > 0) {
|
|
if (Place::count() > 0) {
|
|
DB::table('places')->truncate();
|
|
DB::table('places')->truncate();
|
|
}
|
|
}
|
|
|
|
+
|
|
$this->info('Importing city data into database ...');
|
|
$this->info('Importing city data into database ...');
|
|
- $cities = file_get_contents($path);
|
|
|
|
- $cities = json_decode($cities);
|
|
|
|
|
|
+
|
|
|
|
+ $cities = json_decode(file_get_contents($path));
|
|
$cityCount = count($cities);
|
|
$cityCount = count($cities);
|
|
|
|
+
|
|
|
|
+ $this->line('');
|
|
$this->info("Found {$cityCount} cities to insert ...");
|
|
$this->info("Found {$cityCount} cities to insert ...");
|
|
|
|
+ $this->line('');
|
|
|
|
+
|
|
$bar = $this->output->createProgressBar($cityCount);
|
|
$bar = $this->output->createProgressBar($cityCount);
|
|
$bar->start();
|
|
$bar->start();
|
|
|
|
+
|
|
$buffer = [];
|
|
$buffer = [];
|
|
$count = 0;
|
|
$count = 0;
|
|
|
|
+
|
|
foreach ($cities as $city) {
|
|
foreach ($cities as $city) {
|
|
- $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" => $this->codeToCountry($city->country),
|
|
|
|
+ "lat" => $city->lat,
|
|
|
|
+ "long" => $city->lng
|
|
|
|
+ ];
|
|
|
|
+
|
|
$count++;
|
|
$count++;
|
|
|
|
+
|
|
if ($count % $this->argument('chunk') == 0) {
|
|
if ($count % $this->argument('chunk') == 0) {
|
|
- $this->insertBuffer($buffer, $count);
|
|
|
|
|
|
+ $this->insertBuffer($buffer);
|
|
$bar->advance(count($buffer));
|
|
$bar->advance(count($buffer));
|
|
$buffer = [];
|
|
$buffer = [];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- $this->insertBuffer($buffer, $count);
|
|
|
|
|
|
+ $this->insertBuffer($buffer);
|
|
|
|
+
|
|
$bar->advance(count($buffer));
|
|
$bar->advance(count($buffer));
|
|
|
|
+
|
|
$bar->finish();
|
|
$bar->finish();
|
|
- $this->info('Successfully imported ' . $count . ' entries.');
|
|
|
|
|
|
+
|
|
|
|
+ $this->line('');
|
|
|
|
+ $this->line('');
|
|
|
|
+ $this->info('Successfully imported ' . $cityCount . ' entries!');
|
|
|
|
+ $this->line('');
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- private function insertBuffer($buffer, $count)
|
|
|
|
|
|
+ private function insertBuffer($buffer)
|
|
{
|
|
{
|
|
DB::table('places')->insert($buffer);
|
|
DB::table('places')->insert($buffer);
|
|
}
|
|
}
|