This repository was archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathImportCommand.php
More file actions
345 lines (286 loc) · 8.37 KB
/
ImportCommand.php
File metadata and controls
345 lines (286 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php namespace Ipalaus\Geonames\Commands;
use ZipArchive;
use ErrorException;
use RuntimeException;
use Ipalaus\Geonames\Importer;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Process\Process;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ImportCommand extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'geonames:import';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import and seed the geonames database with fresh records.';
/**
* Importer instance.
*
* @var \Ipalaus\Geonames\Importer
*/
protected $importer;
/**
* Filesystem implementation.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $filesystem;
/**
* File archive instance.
*
* @var \ZipArchive
*/
protected $archive;
/**
* Configuration options.
*
* @var array
*/
protected $config = array();
/**
* Create a new console command instance.
*
* @param \Ipalaus\Geonames\Importer $importer
* @param \Illuminate\Filesystem\Filesystem $filesystem
* @return void
*/
public function __construct(Importer $importer, Filesystem $filesystem, array $config)
{
$this->importer = $importer;
$this->filesystem = $filesystem;
$this->config = $config;
$this->archive = new ZipArchive;
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$country = $this->input->getOption('country');
$development = $this->input->getOption('development');
$essentials = $this->input->getOption('essentials');
$fetchOnly = $this->input->getOption('fetch-only');
$wipeFiles = $this->input->getOption('wipe-files');
// i'm sorry but you can't have both :(
if ($development and ! is_null($country)) {
throw new RuntimeException("You have to select between a country or a development version of GeoNames.");
}
// set a development names, lighter download
$development and $this->setDevelopment();
// set a specific country names
$country and $this->setCountry($country);
// path to download our files
$path = $this->getPath();
// if we forced to wipe files, we will delete the directory
$wipeFiles and $this->filesystem->deleteDirectory($path);
// create the directory if it doesn't exists
if ( ! $this->filesystem->isDirectory($path)) {
$this->filesystem->makeDirectory($path, 0755, true);
}
$files = $this->getFiles();
// loop all the files that we need to donwload
foreach ($files as $file) {
$filename = basename($file);
if ($this->fileExists($path, $filename)) {
$this->line("<info>File exists:</info> $filename");
continue;
}
$this->line("<info>Downloading:</info> $file");
$this->downloadFile($file, $path, $filename);
// if the file is ends with zip, we will try to unzip it and remove the zip file
if (substr($filename, -strlen('zip')) === "zip") {
$this->line("<info>Unzip:</info> $filename");
$filename = $this->extractZip($path, $filename);
}
}
// if we only want to fetch files, we must stop the execution of the command
if ($fetchOnly) {
$this->line('<info>Files fetched.</info>');
return;
}
$this->line("<info>It is time to seed the database. This may take 'a while'...</info>");
// finally seed the common seeders
$this->seedCommand('ContinentsTableSeeder');
$this->seedCommand('CountriesTableSeeder');
if (! $essentials)
{
$this->seedCommand('AdminDivionsTableSeeder');
$this->seedCommand('AdminSubdivionsTableSeeder');
$this->seedCommand('HierarchiesTableSeeder');
$this->seedCommand('FeaturesTableSeeder');
$this->seedCommand('TimezonesTableSeeder');
}
// depending if we run a country, development or plain names we will run
// different seeders. Note that the langauge codes file is only
// available with the allCountries.zip file.
if ($development) {
$this->seedCommand('DevelopmentNamesTableSeeder');
} elseif ($country) {
$this->seedCommand('CountryNamesTableSeeder', '--country=' . $country);
} else {
$this->seedCommand('AlternateNamesTableSeeder');
$this->seedCommand('LanguageCodesTableSeeder');
$this->seedCommand('NamesTableSeeder');
}
}
/**
* Download a file from a remote URL to a given path.
*
* @param string $url
* @param string $path
* @return void
*/
protected function downloadFile($url, $path, $filename)
{
if ( ! $fp = fopen ($path . '/' . $filename, 'w+')) {
throw new RuntimeException('Impossible to write to path: ' . $path);
}
// curl looks like shit but whatever
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
/**
* Given a zip archive, extract a the file and remove the original.
*
* @param string $path
* @param string $filename
* @return string
*/
protected function extractZip($path, $filename)
{
$this->archive->open($path . '/' . $filename);
$this->archive->extractTo($path . '/');
$this->archive->close();
$this->filesystem->delete($path . '/' . $filename);
return str_replace('.zip', '.txt', $filename);
}
/**
* Checks if a file already exists on a path. If the file contains .zip in
* the name we will also check for matches with .txt.
*
* @param string $path
* @param string $filename
* @return bool
*/
protected function fileExists($path, $filename)
{
if (file_exists($path . '/' . $filename)) {
return true;
}
if (file_exists($path . '/' . str_replace('.zip', '.txt', $filename))) {
return true;
}
return false;
}
/**
* Run a seed coman in a separate process.
*
* @param string $class
* @return void
*/
protected function seedCommand($class, $extra = '')
{
$string = 'php artisan geonames:seed --class="Ipalaus\Geonames\Seeders\%s" --path="%s" ' . $extra;
$command = sprintf($string, $class, $this->getPath());
$process = $this->makeProcess($command);
$this->runProcess($process);
$this->line("<info>Seeded:</info> $class");
}
/**
* Create a process with the given command.
*
* @param string $command
* @return \Symfony\Component\Process\Process
*/
protected function makeProcess($command)
{
return new Process($command, $this->laravel['path.base'], null, null, 0);
}
/**
* Run a given process.
*
* @param \Symfony\Component\Process\Process $process
* @return \Symfony\Component\Process\Process
*/
public function runProcess(Process $process)
{
$process->run();
if ( ! $process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
return $process;
}
/**
* Return the working path.
*
* @return string
*/
protected function getPath()
{
return $this->config['path'];
}
/**
* Get the files to download.
*
* @return array
*/
public function getFiles()
{
return $this->config['files'];
}
/**
* Sets the names file for a ligher version for development. We also ignore
* the alternate names.
*
* @return void
*/
protected function setDevelopment()
{
$this->config['files']['names'] = $this->config['development'];
unset($this->config['files']['alternate']);
}
/**
* Sets the name file to a specific country.
*
* @param string $country
* @return void
*/
protected function setCountry($country)
{
if (strlen($country) > 2) {
throw new RuntimeException('Country format must be in ISO Alpha 2 code.');
}
$this->config['files']['names'] = sprintf($this->config['wildcard'], strtoupper($country));
unset($this->config['files']['alternate']);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('country', null, InputOption::VALUE_REQUIRED, 'Downloads just the specific country.'),
array('development', null, InputOption::VALUE_NONE, 'Downloads an smaller version of names (~10MB).'),
array('essentials', null, InputOption::VALUE_NONE, 'Only seed the names, countries and continents tables.'),
array('fetch-only', null, InputOption::VALUE_NONE, 'Just download the files.'),
array('wipe-files', null, InputOption::VALUE_NONE, 'Wipe old downloaded files and fetch new ones.'),
);
}
}