]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/log.php
Merge pull request #3367 from lonvia/address-word-counts
[nominatim.git] / lib-php / log.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11
12 function logStart(&$oDB, $sType = '', $sQuery = '', $aLanguageList = array())
13 {
14     $fStartTime = microtime(true);
15     $aStartTime = explode('.', $fStartTime);
16     if (!isset($aStartTime[1])) {
17         $aStartTime[1] = '0';
18     }
19
20     $sOutputFormat = '';
21     if (isset($_GET['format'])) {
22         $sOutputFormat = $_GET['format'];
23     }
24
25     if ($sType == 'reverse') {
26         $sOutQuery = (isset($_GET['lat'])?$_GET['lat']:'').'/';
27         if (isset($_GET['lon'])) {
28             $sOutQuery .= $_GET['lon'];
29         }
30         if (isset($_GET['zoom'])) {
31             $sOutQuery .= '/'.$_GET['zoom'];
32         }
33     } else {
34         $sOutQuery = $sQuery;
35     }
36
37     $hLog = array(
38              date('Y-m-d H:i:s', $aStartTime[0]).'.'.$aStartTime[1],
39              $_SERVER['REMOTE_ADDR'],
40              $_SERVER['QUERY_STRING'],
41              $sOutQuery,
42              $sType,
43              $fStartTime
44             );
45
46     if (CONST_Log_DB) {
47         if (isset($_GET['email'])) {
48             $sUserAgent = $_GET['email'];
49         } elseif (isset($_SERVER['HTTP_REFERER'])) {
50             $sUserAgent = $_SERVER['HTTP_REFERER'];
51         } elseif (isset($_SERVER['HTTP_USER_AGENT'])) {
52             $sUserAgent = $_SERVER['HTTP_USER_AGENT'];
53         } else {
54             $sUserAgent = '';
55         }
56         $sSQL = 'insert into new_query_log (type,starttime,query,ipaddress,useragent,language,format,searchterm)';
57         $sSQL .= ' values (';
58         $sSQL .= join(',', $oDB->getDBQuotedList(array(
59             $sType,
60             $hLog[0],
61             $hLog[2],
62             $hLog[1],
63             $sUserAgent,
64             join(',', $aLanguageList),
65             $sOutputFormat,
66             $hLog[3]
67         )));
68         $sSQL .= ')';
69         $oDB->exec($sSQL);
70     }
71
72     return $hLog;
73 }
74
75 function logEnd(&$oDB, $hLog, $iNumResults)
76 {
77     $fEndTime = microtime(true);
78
79     if (CONST_Log_DB) {
80         $aEndTime = explode('.', $fEndTime);
81         if (!isset($aEndTime[1])) {
82             $aEndTime[1] = '0';
83         }
84         $sEndTime = date('Y-m-d H:i:s', $aEndTime[0]).'.'.$aEndTime[1];
85
86         $sSQL = 'update new_query_log set endtime = '.$oDB->getDBQuoted($sEndTime).', results = '.$iNumResults;
87         $sSQL .= ' where starttime = '.$oDB->getDBQuoted($hLog[0]);
88         $sSQL .= ' and ipaddress = '.$oDB->getDBQuoted($hLog[1]);
89         $sSQL .= ' and query = '.$oDB->getDBQuoted($hLog[2]);
90         $oDB->exec($sSQL);
91     }
92
93     if (CONST_Log_File) {
94         $aOutdata = sprintf(
95             "[%s] %.4f %d %s \"%s\"\n",
96             $hLog[0],
97             $fEndTime-$hLog[5],
98             $iNumResults,
99             $hLog[4],
100             $hLog[2]
101         );
102         file_put_contents(CONST_Log_File, $aOutdata, FILE_APPEND | LOCK_EX);
103     }
104 }