enwikipedia-acc /
waca
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | if (isset($_SERVER['REQUEST_METHOD'])) { |
||
| 4 | die(); |
||
| 5 | } // Web clients die. |
||
| 6 | |||
| 7 | ini_set('display_errors', 1); |
||
| 8 | ini_set('memory_limit', '256M'); |
||
| 9 | |||
| 10 | require_once 'config.inc.php'; |
||
| 11 | require_once 'functions.php'; |
||
| 12 | require_once 'includes/PdoDatabase.php'; |
||
| 13 | |||
| 14 | /** @var PdoDatabase $database */ |
||
| 15 | $database = gGetDb(); |
||
| 16 | |||
| 17 | $locationProvider = new IpLocationProvider($database, $locationProviderApiKey); |
||
| 18 | |||
| 19 | while (true) { |
||
| 20 | echo "Beginning txn\n"; |
||
| 21 | $database->beginTransaction(); |
||
| 22 | |||
| 23 | try { |
||
| 24 | // fetch a bunch of un-geolocated IPs from the database. |
||
| 25 | // note we have to parse the forwardedip field in the database so we can test against the geolocation table. |
||
| 26 | // This guarantees we get ten unlocated IPs back, unless there actually aren't 10 available. |
||
| 27 | // Alternatives include downloading a small set of forwarded IPs, splitting it in PHP, constructing an IN() |
||
| 28 | // clause dynamically, sending that back to the database to check if there are geolocation entries, then repeating |
||
|
0 ignored issues
–
show
|
|||
| 29 | // until we have 10 to process - and the fact that we'd have to potentially retrieve all IPs from the database |
||
| 30 | // before we find any at all. This way keeps all of that legwork in the database, at the cost of a more complex |
||
| 31 | // query. |
||
| 32 | $statement = $database->query(<<<SQL |
||
| 33 | SELECT p.prox |
||
| 34 | FROM ( |
||
| 35 | SELECT trim(substring_index(substring_index(r.forwardedip, ',', n.n), ',', -1)) prox |
||
| 36 | FROM request r |
||
| 37 | INNER JOIN ( |
||
| 38 | SELECT 1 n |
||
| 39 | UNION ALL SELECT 2 |
||
| 40 | UNION ALL SELECT 3 |
||
| 41 | UNION ALL SELECT 4 |
||
| 42 | UNION ALL SELECT 5) n |
||
| 43 | ON char_length(r.forwardedip) - char_length(replace(r.forwardedip, ',', '')) >= n.n - 1 |
||
| 44 | WHERE ip <> '127.0.0.1' |
||
| 45 | ) p |
||
| 46 | WHERE NOT EXISTS (SELECT 1 FROM geolocation g WHERE g.address = p.prox) |
||
| 47 | LIMIT 10; |
||
| 48 | SQL |
||
| 49 | ); |
||
| 50 | |||
| 51 | $missingIps = $statement->fetchAll(PDO::FETCH_COLUMN); |
||
| 52 | |||
| 53 | $count = count($missingIps); |
||
| 54 | if ($count === 0) { |
||
| 55 | echo ". Found nothing to do.\n"; |
||
| 56 | break; |
||
| 57 | } |
||
| 58 | |||
| 59 | echo ". Picked {$count} IP addresses\n"; |
||
| 60 | |||
| 61 | foreach ($missingIps as $ip) { |
||
| 62 | echo ". . Getting location for {$ip}...\n"; |
||
| 63 | $data = json_encode($locationProvider->getIpLocation($ip)); |
||
| 64 | echo ". . . {$data}\n"; |
||
| 65 | } |
||
| 66 | |||
| 67 | echo ". IP location fetch complete.\n"; |
||
| 68 | $database->commit(); |
||
| 69 | echo ". Committed txn.\n"; |
||
| 70 | } catch (Exception $ex) { |
||
| 71 | echo ". Encountered exception: " . $ex->getMessage(). "\n"; |
||
| 72 | $database->rollBack(); |
||
| 73 | echo ". Rolled back txn\n"; |
||
| 74 | throw $ex; |
||
| 75 | } finally { |
||
| 76 | if($database->hasActiveTransaction()){ |
||
| 77 | $database->rollBack(); |
||
| 78 | echo ". Rolled back txn\n"; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | echo "Done.\n"; |
||
| 84 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.