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