2013-07-10 00:38:13 +00:00
|
|
|
<?php
|
2014-01-04 22:26:02 +00:00
|
|
|
$drivers["elastic"] = "Elasticsearch (beta)";
|
2013-07-10 00:38:13 +00:00
|
|
|
|
|
|
|
if (isset($_GET["elastic"])) {
|
|
|
|
$possible_drivers = array("json");
|
|
|
|
define("DRIVER", "elastic");
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
if (function_exists('json_decode')) {
|
|
|
|
class Min_DB {
|
|
|
|
var $extension = "JSON", $server_info, $errno, $error, $_url;
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2014-01-12 11:29:51 +00:00
|
|
|
/** Performs query
|
|
|
|
* @param string
|
|
|
|
* @param array
|
|
|
|
* @param string
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function rootQuery($path, $content = array(), $method = 'GET') {
|
2013-07-19 21:09:43 +00:00
|
|
|
@ini_set('track_errors', 1); // @ - may be disabled
|
2014-01-12 11:29:51 +00:00
|
|
|
$file = @file_get_contents($this->_url . '/' . ltrim($path, '/'), false, stream_context_create(array('http' => array(
|
2014-01-09 21:27:06 +00:00
|
|
|
'method' => $method,
|
2013-08-08 20:21:34 +00:00
|
|
|
'content' => json_encode($content),
|
2013-07-10 00:38:13 +00:00
|
|
|
'ignore_errors' => 1, // available since PHP 5.2.10
|
|
|
|
))));
|
|
|
|
if (!$file) {
|
|
|
|
$this->error = $php_errormsg;
|
|
|
|
return $file;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
if (!preg_match('~^HTTP/[0-9.]+ 2~i', $http_response_header[0])) {
|
2013-07-10 00:38:13 +00:00
|
|
|
$this->error = $file;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$return = json_decode($file, true);
|
2014-09-05 23:08:13 +00:00
|
|
|
if ($return === null) {
|
2013-07-10 00:38:13 +00:00
|
|
|
$this->errno = json_last_error();
|
|
|
|
if (function_exists('json_last_error_msg')) {
|
|
|
|
$this->error = json_last_error_msg();
|
|
|
|
} else {
|
|
|
|
$constants = get_defined_constants(true);
|
|
|
|
foreach ($constants['json'] as $name => $value) {
|
2013-07-24 23:26:41 +00:00
|
|
|
if ($value == $this->errno && preg_match('~^JSON_ERROR_~', $name)) {
|
2013-07-10 00:38:13 +00:00
|
|
|
$this->error = $name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2014-01-12 11:29:51 +00:00
|
|
|
/** Performs query relative to actual selected DB
|
|
|
|
* @param string
|
|
|
|
* @param array
|
|
|
|
* @param string
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function query($path, $content = array(), $method = 'GET') {
|
|
|
|
return $this->rootQuery(($this->_db != "" ? "$this->_db/" : "/") . ltrim($path, '/'), $content, $method);
|
|
|
|
}
|
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function connect($server, $username, $password) {
|
2017-02-20 17:07:30 +00:00
|
|
|
preg_match('~^(https?://)?(.*)~', $server, $match);
|
2017-02-20 17:06:14 +00:00
|
|
|
$this->_url = ($match[1] ? $match[1] : "http://") . "$username:$password@$match[2]/";
|
2013-07-10 00:38:13 +00:00
|
|
|
$return = $this->query('');
|
|
|
|
if ($return) {
|
|
|
|
$this->server_info = $return['version']['number'];
|
|
|
|
}
|
|
|
|
return (bool) $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function select_db($database) {
|
|
|
|
$this->_db = $database;
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function quote($string) {
|
|
|
|
return $string;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
class Min_Result {
|
2013-08-08 23:02:23 +00:00
|
|
|
var $num_rows, $_rows;
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2015-08-15 15:04:21 +00:00
|
|
|
function __construct($rows) {
|
2013-08-08 23:02:23 +00:00
|
|
|
$this->num_rows = count($this->_rows);
|
2013-07-10 00:38:13 +00:00
|
|
|
$this->_rows = $rows;
|
|
|
|
reset($this->_rows);
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function fetch_assoc() {
|
|
|
|
$return = current($this->_rows);
|
|
|
|
next($this->_rows);
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function fetch_row() {
|
|
|
|
return array_values($this->fetch_assoc());
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
class Min_Driver extends Min_SQL {
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2014-01-15 16:23:26 +00:00
|
|
|
function select($table, $select, $where, $group, $order = array(), $limit = 1, $page = 0, $print = false) {
|
2013-07-10 00:38:13 +00:00
|
|
|
global $adminer;
|
2013-08-08 20:21:34 +00:00
|
|
|
$data = array();
|
2014-01-09 07:16:51 +00:00
|
|
|
$query = "$table/_search";
|
|
|
|
if ($select != array("*")) {
|
|
|
|
$data["fields"] = $select;
|
|
|
|
}
|
|
|
|
if ($order) {
|
|
|
|
$sort = array();
|
|
|
|
foreach ($order as $col) {
|
|
|
|
$col = preg_replace('~ DESC$~', '', $col, 1, $count);
|
|
|
|
$sort[] = ($count ? array($col => "desc") : $col);
|
2013-08-08 20:21:34 +00:00
|
|
|
}
|
2014-01-09 07:16:51 +00:00
|
|
|
$data["sort"] = $sort;
|
|
|
|
}
|
|
|
|
if ($limit) {
|
|
|
|
$data["size"] = +$limit;
|
|
|
|
if ($page) {
|
|
|
|
$data["from"] = ($page * $limit);
|
2013-08-08 20:21:34 +00:00
|
|
|
}
|
2014-01-09 07:16:51 +00:00
|
|
|
}
|
2014-09-06 01:24:03 +00:00
|
|
|
foreach ($where as $val) {
|
|
|
|
list($col,$op,$val) = explode(" ",$val,3);
|
|
|
|
if ($col == "_id") {
|
|
|
|
$data["query"]["ids"]["values"][] = $val;
|
|
|
|
}
|
|
|
|
elseif ($col . $val != "") {
|
|
|
|
$term = array("term" => array(($col != "" ? $col : "_all") => $val));
|
|
|
|
if ($op == "=") {
|
2014-01-09 07:16:51 +00:00
|
|
|
$data["query"]["filtered"]["filter"]["and"][] = $term;
|
|
|
|
} else {
|
|
|
|
$data["query"]["filtered"]["query"]["bool"]["must"][] = $term;
|
2013-07-19 21:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-09 07:16:51 +00:00
|
|
|
}
|
2014-09-06 01:24:03 +00:00
|
|
|
if ($data["query"] && !$data["query"]["filtered"]["query"] && !$data["query"]["ids"]) {
|
2014-01-09 07:16:51 +00:00
|
|
|
$data["query"]["filtered"]["query"] = array("match_all" => array());
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
2014-03-07 17:33:37 +00:00
|
|
|
$start = microtime(true);
|
|
|
|
$search = $this->_conn->query($query, $data);
|
2014-01-09 07:14:37 +00:00
|
|
|
if ($print) {
|
2014-03-11 16:37:56 +00:00
|
|
|
echo $adminer->selectQuery("$query: " . print_r($data, true), format_time($start));
|
2014-01-09 07:14:37 +00:00
|
|
|
}
|
2013-07-10 00:38:13 +00:00
|
|
|
if (!$search) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$return = array();
|
|
|
|
foreach ($search['hits']['hits'] as $hit) {
|
|
|
|
$row = array();
|
2014-09-06 01:24:03 +00:00
|
|
|
if ($select == array("*")) {
|
|
|
|
$row["_id"] = $hit["_id"];
|
|
|
|
}
|
2013-07-19 21:09:43 +00:00
|
|
|
$fields = $hit['_source'];
|
|
|
|
if ($select != array("*")) {
|
|
|
|
$fields = array();
|
|
|
|
foreach ($select as $key) {
|
|
|
|
$fields[$key] = $hit['fields'][$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($fields as $key => $val) {
|
2014-09-06 00:14:01 +00:00
|
|
|
if ($data["fields"]) {
|
|
|
|
$val = $val[0];
|
|
|
|
}
|
2013-07-19 21:09:43 +00:00
|
|
|
$row[$key] = (is_array($val) ? json_encode($val) : $val); //! display JSON and others differently
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
|
|
|
$return[] = $row;
|
|
|
|
}
|
|
|
|
return new Min_Result($return);
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function connect() {
|
|
|
|
global $adminer;
|
|
|
|
$connection = new Min_DB;
|
|
|
|
$credentials = $adminer->credentials();
|
|
|
|
if ($connection->connect($credentials[0], $credentials[1], $credentials[2])) {
|
|
|
|
return $connection;
|
|
|
|
}
|
|
|
|
return $connection->error;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function support($feature) {
|
2013-08-08 20:24:20 +00:00
|
|
|
return preg_match("~database|table|columns~", $feature);
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function logged_user() {
|
|
|
|
global $adminer;
|
|
|
|
$credentials = $adminer->credentials();
|
|
|
|
return $credentials[1];
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function get_databases() {
|
|
|
|
global $connection;
|
2014-01-12 11:29:51 +00:00
|
|
|
$return = $connection->rootQuery('_aliases');
|
2013-07-10 00:38:13 +00:00
|
|
|
if ($return) {
|
|
|
|
$return = array_keys($return);
|
2014-09-16 15:40:47 +00:00
|
|
|
sort($return, SORT_STRING);
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function collations() {
|
|
|
|
return array();
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function db_collation($db, $collations) {
|
|
|
|
}
|
2014-09-05 23:13:15 +00:00
|
|
|
|
|
|
|
function engines() {
|
|
|
|
return array();
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function count_tables($databases) {
|
|
|
|
global $connection;
|
|
|
|
$return = $connection->query('_mapping');
|
|
|
|
if ($return) {
|
|
|
|
$return = array_map('count', $return);
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function tables_list() {
|
|
|
|
global $connection;
|
|
|
|
$return = $connection->query('_mapping');
|
|
|
|
if ($return) {
|
2014-09-05 23:43:34 +00:00
|
|
|
$return = array_fill_keys(array_keys($return[$connection->_db]["mappings"]), 'table');
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function table_status($name = "", $fast = false) {
|
2014-03-24 18:21:34 +00:00
|
|
|
global $connection;
|
|
|
|
$search = $connection->query("_search?search_type=count", array(
|
|
|
|
"facets" => array(
|
|
|
|
"count_by_type" => array(
|
|
|
|
"terms" => array(
|
2014-03-25 16:38:40 +00:00
|
|
|
"field" => "_type",
|
2014-03-24 18:21:34 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
), "POST");
|
|
|
|
$return = array();
|
|
|
|
if ($search) {
|
|
|
|
foreach ($search["facets"]["count_by_type"]["terms"] as $table) {
|
|
|
|
$return[$table["term"]] = array(
|
|
|
|
"Name" => $table["term"],
|
|
|
|
"Engine" => "table",
|
2014-03-25 16:38:40 +00:00
|
|
|
"Rows" => $table["count"],
|
2014-03-24 18:21:34 +00:00
|
|
|
);
|
2016-03-16 11:05:51 +00:00
|
|
|
if ($name != "" && $name == $table["term"]) {
|
|
|
|
return $return[$name];
|
|
|
|
}
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function error() {
|
|
|
|
global $connection;
|
|
|
|
return h($connection->error);
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function information_schema() {
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function is_view($table_status) {
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function indexes($table, $connection2 = null) {
|
|
|
|
return array(
|
|
|
|
array("type" => "PRIMARY", "columns" => array("_id")),
|
|
|
|
);
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function fields($table) {
|
|
|
|
global $connection;
|
2014-06-23 19:38:35 +00:00
|
|
|
$result = $connection->query("$table/_mapping");
|
2013-07-10 00:38:13 +00:00
|
|
|
$return = array();
|
2014-06-23 19:38:35 +00:00
|
|
|
if ($result) {
|
|
|
|
$mappings = $result[$table]['properties'];
|
|
|
|
if (!$mappings) {
|
|
|
|
$mappings = $result[$connection->_db]['mappings'][$table]['properties'];
|
|
|
|
}
|
2014-09-05 23:20:20 +00:00
|
|
|
if ($mappings) {
|
|
|
|
foreach ($mappings as $name => $field) {
|
|
|
|
$return[$name] = array(
|
|
|
|
"field" => $name,
|
|
|
|
"full_type" => $field["type"],
|
|
|
|
"type" => $field["type"],
|
|
|
|
"privileges" => array("insert" => 1, "select" => 1, "update" => 1),
|
|
|
|
);
|
2014-09-06 01:24:03 +00:00
|
|
|
if ($field["properties"]) { // only leaf fields can be edited
|
|
|
|
unset($return[$name]["privileges"]["insert"]);
|
|
|
|
unset($return[$name]["privileges"]["update"]);
|
|
|
|
}
|
2014-09-05 23:20:20 +00:00
|
|
|
}
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function foreign_keys($table) {
|
|
|
|
return array();
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function table($idf) {
|
|
|
|
return $idf;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function idf_escape($idf) {
|
|
|
|
return $idf;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function convert_field($field) {
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function unconvert_field($field, $return) {
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function fk_support($table_status) {
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function found_rows($table_status, $where) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2014-01-09 21:27:06 +00:00
|
|
|
/** Create database
|
|
|
|
* @param string
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function create_database($db) {
|
|
|
|
global $connection;
|
2014-01-12 11:29:51 +00:00
|
|
|
return $connection->rootQuery(urlencode($db), array(), 'PUT');
|
2014-01-09 21:27:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 21:39:19 +00:00
|
|
|
/** Drop databases
|
|
|
|
* @param array
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function drop_databases($databases) {
|
|
|
|
global $connection;
|
2014-01-12 11:29:51 +00:00
|
|
|
return $connection->rootQuery(urlencode(implode(',', $databases)), array(), 'DELETE');
|
2014-01-09 21:39:19 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 21:53:23 +00:00
|
|
|
/** Drop tables
|
|
|
|
* @param array
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function drop_tables($tables) {
|
|
|
|
global $connection;
|
2014-01-10 06:28:42 +00:00
|
|
|
$return = true;
|
|
|
|
foreach ($tables as $table) { //! convert to bulk api
|
|
|
|
$return = $return && $connection->query(urlencode($table), array(), 'DELETE');
|
2014-01-09 21:53:23 +00:00
|
|
|
}
|
2014-01-10 06:28:42 +00:00
|
|
|
return $return;
|
2014-01-09 21:53:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
$jush = "elastic";
|
2013-08-08 20:21:34 +00:00
|
|
|
$operators = array("=", "query");
|
2013-07-10 00:38:13 +00:00
|
|
|
$functions = array();
|
|
|
|
$grouping = array();
|
2013-08-09 22:49:34 +00:00
|
|
|
$edit_functions = array(array("json"));
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|