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
|
|
|
|
2013-08-08 20:21:34 +00:00
|
|
|
function query($path, $content = array()) {
|
2013-07-19 21:09:43 +00:00
|
|
|
@ini_set('track_errors', 1); // @ - may be disabled
|
2013-07-10 00:38:13 +00:00
|
|
|
$file = @file_get_contents($this->_url . ($this->_db != "" ? "$this->_db/" : "") . $path, false, stream_context_create(array('http' => array(
|
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);
|
|
|
|
if (!$return) {
|
|
|
|
$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
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function connect($server, $username, $password) {
|
|
|
|
$this->_url = "http://$username:$password@$server/";
|
|
|
|
$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
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function Min_Result($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-09 07:14:37 +00:00
|
|
|
function select($table, $select, $where, $group, $order, $limit, $page, $print = false) {
|
2013-07-10 00:38:13 +00:00
|
|
|
global $adminer;
|
|
|
|
$query = $adminer->selectQueryBuild($select, $where, $group, $order, $limit, $page);
|
2013-08-08 20:21:34 +00:00
|
|
|
$data = array();
|
2013-07-10 00:38:13 +00:00
|
|
|
if (!$query) {
|
2013-08-08 20:21:34 +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);
|
|
|
|
}
|
|
|
|
$data["sort"] = $sort;
|
|
|
|
}
|
|
|
|
if ($limit) {
|
|
|
|
$data["size"] = +$limit;
|
|
|
|
if ($page) {
|
|
|
|
$data["from"] = ($page * $limit);
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 21:09:43 +00:00
|
|
|
foreach ((array) $_GET["where"] as $val) {
|
|
|
|
if ("$val[col]$val[val]" != "") {
|
2013-08-08 20:21:34 +00:00
|
|
|
$term = array("match" => array(($val["col"] != "" ? $val["col"] : "_all") => $val["val"]));
|
|
|
|
if ($val["op"] == "=") {
|
|
|
|
$data["query"]["filtered"]["filter"]["and"][] = $term;
|
|
|
|
} else {
|
|
|
|
$data["query"]["filtered"]["query"]["bool"]["must"][] = $term;
|
|
|
|
}
|
2013-07-19 21:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 20:21:34 +00:00
|
|
|
if ($data["query"] && !$data["query"]["filtered"]["query"]) {
|
|
|
|
$data["query"]["filtered"]["query"] = array("match_all" => array());
|
|
|
|
}
|
2013-07-10 00:38:13 +00:00
|
|
|
}
|
2014-01-09 07:14:37 +00:00
|
|
|
if ($print) {
|
|
|
|
echo $adminer->selectQuery("$query: " . print_r($data, true));
|
|
|
|
}
|
2013-08-08 20:21:34 +00:00
|
|
|
$search = $this->_conn->query($query, $data);
|
2013-07-10 00:38:13 +00:00
|
|
|
if (!$search) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$return = array();
|
|
|
|
foreach ($search['hits']['hits'] as $hit) {
|
|
|
|
$row = array();
|
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) {
|
|
|
|
$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;
|
|
|
|
$return = $connection->query('_aliases');
|
|
|
|
if ($return) {
|
|
|
|
$return = array_keys($return);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
}
|
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) {
|
|
|
|
$return = array_fill_keys(array_keys(reset($return)), 'table');
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2013-07-24 23:26:41 +00:00
|
|
|
|
2013-07-10 00:38:13 +00:00
|
|
|
function table_status($name = "", $fast = false) {
|
|
|
|
$return = tables_list();
|
|
|
|
if ($return) {
|
|
|
|
foreach ($return as $key => $type) { // _stats have just info about database
|
|
|
|
$return[$key] = array("Name" => $key, "Engine" => $type);
|
|
|
|
if ($name != "") {
|
|
|
|
return $return[$name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
$mapping = $connection->query("$table/_mapping");
|
|
|
|
$return = array();
|
|
|
|
if ($mapping) {
|
|
|
|
foreach ($mapping[$table]['properties'] as $name => $field) {
|
|
|
|
$return[$name] = array(
|
|
|
|
"field" => $name,
|
|
|
|
"full_type" => $field["type"],
|
|
|
|
"type" => $field["type"],
|
|
|
|
"privileges" => array("insert" => 1, "select" => 1, "update" => 1),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|