PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/framework/data/SQLite3/Debug.php

https://github.com/hinablue/TextCube
PHP | 479 lines | 416 code | 52 blank | 11 comment | 74 complexity | 7195418d35ce976be9a5d807168648b4 MD5 | raw file
Possible License(s): ISC, GPL-2.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, MIT
  1. <?php
  2. /// Copyright (c) 2004-2016, Needlworks / Tatter Network Foundation
  3. /// All rights reserved. Licensed under the GPL.
  4. /// See the GNU General Public License for more details. (/documents/LICENSE, /documents/COPYRIGHT)
  5. define('TCDEBUG', true);
  6. set_error_handler("__error");
  7. function __error($errno, $errstr, $errfile, $errline) {
  8. if (in_array($errno, array(2048))) {
  9. return;
  10. }
  11. print("$errstr($errno)<br />");
  12. print("File: $errfile:$errline<br /><hr size='1' />");
  13. }
  14. global $__tcSqlLog;
  15. global $__tcSqlLogCount;
  16. global $__tcSqlQueryBeginTime;
  17. global $__tcPageStartTime;
  18. global $__tcPageEndTime;
  19. $__tcPageStartTime = explode(' ', microtime());
  20. $__tcSqlLog = array();
  21. $__tcSqlLogCount = 0;
  22. function __tcSqlLogBegin($sql) {
  23. global $__tcSqlLog, $__tcSqlQueryBeginTime, $__tcSqlLogCount;
  24. $backtrace = debug_backtrace();
  25. array_shift($backtrace);
  26. array_shift($backtrace);
  27. $__tcSqlLog[$__tcSqlLogCount] = array('sql' => trim($sql), 'backtrace' => $backtrace);
  28. $__tcSqlQueryBeginTime = explode(' ', microtime());
  29. //dumpAsFile($sql);
  30. }
  31. function __tcSqlLogEnd($result, $cachedResult = 0) {
  32. global $__tcSqlLog, $__tcSqlQueryBeginTime, $__tcSqlLogCount, $__tcPageStartTime;
  33. static $client_encoding = '';
  34. $tcSqlQueryEndTime = explode(' ', microtime());
  35. $elapsed = ($tcSqlQueryEndTime[1] - $__tcSqlQueryBeginTime[1]) + ($tcSqlQueryEndTime[0] - $__tcSqlQueryBeginTime[0]);
  36. if (!$client_encoding) {
  37. $client_encoding = 'utf8';
  38. // $client_encoding = str_replace('_','-',mysqli_client_encoding(POD::$db));
  39. }
  40. if ($client_encoding != 'utf8' && function_exists('iconv')) {
  41. $__tcSqlLog[$__tcSqlLogCount]['error'] = iconv($client_encoding, 'utf-8', POD::$db->lastErrorMsg());
  42. } else {
  43. $__tcSqlLog[$__tcSqlLogCount]['error'] = POD::$db->lastErrorMsg();
  44. }
  45. $__tcSqlLog[$__tcSqlLogCount]['errno'] = POD::$db->lastErrorCode();
  46. if ($cachedResult == 0) {
  47. $__tcSqlLog[$__tcSqlLogCount]['elapsed'] = ceil($elapsed * 10000) / 10;
  48. } else {
  49. $__tcSqlLog[$__tcSqlLogCount]['elapsed'] = 0;
  50. }
  51. $__tcSqlLog[$__tcSqlLogCount]['elapsed'] = sprintf("%4.1f", $__tcSqlLog[$__tcSqlLogCount]['elapsed']);
  52. $__tcSqlLog[$__tcSqlLogCount]['cached'] = $cachedResult;
  53. $__tcSqlLog[$__tcSqlLogCount]['rows'] = 0;
  54. $__tcSqlLog[$__tcSqlLogCount]['endtime'] = ($tcSqlQueryEndTime[1] - $__tcPageStartTime[1]) + ($tcSqlQueryEndTime[0] - $__tcPageStartTime[0]);
  55. $__tcSqlLog[$__tcSqlLogCount]['endtime'] = sprintf("%4.1f", ceil($__tcSqlLog[$__tcSqlLogCount]['endtime'] * 10000) / 10);
  56. if (!$cachedResult && POD::$db->lastErrorCode() == 0) {
  57. switch (strtolower(substr($__tcSqlLog[$__tcSqlLogCount]['sql'], 0, 6))) {
  58. case 'select':
  59. $__tcSqlLog[$__tcSqlLogCount]['rows'] = __tcSqlQueryRowCount($result);
  60. break;
  61. case 'insert':
  62. case 'delete':
  63. case 'update':
  64. $__tcSqlLog[$__tcSqlLogCount]['rows'] = POD::$db->changes();
  65. break;
  66. }
  67. }
  68. $__tcSqlLogCount++;
  69. $__tcSqlQueryBeginTime = 0;
  70. }
  71. function __tcSqlQueryRowCount($resource) {
  72. $count = 0;
  73. while ($buf = $resource->fetchArray()) {
  74. $count++;
  75. }
  76. $resource->reset();
  77. return $count;
  78. }
  79. function __tcSqlLogPoint($description = null) {
  80. global $__tcSqlLog, $__tcSqlQueryBeginTime, $__tcSqlLogCount, $__tcPageStartTime, $__tcPageEndTime;
  81. if (is_null($description)) {
  82. $description = 'Point';
  83. }
  84. $backtrace = debug_backtrace();
  85. array_shift($backtrace);
  86. array_shift($backtrace);
  87. $__tcSqlLog[$__tcSqlLogCount] = array('sql' => '[' . trim($description) . ']', 'backtrace' => $backtrace);
  88. $__tcSqlQueryBeginTime = explode(' ', microtime());
  89. $tcSqlQueryEndTime = explode(' ', microtime());
  90. $__tcSqlLog[$__tcSqlLogCount]['error'] = '';
  91. $__tcSqlLog[$__tcSqlLogCount]['errno'] = '';
  92. $__tcSqlLog[$__tcSqlLogCount]['elapsed'] = '';
  93. $__tcSqlLog[$__tcSqlLogCount]['cached'] = '';
  94. $__tcSqlLog[$__tcSqlLogCount]['rows'] = '';
  95. $__tcSqlLog[$__tcSqlLogCount]['endtime'] = ($tcSqlQueryEndTime[1] - $__tcPageStartTime[1]) + ($tcSqlQueryEndTime[0] - $__tcPageStartTime[0]);
  96. $__tcSqlLog[$__tcSqlLogCount]['endtime'] = sprintf("%4.1f", ceil($__tcSqlLog[$__tcSqlLogCount]['endtime'] * 10000) / 10);
  97. $__tcPageEndTime = $__tcSqlLog[$__tcSqlLogCount]['endtime'];
  98. $__tcSqlLog[$__tcSqlLogCount]['rows'] = '';
  99. $__tcSqlLogCount++;
  100. $__tcSqlQueryBeginTime = 0;
  101. }
  102. function __tcSqlLoggetCallstack($backtrace, $level = 0) {
  103. $callstack = '';
  104. for ($i = $level; $i < count($backtrace); $i++) {
  105. if (isset($backtrace[$i]['file'])) {
  106. $callstack .= "{$backtrace[$i]['file']}:{$backtrace[$i]['line']}";
  107. if (!empty($backtrace[$i + 1]['type'])) {
  108. $callstack .= " {$backtrace[$i + 1]['class']}{$backtrace[$i + 1]['type']}{$backtrace[$i + 1]['function']}";
  109. } else {
  110. if (isset($backtrace[$i + 1]['function'])) {
  111. $callstack .= " {$backtrace[$i + 1]['function']}";
  112. }
  113. }
  114. $callstack .= "\r\n";
  115. }
  116. }
  117. if (empty($callstack)) {
  118. $callstack = $_SERVER['SCRIPT_FILENAME'] . "\r\n";
  119. }
  120. return $callstack;
  121. }
  122. function __tcSqlLogDump() {
  123. global $__tcSqlLog, $__tcPageEndTime;
  124. global $service, $memcache;
  125. static $sLogPumped = false;
  126. if (!empty($sLogPumped)) {
  127. return;
  128. }
  129. $sLogPumped = true;
  130. __tcSqlLogPoint('shutdown');
  131. $headers = array();
  132. if (function_exists('apache_response_headers') || function_exists('headers_list')) {
  133. if (function_exists('apache_response_headers')) {
  134. flush();
  135. $headers = apache_response_headers();
  136. } else {
  137. $headers = headers_list();
  138. }
  139. }
  140. $commentBlosk = false;
  141. foreach ($headers as $row) {
  142. if (strpos($row, '/xml') !== false || strpos($row, '+xml') !== false) {
  143. /* To check text/xml, application/xml and application/xml+blah, application/blah+xml... types */
  144. $commentBlosk = true;
  145. break;
  146. }
  147. if (strpos($row, 'text/javascript') !== false) {
  148. return;
  149. }
  150. }
  151. if ($commentBlosk == true) {
  152. echo '<!--';
  153. }
  154. if (!$commentBlosk) {
  155. print <<<EOS
  156. <style type='text/css'>
  157. /*<![CDATA[*/
  158. .debugTable
  159. {
  160. background-color: #fff;
  161. border-left: 1px solid #999;
  162. border-top: 1px solid #999;
  163. border-collapse: collapse;
  164. margin-bottom: 20px;
  165. }
  166. .debugTable *
  167. {
  168. border: none;
  169. margin: 0;
  170. padding: 0;
  171. }
  172. .debugTable td, .debugTable th
  173. {
  174. border-bottom: 1px solid #999;
  175. border-right: 1px solid #999;
  176. color: #000;
  177. font-family: Arial, Tahoma, Verdana, sans-serif;
  178. font-size: 12px;
  179. padding: 3px 5px;
  180. }
  181. .debugTable th
  182. {
  183. background-color: #dedede;
  184. text-align: center;
  185. }
  186. tr.debugSQLLine .rows
  187. {
  188. text-align: center;
  189. }
  190. tr.debugSQLLine .error
  191. {
  192. text-align: left;
  193. }
  194. tr.debugSQLLine .elapsed, tr.debugSQLLine .elapsedSum
  195. {
  196. text-align: right;
  197. }
  198. tr.debugSQLLine .backtrace
  199. {
  200. font-family: Courier, 'Courier new', monospace;
  201. font-size: 11px;
  202. letter-spacing: -1px;
  203. }
  204. tr.debugCached *, tr.debugSystem *
  205. {
  206. color: #888888 !important;
  207. }
  208. /* warning */
  209. tr.debugWarning *
  210. {
  211. background-color: #fefff1;
  212. color: #4b4b3b !important;
  213. }
  214. tr.debugWarning th
  215. {
  216. background-color: #e5e5ca;
  217. }
  218. /* error */
  219. tr.debugError *
  220. {
  221. background-color: #fee5e5;
  222. color: #961f1d !important;
  223. }
  224. tr.debugError th
  225. {
  226. background-color: #fccbca;
  227. }
  228. tfoot td
  229. {
  230. padding: 15px !important;
  231. text-align: center;
  232. }
  233. /*]]>*/
  234. </style>
  235. EOS;
  236. }
  237. $elapsed_total_db = 0;
  238. $elapsed = array();
  239. $count = 1;
  240. $cached_count = 0;
  241. foreach ($__tcSqlLog as $c => $log) {
  242. $elapsed[$count] = array($log['elapsed'], $count, $log['cached'] ? "cached" : "");
  243. $__tcSqlLog[$c]['percent'] = sprintf("%4.1f", $log['endtime'] * 100 / $__tcPageEndTime);
  244. $count++;
  245. }
  246. arsort($elapsed);
  247. $bgcolor = array();
  248. foreach (array_splice($elapsed, 0, 5) as $e) {
  249. if ($e[2] != "cached") {
  250. $top5[$e[1]] = true;
  251. }
  252. }
  253. $count = 1;
  254. if (!$commentBlosk) {
  255. print '<table class="debugTable">';
  256. print <<<THEAD
  257. <thead>
  258. <tr>
  259. <th>count</th><th class="sql">query string</th><th>elapsed (ms)</th><th>elapsed sum (ms)</th><th></th><th>rows</th><th>error</th><th>stack</th>
  260. </tr>
  261. </thead>
  262. THEAD;
  263. print '<tbody>';
  264. }
  265. foreach ($__tcSqlLog as $c => $log) {
  266. $error = '';
  267. $backtrace = '';
  268. $frame_count = 1;
  269. $backtrace = __tcSqlLoggetCallstack($log['backtrace']);
  270. if ($log['errno']) {
  271. $error = "Error no. {$log['errno']} : {$log['error']}";
  272. }
  273. $trclass = '';
  274. $count_label = $count;
  275. if (!empty($error)) {
  276. $trclass = ' debugError';
  277. } else {
  278. if (isset($top5[$count])) {
  279. $trclass = ' debugWarning';
  280. } else {
  281. if ($log['cached'] == 1) {
  282. $error = "(cached)";
  283. $trclass .= ' debugCached';
  284. $cached_count++;
  285. } else {
  286. if ($log['cached'] == 2) {
  287. $error = "";
  288. $trclass .= ' debugCached';
  289. $count_label = '';
  290. $backtrace = '';
  291. }
  292. }
  293. }
  294. }
  295. if ($log['sql'] == '[shutdown]') {
  296. $error = "";
  297. $log['sql'] = 'Shutdown';
  298. $trclass .= ' debugSystem';
  299. $count_label = '';
  300. $backtrace = '';
  301. }
  302. $elapsed_total_db += $log['elapsed'];
  303. $elapsed_total = $log['endtime'];
  304. $progress_bar = $log['percent'] / 2; //Max 50px;
  305. if (!$commentBlosk) {
  306. $log['sql'] = htmlspecialchars($log['sql']);
  307. $log['percent'] = "<div style='background:#f00;line-height:10px;width:{$progress_bar}px'>&nbsp;</div>";
  308. print <<<TBODY
  309. <tr class="debugSQLLine{$trclass}">
  310. <th>{$count_label}</th>
  311. <td class="code"><code>{$log['sql']}</code></td>
  312. <td class="elapsed">{$log['elapsed']}</td>
  313. <td class="elapsedSum">{$log['endtime']}</td>
  314. <td class="elapsedSum">{$log['percent']}</td>
  315. <td class="rows">{$log['rows']}</td>
  316. <td class="error">{$error}</td>
  317. <td class="backtrace"><pre>{$backtrace}</pre></td>
  318. </tr>
  319. TBODY;
  320. } else {
  321. $log['sql'] = str_replace('-->', '-- >', $log['sql']);
  322. print <<<TBODY
  323. ===============================================================================================
  324. $count_label:{$log['sql']}
  325. Elapsed:{$log['elapsed']} ms/End time:{$log['endtime']}/Percent:{$log['percent']}/Rows:{$log['rows']} rows
  326. {$error}
  327. {$backtrace}
  328. TBODY;
  329. }
  330. if ($log['cached'] < 2) {
  331. $count++;
  332. }
  333. }
  334. $count--;
  335. $real_query_count = $count - $cached_count;
  336. if (!$commentBlosk) {
  337. print '</tbody>';
  338. print <<<TFOOT
  339. <tfoot>
  340. <tr>
  341. <td colspan='8'>
  342. $count ($real_query_count+$cached_count cache) Queries <br />
  343. $elapsed_total_db ms elapsed in db query, overall $elapsed_total ms elapsed
  344. </td>
  345. </tr>
  346. </tfoot>
  347. TFOOT;
  348. print '</table>';
  349. }
  350. global $service, $URLInfo, $suri;
  351. print '<div class="debugTable">' . CRLF;
  352. print '<h4>Current Database Management System :</h4>' . CRLF . '<p>' . POD::dbms() . ' ' . POD::version() . '</p>' . CRLF;
  353. print '<h4>SSL support : </h4>' . CRLF;
  354. if (isset($service['useSSL']) && $service['useSSL'] == true) {
  355. print '<p>Enabled</p>' . CRLF;
  356. } else {
  357. print '<p>Disabled</p>' . CRLF;
  358. }
  359. print '<h4>Cache system :</h4>' . CRLF;
  360. if (isset($service['pagecache']) && $service['pagecache'] == true) {
  361. print '<p>Page cache Enabled</p>' . CRLF;
  362. } else {
  363. print '<p>Page cache Disabled</p>' . CRLF;
  364. }
  365. if (isset($service['skincache']) && $service['skincache'] == true) {
  366. print '<p>Skin cache Enabled</p>' . CRLF;
  367. } else {
  368. print '<p>Skin cache Disabled</p>' . CRLF;
  369. }
  370. if (isset($service['memcached']) && $service['memcached'] == true) {
  371. print '<p>Memcached module Enabled</p>' . CRLF;
  372. } else {
  373. print '<p>Memcached module Disabled</p>' . CRLF;
  374. }
  375. if (!empty($service['debug_session_dump'])) {
  376. print '<h4>Session Information</h4>' . CRLF;
  377. print '<pre>session_id = ' . session_id() . CRLF;
  378. print '$_SESSION = ';
  379. print_r($_SESSION);
  380. print '$_COOKIE = ';
  381. print_r($_COOKIE);
  382. print '</pre>';
  383. }
  384. if (!empty($service['debug_rewrite_module'])) {
  385. print '<h4>Path parse result</h4>' . CRLF;
  386. print '<pre>' . CRLF;
  387. print_r($URLInfo);
  388. print_r($suri);
  389. print '</pre>';
  390. }
  391. print '</div>' . CRLF;
  392. if ($commentBlosk == true) {
  393. echo '-->';
  394. }
  395. }
  396. register_shutdown_function('__tcSqlLogDump');
  397. function dump($data) {
  398. echo '<pre>';
  399. var_dump($data);
  400. echo '</pre>';
  401. }
  402. function dumpToHeader($data) {
  403. static $count = 0;
  404. $debug_string = print_r($data, true);
  405. foreach (split("\n", $debug_string) as $line) {
  406. $count++;
  407. header("X-TC-Debug-$count: $line");
  408. }
  409. }
  410. function dumpAsFile($data) {
  411. if (!is_dir(__TEXTCUBE_CACHE_DIR__ . "")) {
  412. @mkdir(__TEXTCUBE_CACHE_DIR__ . "");
  413. @chmod(__TEXTCUBE_CACHE_DIR__ . "", 0777);
  414. }
  415. $dumpFile = __TEXTCUBE_CACHE_DIR__ . '/dump';
  416. if (file_exists($dumpFile)) {
  417. $dumpedLog = @file_get_contents($dumpFile);
  418. } else {
  419. $dumpedLog = '';
  420. }
  421. //$dumpedLog = $dumpedLog.Timestamp::format5()." : ".var_export($data,true).CRLF;
  422. $dumpedLog = $dumpedLog . var_export($data, true) . CRLF;
  423. $fileHandle = fopen($dumpFile, 'w');
  424. fwrite($fileHandle, $dumpedLog);
  425. fclose($fileHandle);
  426. }
  427. ?>