Project

General

Profile

Bug #19306 » class.ux_t3lib_db.php

Administrator Admin, 2009-08-31 15:00

 
<?php
/**
* User-Extension of t3lib_db class.
** @author Enrico Aillaud <e.aillaud@cgiar.org>
** @author Andrea De Pirro <a.depirro@cgiar.org>
*/
class ux_t3lib_DB extends t3lib_DB {
function exec_SELECT_mm_query($select,$local_table,$mm_table,$foreign_table,$whereClause='',$groupBy='',$orderBy='',$limit='') {
if($foreign_table == $local_table) {
$foreign_table_as = $foreign_table.uniqid('_join');
}

$mmWhere = $local_table ? $local_table.'.uid='.$mm_table.'.uid_local' : '';
$mmWhere.= ($local_table AND $foreign_table) ? ' AND ' : '';
$mmWhere.= $foreign_table ? ($foreign_table_as ? $foreign_table_as : $foreign_table).'.uid='.$mm_table.'.uid_foreign' : '';

return $GLOBALS['TYPO3_DB']->exec_SELECTquery(
$select,
($local_table ? $local_table.',' : '').$mm_table.($foreign_table ? ','. $foreign_table.($foreign_table_as ? ' AS '.$foreign_table_as : '') : ''),
$mmWhere.' '.$whereClause, // whereClauseMightContainGroupOrderBy
$groupBy,
$orderBy,
$limit
);
}
function listQuery($field, $value, $table) {
$command = $this->quoteStr($value, $table);
$where = '('.$field.' LIKE \'%,'.$command.',%\' OR '.$field.' LIKE \''.$command.',%\' OR '.$field.' LIKE \'%,'.$command.'\' OR '.$field.'=\''.$command.'\')';
return $where;
}
function sql_num_rows($res) {
if ($this->debug_check_recordset($res)) {
return mysql_num_rows($res);
} else {
return false;
}
}
function sql_fetch_assoc($res) {
if ($this->debug_check_recordset($res)) {
return mysql_fetch_assoc($res);
} else {
return false;
}
}
function sql_fetch_row($res) {
if ($this->debug_check_recordset($res)) {
return mysql_fetch_row($res);
} else {
return false;
}
}
function sql_free_result($res) {
if ($this->debug_check_recordset($res)) {
return mysql_free_result($res);
} else {
return false;
}
}
function sql_data_seek($res,$seek) {
if ($this->debug_check_recordset($res)) {
return mysql_data_seek($res,$seek);
} else {
return false;
}
}
function sql_field_type($res,$pointer) {
if ($this->debug_check_recordset($res)) {
return mysql_field_type($res,$pointer);
} else {
return false;
}
}
function sql_pconnect($TYPO3_db_host, $TYPO3_db_username, $TYPO3_db_password) {
// mysql_error() is tied to an established connection
// if the connection fails we need a different method to get the error message
ini_set('track_errors', 1);
ini_set('html_errors', 0);
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['no_pconnect']) {
$this->link = @mysql_connect($TYPO3_db_host, $TYPO3_db_username, $TYPO3_db_password);
} else {
$this->link = @mysql_pconnect($TYPO3_db_host, $TYPO3_db_username, $TYPO3_db_password);
}
$error_msg = $php_errormsg;
ini_restore('track_errors');
ini_restore('html_errors');

if (!$this->link) {
t3lib_div::sysLog('Could not connect to MySQL server '.$TYPO3_db_host.' with user '.$TYPO3_db_username.': '.$error_msg,'Core',4);
} else {
$setDBinit = t3lib_div::trimExplode(chr(10), $GLOBALS['TYPO3_CONF_VARS']['SYS']['setDBinit'],TRUE);
foreach ($setDBinit as $v) {
if (mysql_query($v, $this->link) === FALSE) {
t3lib_div::sysLog('Could not initialize DB connection with query "'.$v.'": '.mysql_error($this->link),'Core',3);
}
}
}

return $this->link;
}
function admin_get_charsets() {
$output = array();

$columns_res = mysql_query('SHOW CHARACTER SET', $this->link);
while ($row = mysql_fetch_assoc($columns_res)) {
$output[$row['Charset']] = $row;
}

return $output;
}
function debug_check_recordset($res) {
if (!$res) {
$trace = FALSE;
$msg = 'Invalid database result resource detected';
$trace = debug_backtrace();
array_shift($trace);
$cnt = count($trace);
for ($i=0; $i<$cnt; $i++) {
// complete objects are too large for the log
if (isset($trace['object'])) unset($trace['object']);
}
$msg .= ': function t3lib_DB->' . $trace[0]['function'] . ' called from file ' . substr($trace[0]['file'],strlen(PATH_site)+2) . ' in line ' . $trace[0]['line'];
t3lib_div::sysLog($msg.'. Use a devLog extension to get more details.', 'Core/t3lib_db', 3);
t3lib_div::devLog($msg.'.', 'Core/t3lib_db', 3, $trace);

return FALSE;
}
return TRUE;
}
protected function explain($query,$from_table,$row_count) {

if ((int)$this->explainOutput==1 || ((int)$this->explainOutput==2 && t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']))) {
$explainMode = 1; // raw HTML output
} elseif ((int)$this->explainOutput==3 && is_object($GLOBALS['TT'])) {
$explainMode = 2; // embed the output into the TS admin panel
} else {
return false;
}

$error = $GLOBALS['TYPO3_DB']->sql_error();
$trail = t3lib_div::debug_trail();

$explain_tables = array();
$explain_output = array();
$res = $this->sql_query('EXPLAIN '.$query, $this->link);
if (is_resource($res)) {
while ($tempRow = $this->sql_fetch_assoc($res)) {
$explain_output[] = $tempRow;
$explain_tables[] = $tempRow['table'];
}
$this->sql_free_result($res);
}

$indices_output = array();
if ($explain_output[0]['rows']>1 || t3lib_div::inList('ALL',$explain_output[0]['type'])) { // Notice: Rows are skipped if there is only one result, or if no conditions are set
$debug = true; // only enable output if it's really useful

foreach ($explain_tables as $table) {
$res = $this->sql_query('SHOW INDEX FROM '.$table, $this->link);
if (is_resource($res)) {
while ($tempRow = $this->sql_fetch_assoc($res)) {
$indices_output[] = $tempRow;
}
$this->sql_free_result($res);
}
}
} else {
$debug = false;
}

if ($debug) {
if ($explainMode==1) {
t3lib_div::debug('QUERY: '.$query);
t3lib_div::debug(array('Debug trail:'=>$trail), 'Row count: '.$row_count);

if ($error) {
t3lib_div::debug($error);
}
if (count($explain_output)) {
t3lib_div::debug($explain_output);
}
if (count($indices_output)) {
t3lib_div::debugRows($indices_output);
}

} elseif ($explainMode==2) {
$data = array();
$data['query'] = $query;
$data['trail'] = $trail;
$data['row_count'] = $row_count;

if ($error) {
$data['error'] = $error;
}
if (count($explain_output)) {
$data['explain'] = $explain_output;
}
if (count($indices_output)) {
$data['indices'] = $indices_output;
}
$GLOBALS['TT']->setTSselectQuery($data);
}
return true;
}

return false;
}

}
(2-2/2)