From ee42077e5415b22ac054470cc6ef381503112af9 Mon Sep 17 00:00:00 2001 From: Peter Knut Date: Sun, 17 Mar 2024 12:54:43 +0100 Subject: [PATCH] Improve code readability by using of empty lines --- adminer/drivers/elastic.inc.php | 92 +++++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 21 deletions(-) diff --git a/adminer/drivers/elastic.inc.php b/adminer/drivers/elastic.inc.php index ec4fae1d..e3022715 100644 --- a/adminer/drivers/elastic.inc.php +++ b/adminer/drivers/elastic.inc.php @@ -16,6 +16,7 @@ if (isset($_GET["elastic"])) { */ function rootQuery($path, array $content = null, $method = 'GET') { @ini_set('track_errors', 1); // @ - may be disabled + $file = @file_get_contents("$this->_url/" . ltrim($path, '/'), false, stream_context_create(array('http' => array( 'method' => $method, 'content' => $content !== null ? json_encode($content) : null, @@ -63,6 +64,7 @@ if (isset($_GET["elastic"])) { return $driver->select($matches[1], array("*"), $where, null, array(), $matches[3]); } + return $this->rootQuery(($this->_db != "" ? "$this->_db/" : "/") . ltrim($path, '/'), $content, $method); } @@ -91,13 +93,13 @@ if (isset($_GET["elastic"])) { function select_db($database) { $this->_db = $database; + return true; } function quote($string) { return $string; } - } class Min_Result { @@ -106,12 +108,14 @@ if (isset($_GET["elastic"])) { function __construct($rows) { $this->num_rows = count($rows); $this->_rows = $rows; + reset($this->_rows); } function fetch_assoc() { $return = current($this->_rows); next($this->_rows); + return $return; } @@ -120,22 +124,19 @@ if (isset($_GET["elastic"])) { return $row ? array_values($row) : false; } - } - } - - class Min_Driver extends Min_SQL { function select($table, $select, $where, $group, $order = array(), $limit = 1, $page = 0, $print = false) { global $adminer; + $data = array(); - $query = (min_version(7) ? "" : "$table/") . "_search"; if ($select != array("*")) { $data["fields"] = $select; } + if ($order) { $sort = array(); foreach ($order as $col) { @@ -144,12 +145,14 @@ if (isset($_GET["elastic"])) { } $data["sort"] = $sort; } + if ($limit) { $data["size"] = +$limit; if ($page) { $data["from"] = ($page * $limit); } } + foreach ($where as $val) { if (preg_match('~^\((.+ OR .+)\)$~', $val, $matches)) { $parts = explode(" OR ", $matches[1]); @@ -177,20 +180,25 @@ if (isset($_GET["elastic"])) { } } } + + $query = (min_version(7) ? "" : "$table/") . "_search"; $start = microtime(true); $search = $this->_conn->query($query, $data); + if ($print) { echo $adminer->selectQuery("$query: " . json_encode($data), $start, !$search); } if (!$search) { return false; } + $return = array(); foreach ($search['hits']['hits'] as $hit) { $row = array(); if ($select == array("*")) { $row["_id"] = $hit["_id"]; } + $fields = $hit['_source']; if ($select != array("*")) { $fields = array(); @@ -198,14 +206,17 @@ if (isset($_GET["elastic"])) { $fields[$key] = $key == "_id" ? [$hit["_id"]] : $hit['fields'][$key]; } } + foreach ($fields as $key => $val) { if ($data["fields"]) { $val = $val[0]; } $row[$key] = (is_array($val) ? json_encode($val) : $val); //! display JSON and others differently } + $return[] = $row; } + return new Min_Result($return); } @@ -215,8 +226,10 @@ if (isset($_GET["elastic"])) { if (count($parts) == 2) { $id = trim($parts[1]); $query = "$type/$id"; + return $this->_conn->query($query, $record, 'POST'); } + return false; } @@ -225,6 +238,7 @@ if (isset($_GET["elastic"])) { $query = "$type/$id"; $response = $this->_conn->query($query, $record, 'POST'); $this->_conn->last_id = $response['_id']; + return $response['created']; } @@ -242,7 +256,9 @@ if (isset($_GET["elastic"])) { } } } + $this->_conn->affected_rows = 0; + foreach ($ids as $id) { $query = "{$type}/{$id}"; $response = $this->_conn->query($query, null, 'DELETE'); @@ -250,6 +266,7 @@ if (isset($_GET["elastic"])) { $this->_conn->affected_rows++; } } + return $this->_conn->affected_rows; } @@ -258,18 +275,20 @@ if (isset($_GET["elastic"])) { } } - - function connect() { global $adminer; + $connection = new Min_DB; + list($server, $username, $password) = $adminer->credentials(); if ($password != "" && $connection->connect($server, $username, "")) { return lang('Database does not support password.'); } + if ($connection->connect($server, $username, $password)) { return $connection; } + return $connection->error; } @@ -279,17 +298,21 @@ if (isset($_GET["elastic"])) { function logged_user() { global $adminer; + $credentials = $adminer->credentials(); + return $credentials[1]; } function get_databases() { global $connection; + $return = $connection->rootQuery('_aliases'); if ($return) { $return = array_keys($return); sort($return, SORT_STRING); } + return $return; } @@ -302,6 +325,7 @@ if (isset($_GET["elastic"])) { } function db_collation($db, $collations) { + // } function engines() { @@ -310,8 +334,10 @@ if (isset($_GET["elastic"])) { function count_tables($databases) { global $connection; - $return = array(); + $result = $connection->query('_stats'); + + $return = array(); if ($result && $result['indices']) { $indices = $result['indices']; foreach ($indices as $indice => $stats) { @@ -319,6 +345,7 @@ if (isset($_GET["elastic"])) { $return[$indice] = $indexing['index_total']; } } + return $return; } @@ -333,11 +360,13 @@ if (isset($_GET["elastic"])) { if ($return) { $return = array_fill_keys(array_keys($return[$connection->_db]["mappings"]), 'table'); } + return $return; } function table_status($name = "", $fast = false) { global $connection; + $search = $connection->query("_search", array( "size" => 0, "aggregations" => array( @@ -348,25 +377,31 @@ if (isset($_GET["elastic"])) { ) ) ), "POST"); + $return = array(); + if ($search) { $tables = $search["aggregations"]["count_by_type"]["buckets"]; + foreach ($tables as $table) { $return[$table["key"]] = array( "Name" => $table["key"], "Engine" => "table", "Rows" => $table["doc_count"], ); + if ($name != "" && $name == $table["key"]) { return $return[$name]; } } } + return $return; } function error() { global $connection; + return h($connection->error); } @@ -386,6 +421,7 @@ if (isset($_GET["elastic"])) { global $connection; $mappings = array(); + if (min_version(7)) { $result = $connection->query("_mapping"); if ($result) { @@ -425,11 +461,13 @@ if (isset($_GET["elastic"])) { "order" => $field["type"] != "text" ?: null ), ); + if ($field["properties"]) { // only leaf fields can be edited unset($return[$name]["privileges"]["insert"]); unset($return[$name]["privileges"]["update"]); } } + return $return; } @@ -446,6 +484,7 @@ if (isset($_GET["elastic"])) { } function convert_field($field) { + // } function unconvert_field($field, $return) { @@ -453,6 +492,7 @@ if (isset($_GET["elastic"])) { } function fk_support($table_status) { + // } function found_rows($table_status, $where) { @@ -460,29 +500,32 @@ if (isset($_GET["elastic"])) { } /** Create index - * @param string - * @return mixed - */ + * @param string + * @return mixed + */ function create_database($db) { global $connection; + return $connection->rootQuery(urlencode($db), null, 'PUT'); } /** Remove index - * @param array - * @return mixed - */ + * @param array + * @return mixed + */ function drop_databases($databases) { global $connection; + return $connection->rootQuery(urlencode(implode(',', $databases)), null, 'DELETE'); } /** Alter type - * @param array - * @return mixed - */ + * @param array + * @return mixed + */ function alter_table($table, $name, $fields, $foreign, $comment, $engine, $collation, $auto_increment, $partitioning) { global $connection; + $properties = array(); foreach ($fields as $f) { $field_name = trim($f[1][0]); @@ -491,33 +534,39 @@ if (isset($_GET["elastic"])) { 'type' => $field_type ); } + if (!empty($properties)) { $properties = array('properties' => $properties); } + return $connection->query("_mapping/{$name}", $properties, 'PUT'); } /** Drop types - * @param array - * @return bool - */ + * @param array + * @return bool + */ function drop_tables($tables) { global $connection; + $return = true; foreach ($tables as $table) { //! convert to bulk api $return = $return && $connection->query(urlencode($table), null, 'DELETE'); } + return $return; } function last_id() { global $connection; + return $connection->last_id; } function driver_config() { $types = array(); $structured_types = array(); + foreach (array( lang('Numbers') => array("long" => 3, "integer" => 5, "short" => 8, "byte" => 10, "double" => 20, "float" => 66, "half_float" => 12, "scaled_float" => 21), lang('Date and time') => array("date" => 10), @@ -527,6 +576,7 @@ if (isset($_GET["elastic"])) { $types += $val; $structured_types[$key] = array_keys($val); } + return array( 'possible_drivers' => array("json + allow_url_fopen"), 'jush' => "elastic",