Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.02 KB | None | 0 0
  1.    //This function
  2.     public function importCities() {
  3.         //England
  4.         $en_path = Yii::getAlias('@app') . '/data/cities/en_test/';
  5.         $this->importSplit([
  6.             'path' => $en_path,
  7.             'postalcode' => 0,
  8.             'country' => 'GB',
  9.             'name' => 7,
  10.             'language' => 'en'
  11.         ]);
  12.     }
  13.  
  14.     /**
  15.      *
  16.      * @param array[string] $params
  17.      * @param string $path
  18.      */
  19.     public function importSplit($params) {
  20.         $searchPath = $params['path'];
  21.         echo "Loading Cities from Split Files in $searchPath ... \n";
  22.         $donePath = $searchPath . 'done/';
  23.         $counter = 0;
  24.         $it = new \FilesystemIterator($searchPath);
  25.         foreach ($it as $fileinfo) {
  26.             list($usec, $sec) = explode(" ", microtime());
  27.             $time_start = ((float) $usec + (float) $sec);
  28.  
  29.             $current = $it->current();
  30.             $fn = $fileinfo->getFilename();
  31.             if ($fn != '.gitkeep' && $fn != 'done' && $fn != 'hold') {
  32.  
  33.                 $this->parseFile($searchPath . $fn, $params);
  34.                 //Move file to done on success
  35.                 $doneFile = str_replace('/pub_', '/done/pub_', $current);
  36.                 rename($current, $doneFile);
  37.                 $counter++;
  38.             }
  39.             list($usec, $sec) = explode(" ", microtime());
  40.             $time_end = ((float) $usec + (float) $sec);
  41.             $time = $time_end - $time_start;
  42.             $timeString = (string) $time;
  43.             echo 'Completed file ' . $counter . ' having filename  ' . $current . ' in ' . round($timeString, 3) . ' seconds' . "\n";
  44.         }
  45.     }
  46.  
  47.     private function parseFile($fn, $params) {
  48.         echo "parsing $fn" . "\n";
  49.         $file = fopen($fn, "r");
  50.         while (!feof($file)) {
  51.             $data = fgetcsv($file);
  52.             //Check if the line contains data
  53.             if (isset($data[0])) {
  54.                 $lparams = [];
  55.                 //Append Parameters (Locally)
  56.                 $lparams['country'] = $params['country'];
  57.                 $lparams['city'] = $data[$params['name']];
  58.                 $lparams['postcode'] = $data[$params['postalcode']];
  59.                 $lparams['language'] = 'en';
  60.                 if (isset($params['language'])) {
  61.                     $lparams['language'] = $params['language'];
  62.                 }
  63.  
  64.                 if ($lparams['city'] != '') {
  65.                     $this->insertTupple($lparams);
  66.                 }
  67.             } else {
  68.                 break;
  69.             }
  70.         }
  71.  
  72.  
  73.         fclose($file);
  74.     }
  75.  
  76.     private function insertTupple($params) {
  77.         $cityId = $this->insertCity($params);
  78.  
  79.         $this->insertLocation($cityId, $params);
  80.     }
  81.  
  82.     private function insertLocation($cityId, $params) {
  83.         Yii::$app->db->createCommand()->insert('location', [
  84.             'postcode' => $params['postcode'],
  85.             'country' => $params['country'],
  86.             'city' => $cityId
  87.         ])->execute();
  88.     }
  89.  
  90.     private function insertCity($params) {
  91.         $id = $this->getCityID($params['city'], $params['language']);
  92.         if (!$id) {
  93.             Yii::$app->db->createCommand()->insert('city', [
  94.                 'name' => $params['city'],
  95.                 'language' => $params['language']
  96.             ])->execute();
  97.             $id = $this->getCityID($params['city'], $params['language']);
  98.         }
  99.         return $id;
  100.     }
  101.  
  102.     /**
  103.      *
  104.      * @param type $city
  105.      * @param type $language
  106.      * @return mixed return false if not found, else returns integer value for
  107.      * city
  108.      */
  109.     private function getCityID($city, $language) {
  110.         //Initial return value
  111.         $id = false;
  112.         $id = Yii::$app->db->createCommand("SELECT id FROM city WHERE language = '$language' AND name = '$city'")->queryScalar();
  113.         if (!$id) {
  114.             $id = Yii::$app->db->createCommand("SELECT source FROM city_translation WHERE language = '$language' AND name = '$city'")->queryScalar();
  115.         }
  116.         return $id;
  117.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement