coding standards

This commit is contained in:
Marc Delisle
2009-11-23 00:53:24 +00:00
parent 182c02c5e8
commit f0b3b1a8cf

View File

@@ -118,21 +118,25 @@ class PMA_Tracker
*/
static public function isActive()
{
if (!self::$enabled) return false;
if (! self::$enabled) {
return false;
}
/* We need to avoid attempt to track any queries from PMA_getRelationsParam */
self::$enabled = false;
$cfgRelation = PMA_getRelationsParam();
/* Restore original state */
self::$enabled = true;
if (!$cfgRelation['trackingwork']) return false;
if (! $cfgRelation['trackingwork']) {
return false;
}
self::init();
if(isset(self::$pma_table))
if (isset(self::$pma_table)) {
return true;
else
} else {
return false;
}
}
/**
* Returns a simple DROP TABLE statement.
@@ -189,7 +193,6 @@ class PMA_Tracker
$str = explode("\n", $tablename);
$tablename = $str[0];
$tablename = str_replace(';', '', $tablename);
$tablename = str_replace('`', '', $tablename);
$tablename = trim($tablename);
@@ -210,13 +213,17 @@ class PMA_Tracker
*/
static public function isTracked($dbname, $tablename)
{
if (!self::$enabled) return false;
if (! self::$enabled) {
return false;
}
/* We need to avoid attempt to track any queries from PMA_getRelationsParam */
self::$enabled = false;
$cfgRelation = PMA_getRelationsParam();
/* Restore original state */
self::$enabled = true;
if (!$cfgRelation['trackingwork']) return false;
if (! $cfgRelation['trackingwork']) {
return false;
}
$sql_query =
" SELECT tracking_active FROM " . self::$pma_table .
@@ -226,11 +233,12 @@ class PMA_Tracker
$row = PMA_DBI_fetch_array(PMA_query_as_controluser($sql_query));
if(isset($row['tracking_active']) and $row['tracking_active'] == 1)
if (isset($row['tracking_active']) && $row['tracking_active'] == 1) {
return true;
else
} else {
return false;
}
}
/**
* Returns the comment line for the log.
@@ -262,8 +270,9 @@ class PMA_Tracker
{
global $sql_backquotes;
if($tracking_set == '')
if ($tracking_set == '') {
$tracking_set = self::$default_tracking_set;
}
require_once './libraries/export/sql.php';
@@ -277,9 +286,9 @@ class PMA_Tracker
$sql_result = PMA_DBI_query($sql_query);
while($row = PMA_DBI_fetch_array($sql_result))
while ($row = PMA_DBI_fetch_array($sql_result)) {
$columns[] = $row;
}
$sql_query = '
SHOW INDEX FROM ' . PMA_backquote($dbname) . '.' . PMA_backquote($tablename);
@@ -288,35 +297,29 @@ class PMA_Tracker
$indexes = array();
while( $row = PMA_DBI_fetch_array($sql_result))
while( $row = PMA_DBI_fetch_array($sql_result)) {
$indexes[] = $row;
}
$snapshot = array('COLUMNS' => $columns , 'INDEXES' => $indexes);
$snapshot = serialize($snapshot);
// Get DROP TABLE / DROP VIEW and CREATE TABLE SQL statements
$sql_backquotes = true;
$create_sql = "";
if(self::$add_drop_table == true and $is_view == false)
{
if (self::$add_drop_table == true && $is_view == false) {
$create_sql .= self::getLogComment() .
self::getStatementDropTable(PMA_backquote($tablename)) . ";\n";
}
if(self::$add_drop_view == true and $is_view == true)
{
if (self::$add_drop_view == true && $is_view == true) {
$create_sql .= self::getLogComment() .
self::getStatementDropView(PMA_backquote($tablename)) . ";\n";
}
$create_sql .= self::getLogComment() .
PMA_getTableDef($dbname, $tablename, "\n", "");
@@ -348,8 +351,7 @@ class PMA_Tracker
$result = PMA_query_as_controluser($sql_query);
if($result)
{
if ($result) {
// Deactivate previous version
self::deactivateTracking($dbname, $tablename, ($version - 1));
}
@@ -377,19 +379,17 @@ class PMA_Tracker
$date = date('Y-m-d H:i:s');
if($tracking_set == '')
if ($tracking_set == '') {
$tracking_set = self::$default_tracking_set;
}
require_once './libraries/export/sql.php';
$create_sql = "";
if(self::$add_drop_database == true)
{
if (self::$add_drop_database == true) {
$create_sql .= self::getLogComment() .
self::getStatementDropDatabase(PMA_backquote($dbname)) . ";\n";
}
$create_sql .= self::getLogComment() . $query;
@@ -497,15 +497,16 @@ class PMA_Tracker
" WHERE " . PMA_backquote('db_name') . " = '" . PMA_sqlAddslashes($dbname) . "' " .
" AND " . PMA_backquote('table_name') . " = '" . PMA_sqlAddslashes($tablename) . "' ";
if($statement != "")
if ($statement != "") {
$sql_query .= " AND FIND_IN_SET('" . $statement . "',tracking) > 0" ;
}
$row = PMA_DBI_fetch_array(PMA_query_as_controluser($sql_query));
if(isset($row[0]))
if (isset($row[0])) {
$version = $row[0];
if(!isset($version)) $version = -1;
}
if (! isset($version)) {
$version = -1;
}
return $version;
}
@@ -542,13 +543,13 @@ class PMA_Tracker
// Iterate tracked data definition statements
// For each log entry we want to get date, username and statement
foreach ($log_schema_entries as $log_entry)
{
if(trim($log_entry) != '')
{
foreach ($log_schema_entries as $log_entry) {
if (trim($log_entry) != '') {
$date = substr($log_entry, 0, 19);
$username = substr($log_entry, 20, strpos($log_entry, "\n") - 20);
if($i == 0) $ddl_date_from = $date;
if ($i == 0) {
$ddl_date_from = $date;
}
$statement = rtrim(strstr($log_entry, "\n"));
$ddlog[] = array( 'date' => $date,
@@ -568,13 +569,13 @@ class PMA_Tracker
// Iterate tracked data manipulation statements
// For each log entry we want to get date, username and statement
foreach ($log_data_entries as $log_entry)
{
if(trim($log_entry) != '')
{
foreach ($log_data_entries as $log_entry) {
if (trim($log_entry) != '') {
$date = substr($log_entry, 0, 19);
$username = substr($log_entry, 20, strpos($log_entry, "\n") - 20);
if($i == 0) $dml_date_from = $date;
if ($i == 0) {
$dml_date_from = $date;
}
$statement = rtrim(strstr($log_entry, "\n"));
$dmlog[] = array( 'date' => $date,
@@ -587,16 +588,16 @@ class PMA_Tracker
$dml_date_to = $date;
// Define begin and end of date range for both logs
if(strtotime($ddl_date_from) <= strtotime($dml_date_from))
if (strtotime($ddl_date_from) <= strtotime($dml_date_from)) {
$data['date_from'] = $ddl_date_from;
else
} else {
$data['date_from'] = $dml_date_from;
if(strtotime($ddl_date_to) >= strtotime($dml_date_to))
}
if (strtotime($ddl_date_to) >= strtotime($dml_date_to)) {
$data['date_to'] = $ddl_date_to;
else
} else {
$data['date_to'] = $dml_date_to;
}
$data['ddlog'] = $ddlog;
$data['dmlog'] = $dmlog;
$data['tracking'] = $mixed['tracking'];
@@ -640,8 +641,7 @@ class PMA_Tracker
$tokens = array_map('strtoupper', $tokens);
// Parse USE statement, need it for SQL dump imports
if(substr($query, 0, 4) == 'USE ')
{
if (substr($query, 0, 4) == 'USE ') {
$prefix = explode('USE ', $query);
$GLOBALS['db'] = self::getTableName($prefix[1]);
}
@@ -653,10 +653,9 @@ class PMA_Tracker
$result['type'] = 'DDL';
// Parse CREATE VIEW statement
if(in_array('CREATE', $tokens) == true and
in_array('VIEW', $tokens) == true and
in_array('AS', $tokens) == true)
{
if (in_array('CREATE', $tokens) == true &&
in_array('VIEW', $tokens) == true &&
in_array('AS', $tokens) == true) {
$result['identifier'] = 'CREATE VIEW';
$index = array_search('VIEW', $tokens);
@@ -665,11 +664,10 @@ class PMA_Tracker
}
// Parse ALTER VIEW statement
if(in_array('ALTER', $tokens) == true and
in_array('VIEW', $tokens) == true and
in_array('AS', $tokens) == true and
!isset($result['identifier']))
{
if (in_array('ALTER', $tokens) == true &&
in_array('VIEW', $tokens) == true &&
in_array('AS', $tokens) == true &&
! isset($result['identifier'])) {
$result['identifier'] = 'ALTER VIEW';
$index = array_search('VIEW', $tokens);
@@ -678,22 +676,20 @@ class PMA_Tracker
}
// Parse DROP VIEW statement
if(!isset($result['identifier']) and substr($query, 0, 10) == 'DROP VIEW ')
{
if (! isset($result['identifier']) && substr($query, 0, 10) == 'DROP VIEW ') {
$result['identifier'] = 'DROP VIEW';
$prefix = explode('DROP VIEW ', $query);
$str = strstr($prefix[1], 'IF EXISTS');
if($str == FALSE )
if ($str == FALSE ) {
$str = $prefix[1];
}
$result['tablename'] = self::getTableName($str);
}
// Parse CREATE DATABASE statement
if(!isset($result['identifier']) and substr($query, 0, 15) == 'CREATE DATABASE')
{
if (! isset($result['identifier']) && substr($query, 0, 15) == 'CREATE DATABASE') {
$result['identifier'] = 'CREATE DATABASE';
$str = str_replace('CREATE DATABASE', '', $query);
$str = str_replace('IF NOT EXISTS', '', $str);
@@ -705,15 +701,13 @@ class PMA_Tracker
}
// Parse ALTER DATABASE statement
if(!isset($result['identifier']) and substr($query, 0, 14) == 'ALTER DATABASE')
{
if (! isset($result['identifier']) && substr($query, 0, 14) == 'ALTER DATABASE') {
$result['identifier'] = 'ALTER DATABASE';
$result['tablename'] = '';
}
// Parse DROP DATABASE statement
if(!isset($result['identifier']) and substr($query, 0, 13) == 'DROP DATABASE')
{
if (! isset($result['identifier']) && substr($query, 0, 13) == 'DROP DATABASE') {
$result['identifier'] = 'DROP DATABASE';
$str = str_replace('DROP DATABASE', '', $query);
$str = str_replace('IF EXISTS', '', $str);
@@ -722,8 +716,7 @@ class PMA_Tracker
}
// Parse CREATE TABLE statement
if(!isset($result['identifier']) and substr($query, 0, 12) == 'CREATE TABLE')
{
if (! isset($result['identifier']) && substr($query, 0, 12) == 'CREATE TABLE' ) {
$result['identifier'] = 'CREATE TABLE';
$query = str_replace('IF NOT EXISTS', '', $query);
$prefix = explode('CREATE TABLE ', $query);
@@ -732,8 +725,7 @@ class PMA_Tracker
}
// Parse ALTER TABLE statement
if(!isset($result['identifier']) and substr($query, 0, 12) == 'ALTER TABLE ')
{
if (! isset($result['identifier']) && substr($query, 0, 12) == 'ALTER TABLE ') {
$result['identifier'] = 'ALTER TABLE';
$prefix = explode('ALTER TABLE ', $query);
@@ -742,27 +734,25 @@ class PMA_Tracker
}
// Parse DROP TABLE statement
if(!isset($result['identifier']) and substr($query, 0, 11) == 'DROP TABLE ')
{
if (! isset($result['identifier']) && substr($query, 0, 11) == 'DROP TABLE ') {
$result['identifier'] = 'DROP TABLE';
$prefix = explode('DROP TABLE ', $query);
$str = strstr($prefix[1], 'IF EXISTS');
if($str == FALSE )
if ($str == FALSE ) {
$str = $prefix[1];
}
$result['tablename'] = self::getTableName($str);
}
// Parse CREATE INDEX statement
if(!isset($result['identifier']) and
( substr($query, 0, 12) == 'CREATE INDEX' or
substr($query, 0, 19) == 'CREATE UNIQUE INDEX' or
if (! isset($result['identifier']) &&
( substr($query, 0, 12) == 'CREATE INDEX' ||
substr($query, 0, 19) == 'CREATE UNIQUE INDEX' ||
substr($query, 0, 20) == 'CREATE SPATIAL INDEX'
)
)
{
) {
$result['identifier'] = 'CREATE INDEX';
$prefix = explode('ON ', $query);
$suffix = explode('(', $prefix[1]);
@@ -770,16 +760,14 @@ class PMA_Tracker
}
// Parse DROP INDEX statement
if(!isset($result['identifier']) and substr($query, 0, 10) == 'DROP INDEX')
{
if (! isset($result['identifier']) && substr($query, 0, 10) == 'DROP INDEX') {
$result['identifier'] = 'DROP INDEX';
$prefix = explode('ON ', $query);
$result['tablename'] = self::getTableName($prefix[1]);
}
// Parse RENAME TABLE statement
if(!isset($result['identifier']) and substr($query, 0, 13) == 'RENAME TABLE ')
{
if (! isset($result['identifier']) && substr($query, 0, 13) == 'RENAME TABLE ') {
$result['identifier'] = 'RENAME TABLE';
$prefix = explode('RENAME TABLE ', $query);
$names = explode(' TO ', $prefix[1]);
@@ -791,12 +779,11 @@ class PMA_Tracker
* DML statements
*/
if(!isset($result['identifier']))
if (! isset($result['identifier'])) {
$result["type"] = 'DML';
}
// Parse UPDATE statement
if(!isset($result['identifier']) and substr($query, 0, 6) == 'UPDATE')
{
if (! isset($result['identifier']) && substr($query, 0, 6) == 'UPDATE') {
$result['identifier'] = 'UPDATE';
$prefix = explode('UPDATE ', $query);
$suffix = explode(' ', $prefix[1]);
@@ -804,8 +791,7 @@ class PMA_Tracker
}
// Parse INSERT INTO statement
if(!isset($result['identifier']) and substr($query, 0, 11 ) == 'INSERT INTO')
{
if (! isset($result['identifier']) && substr($query, 0, 11 ) == 'INSERT INTO') {
$result['identifier'] = 'INSERT';
$prefix = explode('INSERT INTO', $query);
$suffix = explode('(', $prefix[1]);
@@ -813,8 +799,7 @@ class PMA_Tracker
}
// Parse DELETE statement
if(!isset($result['identifier']) and substr($query, 0, 6 ) == 'DELETE')
{
if (! isset($result['identifier']) && substr($query, 0, 6 ) == 'DELETE') {
$result['identifier'] = 'DELETE';
$prefix = explode('FROM ', $query);
$suffix = explode(' ', $prefix[1]);
@@ -822,8 +807,7 @@ class PMA_Tracker
}
// Parse TRUNCATE statement
if(!isset($result['identifier']) and substr($query, 0, 8 ) == 'TRUNCATE')
{
if (! isset($result['identifier']) && substr($query, 0, 8 ) == 'TRUNCATE') {
$result['identifier'] = 'TRUNCATE';
$prefix = explode('TRUNCATE', $query);
$result['tablename'] = self::getTableName($prefix[1]);
@@ -843,16 +827,13 @@ class PMA_Tracker
static public function handleQuery($query)
{
// If query is marked as untouchable, leave
if(strstr($query, "/*NOTRACK*/"))
if (strstr($query, "/*NOTRACK*/")) {
return false;
}
//$query = trim($query);
//$query = ltrim($query, '-');
if(!(substr($query, -1) == ';'))
if (! (substr($query, -1) == ';')) {
$query = $query . ";\n";
}
// Get some information about query
$result = self::parseQuery($query);
@@ -865,41 +846,41 @@ class PMA_Tracker
}
// If we found a valid statement
if(isset($result['identifier']))
{
if (isset($result['identifier'])) {
$version = self::getVersion($dbname, $result['tablename'], $result['identifier']);
// If version not exists and auto-creation is enabled
if(self::$version_auto_create == true and
self::isTracked($dbname, $result['tablename']) == false and
$version == -1)
{
if (self::$version_auto_create == true
&& self::isTracked($dbname, $result['tablename']) == false
&& $version == -1) {
// Create the version
if($result['identifier'] == 'CREATE TABLE')
switch ($result['identifier']) {
case 'CREATE TABLE':
self::createVersion($dbname, $result['tablename'], '1');
if($result['identifier'] == 'CREATE VIEW')
break;
case 'CREATE VIEW':
self::createVersion($dbname, $result['tablename'], '1', '', true);
if($result['identifier'] == 'CREATE DATABASE')
break;
case 'CREATE DATABASE':
self::createDatabaseVersion($dbname, '1', $query);
break;
} // end switch
}
// If version exists
if(self::isTracked($dbname, $result['tablename']) and $version != -1)
{
if($result['type'] == 'DDL')
if (self::isTracked($dbname, $result['tablename']) && $version != -1) {
if ($result['type'] == 'DDL') {
$save_to = 'schema_sql';
else if($result['type'] == 'DML')
} elseif ($result['type'] == 'DML') {
$save_to = 'data_sql';
else
} else {
$save_to = '';
}
$date = date('Y-m-d H:i:s');
// Cut off `dbname`. from query
$query = preg_replace('/`'.$dbname.'`\s?\./', '', $query);
$query = preg_replace('/`' . $dbname . '`\s?\./', '', $query);
// Add log information
$query = self::getLogComment() . $query ;
@@ -912,8 +893,9 @@ class PMA_Tracker
" " . PMA_backquote('date_updated') . " = '" . $date . "' ";
// If table was renamed we have to change the tablename attribute in pma_tracking too
if($result['identifier'] == 'RENAME TABLE')
if ($result['identifier'] == 'RENAME TABLE') {
$sql_query .= ', ' . PMA_backquote('table_name') . ' = \'' . PMA_sqlAddslashes($result['tablename_after_rename']) . '\' ';
}
// Save the tracking information only for
// 1. the database