]> git.openstreetmap.org Git - nominatim.git/blob - lib/DebugHtml.php
ce2b8361c3fa5bf211846bfe5cc347c9eea1e2bb
[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">';
43         if (!empty($aVar)) {
44             echo '<tr>';
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>';
52                 $aKeys[] = $sKey;
53             }
54             echo '</tr>';
55             foreach ($aVar as $oRow) {
56                 $aInfo = $oRow;
57                 if (!is_array($oRow)) {
58                     $aInfo = $oRow->debugInfo();
59                 }
60                 echo '<tr>';
61                 foreach ($aKeys as $sKey) {
62                     echo '<td><pre>';
63                     if (isset($aInfo[$sKey])) {
64                         Debug::outputVar($aInfo[$sKey], '');
65                     }
66                     echo '</pre></td>';
67                 }
68                 echo '<tr>';
69             }
70         }
71         echo '</table>';
72     }
73
74     public static function printGroupTable($sHeading, $aVar)
75     {
76         echo '<b>'.$sHeading.":</b>\n";
77         echo '<table border="1">';
78         if (!empty($aVar)) {
79             echo '<tr><th><small>Group</small></th>';
80             $aKeys = array();
81             $aInfo = reset(reset($aVar));
82             if (!is_array($aInfo)) {
83                 $aInfo = $aInfo->debugInfo();
84             }
85             foreach ($aInfo as $sKey => $mVal) {
86                 echo '<th><small>'.$sKey.'</small></th>';
87                 $aKeys[] = $sKey;
88             }
89             echo '</tr>';
90             foreach ($aVar as $sGrpKey => $aGroup) {
91                 foreach ($aGroup as $oRow) {
92                     $aInfo = $oRow;
93                     if (!is_array($oRow)) {
94                         $aInfo = $oRow->debugInfo();
95                     }
96                     echo '<tr><td><pre>'.$sGrpKey.'</pre></td>';
97                     foreach ($aKeys as $sKey) {
98                         echo '<td><pre>';
99                         if (!empty($aInfo[$sKey])) {
100                             Debug::outputVar($aInfo[$sKey], '');
101                         }
102                         echo '</pre></td>';
103                     }
104                     echo '<tr>';
105                 }
106             }
107         }
108         echo '</table>';
109     }
110
111     public static function printSQL($sSQL)
112     {
113         echo '<p><tt><font color="#aaa">'.$sSQL.'</font></tt></p>'."\n";
114     }
115
116     private static function outputVar($mVar, $sPreNL)
117     {
118         if (is_array($mVar) && !isset($mVar['__debug_format'])) {
119             $sPre = '';
120             foreach ($mVar as $mKey => $aValue) {
121                 echo $sPre;
122                 $iKeyLen = Debug::outputSimpleVar($mKey);
123                 echo ' => ';
124                 Debug::outputVar(
125                     $aValue,
126                     $sPreNL.str_repeat(' ', $iKeyLen + 4)
127                 );
128                 $sPre = "\n".$sPreNL;
129             }
130         } elseif (is_array($mVar) && isset($mVar['__debug_format'])) {
131             if (!empty($mVar[data])) {
132                 $sPre = '';
133                 foreach ($mVar[data] as $mValue) {
134                     echo $sPre;
135                     Debug::outputSimpleVar($mValue);
136                     $sPre = ', ';
137                 }
138             }
139         } else {
140             Debug::outputSimpleVar($mVar);
141         }
142     }
143
144     private static function outputSimpleVar($mVar)
145     {
146         if (is_bool($mVar)) {
147             echo '<i>'.($mVar ? 'True' : 'False').'</i>';
148             return $mVar ? 4 : 5;
149         }
150
151         if (is_string($mVar)) {
152             echo "'$mVar'";
153             return strlen($mVar) + 2;
154         }
155
156         echo (string)$mVar;
157         return strlen((string)$mVar);
158     }
159 }