[FEATURE] Execute native prepared queries
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / Classes / Controller / ModuleController.php
1 <?php
2 namespace TYPO3\CMS\Dbal\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2004-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * (c) 2004-2013 Karsten Dambekalns (karsten@typo3.org)
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 use TYPO3\CMS\Backend\Utility\BackendUtility;
29
30 /**
31 * Script class; Backend module for DBAL extension
32 *
33 * @author Kasper Skårhøj <kasper@typo3.com>
34 * @author Karsten Dambekalns <karsten@typo3.org>
35 */
36 class ModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClass {
37
38 /**
39 * @var string
40 */
41 protected $thisScript;
42
43 /**
44 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
45 *
46 * @return void
47 */
48 public function menuConfig() {
49 $this->MOD_MENU = array(
50 'function' => array(
51 0 => $GLOBALS['LANG']->getLL('Debug_log'),
52 'info' => $GLOBALS['LANG']->getLL('Cached_info'),
53 'sqlcheck' => $GLOBALS['LANG']->getLL('SQL_check')
54 )
55 );
56 parent::menuConfig();
57 }
58
59 /**
60 * Main function of the module. Write the content to $this->content
61 *
62 * @return void
63 */
64 public function main() {
65 $this->thisScript = BackendUtility::getModuleUrl($this->MCONF['name']);
66 // Clean up settings:
67 $this->MOD_SETTINGS = BackendUtility::getModuleData($this->MOD_MENU, \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SET'), $this->MCONF['name']);
68 // Draw the header
69 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
70 $this->doc->backPath = $GLOBALS['BACK_PATH'];
71 $this->doc->form = '<form action="" method="post">';
72 // DBAL page title:
73 $this->content .= $this->doc->startPage($GLOBALS['LANG']->getLL('title'));
74 $this->content .= $this->doc->header($GLOBALS['LANG']->getLL('title'));
75 $this->content .= $this->doc->spacer(5);
76 $this->content .= $this->doc->section('', $this->doc->funcMenu('', BackendUtility::getFuncMenu(0, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function'])));
77 // Debug log:
78 switch ($this->MOD_SETTINGS['function']) {
79 case 'info':
80 $this->content .= $this->doc->section($GLOBALS['LANG']->getLL('Cached_info'), $this->printCachedInfo());
81 break;
82 case 'sqlcheck':
83 $this->content .= $this->doc->section($GLOBALS['LANG']->getLL('SQL_check'), $this->printSqlCheck());
84 break;
85 case 0:
86 $this->content .= $this->doc->section($GLOBALS['LANG']->getLL('Debug_log'), $this->printLogMgm());
87 break;
88 }
89 // ShortCut
90 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
91 $this->content .= $this->doc->spacer(20) . $this->doc->section('', $this->doc->makeShortcutIcon('id', implode(',', array_keys($this->MOD_MENU)), $this->MCONF['name']));
92 }
93 $this->content .= $this->doc->spacer(10);
94 }
95
96 /**
97 * Prints out the module HTML
98 *
99 * @return string HTML output
100 */
101 public function printContent() {
102 $this->content .= $this->doc->endPage();
103 echo $this->content;
104 }
105
106 /**
107 * Displays a form to check DBAL SQL methods and parse raw SQL.
108 *
109 * @return string HTML output
110 */
111 protected function printSqlCheck() {
112 $input = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tx_dbal');
113 $out = '
114 <form name="sql_check" action="' . $this->thisScript . '" method="post" enctype="' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['form_enctype'] . '">
115 <script type="text/javascript">
116 /*<![CDATA[*/
117 function updateQryForm(s) {
118 document.getElementById(\'tx-dbal-result\').style.display = \'none\';
119 switch(s) {
120 case \'SELECT\':
121 document.getElementById(\'tx-dbal-qryupdate\').style.display = \'none\';
122 document.getElementById(\'tx-dbal-qryfields\').style.display = \'table-row\';
123 document.getElementById(\'tx-dbal-qryinsertvalues\').style.display = \'none\';
124 document.getElementById(\'tx-dbal-qryupdatevalues\').style.display = \'none\';
125 document.getElementById(\'tx-dbal-qryfrom\').style.display = \'table-row\';
126 document.getElementById(\'tx-dbal-qryinto\').style.display = \'none\';
127 document.getElementById(\'tx-dbal-qrywhere\').style.display = \'table-row\';
128 document.getElementById(\'tx-dbal-qrygroup\').style.display = \'table-row\';
129 document.getElementById(\'tx-dbal-qryorder\').style.display = \'table-row\';
130 document.getElementById(\'tx-dbal-qrylimit\').style.display = \'table-row\';
131 break;
132 case \'INSERT\':
133 document.getElementById(\'tx-dbal-qryupdate\').style.display = \'none\';
134 document.getElementById(\'tx-dbal-qryfields\').style.display = \'none\';
135 document.getElementById(\'tx-dbal-qryinsertvalues\').style.display = \'table-row\';
136 document.getElementById(\'tx-dbal-qryupdatevalues\').style.display = \'none\';
137 document.getElementById(\'tx-dbal-qryfrom\').style.display = \'none\';
138 document.getElementById(\'tx-dbal-qryinto\').style.display = \'table-row\';
139 document.getElementById(\'tx-dbal-qrywhere\').style.display = \'table-row\';
140 document.getElementById(\'tx-dbal-qrygroup\').style.display = \'table-row\';
141 document.getElementById(\'tx-dbal-qryorder\').style.display = \'table-row\';
142 document.getElementById(\'tx-dbal-qrylimit\').style.display = \'table-row\';
143 break;
144 case \'UPDATE\':
145 document.getElementById(\'tx-dbal-qryupdate\').style.display = \'table-row\';
146 document.getElementById(\'tx-dbal-qryfields\').style.display = \'none\';
147 document.getElementById(\'tx-dbal-qryinsertvalues\').style.display = \'none\';
148 document.getElementById(\'tx-dbal-qryupdatevalues\').style.display = \'table-row\';
149 document.getElementById(\'tx-dbal-qryfrom\').style.display = \'none\';
150 document.getElementById(\'tx-dbal-qryinto\').style.display = \'none\';
151 document.getElementById(\'tx-dbal-qryupdate\').style.display = \'table-row\';
152 document.getElementById(\'tx-dbal-qrywhere\').style.display = \'table-row\';
153 document.getElementById(\'tx-dbal-qrygroup\').style.display = \'none\';
154 document.getElementById(\'tx-dbal-qryorder\').style.display = \'none\';
155 document.getElementById(\'tx-dbal-qrylimit\').style.display = \'none\';
156 break;
157 case \'DELETE\':
158 document.getElementById(\'tx-dbal-qryupdate\').style.display = \'none\';
159 document.getElementById(\'tx-dbal-qryfields\').style.display = \'none\';
160 document.getElementById(\'tx-dbal-qryinsertvalues\').style.display = \'none\';
161 document.getElementById(\'tx-dbal-qryupdatevalues\').style.display = \'none\';
162 document.getElementById(\'tx-dbal-qryfrom\').style.display = \'table-row\';
163 document.getElementById(\'tx-dbal-qryinto\').style.display = \'none\';
164 document.getElementById(\'tx-dbal-qrywhere\').style.display = \'table-row\';
165 document.getElementById(\'tx-dbal-qrygroup\').style.display = \'none\';
166 document.getElementById(\'tx-dbal-qryorder\').style.display = \'none\';
167 document.getElementById(\'tx-dbal-qrylimit\').style.display = \'none\';
168 break;
169 }
170 }
171 /*]]>*/
172 </script>
173 <table>
174 <tr class="tableheader bgColor5"><th colspan="2">Easy SQL check</th></tr>
175 <tr><td colspan="2">
176 <select name="tx_dbal[QUERY]"size="1" onchange="updateQryForm(this.options[this.selectedIndex].value)">
177 <option value="SELECT" ' . ($input['QUERY'] === 'SELECT' ? 'selected="selected"' : '') . '>SELECT</option>
178 <option value="INSERT" ' . ($input['QUERY'] === 'INSERT' ? 'selected="selected"' : '') . '>INSERT</option>
179 <option value="UPDATE" ' . ($input['QUERY'] === 'UPDATE' ? 'selected="selected"' : '') . '>UPDATE</option>
180 <option value="DELETE" ' . ($input['QUERY'] === 'DELETE' ? 'selected="selected"' : '') . '>DELETE</option>
181 </select>
182 </td></tr>
183 <tr id="tx-dbal-qryupdate" style="display:none;"><td></td><td><input name="tx_dbal[UPDATE]" value="' . $input['UPDATE'] . '" type="text" size="30" maxsize="100" /></td></tr>
184 <tr id="tx-dbal-qryfields"><td></td><td><input name="tx_dbal[FIELDS]" value="' . $input['FIELDS'] . '" type="text" size="30" maxsize="100" /></td></tr>
185 <tr id="tx-dbal-qryinsertvalues" style="display:none;"><td></td><td><textarea name="tx_dbal[INSERTVALUES]" cols="30" rows="4">' . $input['INSERTVALUES'] . '</textarea></td></tr>
186 <tr id="tx-dbal-qryupdatevalues" style="display:none;"><th>SET</th><td><textarea name="tx_dbal[UPDATEVALUES]" cols="30" rows="4">' . $input['UPDATEVALUES'] . '</textarea></td></tr>
187 <tr id="tx-dbal-qryfrom"><th>FROM</th><td><input name="tx_dbal[FROM]" value="' . $input['FROM'] . '" type="text" size="30" maxsize="100" /></td></tr>
188 <tr id="tx-dbal-qryinto" style="display:none;"><th>INTO</th><td><input name="tx_dbal[INTO]" value="' . $input['INTO'] . '" type="text" size="30" maxsize="100" /></td></tr>
189 <tr id="tx-dbal-qrywhere"><th>WHERE</th><td><input name="tx_dbal[WHERE]" value="' . $input['WHERE'] . '" type="text" size="30" maxsize="100" /></td></tr>
190 <tr id="tx-dbal-qrygroup"><th>GROUP BY</th><td><input name="tx_dbal[GROUP]" value="' . $input['GROUP'] . '" type="text" size="30" maxsize="100" /></td></tr>
191 <tr id="tx-dbal-qryorder"><th>ORDER BY</th><td><input name="tx_dbal[ORDER]" value="' . $input['ORDER'] . '" type="text" size="30" maxsize="100" /></td></tr>
192 <tr id="tx-dbal-qrylimit"><th>LIMIT</th><td><input name="tx_dbal[LIMIT]" value="' . $input['LIMIT'] . '" type="text" size="30" maxsize="100" /></td></tr>
193 <tr><td></td><td style="text-align:right;"><input type="submit" value="CHECK" /></td></tr>
194 <script type="text/javascript">
195 /*<![CDATA[*/
196 updateQryForm(\'' . $input['QUERY'] . '\');
197 /*]]>*/
198 </script>
199 ';
200 $out .= '<tr id="tx-dbal-result" class="bgColor4"><th>Result:</th><td>';
201 switch ($input['QUERY']) {
202 case 'SELECT':
203 $qry = $GLOBALS['TYPO3_DB']->SELECTquery($input['FIELDS'], $input['FROM'], $input['WHERE'], $input['GROUP'], $input['ORDER'], $input['LIMIT']);
204 break;
205 case 'INSERT':
206 $qry = $GLOBALS['TYPO3_DB']->INSERTquery($input['INTO'], $this->createFieldsValuesArray($input['INSERTVALUES']));
207 break;
208 case 'UPDATE':
209 $qry = $GLOBALS['TYPO3_DB']->UPDATEquery($input['UPDATE'], $input['WHERE'], $this->createFieldsValuesArray($input['UPDATEVALUES']));
210 break;
211 case 'DELETE':
212 $qry = $GLOBALS['TYPO3_DB']->DELETEquery($input['FROM'], $input['WHERE']);
213 break;
214 }
215 $out .= '<pre>' . htmlspecialchars($qry) . '</pre></td></tr>';
216 $out .= '
217 <tr class="tableheader bgColor5"><th colspan="2">RAW SQL check</th></tr>
218 <tr><td colspan="2" style="text-align:right;"><textarea name="tx_dbal[RAWSQL]" cols="60" rows="5">' . $input['RAWSQL'] . '</textarea><br /><input type="submit" value="CHECK" /></td></tr>';
219 if (!empty($input['RAWSQL'])) {
220 $out .= '<tr class="bgColor4">';
221 $parseResult = $GLOBALS['TYPO3_DB']->SQLparser->parseSQL($input['RAWSQL']);
222 if (is_array($parseResult)) {
223 $newQuery = $GLOBALS['TYPO3_DB']->SQLparser->compileSQL($parseResult);
224 $testResult = $GLOBALS['TYPO3_DB']->SQLparser->debug_parseSQLpartCompare($input['RAWSQL'], $newQuery);
225 if (!is_array($testResult)) {
226 $out .= '<td colspan="2">' . $newQuery;
227 } else {
228 $out .= '<td colspan="2">' . htmlspecialchars($testResult[0]) . '</td></tr>
229 <tr><th>Error:</th><td style="border:2px solid #f00;">Input query did not match the parsed and recompiled query exactly (not observing whitespace):<br />' . htmlspecialchars($testResult[1]);
230 }
231 } else {
232 $out .= '<th>Result:</th><td style="border:2px solid #f00;">' . $parseResult;
233 }
234 $out .= '</td></tr>';
235 }
236 $out .= '</table></form>';
237 return $out;
238 }
239
240 /**
241 * Parses a very simple text format into an array.
242 *
243 * Each line is seen as a key/value pair that is exploded at =. This is used
244 * in the simple SQL check to input values for INSERT and UPDATE statements.
245 *
246 * @param string $in String to parse into key/value array.
247 * @return array Array created from the input string.
248 */
249 protected function createFieldsValuesArray($in) {
250 $ret = array();
251 $in = explode(chr(10), $in);
252 foreach ($in as $v) {
253 $fv = explode('=', $v);
254 $ret[$fv[0]] = $fv[1];
255 }
256 return $ret;
257 }
258
259 /**
260 * Prints out the cached information about the database.
261 *
262 * The DBAL caches a lot of information, e.g. about auto increment fields,
263 * field types and primary keys. This method formats all this into a HTML
264 * table to display in the BE.
265 *
266 * @return string HTML output
267 */
268 protected function printCachedInfo() {
269 // Get cmd:
270 if ((string) \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('cmd') === 'clear') {
271 $GLOBALS['TYPO3_DB']->clearCachedFieldInfo();
272 $GLOBALS['TYPO3_DB']->cacheFieldInfo();
273 }
274 $out = '<a name="autoincrement"></a><h2>auto_increment</h2>';
275 $out .= '<table border="1" cellspacing="0"><tbody><tr><th>Table</th><th>Field</th></tr>';
276 ksort($GLOBALS['TYPO3_DB']->cache_autoIncFields);
277 foreach ($GLOBALS['TYPO3_DB']->cache_autoIncFields as $table => $field) {
278 $out .= '<tr>';
279 $out .= '<td>' . $table . '</td>';
280 $out .= '<td>' . $field . '</td>';
281 $out .= '</tr>';
282 }
283 $out .= '</tbody></table>';
284 $out .= $this->doc->spacer(5);
285 $out .= '<a name="primarykeys"></a><h2>Primary keys</h2>';
286 $out .= '<table border="1" cellspacing="0"><tbody><tr><th>Table</th><th>Field(s)</th></tr>';
287 ksort($GLOBALS['TYPO3_DB']->cache_primaryKeys);
288 foreach ($GLOBALS['TYPO3_DB']->cache_primaryKeys as $table => $field) {
289 $out .= '<tr>';
290 $out .= '<td>' . $table . '</td>';
291 $out .= '<td>' . $field . '</td>';
292 $out .= '</tr>';
293 }
294 $out .= '</tbody></table>';
295 $out .= $this->doc->spacer(5);
296 $out .= '<a name="fieldtypes"></a><h2>Field types</h2>';
297 $out .= '<table border="1" cellspacing="0"><tbody><tr><th colspan="5">Table</th></tr><tr><th>Field</th><th>Type</th><th><a href="#metatypes">Metatype</a></th><th>NOT NULL</th><th>Default</th></th></tr>';
298 ksort($GLOBALS['TYPO3_DB']->cache_fieldType);
299 foreach ($GLOBALS['TYPO3_DB']->cache_fieldType as $table => $fields) {
300 $out .= '<th colspan="5">' . $table . '</th>';
301 foreach ($fields as $field => $data) {
302 $out .= '<tr>';
303 $out .= '<td>' . $field . '</td>';
304 $out .= '<td>' . $data['type'] . '</td>';
305 $out .= '<td>' . $data['metaType'] . '</td>';
306 $out .= '<td>' . ($data['notnull'] ? 'NOT NULL' : '') . '</td>';
307 $out .= '<td>' . $data['default'] . '</td>';
308 $out .= '</tr>';
309 }
310 }
311 $out .= '</tbody></table>';
312 $out .= $this->doc->spacer(5);
313 $out .= '<a name="metatypes"></a><h2>Metatype explanation</h2>';
314 $out .= '<pre>
315 C: Varchar, capped to 255 characters.
316 X: Larger varchar, capped to 4000 characters (to be compatible with Oracle).
317 XL: For Oracle, returns CLOB, otherwise the largest varchar size.
318
319 C2: Multibyte varchar
320 X2: Multibyte varchar (largest size)
321
322 B: BLOB (binary large object)
323
324 D: Date (some databases do not support this, and we return a datetime type)
325 T: Datetime or Timestamp
326 L: Integer field suitable for storing booleans (0 or 1)
327 I: Integer (mapped to I4)
328 I1: 1-byte integer
329 I2: 2-byte integer
330 I4: 4-byte integer
331 I8: 8-byte integer
332 F: Floating point number
333 N: Numeric or decimal number</pre>';
334 $menu = '<a href="' . $this->thisScript . '&amp;cmd=clear">CLEAR DATA</a><hr />';
335 $menu .= '<a href="#autoincrement">auto_increment</a> | <a href="#primarykeys">Primary keys</a> | <a href="#fieldtypes">Field types</a> | <a href="#metatypes">Metatype explanation</a><hr />';
336 return $menu . $out;
337 }
338
339 /**
340 * Printing the debug-log from the DBAL extension
341 *
342 * To enabled debugging, you will have to enabled it in the configuration!
343 *
344 * @return string HTML content
345 */
346 protected function printLogMgm() {
347 // Disable debugging in any case...
348 $GLOBALS['TYPO3_DB']->debug = FALSE;
349 // Get cmd:
350 $cmd = (string) \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('cmd');
351 switch ($cmd) {
352 case 'flush':
353 $res = $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('tx_dbal_debuglog');
354 $res = $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery('tx_dbal_debuglog_where');
355 $outStr = 'Log FLUSHED!';
356 break;
357 case 'joins':
358 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('table_join,exec_time,query,script', 'tx_dbal_debuglog', 'table_join!=\'\'', 'table_join,script,exec_time,query');
359 // Init vars in which to pick up the query result:
360 $tableIndex = array();
361 $tRows = array();
362 $tRows[] = '
363 <tr>
364 <td>Execution time</td>
365 <td>Table joins</td>
366 <td>Script</td>
367 <td>Query</td>
368 </tr>';
369 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
370 $tableArray = $GLOBALS['TYPO3_DB']->SQLparser->parseFromTables($row['table_join']);
371 // Create table name index:
372 foreach ($tableArray as $a) {
373 foreach ($tableArray as $b) {
374 if ($b['table'] != $a['table']) {
375 $tableIndex[$a['table']][$b['table']] = 1;
376 }
377 }
378 }
379 // Create output row
380 $tRows[] = '
381 <tr>
382 <td>' . htmlspecialchars($row['exec_time']) . '</td>
383 <td>' . htmlspecialchars($row['table_join']) . '</td>
384 <td>' . htmlspecialchars($row['script']) . '</td>
385 <td>' . htmlspecialchars($row['query']) . '</td>
386 </tr>';
387 }
388 // Printing direct joins:
389 $outStr .= '<h4>Direct joins:</h4>' . \TYPO3\CMS\Core\Utility\DebugUtility::viewArray($tableIndex);
390 // Printing total dependencies:
391 foreach ($tableIndex as $priTable => $a) {
392 foreach ($tableIndex as $tableN => $v) {
393 foreach ($v as $tableP => $vv) {
394 if ($tableP == $priTable) {
395 $tableIndex[$priTable] = array_merge($v, $a);
396 }
397 }
398 }
399 }
400 $outStr .= '<h4>Total dependencies:</h4>' . \TYPO3\CMS\Core\Utility\DebugUtility::viewArray($tableIndex);
401 // Printing data rows:
402 $outStr .= '
403 <table border="1" cellspacing="0">' . implode('', $tRows) . '
404 </table>';
405 break;
406 case 'errors':
407 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('serdata,exec_time,query,script', 'tx_dbal_debuglog', 'errorFlag>0', '', 'tstamp DESC');
408 // Init vars in which to pick up the query result:
409 $tRows = array();
410 $tRows[] = '
411 <tr>
412 <td>Execution time</td>
413 <td>Error data</td>
414 <td>Script</td>
415 <td>Query</td>
416 </tr>';
417 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
418 // Create output row
419 $tRows[] = '
420 <tr>
421 <td>' . htmlspecialchars($row['exec_time']) . '</td>
422 <td>' . \TYPO3\CMS\Core\Utility\DebugUtility::viewArray(unserialize($row['serdata'])) . '</td>
423 <td>' . htmlspecialchars($row['script']) . '</td>
424 <td>' . htmlspecialchars($row['query']) . '</td>
425 </tr>';
426 }
427 // Printing data rows:
428 $outStr .= '
429 <table border="1" cellspacing="0">' . implode('', $tRows) . '
430 </table>';
431 break;
432 case 'parsing':
433 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('query,serdata', 'tx_dbal_debuglog', 'errorFlag&2=2');
434 $tRows = array();
435 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
436 // Create output row
437 $tRows[] = '
438 <tr>
439 <td>' . htmlspecialchars($row['query']) . '</td>
440 </tr>';
441 }
442 // Printing data rows:
443 $outStr .= '
444 <table border="1" cellspacing="0">' . implode('', $tRows) . '
445 </table>';
446 break;
447 case 'where':
448 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tstamp,script,tablename,whereclause', 'tx_dbal_debuglog_where', '', '', 'tstamp DESC');
449 $tRows = array();
450 $tRows[] = '
451 <tr>
452 <td>Time</td>
453 <td>Script</td>
454 <td>Table</td>
455 <td>WHERE clause</td>
456 </tr>';
457 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
458 $tRows[] = '
459 <tr>
460 <td>' . BackendUtility::datetime($row['tstamp']) . '</td>
461 <td>' . htmlspecialchars($row['script']) . '</td>
462 <td>' . htmlspecialchars($row['tablename']) . '</td>
463 <td>' . str_replace(array('\'\'', '""', 'IS NULL', 'IS NOT NULL'), array('<span style="background-color:#ff0000;color:#ffffff;padding:2px;font-weight:bold;">\'\'</span>', '<span style="background-color:#ff0000;color:#ffffff;padding:2px;font-weight:bold;">""</span>', '<span style="background-color:#00ff00;color:#ffffff;padding:2px;font-weight:bold;">IS NULL</span>', '<span style="background-color:#00ff00;color:#ffffff;padding:2px;font-weight:bold;">IS NOT NULL</span>'), htmlspecialchars($row['whereclause'])) . '</td>
464 </tr>';
465 }
466 $outStr = '
467 <table border="1" cellspacing="0">' . implode('', $tRows) . '
468 </table>';
469 break;
470 default:
471 // Look for request to view specific script exec:
472 $specTime = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('specTime');
473 if ($specTime) {
474 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('exec_time,errorFlag,table_join,serdata,query', 'tx_dbal_debuglog', 'tstamp=' . (int)$specTime);
475 $tRows = array();
476 $tRows[] = '
477 <tr>
478 <td>Execution time</td>
479 <td>Error</td>
480 <td>Table joins</td>
481 <td>Data</td>
482 <td>Query</td>
483 </tr>';
484 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
485 $tRows[] = '
486 <tr>
487 <td>' . htmlspecialchars($row['exec_time']) . '</td>
488 <td>' . ($row['errorFlag'] ? 1 : 0) . '</td>
489 <td>' . htmlspecialchars($row['table_join']) . '</td>
490 <td>' . \TYPO3\CMS\Core\Utility\DebugUtility::viewArray(unserialize($row['serdata'])) . '</td>
491 <td>' . str_replace(array('\'\'', '""', 'IS NULL', 'IS NOT NULL'), array('<span style="background-color:#ff0000;color:#ffffff;padding:2px;font-weight:bold;">\'\'</span>', '<span style="background-color:#ff0000;color:#ffffff;padding:2px;font-weight:bold;">""</span>', '<span style="background-color:#00ff00;color:#ffffff;padding:2px;font-weight:bold;">IS NULL</span>', '<span style="background-color:#00ff00;color:#ffffff;padding:2px;font-weight:bold;">IS NOT NULL</span>'), htmlspecialchars($row['query'])) . '</td>
492 </tr>';
493 }
494 } else {
495 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tstamp,script, SUM(exec_time) as calc_sum, count(*) AS qrycount, MAX(errorFlag) as error', 'tx_dbal_debuglog', '', 'tstamp,script', 'tstamp DESC');
496 $tRows = array();
497 $tRows[] = '
498 <tr>
499 <td>Time</td>
500 <td># of queries</td>
501 <td>Error</td>
502 <td>Time (ms)</td>
503 <td>Script</td>
504 </tr>';
505 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
506 $tRows[] = '
507 <tr>
508 <td>' . BackendUtility::datetime($row['tstamp']) . '</td>
509 <td>' . htmlspecialchars($row['qrycount']) . '</td>
510 <td>' . ($row['error'] ? '<strong style="color:#f00">ERR</strong>' : '') . '</td>
511 <td>' . htmlspecialchars($row['calc_sum']) . '</td>
512 <td><a href="' . $this->thisScript . '&amp;specTime=' . (int)$row['tstamp'] . '">' . htmlspecialchars($row['script']) . '</a></td>
513 </tr>';
514 }
515 }
516 $outStr = '
517 <table border="1" cellspacing="0">' . implode('', $tRows) . '
518 </table>';
519 }
520 $menu = '
521 <a href="' . $this->thisScript . '&amp;cmd=flush">FLUSH LOG</a> -
522 <a href="' . $this->thisScript . '&amp;cmd=joins">JOINS</a> -
523 <a href="' . $this->thisScript . '&amp;cmd=errors">ERRORS</a> -
524 <a href="' . $this->thisScript . '&amp;cmd=parsing">PARSING</a> -
525 <a href="' . $this->thisScript . '">LOG</a> -
526 <a href="' . $this->thisScript . '&amp;cmd=where">WHERE</a> -
527
528 <a href="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::linkThisScript()) . '" target="tx_debuglog">[New window]</a>
529 <hr />
530 ';
531 return $menu . $outStr;
532 }
533
534 }