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