Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94f4f9d5e6 | ||
|
|
dac9e94304 | ||
|
|
51e2f84d07 | ||
|
|
42efc47796 | ||
|
|
36624ae87a | ||
|
|
08697f2e7b | ||
|
|
2f9746c32b | ||
|
|
920a92f352 | ||
|
|
0d90c7a78e | ||
|
|
d52a0b057e | ||
|
|
5c4dd5b1e5 | ||
|
|
942ba4b8f5 | ||
|
|
a9fa7194aa | ||
|
|
13fbcae21c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
_site
|
||||
.gh-pages
|
||||
.idea
|
||||
GeoLite2-City.mmdb
|
||||
build
|
||||
composer.lock
|
||||
composer.phar
|
||||
|
||||
27
CHANGELOG.md
27
CHANGELOG.md
@@ -1,12 +1,37 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
0.9.0 (2014-09-15)
|
||||
------------------
|
||||
|
||||
* IMPORTANT: The deprecated `omni()` and `cityIspOrg()` methods have been
|
||||
removed from `GeoIp2\WebService\Client`.
|
||||
|
||||
0.8.1 (2014-09-12)
|
||||
------------------
|
||||
|
||||
* The check added to the `GeoIP2\Database\Reader` lookup methods in 0.8.0 did
|
||||
not work with the GeoIP2 City Database Subset by Continent with World
|
||||
Countries. This has been fixed. Fixes GitHub issue #23.
|
||||
|
||||
0.8.0 (2014-09-10)
|
||||
------------------
|
||||
|
||||
* The `GeoIp2\Database\Reader` lookup methods (e.g., `city()`, `isp()`) now
|
||||
throw a `BadMethodCallException` if they are used with a database that
|
||||
does not match the method. In particular, doing a `city()` lookup on a
|
||||
GeoIP2 Country database will result in an exception, and vice versa.
|
||||
* A `metadata()` method has been added to the `GeoIP2\Database\Reader` class.
|
||||
This returns a `MaxMind\Db\Reader\Metadata` class with information about the
|
||||
database.
|
||||
* The name attribute was missing from the RepresentedCountry class.
|
||||
|
||||
0.7.0 (2014-07-22)
|
||||
------------------
|
||||
|
||||
* The web service client API has been updated for the v2.1 release of the web
|
||||
service. In particular, the `cityIspOrg` and `omni` methods on
|
||||
`GeoIP2\WebService\Client` should be considered deprecated. The `city`
|
||||
`GeoIp2\WebService\Client` should be considered deprecated. The `city`
|
||||
method now provides all of the data formerly provided by `cityIspOrg`, and
|
||||
the `omni` method has been replaced by the `insights` method.
|
||||
* Support was added for GeoIP2 Connection Type, Domain and ISP databases.
|
||||
|
||||
@@ -26,7 +26,7 @@ You should now have the file `composer.phar` in your project directory.
|
||||
Run in your project root:
|
||||
|
||||
```
|
||||
php composer.phar require geoip2/geoip2:~0.7.0
|
||||
php composer.phar require geoip2/geoip2:~0.9.0
|
||||
```
|
||||
|
||||
You should now have the files `composer.json` and `composer.lock` as well as
|
||||
@@ -293,6 +293,9 @@ This library works and is tested with HHVM.
|
||||
This library also relies on the [Guzzle HTTP client](http://guzzlephp.org/)
|
||||
and the [MaxMind DB Reader](https://github.com/maxmind/MaxMind-DB-Reader-php).
|
||||
|
||||
If you are using PHP 5.3 with an autoloader besides Composer, you must load
|
||||
`JsonSerializable.php` in the `compat` directory.
|
||||
|
||||
## Contributing ##
|
||||
|
||||
Patches and pull requests are encouraged. All code should follow the
|
||||
|
||||
26
bin/benchmark.php
Executable file
26
bin/benchmark.php
Executable file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env php
|
||||
|
||||
<?php
|
||||
|
||||
require_once '../vendor/autoload.php';
|
||||
|
||||
use GeoIp2\Database\Reader;
|
||||
use GeoIp2\Exception\AddressNotFoundException;
|
||||
|
||||
$reader = new Reader('GeoLite2-City.mmdb');
|
||||
$count = 40000;
|
||||
$startTime = microtime(true);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$ip = long2ip(rand(0, pow(2, 32) -1));
|
||||
try {
|
||||
$t = $reader->city($ip);
|
||||
} catch (AddressNotFoundException $e) {
|
||||
}
|
||||
if ($i % 1000 == 0) {
|
||||
print($i . ' ' . $ip . "\n");
|
||||
}
|
||||
}
|
||||
$endTime = microtime(true);
|
||||
|
||||
$duration = $endTime - $startTime;
|
||||
print('Requests per second: ' . $count / $duration . "\n");
|
||||
@@ -27,7 +27,7 @@ if [ -n "$(git status --porcelain)" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
apigen --quiet --download --title "GeoIP2 PHP API $TAG" --source ../src --destination doc/$TAG
|
||||
../vendor/bin/apigen --quiet --download --title "GeoIP2 PHP API $TAG" --source ../src --destination doc/$TAG
|
||||
|
||||
PAGE=index.md
|
||||
cat <<EOF > $PAGE
|
||||
|
||||
@@ -14,10 +14,12 @@
|
||||
],
|
||||
"require": {
|
||||
"guzzle/guzzle": "3.*",
|
||||
"maxmind-db/reader": "~0.3.1",
|
||||
"maxmind-db/reader": "~0.3.2",
|
||||
"php": ">=5.3.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"apigen/apigen": "~2.8.0",
|
||||
"nette/nette": "~2.1.3",
|
||||
"phpunit/phpunit": "4.1.*",
|
||||
"satooshi/php-coveralls": "dev-master"
|
||||
},
|
||||
|
||||
Submodule maxmind-db updated: c57c2ccf4a...1d28147617
@@ -71,7 +71,7 @@ class Reader implements ProviderInterface
|
||||
*/
|
||||
public function city($ipAddress)
|
||||
{
|
||||
return $this->modelFor('City', $ipAddress);
|
||||
return $this->modelFor('City', 'City', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,65 +88,39 @@ class Reader implements ProviderInterface
|
||||
*/
|
||||
public function country($ipAddress)
|
||||
{
|
||||
return $this->modelFor('Country', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns a GeoIP2 City model.
|
||||
*
|
||||
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||
*
|
||||
* @return \GeoIp2\Model\City
|
||||
*
|
||||
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
|
||||
* not in the database.
|
||||
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
||||
* is corrupt or invalid
|
||||
*
|
||||
* @deprecated deprecated since version 0.7.0
|
||||
*/
|
||||
public function cityIspOrg($ipAddress)
|
||||
{
|
||||
return $this->modelFor('City', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns a GeoIP2 Insights model.
|
||||
*
|
||||
* @param string $ipAddress IPv4 or IPv6 address as a string.
|
||||
*
|
||||
* @return \GeoIp2\Model\Insights
|
||||
*
|
||||
* @throws \GeoIp2\Exception\AddressNotFoundException if the address is
|
||||
* not in the database.
|
||||
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
||||
* is corrupt or invalid
|
||||
*
|
||||
* @deprecated deprecated since version 0.7.0
|
||||
*/
|
||||
public function omni($ipAddress)
|
||||
{
|
||||
return $this->modelFor('Insights', $ipAddress);
|
||||
return $this->modelFor('Country', 'Country', $ipAddress);
|
||||
}
|
||||
|
||||
public function connectionType($ipAddress)
|
||||
{
|
||||
return $this->flatModelFor('ConnectionType', $ipAddress);
|
||||
return $this->flatModelFor(
|
||||
'ConnectionType',
|
||||
'GeoIP2-Connection-Type',
|
||||
$ipAddress
|
||||
);
|
||||
}
|
||||
|
||||
public function domain($ipAddress)
|
||||
{
|
||||
return $this->flatModelFor('Domain', $ipAddress);
|
||||
return $this->flatModelFor(
|
||||
'Domain',
|
||||
'GeoIP2-Domain',
|
||||
$ipAddress
|
||||
);
|
||||
}
|
||||
|
||||
public function isp($ipAddress)
|
||||
{
|
||||
return $this->flatModelFor('Isp', $ipAddress);
|
||||
return $this->flatModelFor(
|
||||
'Isp',
|
||||
'GeoIP2-ISP',
|
||||
$ipAddress
|
||||
);
|
||||
}
|
||||
|
||||
private function modelFor($class, $ipAddress)
|
||||
private function modelFor($class, $type, $ipAddress)
|
||||
{
|
||||
$record = $this->getRecord($ipAddress);
|
||||
$record = $this->getRecord($class, $type, $ipAddress);
|
||||
|
||||
$record['traits']['ip_address'] = $ipAddress;
|
||||
$class = "GeoIp2\\Model\\" . $class;
|
||||
@@ -154,9 +128,9 @@ class Reader implements ProviderInterface
|
||||
return new $class($record, $this->locales);
|
||||
}
|
||||
|
||||
private function flatModelFor($class, $ipAddress)
|
||||
private function flatModelFor($class, $type, $ipAddress)
|
||||
{
|
||||
$record = $this->getRecord($ipAddress);
|
||||
$record = $this->getRecord($class, $type, $ipAddress);
|
||||
|
||||
$record['ip_address'] = $ipAddress;
|
||||
$class = "GeoIp2\\Model\\" . $class;
|
||||
@@ -164,8 +138,15 @@ class Reader implements ProviderInterface
|
||||
return new $class($record);
|
||||
}
|
||||
|
||||
private function getRecord($ipAddress)
|
||||
private function getRecord($class, $type, $ipAddress)
|
||||
{
|
||||
if (strpos($this->metadata()->databaseType, $type) === false) {
|
||||
$method = lcfirst($class);
|
||||
throw new \BadMethodCallException(
|
||||
"The $method method cannot be used to open a "
|
||||
. $this->metadata()->databaseType . " database"
|
||||
);
|
||||
}
|
||||
$record = $this->dbReader->get($ipAddress);
|
||||
if ($record === null) {
|
||||
throw new AddressNotFoundException(
|
||||
@@ -175,6 +156,16 @@ class Reader implements ProviderInterface
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException if arguments are passed to the method.
|
||||
* @throws \BadMethodCallException if the database has been closed.
|
||||
* @return \MaxMind\Db\Reader\Metadata object for the database.
|
||||
*/
|
||||
public function metadata()
|
||||
{
|
||||
return $this->dbReader->metadata();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the GeoIP2 database and returns the resources to the system.
|
||||
*/
|
||||
|
||||
@@ -17,22 +17,4 @@ interface ProviderInterface
|
||||
* @return \GeoIp2\Model\City A City model for the requested IP address.
|
||||
*/
|
||||
public function city($ipAddress);
|
||||
|
||||
/**
|
||||
* @param ipAddress
|
||||
* IPv4 or IPv6 address to lookup.
|
||||
* @return \GeoIp2\Model\City A City model for the requested IP address.
|
||||
*
|
||||
* @deprecated deprecated since version 0.7.0
|
||||
*/
|
||||
public function cityIspOrg($ipAddress);
|
||||
|
||||
/**
|
||||
* @param ipAddress
|
||||
* IPv4 or IPv6 address to lookup.
|
||||
* @return \GeoIp2\Model\Insights An Insights model for the requested IP address.
|
||||
*
|
||||
* @deprecated deprecated since version 0.7.0
|
||||
*/
|
||||
public function omni($ipAddress);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ class RepresentedCountry extends Country
|
||||
'confidence',
|
||||
'geonameId',
|
||||
'isoCode',
|
||||
'namespace',
|
||||
'names',
|
||||
'type'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -111,40 +111,6 @@ class Client implements ProviderInterface
|
||||
return $this->responseFor('city', 'City', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calls the GeoIP2 Precision: City endpoint.
|
||||
*
|
||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||
* address is provided, the address that the web service is called
|
||||
* from will be used.
|
||||
*
|
||||
* @return \GeoIp2\Model\City
|
||||
*
|
||||
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
|
||||
* provided is not in our database (e.g., a private address).
|
||||
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||
* with the user ID or license key that you provided.
|
||||
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||
* of queries.
|
||||
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
||||
* received by the web service but is invalid for some other reason.
|
||||
* This may indicate an issue with this API. Please report the error to
|
||||
* MaxMind.
|
||||
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
|
||||
* code or message was returned. This could indicate a problem with the
|
||||
* connection between your server and the web service or that the web
|
||||
* service returned an invalid document or 500 error code.
|
||||
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
|
||||
* class to the above exceptions. It will be thrown directly if a 200
|
||||
* status code is returned but the body is invalid.
|
||||
*
|
||||
* @deprecated deprecated since version 0.7.0
|
||||
*/
|
||||
public function cityIspOrg($ipAddress = 'me')
|
||||
{
|
||||
return $this->city($ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calls the GeoIP2 Precision: Country endpoint.
|
||||
*
|
||||
@@ -203,48 +169,12 @@ class Client implements ProviderInterface
|
||||
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
|
||||
* class to the above exceptions. It will be thrown directly if a 200
|
||||
* status code is returned but the body is invalid.
|
||||
*
|
||||
* @deprecated deprecated since version 0.7.0
|
||||
*/
|
||||
public function insights($ipAddress = 'me')
|
||||
{
|
||||
return $this->responseFor('insights', 'Insights', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calls the GeoIP2 Precision: Insights (prev. Omni) endpoint.
|
||||
*
|
||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||
* address is provided, the address that the web service is called
|
||||
* from will be used.
|
||||
*
|
||||
* @return \GeoIp2\Model\Insights
|
||||
*
|
||||
* @throws \GeoIp2\Exception\AddressNotFoundException if the address you
|
||||
* provided is not in our database (e.g., a private address).
|
||||
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||
* with the user ID or license key that you provided.
|
||||
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||
* of queries.
|
||||
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
||||
* received by the web service but is invalid for some other reason.
|
||||
* This may indicate an issue with this API. Please report the error to
|
||||
* MaxMind.
|
||||
* @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
|
||||
* code or message was returned. This could indicate a problem with the
|
||||
* connection between your server and the web service or that the web
|
||||
* service returned an invalid document or 500 error code.
|
||||
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
|
||||
* class to the above exceptions. It will be thrown directly if a 200
|
||||
* status code is returned but the body is invalid.
|
||||
*
|
||||
* @deprecated deprecated since version 0.7.0
|
||||
*/
|
||||
public function omni($ipAddress = 'me')
|
||||
{
|
||||
return $this->insights($ipAddress);
|
||||
}
|
||||
|
||||
private function responseFor($endpoint, $class, $ipAddress)
|
||||
{
|
||||
$uri = implode('/', array($this->baseUri(), $endpoint, $ipAddress));
|
||||
|
||||
@@ -8,44 +8,39 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testDefaultLocale()
|
||||
{
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||
// Needed for PHP 5.3
|
||||
$that = $this;
|
||||
$this->checkAllMethods(
|
||||
function ($method) use (&$that, &$reader) {
|
||||
foreach (array('City', 'Country') as $type) {
|
||||
$reader = new Reader("maxmind-db/test-data/GeoIP2-$type-Test.mmdb");
|
||||
$method = lcfirst($type);
|
||||
$record = $reader->$method('81.2.69.160');
|
||||
$that->assertEquals('United Kingdom', $record->country->name);
|
||||
$this->assertEquals('United Kingdom', $record->country->name);
|
||||
}
|
||||
);
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
public function testLocaleList()
|
||||
{
|
||||
|
||||
foreach (array('City', 'Country') as $type) {
|
||||
$reader = new Reader(
|
||||
'maxmind-db/test-data/GeoIP2-City-Test.mmdb',
|
||||
"maxmind-db/test-data/GeoIP2-$type-Test.mmdb",
|
||||
array('xx', 'ru', 'pt-BR', 'es', 'en')
|
||||
);
|
||||
$that = $this;
|
||||
$this->checkAllMethods(
|
||||
function ($method) use (&$that, &$reader) {
|
||||
$method = lcfirst($type);
|
||||
|
||||
$record = $reader->$method('81.2.69.160');
|
||||
$that->assertEquals('Великобритания', $record->country->name);
|
||||
$this->assertEquals('Великобритания', $record->country->name);
|
||||
}
|
||||
);
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
public function testHasIpAddress()
|
||||
{
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||
$that = $this;
|
||||
$this->checkAllMethods(
|
||||
function ($method) use (&$that, &$reader) {
|
||||
foreach (array('City', 'Country') as $type) {
|
||||
$reader = new Reader("maxmind-db/test-data/GeoIP2-$type-Test.mmdb");
|
||||
$method = lcfirst($type);
|
||||
$record = $reader->$method('81.2.69.160');
|
||||
$that->assertEquals('81.2.69.160', $record->traits->ipAddress);
|
||||
$this->assertEquals('81.2.69.160', $record->traits->ipAddress);
|
||||
}
|
||||
);
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
@@ -60,6 +55,28 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException BadMethodCallException
|
||||
* @expectedExceptionMessage The country method cannot be used to open a GeoIP2-City database
|
||||
*/
|
||||
public function testIncorrectDatabase()
|
||||
{
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||
$reader->country('10.10.10.10');
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException BadMethodCallException
|
||||
* @expectedExceptionMessage The domain method cannot be used to open a GeoIP2-City database
|
||||
*/
|
||||
public function testIncorrectDatabaseFlat()
|
||||
{
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||
$reader->domain('10.10.10.10');
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedExceptionMessage is not a valid IP address
|
||||
@@ -71,6 +88,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
|
||||
public function testConnectionType()
|
||||
{
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-Connection-Type-Test.mmdb');
|
||||
@@ -112,10 +130,11 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
public function checkAllMethods($testCb)
|
||||
public function testMetadata()
|
||||
{
|
||||
foreach (array('city', 'cityIspOrg', 'country', 'omni') as $method) {
|
||||
$testCb($method);
|
||||
}
|
||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||
$this->assertEquals('GeoIP2-City', $reader->metadata()->databaseType);
|
||||
|
||||
$reader->close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,10 +201,8 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
public function testInsights()
|
||||
{
|
||||
|
||||
$methods = array('omni', 'insights');
|
||||
foreach ($methods as $method) {
|
||||
$record = $this->client($this->getResponse('1.2.3.4'))
|
||||
->$method('1.2.3.4');
|
||||
->insights('1.2.3.4');
|
||||
|
||||
$this->assertInstanceOf('GeoIp2\Model\Insights', $record);
|
||||
|
||||
@@ -214,7 +212,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
'continent geoname_id is 42'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testCity()
|
||||
{
|
||||
@@ -230,7 +227,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertInstanceOf(
|
||||
'GeoIp2\Model\City',
|
||||
$client->cityIspOrg('me'),
|
||||
$client->city('me'),
|
||||
'can set ip parameter to me'
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user