From 2396397b7517f8b42a77f56678d6b3441e26254a Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Fri, 28 Mar 2025 16:17:41 +0100 Subject: [PATCH] Elasticsearch: Make it work with Elasticsearch 8 --- CHANGELOG.md | 1 + adminer/include/functions.inc.php | 6 +- plugins/drivers/elastic.php | 43 +++++++----- tests/elastic.html | 109 ++++++++++++++++++++++++++++++ tests/generate-pdo.php | 2 +- 5 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 tests/elastic.html diff --git a/CHANGELOG.md b/CHANGELOG.md index ef8f4e77..290c1f86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Adminer dev Export: Fix tar (regression from 5.0.3) +Elasticsearch: Make it work with Elasticsearch 8 CSS: Invert icons in dark mode ## Adminer 5.1.0 (released 2025-03-24) diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 58db6475..947d0775 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -438,8 +438,10 @@ function redirect(?string $location, string $message = null): void { } } -/** Execute query and redirect if successful */ -function query_redirect(string $query, string $location, string $message, bool $redirect = true, bool $execute = true, bool $failed = false, string $time = ""): bool { +/** Execute query and redirect if successful +* @param bool $redirect +*/ +function query_redirect(string $query, string $location, string $message, $redirect = true, bool $execute = true, bool $failed = false, string $time = ""): bool { global $connection, $error, $adminer; if ($execute) { $start = microtime(true); diff --git a/plugins/drivers/elastic.php b/plugins/drivers/elastic.php index 859256c7..b1ed0f84 100644 --- a/plugins/drivers/elastic.php +++ b/plugins/drivers/elastic.php @@ -16,14 +16,17 @@ if (isset($_GET["elastic"])) { * @return array|false */ function rootQuery(string $path, ?array $content = null, string $method = 'GET') { - $file = @file_get_contents("$this->url/" . ltrim($path, '/'), false, stream_context_create(array('http' => array( - 'method' => $method, - 'content' => $content !== null ? json_encode($content) : null, - 'header' => $content !== null ? 'Content-Type: application/json' : array(), - 'ignore_errors' => 1, - 'follow_location' => 0, - 'max_redirects' => 0, - )))); + $file = @file_get_contents("$this->url/" . ltrim($path, '/'), false, stream_context_create(array( + //~ 'ssl' => array('verify_peer' => false), + 'http' => array( + 'method' => $method, + 'content' => $content !== null ? json_encode($content) : null, + 'header' => $content !== null ? 'Content-Type: application/json' : array(), + 'ignore_errors' => 1, + 'follow_location' => 0, + 'max_redirects' => 0, + ), + ))); if ($file === false) { $this->error = lang('Invalid server or credentials.'); @@ -219,9 +222,9 @@ if (isset($_GET["elastic"])) { $parts = preg_split('~ *= *~', $queryWhere); if (count($parts) == 2) { $id = trim($parts[1]); - $query = "$type/$id"; - - return $this->conn->rootQuery($query, $record, 'POST'); + $query = "$table/_update/$id"; + $this->conn->affected_rows = 0; + return $this->conn->rootQuery($query, array('doc' => $set), 'POST'); } return false; @@ -518,18 +521,24 @@ if (isset($_GET["elastic"])) { function alter_table(string $table, $name, $fields, $foreign, $comment, $engine, $collation, $auto_increment, $partitioning) { $properties = array(); foreach ($fields as $f) { - $field_name = trim($f[1][0]); - $field_type = trim($f[1][1] ?: "text"); - $properties[$field_name] = array( - 'type' => $field_type - ); + if ($f[1]) { + $field_name = trim($f[1][0]); + $field_type = trim($f[1][1] ?: "text"); + $properties[$field_name] = array( + 'type' => $field_type + ); + } } if (!empty($properties)) { $properties = array('properties' => $properties); } - return connection()->rootQuery("_mapping/$name", $properties, 'PUT'); + if ($table != '') { + return connection()->rootQuery("$name/_mapping", $properties, 'POST'); + } else { + return connection()->rootQuery($name, array('mappings' => $properties), 'PUT'); + } } /** Drop types diff --git a/tests/elastic.html b/tests/elastic.html new file mode 100644 index 00000000..f0cf3937 --- /dev/null +++ b/tests/elastic.html @@ -0,0 +1,109 @@ + + + + + + Katalon Elasticsearch + + + + + + + + + + + + + +
Login
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200
selectname=langlabel=English
clickAndWaitcss=#lang > input[type="submit"]
typeid=usernameODBC
typename=auth[password]ODBC12
clickAndWait//input[@value='Login']
verifyTextPresentJSON
+ + + + + + + + + + + +
Create table
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200&username=ODBC&db=elastic
clickAndWaitlink=Create table
typenameinterprets
typefields[1][field]name
selectfields[1][type]label=text
clickAndWait//input[@value='Save']
verifyTextPresentTable has been created.
+ + + + + + + + + + + +
Alter table
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200&username=ODBC&db=elastic&table=interprets
clickAndWaitlink=Alter table
clickadd[2]
typefields[3][field]albums
selectfields[3][type]label=integer
clickAndWait//input[@value='Save']
verifyTextPresentTable has been altered.
+ + + + + + + + +
Insert
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200&username=ODBC&db=elastic&edit=interprets
typefields[name]Michael Jackson
clickAndWait//input[@value='Save']
verifyTextPresenthas been inserted.
+ + + + + + + + + + + + + +
Select
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200&username=ODBC&db=elastic&select=interprets
clicklink=Search
selectname=where[0][col]label=name
selectname=where[0][op]label=should
typename=where[0][val]Jackson
clicklink=Sort
selectname=order[0]label=albums
clickAndWaitxpath=//input[@value='Select']
verifyTextPresent1 row
+ + + + + + + + + +
Update
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200&username=ODBC&db=elastic&select=interprets
clickAndWaitlink=edit
typefields[albums]1
clickAndWait//input[@value='Save']
verifyTextPresentItem has been updated.
+ + + + + + + + + + +
Delete
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200&username=ODBC&db=elastic&select=interprets
clickname=check[]
waitForCheckedname=check[]
chooseOkOnNextConfirmationAre you sure?
clickdelete
verifyTextPresent1 item has been affected.
+ + + + + + + + +
Drop
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200&username=ODBC&db=elastic&create=interprets
chooseOkOnNextConfirmationDrop interprets?
clickname=drop
verifyTextPresentNo tables.
+ + + + + + + + +
Logout
open/adminer/elastic.php?elastic=https%3A%2F%2Flocalhost:9200&username=ODBC
clickAndWaitlogout
verifyTextPresentLogout successful.
open/coverage.php
+ + + diff --git a/tests/generate-pdo.php b/tests/generate-pdo.php index 5811301c..4f65d308 100755 --- a/tests/generate-pdo.php +++ b/tests/generate-pdo.php @@ -5,7 +5,7 @@ chdir(__DIR__); foreach (glob("*.html") as $filename) { - if (!preg_match('~^pdo-~', $filename)) { + if (!preg_match('~^pdo-|elastic~', $filename)) { $file = file_get_contents($filename); $file = preg_replace_callback('~/(adminer(/sqlite)?|editor/example)\.php(\??)~', function ($match) { return "/$match[1].php?ext=pdo" . ($match[3] ? "&" : "");