Index: trunk/extensions/DonationInterface/GeoIP/RebuildGeoIP.php |
— | — | @@ -1,5 +1,4 @@ |
2 | 2 | <?php |
3 | | - |
4 | 3 | /** |
5 | 4 | * This program is free software; you can redistribute it and/or modify |
6 | 5 | * it under the terms of the GNU General Public License as published by |
— | — | @@ -16,62 +15,65 @@ |
17 | 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | 17 | * http://www.gnu.org/copyleft/gpl.html |
19 | 18 | * |
| 19 | + * @file |
20 | 20 | * @ingroup Maintenance |
21 | 21 | */ |
22 | 22 | |
23 | 23 | require_once( dirname(__FILE__) . '/../../maintenance/Maintenance.php' ); |
24 | 24 | |
25 | 25 | class RebuildGeoIP extends Maintenance { |
26 | | - public function __construct() { |
27 | | - parent::__construct(); |
28 | | - $this->mDescription = 'Rebuild GeoIP data from a CSV file.'; |
29 | | - $this->addArg( 'csv-file', 'CSV-formatted file with geographic IP data.', true ); |
30 | | - } |
31 | | - |
32 | | - public function memoryLimit() { |
33 | | - return '200M'; |
34 | | - } |
| 26 | + public function __construct() { |
| 27 | + parent::__construct(); |
| 28 | + $this->mDescription = 'Rebuild GeoIP data from a CSV file.'; |
| 29 | + $this->addArg( 'csv-file', 'CSV-formatted file with geographic IP data.', true ); |
| 30 | + } |
35 | 31 | |
36 | | - public function execute() { |
37 | | - $dbw = wfGetDB( DB_MASTER ); |
38 | | - |
39 | | - // Perform this in a transaction so an failed load doesn't erase current data. |
40 | | - $dbw->immediateBegin(); |
41 | | - |
42 | | - // Clear existing GeoIP data. |
43 | | - try { |
44 | | - $dbw->delete( 'geoip', '*' ); |
45 | | - } |
46 | | - catch (DBQueryError $e) { |
47 | | - $this->error('ERROR: Could not delete existing geographic data. Is the GeoIP schema loaded?', true); |
48 | | - } |
49 | | - |
50 | | - // Load fresh data from the first (and only) argument. |
51 | | - $filename = $this->getArg(0); |
52 | | - $lines = exec( 'wc -l ' . $filename ); |
53 | | - $handle = fopen( $filename, 'r' ); |
54 | | - |
55 | | - $count = 0; |
56 | | - while (($data = fgetcsv( $handle, 256, ',' )) !== FALSE) { |
57 | | - // Output a nice progress bar. |
58 | | - if ($count % 1000 == 0) { |
59 | | - $progress = ceil(($count / $lines) * 50); |
60 | | - $this->output('[' . str_repeat('=', $progress) . str_repeat(' ', 50 - $progress) . '] ' . ceil(($count / $lines) * 100) . '%' . "\r"); |
61 | | - } |
62 | | - ++$count; |
| 32 | + public function memoryLimit() { |
| 33 | + return '200M'; |
| 34 | + } |
63 | 35 | |
64 | | - $record = array( |
65 | | - 'begin_ip_long' => IP::toUnsigned( $data[0] ), |
66 | | - 'end_ip_long' => IP::toUnsigned( $data[1] ), |
67 | | - 'country_code' => $data[4], |
68 | | - ); |
69 | | - $dbw->insert( 'geoip', $record ); |
70 | | - } |
71 | | - $this->output("\n"); |
72 | | - |
73 | | - $dbw->immediateCommit(); |
74 | | - $this->output('Successfully loaded ' . $count . ' geographic IP ranges.' . "\n"); |
75 | | - } |
| 36 | + public function execute() { |
| 37 | + $dbw = wfGetDB( DB_MASTER ); |
| 38 | + |
| 39 | + // Perform this in a transaction so an failed load doesn't erase current data. |
| 40 | + $dbw->begin(); |
| 41 | + |
| 42 | + // Clear existing GeoIP data. |
| 43 | + try { |
| 44 | + $dbw->delete( 'geoip', '*', __METHOD__ ); |
| 45 | + } catch ( DBQueryError $e ) { |
| 46 | + $this->error( 'ERROR: Could not delete existing geographic data. Is the GeoIP schema loaded?', true ); |
| 47 | + } |
| 48 | + |
| 49 | + // Load fresh data from the first (and only) argument. |
| 50 | + $filename = $this->getArg( 0 ); |
| 51 | + $lines = exec( 'wc -l ' . $filename ); |
| 52 | + $handle = fopen( $filename, 'r' ); |
| 53 | + |
| 54 | + $count = 0; |
| 55 | + while( ( $data = fgetcsv( $handle, 256, ',' ) ) !== false ) { |
| 56 | + // Output a nice progress bar. |
| 57 | + if( $count % 1000 == 0 ) { |
| 58 | + $progress = ceil( ( $count / $lines ) * 50 ); |
| 59 | + $this->output( '[' . str_repeat( '=', $progress ) . |
| 60 | + str_repeat( ' ', 50 - $progress ) . '] ' . |
| 61 | + ceil( ( $count / $lines ) * 100 ) . '%' . "\r" |
| 62 | + ); |
| 63 | + } |
| 64 | + ++$count; |
| 65 | + |
| 66 | + $record = array( |
| 67 | + 'begin_ip_long' => IP::toUnsigned( $data[0] ), |
| 68 | + 'end_ip_long' => IP::toUnsigned( $data[1] ), |
| 69 | + 'country_code' => $data[4], |
| 70 | + ); |
| 71 | + $dbw->insert( 'geoip', $record, __METHOD__ ); |
| 72 | + } |
| 73 | + $this->output( "\n" ); |
| 74 | + |
| 75 | + $dbw->commit(); |
| 76 | + $this->output( 'Successfully loaded ' . $count . ' geographic IP ranges.' . "\n" ); |
| 77 | + } |
76 | 78 | } |
77 | 79 | |
78 | 80 | $maintClass = 'RebuildGeoIP'; |
Index: trunk/extensions/DonationInterface/GeoIP/GeoIP.php |
— | — | @@ -1,5 +1,4 @@ |
2 | 2 | <?php |
3 | | - |
4 | 3 | /** |
5 | 4 | * This program is free software; you can redistribute it and/or modify |
6 | 5 | * it under the terms of the GNU General Public License as published by |
— | — | @@ -15,78 +14,101 @@ |
16 | 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | 17 | * http://www.gnu.org/copyleft/gpl.html |
| 18 | + * |
| 19 | + * @file |
| 20 | + * @ingroup Extensions |
| 21 | + * @version 1.0 |
| 22 | + * @author David Strauss <david@fourkitchens.com> |
| 23 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
19 | 24 | */ |
20 | 25 | |
21 | | -// Alert the user that this is not a valid entry point to MediaWiki if they try to access the special pages file directly. |
22 | | -if (!defined('MEDIAWIKI')) { |
23 | | - echo <<<EOT |
24 | | -To install my extension, put the following line in LocalSettings.php: |
| 26 | +// Alert the user that this is not a valid entry point to MediaWiki if they try to access this file directly. |
| 27 | +if ( !defined ('MEDIAWIKI' ) ) { |
| 28 | + echo <<<EOT |
| 29 | +To install GeoIP extension, put the following line in LocalSettings.php: |
25 | 30 | require_once( "\$IP/extensions/GeoIP/GeoIP.php" ); |
26 | 31 | EOT; |
27 | | - exit( 1 ); |
| 32 | + exit( 1 ); |
28 | 33 | } |
29 | | - |
| 34 | + |
| 35 | +// Extension credits that will show up on Special:Version |
| 36 | +$wgExtensionCredits['specialpage'][] = array( |
| 37 | + 'name' => 'GeoIP', |
| 38 | + 'version' => '1.0', |
| 39 | + 'author' => 'David Strauss', |
| 40 | + 'description' => "[[Special:GeoIP|Shows user's geographical location, based on their IP address]]", |
| 41 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:GeoIP' |
| 42 | +); |
| 43 | + |
30 | 44 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'fnGeoIPSchema'; |
31 | 45 | $wgSpecialPages['GeoIP'] = 'GeoIP'; |
32 | 46 | |
33 | 47 | function fnGeoIPSchema() { |
34 | | - global $wgExtNewTables; |
35 | | - $wgExtNewTables[] = array( |
36 | | - 'geoip', |
37 | | - dirname( __FILE__ ) . '/GeoIP.sql' ); |
38 | | - return true; |
| 48 | + global $wgExtNewTables; |
| 49 | + $wgExtNewTables[] = array( |
| 50 | + 'geoip', |
| 51 | + dirname( __FILE__ ) . '/GeoIP.sql' |
| 52 | + ); |
| 53 | + return true; |
39 | 54 | } |
40 | 55 | |
41 | 56 | class UnsupportedGeoIP extends Exception { } |
42 | 57 | class NotFoundGeoIP extends Exception { } |
43 | 58 | |
44 | | -function fnGetGeoIP($ip_address = NULL) { |
45 | | - if (!isset($ip_address)) { |
46 | | - $ip_address = IP::sanitizeIP(wfGetIP()); |
47 | | - } |
| 59 | +function fnGetGeoIP( $ip_address = null ) { |
| 60 | + if( !isset( $ip_address ) ) { |
| 61 | + $ip_address = IP::sanitizeIP( wfGetIP() ); |
| 62 | + } |
48 | 63 | |
49 | | - if (isset($_GET['ip'])) { |
50 | | - $ip_address = IP::sanitizeIP($_GET['ip']); |
51 | | - } |
| 64 | + if( isset( $_GET['ip'] ) ) { |
| 65 | + $ip_address = IP::sanitizeIP( $_GET['ip'] ); |
| 66 | + } |
52 | 67 | |
53 | | - if (!IP::isIPv4($ip_address)) { |
54 | | - throw new UnsupportedGeoIP('Only IPv4 addresses are supported.'); |
55 | | - } |
56 | | - |
57 | | - $country_code = NULL; |
58 | | - |
59 | | - $dbr = wfGetDB( DB_SLAVE ); |
60 | | - $long_ip = IP::toUnsigned( $ip_address ); |
61 | | - $conditions = array( |
62 | | - 'begin_ip_long <= ' . $long_ip, |
63 | | - $long_ip . ' <= end_ip_long', |
64 | | - ); |
65 | | - |
66 | | - $country_code = $dbr->selectField( 'geoip', 'country_code', $conditions); |
67 | | - |
68 | | - if ( !$country_code ) { |
69 | | - throw new NotFoundGeoIP('Could not identify the country for the provided IP address.'); |
70 | | - } |
71 | | - |
72 | | - return $country_code; |
| 68 | + if( !IP::isIPv4( $ip_address ) ) { |
| 69 | + throw new UnsupportedGeoIP( 'Only IPv4 addresses are supported.' ); |
| 70 | + } |
| 71 | + |
| 72 | + $country_code = null; |
| 73 | + |
| 74 | + $dbr = wfGetDB( DB_SLAVE ); |
| 75 | + $long_ip = IP::toUnsigned( $ip_address ); |
| 76 | + $conditions = array( |
| 77 | + 'begin_ip_long <= ' . $long_ip, |
| 78 | + $long_ip . ' <= end_ip_long', |
| 79 | + ); |
| 80 | + |
| 81 | + $country_code = $dbr->selectField( 'geoip', 'country_code', $conditions, __METHOD__ ); |
| 82 | + |
| 83 | + if ( !$country_code ) { |
| 84 | + throw new NotFoundGeoIP( 'Could not identify the country for the provided IP address.' ); |
| 85 | + } |
| 86 | + |
| 87 | + return $country_code; |
73 | 88 | } |
74 | 89 | |
75 | 90 | class GeoIP extends SpecialPage { |
76 | | - function __construct() { |
| 91 | + |
| 92 | + /** |
| 93 | + * Constructor -- set up the new special page |
| 94 | + */ |
| 95 | + public function __construct() { |
77 | 96 | parent::__construct( 'GeoIP' ); |
78 | | - wfLoadExtensionMessages('GeoIP'); |
79 | 97 | } |
80 | | - |
81 | | - function execute( $par ) { |
82 | | - global $wgRequest, $wgOut; |
83 | | - |
| 98 | + |
| 99 | + /** |
| 100 | + * Show the special page |
| 101 | + * |
| 102 | + * @param $par Mixed: parameter passed to the page or null |
| 103 | + */ |
| 104 | + public function execute( $par ) { |
| 105 | + global $wgOut; |
| 106 | + |
84 | 107 | $this->setHeaders(); |
85 | | - |
86 | | - try { |
87 | | - $wgOut->addHTML( '<p>' . fnGetGeoIP() . '</p>' ); |
88 | | - } |
89 | | - catch (Exception $e) { |
90 | | - $wgOut->addHTML( '<p>Unknown</p>' ); |
91 | | - } |
| 108 | + |
| 109 | + try { |
| 110 | + $wgOut->addHTML( '<p>' . fnGetGeoIP() . '</p>' ); |
| 111 | + } catch ( Exception $e ) { |
| 112 | + $wgOut->addHTML( '<p>Unknown</p>' ); |
| 113 | + } |
92 | 114 | } |
93 | | -} |
| 115 | +} |
\ No newline at end of file |