<?php
//$langA checked

defined('WikyBlog') or die("Not an entry point...");
define('wbDebugQuery',true);

global $queryCache;

function debugQueryAll(){
	global $queryCache;
	
	if( !defined('wbDebugQueryInfo') || wbDebugQueryInfo === true){
		foreach($queryCache as $query){
			debugQueryInfo($query);
		}
	}
}
function debugQueryAdd(&$query){
	global $queryCache;
	$queryCache[] = $query;
	if( defined('wbDebugQueryMess') && wbDebugQueryMess === true){
		debugQueryMess($query);
	}
}
	

function debugQueryMess(&$query){
	$result = mysql_query('SHOW WARNINGS');
	$num = mysql_num_rows($result);
	if( $num <= 0 ){
		//message('No Warnings for the submitted query.');
		return;
	}
	$mess = '<h4>MySQL Warning:</h4> '.$query;
	$mess .='<ul>';
	while($row = mysql_fetch_assoc($result) ){
		$mess .= '<li>'.showArray($row).'</li>';
	}
	$mess .= '</ul>';
	message($mess);
}

function debugQueryInfo(&$query){
	global $page,$dbObject;
	if( !isset($page->contentA['Debug Query Info']) ){
		//$page->regLink('Debug Query Info','/include/#WBnoneDebugLocal');
		$page->contentA['Debug Query Info'] = '';
	}
	$t =&$page->contentA['Debug Query Info'];
	
	
	$pos = strpos($query,'SELECT');
	if( ($pos === false) || ($pos > 10 ) ){
		$t .= "\n";
		$t .= '<h2 class="underline">Non Select Query</h2>';
		$t .= "\n<p>";
		// something about history inserts that the xmlResponse doesn't like... I'd like to know
		//	what it is..
		//$t .= chunk_split($query,30); 
		$t .= wbSubstr($query,0,50). '...';
		//$t .= wbHtmlspecialchars($query);
		$t .= "</p>\n";
		return;
	}
		
	($result = mysql_query('EXPLAIN '. $query )) or trigger_error('EXPLAIN '.$query);

		
	// Printing results in HTML
	//$t .= '<h2 class="underline">Select Query</h2>';
	
	$t .= '<p>&nbsp;<br/></p>';
	$t .= '<table class="tableRows" cellspacing="9" width="100%" style="background-color:white;">';
	
	
	$i = 0;
	while( $line = mysql_fetch_assoc($result) ){
		$t .= "\n";
		if( $i === 0){
			$t .= '<tr>';
			foreach ($line as $key => $value) {
				$t .= '<th>'.toDisplay($key).'</th>';
			}
			$t .= '</tr>';
			$i++;
		}
		
		
		$t .= '<tr>';
		foreach ($line as $value) {
			$t .= '<td style="white-space:nowrap;">'.$value.'</td>';
		}
		$t .= '</tr>';
	}
	
	$t .= '<tr><td colspan="10" style="padding:2em 0 2em 0;">';
	$t .= nl2br($query);
	//$t .= str_replace('(','(<br/>&nbsp;',$query);
	$t .= '</td></tr>';
	$t .= '</table>';
	$t .= "\n";
}






