From 396c063d9a25f03307c096cbfc735b37b641297e Mon Sep 17 00:00:00 2001 From: Hugues Peccatte Date: Thu, 13 Nov 2014 22:18:25 +0100 Subject: [PATCH] Reduce nested levels in Node.class.php. Signed-off-by: Hugues Peccatte --- libraries/navigation/Nodes/Node.class.php | 281 ++++++++++++---------- 1 file changed, 148 insertions(+), 133 deletions(-) diff --git a/libraries/navigation/Nodes/Node.class.php b/libraries/navigation/Nodes/Node.class.php index 446b2d0e87..febecc96d2 100644 --- a/libraries/navigation/Nodes/Node.class.php +++ b/libraries/navigation/Nodes/Node.class.php @@ -360,107 +360,7 @@ class Node public function getData($type, $pos, $searchClause = '') { $maxItems = $GLOBALS['cfg']['FirstLevelNavigationItems']; - if ($GLOBALS['cfg']['NavigationTreeEnableGrouping']) { - $dbSeparator = $GLOBALS['cfg']['NavigationTreeDbSeparator']; - if (! $GLOBALS['cfg']['Server']['DisableIS']) { - $query = "SELECT `SCHEMA_NAME` "; - $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA`, "; - $query .= "("; - $query .= "SELECT DB_first_level "; - $query .= "FROM ( "; - $query .= "SELECT DISTINCT SUBSTRING_INDEX(SCHEMA_NAME, "; - $query .= "'$dbSeparator', 1) "; - $query .= "DB_first_level "; - $query .= "FROM INFORMATION_SCHEMA.SCHEMATA "; - $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause); - $query .= ") t "; - $query .= "ORDER BY DB_first_level ASC "; - $query .= "LIMIT $pos, $maxItems"; - $query .= ") t2 "; - $query .= "WHERE 1 = LOCATE(CONCAT(DB_first_level, "; - $query .= "'$dbSeparator'), "; - $query .= "CONCAT(SCHEMA_NAME, "; - $query .= "'$dbSeparator')) "; - $query .= "ORDER BY SCHEMA_NAME ASC"; - $retval = $GLOBALS['dbi']->fetchResult($query); - } else { - if ($GLOBALS['dbs_to_test'] === false) { - $query = "SHOW DATABASES "; - $query .= $this->_getWhereClause('Database', $searchClause); - $handle = $GLOBALS['dbi']->tryQuery($query); - if ($handle !== false) { - $prefixMap = array(); - $total = $pos + $maxItems; - while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { - $prefix = strstr($arr[0], $dbSeparator, true); - if ($prefix === false) { - $prefix = $arr[0]; - } - $prefixMap[$prefix] = 1; - if (sizeof($prefixMap) == $total) { - break; - } - } - $prefixes = array_slice(array_keys($prefixMap), $pos); - } - - $query = "SHOW DATABASES "; - $query .= $this->_getWhereClause('Database', ''); - $query .= " AND ("; - $subClauses = array(); - foreach ($prefixes as $prefix) { - $subClauses[] = " LOCATE('" - . PMA_Util::sqlAddSlashes($prefix) . $dbSeparator . "', " - . "CONCAT(`Database`, '" . $dbSeparator . "')) = 1 "; - } - $query .= implode("OR", $subClauses) . ")"; - $retval = $GLOBALS['dbi']->fetchResult($query); - } else { - $retval = array(); - $prefixMap = array(); - $total = $pos + $maxItems; - foreach ($this->_getDatabasesToSearch($searchClause) as $db) { - $query = "SHOW DATABASES LIKE '" . $db . "'"; - $handle = $GLOBALS['dbi']->tryQuery($query); - if ($handle !== false) { - while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { - $prefix = strstr($arr[0], $dbSeparator, true); - if ($prefix === false) { - $prefix = $arr[0]; - } - $prefixMap[$prefix] = 1; - if (sizeof($prefixMap) == $total) { - break 2; - } - } - } - } - $prefixes = array_slice(array_keys($prefixMap), $pos); - - foreach ($this->_getDatabasesToSearch($searchClause) as $db) { - $query = "SHOW DATABASES LIKE '" . $db . "'"; - $handle = $GLOBALS['dbi']->tryQuery($query); - if ($handle !== false) { - while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { - if (! in_array($arr[0], $retval)) { - foreach ($prefixes as $prefix) { - $starts_with = strpos( - $arr[0] . $dbSeparator, - $prefix . $dbSeparator - ) === 0; - if ($starts_with) { - $retval[] = $arr[0]; - break; - } - } - } - } - } - } - sort($retval); - } - } - } else { + if (!$GLOBALS['cfg']['NavigationTreeEnableGrouping']) { if (! $GLOBALS['cfg']['Server']['DisableIS']) { $query = "SELECT `SCHEMA_NAME` "; $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA` "; @@ -468,44 +368,159 @@ class Node $query .= "ORDER BY `SCHEMA_NAME` "; $query .= "LIMIT $pos, $maxItems"; $retval = $GLOBALS['dbi']->fetchResult($query); - } else { - if ($GLOBALS['dbs_to_test'] === false) { - $retval = array(); - $query = "SHOW DATABASES "; - $query .= $this->_getWhereClause('Database', $searchClause); - $handle = $GLOBALS['dbi']->tryQuery($query); - if ($handle !== false) { - $count = 0; - while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { - if ($pos <= 0 && $count < $maxItems) { - $retval[] = $arr[0]; - $count++; - } - $pos--; - } - } - } else { - $retval = array(); + return $retval; + } + + if ($GLOBALS['dbs_to_test'] === false) { + $retval = array(); + $query = "SHOW DATABASES "; + $query .= $this->_getWhereClause('Database', $searchClause); + $handle = $GLOBALS['dbi']->tryQuery($query); + if ($handle !== false) { $count = 0; - foreach ($this->_getDatabasesToSearch($searchClause) as $db) { - $query = "SHOW DATABASES LIKE '" . $db . "'"; - $handle = $GLOBALS['dbi']->tryQuery($query); - if ($handle !== false) { - while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { - if (! in_array($arr[0], $retval)) { - if ($pos <= 0 && $count < $maxItems) { - $retval[] = $arr[0]; - $count++; - } - $pos--; - } - } + while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { + if ($pos <= 0 && $count < $maxItems) { + $retval[] = $arr[0]; + $count++; } + $pos--; } - sort($retval); + } + return $retval; + } + + $retval = array(); + $count = 0; + foreach ($this->_getDatabasesToSearch($searchClause) as $db) { + $query = "SHOW DATABASES LIKE '" . $db . "'"; + $handle = $GLOBALS['dbi']->tryQuery($query); + if ($handle === false) { + continue; + } + + while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { + if (in_array($arr[0], $retval)) { + continue; + } + + if ($pos <= 0 && $count < $maxItems) { + $retval[] = $arr[0]; + $count++; + } + $pos--; + } + } + sort($retval); + return $retval; + } + + $dbSeparator = $GLOBALS['cfg']['NavigationTreeDbSeparator']; + if (! $GLOBALS['cfg']['Server']['DisableIS']) { + $query = "SELECT `SCHEMA_NAME` "; + $query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA`, "; + $query .= "("; + $query .= "SELECT DB_first_level "; + $query .= "FROM ( "; + $query .= "SELECT DISTINCT SUBSTRING_INDEX(SCHEMA_NAME, "; + $query .= "'$dbSeparator', 1) "; + $query .= "DB_first_level "; + $query .= "FROM INFORMATION_SCHEMA.SCHEMATA "; + $query .= $this->_getWhereClause('SCHEMA_NAME', $searchClause); + $query .= ") t "; + $query .= "ORDER BY DB_first_level ASC "; + $query .= "LIMIT $pos, $maxItems"; + $query .= ") t2 "; + $query .= "WHERE 1 = LOCATE(CONCAT(DB_first_level, "; + $query .= "'$dbSeparator'), "; + $query .= "CONCAT(SCHEMA_NAME, "; + $query .= "'$dbSeparator')) "; + $query .= "ORDER BY SCHEMA_NAME ASC"; + $retval = $GLOBALS['dbi']->fetchResult($query); + return $retval; + } + + if ($GLOBALS['dbs_to_test'] === false) { + $query = "SHOW DATABASES "; + $query .= $this->_getWhereClause('Database', $searchClause); + $handle = $GLOBALS['dbi']->tryQuery($query); + if ($handle !== false) { + $prefixMap = array(); + $total = $pos + $maxItems; + while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { + $prefix = strstr($arr[0], $dbSeparator, true); + if ($prefix === false) { + $prefix = $arr[0]; + } + $prefixMap[$prefix] = 1; + if (sizeof($prefixMap) == $total) { + break; + } + } + $prefixes = array_slice(array_keys($prefixMap), $pos); + } + + $query = "SHOW DATABASES "; + $query .= $this->_getWhereClause('Database', ''); + $query .= " AND ("; + $subClauses = array(); + foreach ($prefixes as $prefix) { + $subClauses[] = " LOCATE('" + . PMA_Util::sqlAddSlashes($prefix) . $dbSeparator . "', " + . "CONCAT(`Database`, '" . $dbSeparator . "')) = 1 "; + } + $query .= implode("OR", $subClauses) . ")"; + $retval = $GLOBALS['dbi']->fetchResult($query); + return $retval; + } + + $retval = array(); + $prefixMap = array(); + $total = $pos + $maxItems; + foreach ($this->_getDatabasesToSearch($searchClause) as $db) { + $query = "SHOW DATABASES LIKE '" . $db . "'"; + $handle = $GLOBALS['dbi']->tryQuery($query); + if ($handle === false) { + continue; + } + + while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { + $prefix = strstr($arr[0], $dbSeparator, true); + if ($prefix === false) { + $prefix = $arr[0]; + } + $prefixMap[$prefix] = 1; + if (sizeof($prefixMap) == $total) { + break 2; } } } + $prefixes = array_slice(array_keys($prefixMap), $pos); + + foreach ($this->_getDatabasesToSearch($searchClause) as $db) { + $query = "SHOW DATABASES LIKE '" . $db . "'"; + $handle = $GLOBALS['dbi']->tryQuery($query); + if ($handle === false) { + continue; + } + + while ($arr = $GLOBALS['dbi']->fetchArray($handle)) { + if (in_array($arr[0], $retval)) { + continue; + } + + foreach ($prefixes as $prefix) { + $starts_with = strpos( + $arr[0] . $dbSeparator, + $prefix . $dbSeparator + ) === 0; + if ($starts_with) { + $retval[] = $arr[0]; + break; + } + } + } + } + sort($retval); return $retval; }