Newer
Older
Digital_Repository / Repositories / statistics / scripts / eprints-usage_src.php
  1. <?php
  2.  
  3. /* NJS 2007-07-24
  4. The database structure changed between versions 2.x and 3.x of
  5. EPrints, so we now need to check the major version number and alter
  6. the queries appropriately. Use only the MAJOR version number (i.e.,
  7. 2 or 3, don't include the release number).
  8. */
  9. $eprints_version = ##EPRINTS_VERSION##;
  10.  
  11. /* NJS 2006-04-28
  12. In earlier versions of this script, which eprints to count was
  13. determined by comparing the request date of the eprint against the
  14. "lastproc" date of this script (i.e., minimum time unit one day).
  15. This was fine if you only ran the script once per day, but if you ran
  16. it more than that, it counted multiple times requests whose
  17. $request_date == $lastproc. For example, if you ran this script five
  18. times per day, all the downloads that occurred during that day would
  19. be counted EVERY TIME this script ran, thus overinflating your stats
  20. by a factor of up to five :(
  21. The solution is to use the full time stamp for comparison rather than
  22. just the date. This timestamp MUST include time zone information so
  23. that things don't get screwed up by daylight saving time. As long as
  24. this is done consistently, there's no need to do things like convert
  25. to GMT, for example.
  26. The very first thing we need to do is grab the current time stamp
  27. with time zone, which will later be stored in the database as the
  28. "lastproc" time. This needs to happen first so that we don't "lose"
  29. any requests that occur while the script is running.
  30. */
  31. $start_time = date('Y-m-d H:i:s O');
  32.  
  33. /* NJS 2007-01-30
  34. A further twist! The original script ignored log lines that had a
  35. date falling before $lastproc, i.e., if log line date < $lastproc
  36. then it's already been dealt with. This is all fine. However, it
  37. didn't bother checking for log lines that were written after the
  38. script started running (i.e. log line date >= $start_time).
  39. Why is this a problem? We're reading the live Apache log file, so
  40. it's quite likely that new lines will be written to it after the
  41. script has started (i.e., after $start_time). Suppose $start_time is
  42. '2006-06-15 14:03:15 +1200', $lastproc is '2006-06-15 12:03:15 +1200'
  43. (i.e., the script is run every two hours) and the log file contains
  44. lines with the following dates:
  45. '2006-06-15 10:03:15 +1200' [1] <-- written before $lastproc
  46. '2006-06-15 12:03:14 +1200' [2] <-- written before $lastproc
  47. '2006-06-15 13:03:15 +1200' [3] <-- written before $start_time
  48. '2006-06-15 14:03:14 +1200' [4] <-- written before $start_time
  49. '2006-06-15 14:03:15 +1200' [5] <-- written at $start_time
  50. '2006-06-15 14:03:16 +1200' [6] <-- written after $start_time
  51.  
  52. During this run, dates [1] and [2] are both < $lastproc and thus
  53. ignored. The remaining four dates ([4]--[6]) are >= $lastproc and
  54. thus processed.
  55.  
  56. Two hours later, the script runs again, this time with $start_time
  57. set to '2006-06-15 16:03:15 +1200' and $lastproc to '2006-06-15
  58. 14:03:15 +1200'. Dates [1] through [4] are all < $lastproc and
  59. thus ignored. However, dates [5] and [6] are both >= $lastproc
  60. and are processed a second time, resulting in a duplicate entry
  61. in the database.
  62. The solution is to ignore any log line entries that occur at or after
  63. (>=) $start_time. In the example above, this would mean that in the
  64. first run, dates [1], [2], [5] and [6] would be ignored and dates [3]
  65. and [4] processed. In the second run, dates [1]--[4] would be ignored
  66. and dates [5] and [6] processed.
  67. */
  68. $test_starttime = strtotime($start_time);
  69.  
  70.  
  71. // NJS 2005-12-09 Switched to GeoIP from GeoIP:IPfree.
  72. include("geoip.inc");
  73.  
  74. $gi = geoip_open("##GEOIP_DATABASE##",GEOIP_STANDARD);
  75.  
  76. /*
  77.  
  78. Apache log for ePrints uses this format:
  79. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  80.  
  81. If the log format differs the regular expression matching would need to be adjusted.
  82. Parse:
  83. ip
  84. date YYYY MM DD
  85. archive ID
  86.  
  87. */
  88.  
  89. // Web server log files
  90. $log_dir = '##APACHE_LOG_LOCATION##';
  91. $log_file = array(
  92. 'otago_eprints' => '##APACHE_LOG_NAME_1##',
  93. 'cardrona' => '##APACHE_LOG_NAME_2##',
  94. );
  95.  
  96.  
  97. // eprintstats db
  98. $sqlserver = 'localhost';
  99. $sqluser = 'eprintstatspriv';
  100. $sqlpass = 'AuldGrizzel';
  101. $sqldatabase = 'eprintstats';
  102.  
  103. /* NJS 2006-05-26
  104. SQL details of your ePrints installation(s). This has now been
  105. generalised to work with multiple archives. For each archive that you
  106. have, add an entry to this array in the following format:
  107.  
  108. 'archive_name' => array(
  109. 'sqlserver' => 'db_host',
  110. 'username' => 'archive_name',
  111. 'password' => 'password',
  112. ),
  113. */
  114. $eprintsdbs = array(
  115. 'otago_eprints' => array(
  116. 'sqlserver' => 'localhost',
  117. 'username' => 'otago_eprints',
  118. 'password' => 'DrSyntaxRidesAgain',
  119. ),
  120. 'cardrona' => array(
  121. 'sqlserver' => 'localhost',
  122. 'username' => 'cardrona',
  123. 'password' => 'chautquau',
  124. ),
  125. );
  126.  
  127. /* NJS 2005-12-16
  128. IP address ranges for your local Intranet(s). You can have multiple
  129. ranges of IP addresses, each with a different "country name", so that
  130. they will appear as separate entries in the by country stats pages.
  131. You should use a different country code for each range (ISO 3166-1
  132. specifies the range XA through XZ as "user-assignable", so you can use
  133. codes from there as necessary), and create flag icons as appropriate.
  134.  
  135. Each address range key is the name that will appear in the statistics
  136. database (the "country name"), followed by a comma, followed by the
  137. appropriate ISO 3166-1 country code as noted above. Each entry in the
  138. range is either a single IP address, or an array specifying a lower and
  139. upper bound for a contiguous IP address range (see example below).
  140.  
  141. All IP addresses must be converted to long values using the ip2long()
  142. function before being stored.
  143.  
  144. Note that address ranges may overlap. The script will use the first
  145. range that matches a given IP, so list the ranges in the correct order
  146. of precedence for your needs.
  147.  
  148. Example:
  149.  
  150. $local_IPs = array(
  151. 'Repository Admin,XA' => array(
  152. ip2long('192.168.1.5'),
  153. ip2long('192.168.1.22'),
  154. array(
  155. lower => ip2long('192.168.1.30'),
  156. upper => ip2long('192.168.1.35'),
  157. ),
  158. ),
  159. 'Our Intranet,XI' => array(
  160. array(
  161. lower => ip2long('192.168.1.0'),
  162. upper => ip2long('192.168.255.255'),
  163. ),
  164. ),
  165. );
  166.  
  167. 'Repository Admin' covers the IP addresses 192.168.1.5, 192.168.1.22 and
  168. the range 192.168.1.30 to 192.168.1.35, inclusive. 'Our Intranet' covers
  169. the range 192.168.1.0 to 192.168.255.255, inclusive. A machine will only
  170. match the 'Our Intranet' range if it first fails to match the
  171. 'Repository Admin' range.
  172. */
  173. $local_IPs = array(
  174. 'Repository Admin,XA' => array(
  175. ip2long('139.80.75.110'), // Nigel @ Uni
  176. ip2long('60.234.209.74'), // Nigel @ home
  177. ip2long('139.80.92.138'), // Monica & Jeremy
  178. ip2long('139.80.92.151'), // @ Uni
  179. ip2long('203.89.162.155'), // Monica @ home
  180. ip2long('139.80.81.50'), // eprints.otago.ac.nz
  181. ip2long('172.20.1.50'), // eprints.otago.ac.nz pre-switch
  182. ip2long('172.20.1.1'), // eprints.otago.ac.nz pre-switch
  183. ),
  184. 'Otago Intranet,XI' => array(
  185. array(
  186. 'lower' => ip2long('139.80.0.0'),
  187. 'upper' => ip2long('139.80.127.255'),
  188. ),
  189. ),
  190. );
  191.  
  192. /* NJS 2007-01-26
  193. Patterns to match various search engine bots. Ideally, we'd use a similar
  194. mechanism to the $local_IPs variable above, but this isn't feasible because
  195. we'd need to know the IP ranges for the likes of Google, for example. This
  196. clearly isn't possible in practice.
  197.  
  198. Fortunately, most search bots insert a readily identifiable string into
  199. the user-agent part of the HTTP response, which gets recorded in the Apache
  200. log file. We can look for these and re-code log entries as appropriate.
  201.  
  202. The format of this list is similar to that of the $local_IPs variable.
  203. The key is the "country name" (in this case the name of the search
  204. engine) plus a non-standard four-character country code starting with
  205. "X@", separated by a comma. Each key value has an associated list of
  206. corresponding regular expressions that can occur in the user-agent part
  207. of the Apache log entry. If any one of these REs matches the user-agent
  208. part of the log entry, then we should re-code the country appropriately.
  209.  
  210. A four-character code is used because that what the database allows, and
  211. it avoids having to reserve several of the "X" country codes for search
  212. engines.
  213. */
  214. $bot_patterns = array(
  215. // Yahoo! (http://www.yahoo.com/)
  216. 'Yahoo!,X@YH' => array(
  217. '/yahoo! slurp/i',
  218. '/yahooseeker/i',
  219. ),
  220. // Windows Live Search (http://search.msn.com/)
  221. 'Windows Live Search,X@MS' => array(
  222. '/msnbot/i',
  223. ),
  224. // Google (http://www.google.com/)
  225. 'Google,X@GG' => array(
  226. '/googlebot/i',
  227. ),
  228. // Ask.com (http://www.ask.com/)
  229. 'Ask.com,X@AC' => array(
  230. '/ask jeeves\/teoma/i',
  231. ),
  232. // Everything else I could find in our log files :)
  233. 'Other search engine,X@OS' => array(
  234. // TAMU Internet Research Lab (http://irl.cs.tamu.edu/)
  235. '/http:\/\/irl\.cs\.tamu\.edu\/crawler/i',
  236. // Alexa web search (http://www.alexa.com/)
  237. '/ia_archiver/i',
  238. // TrueKnowledge for Web (http://www.authoritativeweb.com/)
  239. '/converacrawler/i',
  240. // Majestic 12 distributed search engine (http://www.majestic12.co.uk/)
  241. '/mj12bot/i',
  242. // Picsearch (http://www.picsearch.com/)
  243. '/psbot/i',
  244. // Exalead (http://www.exalead.com/search)
  245. '/exabot/i',
  246. // Cazoodle (note cazoodle.com doesn't exist)
  247. '/cazoodlebot crawler/i',
  248. '/mqbot@cazoodle\.com/i',
  249. // Gigablast (http://www.gigablast.com/)
  250. '/gigabot/i',
  251. // Houxou (http://www.houxou.com/)
  252. '/houxoucrawler/i',
  253. '/crawler at houxou dot com/i',
  254. // IBM Almaden Research Center Computer Science group (http://www.almaden.ibm.com/cs/)
  255. '/http:\/\/www\.almaden\.ibm\.com\/cs\/crawler/i',
  256. // Goo? (http://help.goo.ne.jp/)
  257. '/ichiro/i',
  258. // Daum Communications Corp (Korea)
  259. '/edacious & intelligent web robot/i',
  260. '/daum communications corp/i',
  261. '/daum web robot/i',
  262. '/msie is not me/i',
  263. '/daumoa/i',
  264. // Girafa (http://www.girafa.com/)
  265. '/girafabot/i',
  266. // The Generations Network (http://www.myfamilyinc.com/)
  267. '/myfamilybot/i',
  268. // Naver? (http://www.naver.com/)
  269. '/naverbot/i',
  270. // WiseNut (http://www.wisenutbot.com/)
  271. '/zyborg/i',
  272. '/wn-[0-9]+\.zyborg@looksmart\.net/i',
  273. // Accelobot (http://www.accelobot.com/)
  274. // This one seems particularly busy!
  275. '/heritrix/i',
  276. // Seeqpod (http://www.seeqpod.com/)
  277. '/seeqpod-vertical-crawler/i',
  278. // University of Illinois at Urbana-Champaign, Computer Science (http://www.cs.uiuc.edu/)
  279. '/mqbot crawler/i',
  280. '/mqbot@cs\.uiuc\.edu/i',
  281. // Microsoft Research (http://research.microsoft.com/)
  282. '/msrbot/i',
  283. // Nusearch
  284. '/nusearch spider/i',
  285. // SourceForge (http://www.sf.net/)
  286. '/nutch-agent@lists\.sourceforge\.net/i',
  287. // Lucene (http://lucene.apache.org/)
  288. '/nutch-agent@lucene\.apache\.org/i',
  289. '/raphael@unterreuth.de/i',
  290. // Computer Science, University of Washington (http://cs.washington.edu/)
  291. '/nutch running at uw/i',
  292. '/sycrawl@cs\.washington\.edu/i',
  293. // Chikayama & Taura Laboratory, University of Tokyo (http://www.logos.ic.i.u-tokyo.ac.jp/)
  294. '/shim-crawler/i',
  295. '/crawl@logos\.ic\.i\.u-tokyo\.ac\.jp/i',
  296. // Sproose (http://www.sproose.com/)
  297. '/sproose bot/i',
  298. '/crawler@sproose\.com/i',
  299. // Turnitin (http://www.turnitin.com/)
  300. '/turnitinbot/i',
  301. // WISH Project (http://wish.slis.tsukuba.ac.jp/)
  302. '/wish-project/i',
  303. // WWWster
  304. '/wwwster/i',
  305. '/gue@cis\.uni-muenchen\.de/i',
  306. // Forex Trading Network Organization (http://www.netforex.org/)
  307. '/forex trading network organization/i',
  308. '/info@netforex\.org/i',
  309. // FunnelBack (http://www.funnelback.com/)
  310. '/funnelback/i',
  311. // Baidu (http://www.baidu.com/)
  312. '/baiduspider/i',
  313. // Brandimensions (http://www.brandimensions.com/)
  314. '/bdfetch/i',
  315. // Blaiz Enterprises (http://www.blaiz.net/)
  316. '/blaiz-bee/i',
  317. // Boitho/SearchDaimon (http://www.boitho.com/ or http://www.searchdaimon.com/)
  318. '/boitho\.com-dc/i',
  319. // Celestial (OAI aggregator, see http://oai-perl.sourceforge.net/ for a little info)
  320. '/celestial/i',
  321. // Cipinet (http://www.cipinet.com/)
  322. '/cipinetbot/i',
  323. // iVia (http://ivia.ucr.edu/)
  324. '/crawlertest crawlertest/i',
  325. // Encyclopedia of Keywords (http://keywen.com/)
  326. '/easydl/i',
  327. // Everest-Vulcan Inc. (http://everest.vulcan.com/)
  328. '/everest-vulcan inc/i',
  329. // FactBites (http://www.factbites.com/)
  330. '/factbot/i',
  331. // Scirus (http://www.scirus.com/)
  332. '/scirus scirus-crawler@fast\.no/i',
  333. // UOL (http://www.uol.com.br/)
  334. '/uolcrawler/i',
  335. '/soscrawler@uol\.com\.br/i',
  336. // Always Updated (http://www.updated.com/)
  337. '/updated crawler/i',
  338. '/crawler@updated\.com/i',
  339. // FAST Enterprise Search (http://www.fast.no/)
  340. '/fast metaweb crawler/i',
  341. '/crawler@fast\.no/i',
  342. '/helpdesk at fastsearch dot com/i',
  343. // Deutsche Wortschatz Portal (http://wortschatz.uni-leipzig.de/)
  344. '/findlinks/i',
  345. // Gais (http://gais.cs.ccu.edu.tw/)
  346. '/gaisbot/i',
  347. '/robot[0-9]{2}@gais.cs.ccu.edu.tw/i',
  348. // http://ilse.net/
  349. '/ingrid/i',
  350. // Krugle (http://corp.krugle.com/)
  351. '/krugle\/krugle/i',
  352. '/krugle web crawler/i',
  353. '/webcrawler@krugle\.com/i',
  354. // WebWobot (http://www.webwobot.com/)
  355. '/scollspider/i',
  356. // Omni-Explorer (http://www.omni-explorer.com/)
  357. '/omniexplorer_bot/i',
  358. '/worldindexer/i',
  359. // PageBull (http://www.pagebull.com/)
  360. '/pagebull http:\/\/www\.pagebull\.com\//i',
  361. // dir.com (http://dir.com/)
  362. '/pompos/i',
  363. // Sensis (http://sensis.com.au/)
  364. '/sensis web crawler/i',
  365. '/search_comments\\\\at\\\\sensis\\\\dot\\\\com\\\\dot\\\\au/i',
  366. // Shopwiki (http://www.shopwiki.com/)
  367. '/shopwiki/i',
  368. // Guruji (http://www.terrawiz.com/)
  369. '/terrawizbot/i',
  370. // Language Observatory Project (http://www.language-observatory.org/)
  371. '/ubicrawler/i',
  372. // MSIE offline bookmarks crawler
  373. '/msiecrawler/i',
  374. // Unidentified
  375. '/bot/i',
  376. '/crawler/i',
  377. '/spider/i',
  378. '/larbin/i', // also larbinSpider
  379. '/httrack/i',
  380. '/voyager/i',
  381. '/acadiauniversitywebcensusclient/i',
  382. '/feedchecker/i',
  383. '/knowitall\(knowitall@cs\.washington\.edu\)/i',
  384. '/mediapartners-google/i',
  385. '/psycheclone/i',
  386. '/topicblogs/i',
  387. '/nutch/i',
  388. ),
  389. );
  390.  
  391. ###########################################
  392. ##
  393. ## No configuration required below here.
  394. ##
  395. ###########################################
  396.  
  397. $connect = mysql_pconnect ($sqlserver,$sqluser,$sqlpass);
  398. $db = mysql_select_db($sqldatabase,$connect) or die("Could not connect");
  399.  
  400. // First get the date of last update
  401. /* NJS 2006-04-28
  402. Changed this from order by timeinsert to order by id. The ID is
  403. always guaranteed to increase temporally, but is otherwise
  404. time-independent and thus not affected by things like daylight
  405. savings.
  406. */
  407. $query = "SELECT lastproc FROM lastproc ORDER BY id DESC LIMIT 1";
  408. $result = mysql_query($query,$connect);
  409. $num_rows = mysql_num_rows($result);
  410. if ($num_rows > 0) {
  411. $row = mysql_fetch_assoc($result);
  412. $lastproc = $row["lastproc"];
  413. // NJS 2007-01-30 Refactored $databaseA to more meaningful $test_lastproc.
  414. $test_lastproc = strtotime($lastproc);
  415. }
  416. else {
  417. $test_lastproc = 0;
  418. }
  419.  
  420. // NJS 2006-06-14: Generalised connection list for multiple archives.
  421. $eprints_connections = array();
  422. foreach ($eprintsdbs as $archive_name => $details)
  423. {
  424. $eprints_connections[$archive_name] =
  425. mysql_connect($details['sqlserver'],$details['username'],$details['password']);
  426. }
  427. $counter = 0;
  428. foreach($log_file as $archive_name=>$archive_log) {
  429. $logf = $log_dir . $archive_log;
  430. $handle = fopen($logf, "r");
  431. while (!feof($handle)) {
  432. $buffer = fgets($handle, 4096);
  433. /* NJS 2007-01-26
  434. Added user-agent match to all regexps to enable bot detection.
  435. NJS 2007-01-31
  436. Refactored regexps from four down to one, after realising
  437. that (a) long EPrints URLs are a superset of the short ones,
  438. and (b) a regexp that matches domain names works just as well
  439. for IP addresses (the GeoIP lookup doesn't care which it
  440. gets). Also fixed the pattern so it can handle an arbitrary
  441. number of subdomains. Note that the latter would be the main
  442. argument for keeping a separate IP address pattern, as IP
  443. addresses always comprise exactly four parts. However, it's
  444. not really up to the script to verify IP addresses; Apache
  445. should be recording them correctly in the first place!
  446. The typical kinds of strings we are matching look something
  447. like this:
  448. fetch abstract (short, long):
  449. 168.192.1.1 - - [31/Jan/2007:09:15:36 +1300] "GET /1/ HTTP/1.1" 200 12345 "referer" "user-agent"
  450. 168.192.1.1 - - [31/Jan/2007:09:15:36 +1300] "GET /archive/00000001/ HTTP/1.1" 200 12345 "referer" "user-agent"
  451. download item (short, long):
  452. 168.192.1.1 - - [31/Jan/2007:09:15:37 +1300] "GET /1/01/foo.pdf HTTP/1.1" 200 12345 "referer" "user-agent"
  453. 168.192.1.1 - - [31/Jan/2007:09:15:37 +1300] "GET /archive/00000001/01/foo.pdf HTTP/1.1" 200 12345 "referer" "user-agent"
  454. Plus any of the above with a domain name substituted for the IP
  455. address (e.g., foo.bar.com instead of 168.192.1.1).
  456. */
  457. if (preg_match("/^(\S+(?:\.\S+)+) - - \[(.*?)\] \"GET \/(?:archive\/0+)?(\d+).*? HTTP\/1..\" 200 .*?(\"[^\"]+\")?$/i",$buffer,$matches))
  458. {
  459. $counter++;
  460. $country_code = '';
  461. $country_name = '';
  462. $insertid = '';
  463. $eprint_name = '';
  464. $view_type = '';
  465. $uniquebits = '';
  466. /* NJS 2007-01-29
  467. Moved date checking to the start of the loop, as there's
  468. no point in doing any of the regexp checks if we've already
  469. processed this log entry and will discard it anyway.
  470. */
  471. $date = $matches[2];
  472. /* NJS 2006-04-28
  473. Switched to timestamp rather than date-based comparison.
  474. First, clean up the Apache request date into something
  475. that strtotime understands. Note that the Apache log
  476. dates include time zone info by default.
  477. */
  478. $date = preg_replace("/:/"," ",$date,1); // Change first ":" to " ".
  479. $date = preg_replace("/\//", " ", $date); // Change all "/" to " ".
  480. // NJS 2007-01-30 Refactored $databaseB to more meaningful
  481. // $test_logdate.
  482. $test_logdate = strtotime($date);
  483.  
  484. // NJS 2007-01-30 Added test for log dates >= $start_time.
  485. if ( ( $test_logdate < $test_lastproc ) ||
  486. ( $test_logdate >= $test_starttime ) )
  487. continue;
  488. // Convert to properly formatted date string.
  489. $request_date = date('Y-m-d H:i:s O', $test_logdate);
  490. /* NJS 2005-12-16
  491. Determine country code and name.
  492. Check whether the IP number falls into any of the local
  493. intranet ranges. If so, then use that.
  494. */
  495. $ip = $matches[1];
  496. $ip_long = ip2long($ip);
  497. $found_country = FALSE;
  498. foreach ($local_IPs as $id => $addresses)
  499. {
  500. foreach ($addresses as $ip_range)
  501. {
  502. if (is_array($ip_range)) // check against lower/upper bounds
  503. {
  504. $found_country = (($ip_long >= $ip_range['lower'])
  505. && ($ip_long <= $ip_range['upper']));
  506. }
  507. else if (is_long($ip_range)) // data type sanity check
  508. {
  509. $found_country = ($ip_long == $ip_range);
  510. }
  511. else // something is seriously broken, ignore this entry
  512. {
  513. print "Unsupported data type " . gettype($ip_range) .
  514. " (value " . $ip_range .
  515. ") in \$local_IPs (expected long).\n";
  516. continue;
  517. }
  518. if ( $found_country ) break;
  519. }
  520. if ($found_country)
  521. {
  522. list($country_name, $country_code) = explode(',', $id);
  523. break;
  524. }
  525. }
  526. // Otherwise, fall back to GeoIP.
  527. if (!$found_country)
  528. {
  529. $country_code = geoip_country_code_by_addr($gi, $ip);
  530. $country_name = geoip_country_name_by_addr($gi, $ip);
  531. }
  532. // end NJS 2005-12-16
  533. /* NJS 2007-01-26
  534. Check whether this is a bot reference.
  535. */
  536. $user_agent = $matches[4];
  537. $found_country = FALSE;
  538. foreach ($bot_patterns as $id => $patterns)
  539. {
  540. foreach ($patterns as $pat)
  541. {
  542. if (preg_match($pat, $user_agent))
  543. {
  544. $found_country = TRUE;
  545. break;
  546. }
  547. }
  548. if ($found_country)
  549. {
  550. list($country_name, $country_code) = explode(',', $id);
  551. break;
  552. }
  553. }
  554. // end NJS 2007-01-26
  555. // Now sort out the remaining bits and we're done.
  556. $eprint_id = $matches[3];
  557. $uniquebits = $buffer;
  558. // NJS 2005-11-25 Added regexp for EPrints short URLs.
  559. // NJS 2007-01-31 Refactored into one regexp for both styles.
  560. if (preg_match("/GET \/(?:archive\/0+)?\d+\/\d+\//i",$buffer)) {
  561. $view_type = "download";
  562. } else {
  563. $view_type = "abstract";
  564. }
  565. if(isset($eprintname[$archive_name . $eprint_id])) {
  566. $eprint_name = $eprintname[$archive_name . $eprint_id];
  567. } else {
  568. $eprint_name = getePrintName($eprints_connections[$archive_name],$archive_name,$eprint_id,$eprints_version);
  569. $eprintname[$archive_name . $eprint_id] = $eprint_name;
  570. }
  571. if($eprint_name=='') {
  572. // Do nothing.
  573. } else {
  574. $eprint_name = mysql_escape_string($eprint_name);
  575. /* NJS 2006-04-25
  576. Requests containing apostrophes (') are dumped by
  577. MySQL unless we escape them. Looking in the GeoIP
  578. files I also see country names with apostrophes, so
  579. escape that as well. Everything else should be fine.
  580. */
  581. $uniquebits = mysql_escape_string($uniquebits);
  582. $country_name = mysql_escape_string($country_name);
  583. // end NJS 2006-04-25
  584. $query = "
  585. INSERT INTO view (uniquebits,archive_name,ip,request_date,archiveid,country_code,country_name,view_type,eprint_name)
  586. VALUES('".$uniquebits."','".$archive_name."','".$ip."','".$request_date."',".$eprint_id.",'".$country_code."','".$country_name."','".$view_type."','".$eprint_name."')";
  587. $result = mysql_query($query,$connect);
  588. $insertid = mysql_insert_id($connect);
  589. }
  590.  
  591. } else {
  592. // print "NO match" . "\n";
  593. }
  594. }
  595. fclose($handle);
  596. }
  597.  
  598. /*
  599. Keep track of where we are. Should avoid duplication of results
  600. if the script is run more than once on the same log file.
  601. */
  602.  
  603. // NJS 2006-04-28 Switched value inserted to $start_time instead of $request_date.
  604. $query = "INSERT into lastproc (lastproc) values('".$start_time."')";
  605. $result = mysql_query($query,$connect);
  606.  
  607. #print "Records counted: $counter\n";
  608. #print "Last count: $request_date\n";
  609. foreach ($eprints_connections as $connection)
  610. {
  611. mysql_close($connection);
  612. }
  613. mysql_close($connect);
  614.  
  615. // Look up the title corresponding to the specified eprint id.
  616. function getePrintName($connection,$archive,$eprintid,$eprints_version) {
  617. // NJS 2006-06-14: DB connection now passed as an argument.
  618. $db = mysql_select_db($archive,$connection);
  619. // NJS 2007-07-24: Added check for EPrints version, as the
  620. // database structure changed between versions 2 and 3.
  621. if ( $eprints_version > 2 )
  622. {
  623. $query3 = "
  624. SELECT title
  625. FROM eprint
  626. WHERE eprintid = $eprintid
  627. AND eprint_status = 'archive'
  628. ";
  629. }
  630. else
  631. {
  632. $query3 = "
  633. SELECT title
  634. FROM archive
  635. WHERE eprintid = $eprintid
  636. ";
  637. }
  638. $result3 = mysql_query($query3,$connection);
  639. $title = '';
  640. $suffix = '';
  641. // NJS 2006-04-25 Added check for empty result, probably a deleted item.
  642. // Look in the deleted items for details.
  643. if (mysql_num_rows($result3) == 0) {
  644. // NJS 2007-07-24: Added check for EPrints version, as the
  645. // database structure changed between versions 2 and 3.
  646. if ( $eprints_version > 2 )
  647. {
  648. $query3 = "
  649. SELECT title
  650. FROM eprint
  651. WHERE eprintid = $eprintid
  652. AND eprint_status = 'deletion'
  653. ";
  654. }
  655. else
  656. {
  657. $query3 = "
  658. SELECT title
  659. FROM deletion
  660. WHERE eprintid = $eprintid
  661. ";
  662. }
  663. $result3 = mysql_query($query3,$connection);
  664. // If it's not in deletion, then we have no clue what it is.
  665. if (mysql_num_rows($result3) == 0) {
  666. $title = "Unknown item [$eprintid]";
  667. }
  668. else {
  669. $suffix = ' [deleted]';
  670. }
  671. }
  672. if ($title == '') {
  673. $row = mysql_fetch_assoc($result3);
  674. $row["title"] = trim($row["title"]);
  675. $row["title"] = preg_replace("/\s+/"," ",$row["title"]);
  676. $title = $row["title"];
  677. }
  678. return $title . $suffix;
  679. }
  680.  
  681. ?>
  682.