isAvailable()) { if ($displayNotes) { Log::info("[note] The GeoIP PECL extension is not installed."); } $provider = null; } else { $workingOrError = $provider->isWorking(); if ($workingOrError !== true) { if ($displayNotes) { Log::info("[note] The GeoIP PECL extension is broken: $workingOrError"); } if (Common::isPhpCliMode()) { Log::info("[note] Make sure your command line PHP is configured to use the PECL extension."); } $provider = null; } } // use php api if pecl extension cannot be used if (is_null($provider)) { if ($displayNotes) { Log::info("[note] Falling back to PHP API. This may become too slow for you. If so, you can read this link on how to install the PECL extension: http://piwik.org/faq/how-to/#faq_164"); } $provider = new Php(); if (!$provider->isAvailable()) { if ($displayNotes) { Log::info("[note] The GeoIP PHP API is not available. This means you do not have a GeoIP location database in your ./misc directory. The database must be named either GeoIP.dat or GeoIPCity.dat based on the type of database it is."); } $provider = null; } else { $workingOrError = $provider->isWorking(); if ($workingOrError !== true) { if ($displayNotes) { Log::info("[note] The GeoIP PHP API is broken: $workingOrError"); } $provider = null; } } } if (is_null($provider)) { geoipUpdateError("\n[error] There is no location provider that can be used with this script. Only the GeoIP PECL module or the GeoIP PHP API can be used at present. Please install and configure one of these first."); } $info = $provider->getInfo(); if ($displayNotes) { Log::info("[note] Found working provider: {$info['id']}"); } // perform update $logVisitFieldsToUpdate = array('location_country' => LocationProvider::COUNTRY_CODE_KEY, 'location_region' => LocationProvider::REGION_CODE_KEY, 'location_city' => LocationProvider::CITY_NAME_KEY, 'location_latitude' => LocationProvider::LATITUDE_KEY, 'location_longitude' => LocationProvider::LONGITUDE_KEY); if ($displayNotes) { Log::info("\n$count rows to process in " . Common::prefixTable('log_visit') . " and " . Common::prefixTable('log_conversion') . "..."); } flush(); for (; $start < $end; $start += $limit) { $rows = Db::fetchAll("SELECT idvisit, location_ip, " . implode(',', array_keys($logVisitFieldsToUpdate)) . " FROM " . Common::prefixTable('log_visit') . " LIMIT $start, $limit"); if (!count($rows)) { continue; } foreach ($rows as $i => $row) { $fieldsToSet = array(); foreach ($logVisitFieldsToUpdate as $field => $ignore) { if (empty($fieldsToSet[$field])) { $fieldsToSet[] = $field; } } // skip if it already has a location if (empty($fieldsToSet)) { continue; } $ip = IP::N2P($row['location_ip']); $location = $provider->getLocation(array('ip' => $ip)); if (!empty($location[LocationProvider::COUNTRY_CODE_KEY])) { $location[LocationProvider::COUNTRY_CODE_KEY] = strtolower($location[LocationProvider::COUNTRY_CODE_KEY]); } $row['location_country'] = strtolower($row['location_country']); $columnsToSet = array(); $bind = array(); foreach ($logVisitFieldsToUpdate as $column => $locationKey) { if (!empty($location[$locationKey]) && $location[$locationKey] != $row[$column] ) { $columnsToSet[] = $column . ' = ?'; $bind[] = $location[$locationKey]; } } if (empty($columnsToSet)) { continue; } $bind[] = $row['idvisit']; // update log_visit $sql = "UPDATE " . Common::prefixTable('log_visit') . " SET " . implode(', ', $columnsToSet) . " WHERE idvisit = ?"; Db::query($sql, $bind); // update log_conversion $sql = "UPDATE " . Common::prefixTable('log_conversion') . " SET " . implode(', ', $columnsToSet) . " WHERE idvisit = ?"; Db::query($sql, $bind); } Log::info(round($start * 100 / $count) . "% done..."); flush(); } if ($start >= $count) { Log::info("100% done!"); Log::info(""); Log::info("[note] Now that you've geolocated your old visits, you need to force your reports to be re-processed. See this FAQ entry: http://piwik.org/faq/how-to/#faq_59"); }