Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This function
- public function importCities() {
- //England
- $en_path = Yii::getAlias('@app') . '/data/cities/en_test/';
- $this->importSplit([
- 'path' => $en_path,
- 'postalcode' => 0,
- 'country' => 'GB',
- 'name' => 7,
- 'language' => 'en'
- ]);
- }
- /**
- *
- * @param array[string] $params
- * @param string $path
- */
- public function importSplit($params) {
- $searchPath = $params['path'];
- echo "Loading Cities from Split Files in $searchPath ... \n";
- $donePath = $searchPath . 'done/';
- $counter = 0;
- $it = new \FilesystemIterator($searchPath);
- foreach ($it as $fileinfo) {
- list($usec, $sec) = explode(" ", microtime());
- $time_start = ((float) $usec + (float) $sec);
- $current = $it->current();
- $fn = $fileinfo->getFilename();
- if ($fn != '.gitkeep' && $fn != 'done' && $fn != 'hold') {
- $this->parseFile($searchPath . $fn, $params);
- //Move file to done on success
- $doneFile = str_replace('/pub_', '/done/pub_', $current);
- rename($current, $doneFile);
- $counter++;
- }
- list($usec, $sec) = explode(" ", microtime());
- $time_end = ((float) $usec + (float) $sec);
- $time = $time_end - $time_start;
- $timeString = (string) $time;
- echo 'Completed file ' . $counter . ' having filename ' . $current . ' in ' . round($timeString, 3) . ' seconds' . "\n";
- }
- }
- private function parseFile($fn, $params) {
- echo "parsing $fn" . "\n";
- $file = fopen($fn, "r");
- while (!feof($file)) {
- $data = fgetcsv($file);
- //Check if the line contains data
- if (isset($data[0])) {
- $lparams = [];
- //Append Parameters (Locally)
- $lparams['country'] = $params['country'];
- $lparams['city'] = $data[$params['name']];
- $lparams['postcode'] = $data[$params['postalcode']];
- $lparams['language'] = 'en';
- if (isset($params['language'])) {
- $lparams['language'] = $params['language'];
- }
- if ($lparams['city'] != '') {
- $this->insertTupple($lparams);
- }
- } else {
- break;
- }
- }
- fclose($file);
- }
- private function insertTupple($params) {
- $cityId = $this->insertCity($params);
- $this->insertLocation($cityId, $params);
- }
- private function insertLocation($cityId, $params) {
- Yii::$app->db->createCommand()->insert('location', [
- 'postcode' => $params['postcode'],
- 'country' => $params['country'],
- 'city' => $cityId
- ])->execute();
- }
- private function insertCity($params) {
- $id = $this->getCityID($params['city'], $params['language']);
- if (!$id) {
- Yii::$app->db->createCommand()->insert('city', [
- 'name' => $params['city'],
- 'language' => $params['language']
- ])->execute();
- $id = $this->getCityID($params['city'], $params['language']);
- }
- return $id;
- }
- /**
- *
- * @param type $city
- * @param type $language
- * @return mixed return false if not found, else returns integer value for
- * city
- */
- private function getCityID($city, $language) {
- //Initial return value
- $id = false;
- $id = Yii::$app->db->createCommand("SELECT id FROM city WHERE language = '$language' AND name = '$city'")->queryScalar();
- if (!$id) {
- $id = Yii::$app->db->createCommand("SELECT source FROM city_translation WHERE language = '$language' AND name = '$city'")->queryScalar();
- }
- return $id;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement