Import/Core_geoip.php
[Pman.Core] / Import / Core_geoip.php
index 6dd10ce..30fae9d 100644 (file)
@@ -9,33 +9,7 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
     static $cli_opts = array();
     
     var $id_mapping = array();
-    
-    var $mask_lenght = array(
-        '100' => 268435456,
-        '101' => 134217728,
-        '102' => 67108864,
-        '103' => 33554432,
-        '104' => 16777216,
-        '105' => 8388608,
-        '106' => 4194304,
-        '107' => 2097152,
-        '108' => 1048576,
-        '109' => 524288,
-        '110' => 262144,
-        '111' => 131072,
-        '112' => 65536,
-        '113' => 131072,
-        '114' => 131072,
-        '115' => 131072,
-        '116' => 131072,
-        '117' => 131072,
-        '118' => 131072,
-        '119' => 131072,
-        '120' => 131072,
-        
-        
-    );
-    
+
     function getAuth()
     {
         $ff = HTML_FlexyFramework::get();
@@ -51,6 +25,10 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
         $this->get();
     }
     
+    var $processed = 0;
+    var $total = 0;
+    var $echo = '';
+    
     function get()
     {
         
@@ -63,16 +41,25 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
             $this->jerr('GeoLite2-City-Locations.csv OR GeoLite2-City-Blocks.csv does not exists?!');
         }
         
-//        $this->insertLocation($location);
+        $this->log("Insert location data start");
+        
+        $this->insertLocation($location);
+        
+        $this->log("Insert Block data start");
         
         $this->insertBlock($block);
+        
+        $this->jok("DONE");
     }
     
     function insertLocation($csv)
     {
         ini_set("auto_detect_line_endings", true);
         
+        
+        
         $fh = fopen($csv, 'r');
+        
         if (!$fh) {
             $this->jerr("invalid location file");
         }
@@ -86,6 +73,10 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
         
         $cols = false;
         
+        $this->processed = 0;
+        $this->total = count(file($csv));
+        $this->echo = '';
+        
         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
             if(!array_filter($n)){ // empty row
                 continue;
@@ -116,6 +107,10 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
             }
             
             $this->processLocation($row);
+            
+            $this->processed++;
+            
+            $this->processStatus();
         }
         
     }
@@ -175,7 +170,7 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
     
     
     function processLocation($row)
-    {
+    {   
         $continent = $this->processContinent($row['CONTINENT_CODE'], $row['CONTINENT_NAME']);
         
         $country = $this->processCountry($row['COUNTRY_ISO_CODE'], $row['COUNTRY_NAME'], $continent);
@@ -275,20 +270,67 @@ class Pman_Core_Import_Core_geoip extends Pman_Roo
     
     function processBlock($row)
     {
+        if(empty($this->id_mapping[$row['GEONAME_ID']])){
+            $this->log("Missing mapping for {$row['GEONAME_ID']}");
+            $this->log("IP : {$row['NETWORK_START_IP']}");
+            return;
+        }
+        
         $network_mapping = DB_DataObject::factory('core_geoip_network_mapping');
         
         $start_ip = array_pop(explode(":", $row['NETWORK_START_IP']));
         
         $network_mapping->setFrom(array(
-            
+            'start_ip' => $start_ip,
+            'mask_length' => pow(2, (128 - $row['NETWORK_MASK_LENGTH'])),
+            'city_id' => $this->id_mapping[$row['GEONAME_ID']]
         ));
         
+        if(!$network_mapping->find(true)){
+            $network_mapping->insert();
+        }
+        
+        $location = DB_DataObject::factory('core_geoip_location');
+        if(!$location->get('city_id', $network_mapping->city_id)){
+            $location->setFrom(array(
+                'latitude' => $row['LATITUDE'],
+                'longitude' => $row['LONGITUDE'],
+                'city_id' => $network_mapping->city_id
+            ));
+        }
+        
+        
+        if(!empty($row['POSTAL_CODE'])){
+            $city = DB_DataObject::factory('core_geoip_city');
+            if($city->get($network_mapping->city_id)){
+                return;
+            }
+
+            $oc = clone($city);
+            $city->postal_code = $row['POSTAL_CODE'];
+            
+            $city->update($oc);
+        }
+        
     }
     
-    
-    
-    function log($str)
+    function processStatus()
     {
-        echo "$str \n";
+        echo "\033[K"; // Erase to end of line:
+            
+        if (strlen($this->echo)) {
+            echo "\033[".strlen($this->echo)."D";    // Move $length characters backward
+        }
+
+        $this->echo = str_pad(ROUND(($this->processed / $this->total),2) * 100, 3, ' ', STR_PAD_LEFT) .
+            " % (" . str_pad(($this->processed), strlen($this->total), ' ', STR_PAD_LEFT) .
+            " / {$this->total})";
+
+
+        echo $this->echo;
+
+        if($this->processed == $this->total){
+            echo "\n";
+        }
     }
 }