]> git.openstreetmap.org Git - nominatim.git/blob - lib/log.php
add combined file query logging
[nominatim.git] / lib / log.php
1 <?php
2
3         function logStart(&$oDB, $sType = '', $sQuery = '', $aLanguageList = array())
4         {
5                 $aStartTime = explode('.',microtime(true));
6                 if (!isset($aStartTime[1])) $aStartTime[1] = '0';
7
8                 if (isset($_GET['email']))
9                                 $sUserAgent = $_GET['email'];
10                         elseif (isset($_SERVER['HTTP_REFERER']))
11                                 $sUserAgent = $_SERVER['HTTP_REFERER'];
12                         elseif (isset($_SERVER['HTTP_USER_AGENT']))
13                                 $sUserAgent = $_SERVER['HTTP_USER_AGENT'];
14                         else
15                                 $sUserAgent = '';
16
17                 $sOutputFormat = '';
18                 if (isset($_GET['format'])) $sOutputFormat = $_GET['format'];
19
20                 if ($sType == 'reverse')
21                 {
22                         $sOutQuery = (isset($_GET['lat'])?$_GET['lat']:'').'/';
23                         if (isset($_GET['lon'])) $sOutQuery .= $_GET['lon'];
24                         if (isset($_GET['zoom'])) $sOutQuery .= '/'.$_GET['zoom'];
25                 }
26                 else
27                         $sOutQuery = $sQuery;
28
29                 $hLog = array(
30                                 date('Y-m-d H:i:s',$aStartTime[0]).'.'.$aStartTime[1],
31                                 $_SERVER["REMOTE_ADDR"],
32                                 $_SERVER['QUERY_STRING'],
33                                 $sOutQuery,
34                                 $sType,
35                                 $sUserAgent,
36                                 $sOutputFormat
37                                 );
38
39                 if (CONST_Log_DB)
40                 {
41                         if (isset($_GET['email']))
42                                 $sUserAgent = $_GET['email'];
43                         elseif (isset($_SERVER['HTTP_REFERER']))
44                                 $sUserAgent = $_SERVER['HTTP_REFERER'];
45                         elseif (isset($_SERVER['HTTP_USER_AGENT']))
46                                 $sUserAgent = $_SERVER['HTTP_USER_AGENT'];
47                         else
48                                 $sUserAgent = '';
49                         $sSQL = 'insert into new_query_log (type,starttime,query,ipaddress,useragent,language,format,searchterm)';
50                         $sSQL .= ' values ('.getDBQuoted($sType).','.getDBQuoted($hLog[0]).','.getDBQuoted($hLog[2]);
51                         $sSQL .= ','.getDBQuoted($hLog[1]).','.getDBQuoted($sUserAgent).','.getDBQuoted(join(',',$aLanguageList)).','.getDBQuoted($sOutputFormat).','.getDBQuoted($hLog[3]).')';
52                         $oDB->query($sSQL);
53                 }
54
55                 return $hLog;
56         }
57
58         function logEnd(&$oDB, $hLog, $iNumResults)
59         {
60                 $aEndTime = explode('.',microtime(true));
61                 if (!$aEndTime[1]) $aEndTime[1] = '0';
62                 $sEndTime = date('Y-m-d H:i:s',$aEndTime[0]).'.'.$aEndTime[1];
63
64                 if (CONST_Log_DB)
65                 {
66                         $sSQL = 'update new_query_log set endtime = '.getDBQuoted($sEndTime).', results = '.$iNumResults;
67                         $sSQL .= ' where starttime = '.getDBQuoted($hLog[0]);
68                         $sSQL .= ' and ipaddress = '.getDBQuoted($hLog[1]);
69                         $sSQL .= ' and query = '.getDBQuoted($hLog[2]);
70                         $oDB->query($sSQL);
71                 }
72
73                 if (CONST_Log_File)
74                 {
75                         array_unshift($hLog, $sEndTime);
76                         $hLog[] = $iNumResults;
77                         $fLog = fopen(CONST_Log_File, 'a');
78                         flock($fLog, LOCK_EX);
79                         fputcsv($fLog, $hLog);
80                         fflush($fLog);
81                         flock($fLog, LOCK_UN);
82                         fclose($fLog);
83                 }
84
85         }