Legoktm has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/376451 )
Change subject: [POC] Geo IP integration ...................................................................... [POC] Geo IP integration Proof of concept demonstrating how we could integrate GeoLite2 databases into MediaWiki. Change-Id: I9650091f367517566c9b7c3cedae746ebdb3d4f8 --- M composer.json M includes/DefaultSettings.php A includes/GeoIPLookup.php M includes/ServiceWiring.php 4 files changed, 126 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/51/376451/1 diff --git a/composer.json b/composer.json index dd7567c..232a420 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,8 @@ "wikimedia/timestamp": "1.0.0", "wikimedia/wait-condition-loop": "1.0.1", "wikimedia/wrappedstring": "2.2.0", - "zordius/lightncandy": "0.23" + "zordius/lightncandy": "0.23", + "geoip2/geoip2": "^2.6" }, "require-dev": { "composer/spdx-licenses": "1.1.4", diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index cf8e089..0a5dfd6 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -8783,6 +8783,19 @@ $wgCommentTableSchemaMigrationStage = MIGRATION_OLD; /** + * Directory where GeoIP databases can be found, + * These files should be named: + * - GeoLite2-ASN.mmdb + * - GeoLite2-City.mmdb + * + * Setting to false disables all GeoIP features + * + * @since 1.30 + * @var string|bool + */ +$wgGeoIPDataDirectory = false; + +/** * For really cool vim folding this needs to be at the end: * vim: foldmarker=@{,@} foldmethod=marker * @} diff --git a/includes/GeoIPLookup.php b/includes/GeoIPLookup.php new file mode 100644 index 0000000..86b06a7 --- /dev/null +++ b/includes/GeoIPLookup.php @@ -0,0 +1,100 @@ +<?php +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + + +namespace MediaWiki; + +use GeoIp2\Database\Reader; +use GeoIp2\Exception\AddressNotFoundException; +use GeoIp2\Model\Asn; +use GeoIp2\Model\City; + +/** + * Wrapper around GeoIP2 libraries + * + * @since 1.30 + */ +class GeoIPLookup { + + /** + * Directory where data files are + * + * @var string + */ + private $dataDirectory; + + /** + * @var Reader|null + */ + private $cityDb; + + /** + * @var Reader|null + */ + private $asnDb; + + public static function isEnabled() { + global $wgGeoIPDataDirectory; + return $wgGeoIPDataDirectory !== false && class_exists( Reader::class ); + } + + /** + * @param string $dataDirectory Directory where data files are or false if disabled + */ + public function __construct( $dataDirectory ) { + $this->dataDirectory = $dataDirectory; + } + + /** + * Get an IP address's city information + * + * @param string $ip IP address + * @return bool|City false if address wasn't found in the database + */ + public function getCityInfo( $ip ) { + if ( !$this->cityDb ) { + $this->cityDb = new Reader( "{$this->dataDirectory}/GeoLite2-City.mmdb" ); + } + + try { + return $this->cityDb->city( $ip ); + } catch ( AddressNotFoundException $e ) { + return false; + } + } + + /** + * Get an IP address's ASN information + * + * @param string $ip IP address + * @return bool|Asn false if the address wasn't found in the database + */ + public function getASNInfo( $ip ) { + if ( !$this->asnDb ) { + $this->asnDb = new Reader( "{$this->dataDirectory}/GeoLite2-ASN.mmdb" ); + } + + try { + return $this->asnDb->asn( $ip ); + } catch ( AddressNotFoundException $e ) { + return false; + } + } +} diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index d048007..06ff947 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -37,6 +37,7 @@ * MediaWiki code base. */ +use MediaWiki\GeoIPLookup; use MediaWiki\Interwiki\ClassicInterwikiLookup; use MediaWiki\Linker\LinkRendererFactory; use MediaWiki\Logger\LoggerFactory; @@ -428,6 +429,16 @@ ); }, + 'GeoIPLookup' => function ( MediaWikiServices $services ) { + if ( !GeoIPLookup::isEnabled() ) { + throw new RuntimeException( "GeoIPLookup is disabled" ); + } + + return new GeoIPLookup( + $services->getMainConfig()->get( 'GeoIPDataDirectory' ) + ); + }, + /////////////////////////////////////////////////////////////////////////// // NOTE: When adding a service here, don't forget to add a getter function // in the MediaWikiServices class. The convenience getter should just call -- To view, visit https://gerrit.wikimedia.org/r/376451 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9650091f367517566c9b7c3cedae746ebdb3d4f8 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Legoktm <lego...@member.fsf.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits