ImportCities.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Place;
  5. use DB;
  6. use Illuminate\Support\Str;
  7. class ImportCities extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'import:cities {chunk=1000}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Import Cities to database';
  21. /**
  22. * Checksum of city dataset.
  23. *
  24. */
  25. const CHECKSUM = 'e203c0247538788b2a91166c7cf4b95f58291d998f514e9306d315aa72b09e48bfd3ddf310bf737afc4eefadca9083b8ff796c67796c6bd8e882a3d268bd16af';
  26. /**
  27. * List of shortened countries.
  28. *
  29. * @var array
  30. */
  31. protected $countries = [
  32. 'AE' => 'UAE',
  33. 'BA' => 'Bosnia-Herzegovina',
  34. 'BO' => 'Bolivia',
  35. 'CD' => 'Democratic Republic of Congo',
  36. 'CG' => 'Republic of Congo',
  37. 'FM' => 'Micronesia',
  38. 'GB' => 'United Kingdom',
  39. 'IR' => 'Iran',
  40. 'KP' => 'DRPK',
  41. 'KR' => 'South Korea',
  42. 'LA' => 'Laos',
  43. 'MD' => 'Moldova',
  44. 'PS' => 'Palestine',
  45. 'RU' => 'Russia',
  46. 'SH' => 'Saint Helena',
  47. 'SY' => 'Syria',
  48. 'TW' => 'Taiwan',
  49. 'TZ' => 'Tanzania',
  50. 'US' => 'USA',
  51. 'VE' => 'Venezuela',
  52. 'XK' => 'Kosovo'
  53. ];
  54. /**
  55. * Create a new command instance.
  56. *
  57. * @return void
  58. */
  59. public function __construct()
  60. {
  61. parent::__construct();
  62. ini_set('memory_limit', '256M');
  63. }
  64. /**
  65. * Execute the console command.
  66. *
  67. * @return mixed
  68. */
  69. public function handle()
  70. {
  71. $path = storage_path('app/cities.json');
  72. if(hash_file('sha512', $path) !== self::CHECKSUM) {
  73. $this->error('Invalid or corrupt storage/app/cities.json data.');
  74. $this->line('');
  75. $this->info('Run the following command to fix:');
  76. $this->info('git checkout storage/app/cities.json');
  77. return;
  78. }
  79. if (!is_file($path)) {
  80. $this->error('Missing storage/app/cities.json file!');
  81. return;
  82. }
  83. if (Place::count() > 0) {
  84. DB::table('places')->truncate();
  85. }
  86. $this->info('Importing city data into database ...');
  87. $cities = json_decode(file_get_contents($path));
  88. $cityCount = count($cities);
  89. $this->line('');
  90. $this->info("Found {$cityCount} cities to insert ...");
  91. $this->line('');
  92. $bar = $this->output->createProgressBar($cityCount);
  93. $bar->start();
  94. $buffer = [];
  95. $count = 0;
  96. foreach ($cities as $city) {
  97. $buffer[] = [
  98. "name" => $city->name,
  99. "slug" => Str::slug($city->name),
  100. "country" => $this->codeToCountry($city->country),
  101. "lat" => $city->lat,
  102. "long" => $city->lng
  103. ];
  104. $count++;
  105. if ($count % $this->argument('chunk') == 0) {
  106. $this->insertBuffer($buffer);
  107. $bar->advance(count($buffer));
  108. $buffer = [];
  109. }
  110. }
  111. $this->insertBuffer($buffer);
  112. $bar->advance(count($buffer));
  113. $bar->finish();
  114. $this->line('');
  115. $this->line('');
  116. $this->info('Successfully imported ' . $cityCount . ' entries!');
  117. $this->line('');
  118. return;
  119. }
  120. private function insertBuffer($buffer)
  121. {
  122. DB::table('places')->insert($buffer);
  123. }
  124. private function codeToCountry($code)
  125. {
  126. $countries = $this->countries;
  127. if(isset($countries[$code])) {
  128. return $countries[$code];
  129. }
  130. $country = (new \League\ISO3166\ISO3166)->alpha2($code);
  131. $this->countries[$code] = $country['name'];
  132. return $this->countries[$code];
  133. }
  134. }