Newer
Older
Digital_Repository / Repositories / statistics / scripts / fix-countries_src.php
  1. <?php
  2.  
  3. /*
  4. Scan through the entire stats database and update all country details.
  5. This is ONLY intended to be run ONCE, and only if you have a database
  6. that clearly contains inaccurate country data.
  7. This is definitely NOT intended to be run on a regular basis, as IP
  8. ranges may change over time, and this will overwrite any existing
  9. country data.
  10. Run this to clean up the existing data, then leave things running as
  11. normal, download the latest country database from MaxMind once per
  12. month.
  13. */
  14.  
  15. include("geoip.inc");
  16.  
  17. $gi = geoip_open("##GEOIP_DATABASE##",GEOIP_STANDARD);
  18.  
  19. // eprintstats db
  20. $sqlserver = 'localhost';
  21. $sqluser = 'eprintstatspriv';
  22. $sqlpass = 'AuldGrizzel';
  23. $sqldatabase = 'eprintstats';
  24.  
  25. /*
  26. IP address ranges for your local Intranet(s). You can have multiple
  27. ranges of IP addresses, each with a different "country name", so that
  28. they will appear as separate entries in the by country stats pages. Note
  29. that all sets are assigned the country code "T5", so they will all use
  30. the flag icon for your local installation. If this isn't what you want,
  31. you'll have to hack this yourself :)
  32. */
  33. $local_IPs = array(
  34. 'Repository Admin' => array(
  35. ip2long('139.80.75.110'), // Nigel @ Uni
  36. ip2long('60.234.209.74'), // Nigel @ home
  37. ip2long('139.80.92.138'), // Monica & Jeremy
  38. ip2long('139.80.92.151'), // @ Uni
  39. ip2long('203.89.162.155'), // Monica @ home
  40. ip2long('139.80.81.50'), // eprints.otago.ac.nz
  41. ),
  42. 'Otago Intranet' => array(
  43. array(
  44. 'lower' => ip2long('139.80.0.0'),
  45. 'upper' => ip2long('139.80.127.255'),
  46. ),
  47. ),
  48. );
  49.  
  50. ###########################################
  51. ##
  52. ## No configuration required below here.
  53. ##
  54. ###########################################
  55.  
  56. $connect = mysql_connect ($sqlserver,$sqluser,$sqlpass);
  57. $db = mysql_select_db($sqldatabase,$connect) or die("Could not connect");
  58.  
  59. $query = "select id, ip, country_code from view";
  60. $result = mysql_query($query, $connect);
  61. while ($row = mysql_fetch_assoc($result))
  62. {
  63. $id = $row['id'];
  64. $ip = $row['ip'];
  65. $stored_country = $row['country_code'];
  66. $ip_long = ip2long($ip);
  67.  
  68. /*
  69. Determine country code and name.
  70. Check whether the IP number falls into any of the local
  71. intranet ranges. If so, then use that.
  72. */
  73. $ip_long = ip2long($ip);
  74. $found_country = FALSE;
  75. foreach ($local_IPs as $name => $addresses)
  76. {
  77. foreach ($addresses as $ip_range)
  78. {
  79. if (is_array($ip_range)) // check against lower/upper bounds
  80. {
  81. $found_country = (($ip_long >= $ip_range['lower'])
  82. && ($ip_long <= $ip_range['upper']));
  83. break;
  84. }
  85. else if (is_long($ip_range)) // data type sanity check
  86. {
  87. $found_country = ($ip_long == $ip_range);
  88. break;
  89. }
  90. else // something is seriously broken, ignore this entry
  91. {
  92. print "Unsupported data type " . gettype($ip_range) .
  93. " (value " . $ip_range .
  94. ") in \$local_IPs (expected long).\n";
  95. continue;
  96. }
  97. }
  98. if ($found_country)
  99. {
  100. $country_code = 'T5';
  101. $country_name = $name;
  102. break;
  103. }
  104. }
  105. // Otherwise, fall back to GeoIP.
  106. if (!$found_country)
  107. {
  108. $country_code = geoip_country_code_by_addr($gi, $ip);
  109. $country_name = geoip_country_name_by_addr($gi, $ip);
  110. }
  111. // end NJS 2005-12-16
  112.  
  113. // Only update the row if its country has changed.
  114. if ($stored_country != $country_code)
  115. {
  116. $update = "update view set country_code='" . $country_code . "', country_name='" . $country_name . "' where id=" . $id;
  117. $result2 = mysql_query($update, $connect);
  118. }
  119. }
  120.  
  121. /*
  122. Keep track of where we are. Should avoid duplication of results
  123. if the script is run more than once on the same log file
  124. */
  125.  
  126. $query = "INSERT into lastproc (lastproc) values('".$request_date."')";
  127. $result = mysql_query($query,$connect);
  128.  
  129. #print "Records counted: $counter\n";
  130. #print "Last count: $request_date\n";
  131. mysql_close($connect);
  132.  
  133. ?>
  134.