]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/DebugHtml.php
e402654f5d03ccd688e15fa2222e836ca655fdb1
[nominatim.git] / lib-php / DebugHtml.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 namespace Nominatim;
12
13 class Debug
14 {
15     public static function newFunction($sHeading)
16     {
17         echo "<pre><h2>Debug output for $sHeading</h2></pre>\n";
18     }
19
20     public static function newSection($sHeading)
21     {
22         echo "<hr><pre><h3>$sHeading</h3></pre>\n";
23     }
24
25     public static function printVar($sHeading, $mVar)
26     {
27         echo '<pre><b>'.$sHeading. ':</b>  ';
28         Debug::outputVar($mVar, str_repeat(' ', strlen($sHeading) + 3));
29         echo "</pre>\n";
30     }
31
32     public static function fmtArrayVals($aArr)
33     {
34         return array('__debug_format' => 'array_vals', 'data' => $aArr);
35     }
36
37     public static function printDebugArray($sHeading, $oVar)
38     {
39
40         if ($oVar === null) {
41             Debug::printVar($sHeading, 'null');
42         } else {
43             Debug::printVar($sHeading, $oVar->debugInfo());
44         }
45     }
46
47     public static function printDebugTable($sHeading, $aVar)
48     {
49         echo '<b>'.$sHeading.":</b>\n";
50         echo "<table border='1'>\n";
51         if (!empty($aVar)) {
52             echo "  <tr>\n";
53             $aKeys = array();
54             $aInfo = reset($aVar);
55             if (!is_array($aInfo)) {
56                 $aInfo = $aInfo->debugInfo();
57             }
58             foreach ($aInfo as $sKey => $mVal) {
59                 echo '    <th><small>'.$sKey.'</small></th>'."\n";
60                 $aKeys[] = $sKey;
61             }
62             echo "  </tr>\n";
63             foreach ($aVar as $oRow) {
64                 $aInfo = $oRow;
65                 if (!is_array($oRow)) {
66                     $aInfo = $oRow->debugInfo();
67                 }
68                 echo "  <tr>\n";
69                 foreach ($aKeys as $sKey) {
70                     echo '    <td><pre>';
71                     if (isset($aInfo[$sKey])) {
72                         Debug::outputVar($aInfo[$sKey], '');
73                     }
74                     echo '</pre></td>'."\n";
75                 }
76                 echo "  </tr>\n";
77             }
78         }
79         echo "</table>\n";
80     }
81
82     public static function printGroupedSearch($aSearches, $aWordsIDs)
83     {
84         echo '<table border="1">';
85         echo '<tr><th>rank</th><th>Name Tokens</th><th>Name Not</th>';
86         echo '<th>Address Tokens</th><th>Address Not</th>';
87         echo '<th>country</th><th>operator</th>';
88         echo '<th>class</th><th>type</th><th>postcode</th><th>housenumber</th></tr>';
89         foreach ($aSearches as $aRankedSet) {
90             foreach ($aRankedSet as $aRow) {
91                 $aRow->dumpAsHtmlTableRow($aWordsIDs);
92             }
93         }
94         echo '</table>';
95     }
96
97     public static function printGroupTable($sHeading, $aVar)
98     {
99         echo '<b>'.$sHeading.":</b>\n";
100         echo "<table border='1'>\n";
101         if (!empty($aVar)) {
102             echo "  <tr>\n";
103             echo '    <th><small>Group</small></th>'."\n";
104             $aKeys = array();
105             $aInfo = reset($aVar)[0];
106             if (!is_array($aInfo)) {
107                 $aInfo = $aInfo->debugInfo();
108             }
109             foreach ($aInfo as $sKey => $mVal) {
110                 echo '    <th><small>'.$sKey.'</small></th>'."\n";
111                 $aKeys[] = $sKey;
112             }
113             echo "  </tr>\n";
114             foreach ($aVar as $sGrpKey => $aGroup) {
115                 foreach ($aGroup as $oRow) {
116                     $aInfo = $oRow;
117                     if (!is_array($oRow)) {
118                         $aInfo = $oRow->debugInfo();
119                     }
120                     echo "  <tr>\n";
121                     echo '    <td><pre>'.$sGrpKey.'</pre></td>'."\n";
122                     foreach ($aKeys as $sKey) {
123                         echo '    <td><pre>';
124                         if (!empty($aInfo[$sKey])) {
125                             Debug::outputVar($aInfo[$sKey], '');
126                         }
127                         echo '</pre></td>'."\n";
128                     }
129                     echo "  </tr>\n";
130                 }
131             }
132         }
133         echo "</table>\n";
134     }
135
136     public static function printSQL($sSQL)
137     {
138         echo '<p><tt><font color="#aaa">'.htmlspecialchars($sSQL).'</font></tt></p>'."\n";
139     }
140
141     private static function outputVar($mVar, $sPreNL)
142     {
143         if (is_array($mVar) && !isset($mVar['__debug_format'])) {
144             $sPre = '';
145             foreach ($mVar as $mKey => $aValue) {
146                 echo $sPre;
147                 $iKeyLen = Debug::outputSimpleVar($mKey);
148                 echo ' => ';
149                 Debug::outputVar(
150                     $aValue,
151                     $sPreNL.str_repeat(' ', $iKeyLen + 4)
152                 );
153                 $sPre = "\n".$sPreNL;
154             }
155         } elseif (is_array($mVar) && isset($mVar['__debug_format'])) {
156             if (!empty($mVar['data'])) {
157                 $sPre = '';
158                 foreach ($mVar['data'] as $mValue) {
159                     echo $sPre;
160                     Debug::outputSimpleVar($mValue);
161                     $sPre = ', ';
162                 }
163             }
164         } elseif (is_object($mVar) && method_exists($mVar, 'debugInfo')) {
165             Debug::outputVar($mVar->debugInfo(), $sPreNL);
166         } elseif (is_a($mVar, 'stdClass')) {
167             Debug::outputVar(json_decode(json_encode($mVar), true), $sPreNL);
168         } else {
169             Debug::outputSimpleVar($mVar);
170         }
171     }
172
173     private static function outputSimpleVar($mVar)
174     {
175         if (is_bool($mVar)) {
176             echo '<i>'.($mVar ? 'True' : 'False').'</i>';
177             return $mVar ? 4 : 5;
178         }
179
180         if (is_string($mVar)) {
181             $sOut = "'$mVar'";
182         } else {
183             $sOut = (string)$mVar;
184         }
185
186         echo htmlspecialchars($sOut);
187         return strlen($sOut);
188     }
189 }