forked from adminerevo/adminerevo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelastic5.php
582 lines (478 loc) · 13.5 KB
/
elastic5.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
add_driver("elastic5", "Elasticsearch 5 (beta)");
if (isset($_GET["elastic5"])) {
define("DRIVER", "elastic5");
if (ini_bool('allow_url_fopen')) {
class Min_DB {
var $extension = "JSON", $server_info, $errno, $error, $_url, $_db;
/**
* @param string $path
* @param array|null $content
* @param string $method
* @return array|false
*/
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,
'header' => $content !== null ? 'Content-Type: application/json' : [],
'ignore_errors' => 1,
'follow_location' => 0,
'max_redirects' => 0,
))));
if ($file === false) {
$this->error = lang('Invalid server or credentials.');
return false;
}
$return = json_decode($file, true);
if ($return === null) {
$this->error = lang('Invalid server or credentials.');
return false;
}
if (!preg_match('~^HTTP/[0-9.]+ 2~i', $http_response_header[0])) {
if (isset($return['error']['root_cause'][0]['type'])) {
$this->error = $return['error']['root_cause'][0]['type'] . ": " . $return['error']['root_cause'][0]['reason'];
} else {
$this->error = lang('Invalid server or credentials.');
}
return false;
}
return $return;
}
/** Performs query relative to actual selected DB
* @param string $path
* @param array|null $content
* @param string $method
* @return array|false
*/
function query($path, array $content = null, $method = 'GET') {
// Support for global search through all tables
if ($path != "" && $path[0] == "S" && preg_match('/SELECT 1 FROM ([^ ]+) WHERE (.+) LIMIT ([0-9]+)/', $path, $matches)) {
global $driver;
$where = explode(" AND ", $matches[2]);
return $driver->select($matches[1], array("*"), $where, null, array(), $matches[3]);
}
return $this->rootQuery(($this->_db != "" ? "$this->_db/" : "/") . ltrim($path, '/'), $content, $method);
}
/**
* @param string $server
* @param string $username
* @param string $password
* @return bool
*/
function connect($server, $username, $password) {
$this->_url = build_http_url($server, $username, $password, "localhost", 9200);
$return = $this->query('');
if (!$return) {
return false;
}
if (!isset($return['version']['number'])) {
$this->error = lang('Invalid server or credentials.');
return false;
}
$this->server_info = $return['version']['number'];
return true;
}
function select_db($database) {
$this->_db = $database;
return true;
}
function quote($string) {
return $string;
}
}
class Min_Result {
var $num_rows, $_rows;
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;
}
function fetch_row() {
$row = $this->fetch_assoc();
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) {
$data = array();
if ($select != array("*")) {
$data["fields"] = array_values($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);
}
}
foreach ($where as $val) {
if (preg_match('~^\((.+ OR .+)\)$~', $val, $matches)) {
$parts = explode(" OR ", $matches[1]);
foreach ($parts as $part) {
$this->addQueryCondition($part, $data);
}
} else {
$this->addQueryCondition($val, $data);
}
}
$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();
foreach ($select as $key) {
$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);
}
private function addQueryCondition($val, &$data)
{
list($col, $op, $val) = explode(" ", $val, 3);
if (!preg_match('~^([^(]+)\(([^)]+)\)$~', $op, $matches)) {
return;
}
$queryType = $matches[1]; // must, should, must_not
$matchType = $matches[2]; // term, match, regexp
if ($matchType == "regexp") {
$data["query"]["bool"][$queryType][] = [
"regexp" => [
$col => [
"value" => $val,
"flags" => "ALL",
"case_insensitive" => true,
]
]
];
} else {
$data["query"]["bool"][$queryType][] = [
$matchType => [$col => $val]
];
}
}
function update($type, $record, $queryWhere, $limit = 0, $separator = "\n") {
//! use $limit
$parts = preg_split('~ *= *~', $queryWhere);
if (count($parts) == 2) {
$id = trim($parts[1]);
$query = "$type/$id";
return $this->_conn->query($query, $record, 'POST');
}
return false;
}
function insert($type, $record) {
$id = ""; //! user should be able to inform _id
$query = "$type/$id";
$response = $this->_conn->query($query, $record, 'POST');
$this->_conn->last_id = $response['_id'];
return $response['created'];
}
function delete($type, $queryWhere, $limit = 0) {
//! use $limit
$ids = array();
if (isset($_GET["where"]["_id"]) && $_GET["where"]["_id"]) {
$ids[] = $_GET["where"]["_id"];
}
if (isset($_POST['check'])) {
foreach ($_POST['check'] as $check) {
$parts = preg_split('~ *= *~', $check);
if (count($parts) == 2) {
$ids[] = trim($parts[1]);
}
}
}
$this->_conn->affected_rows = 0;
foreach ($ids as $id) {
$query = "{$type}/{$id}";
$response = $this->_conn->query($query, null, 'DELETE');
if ((isset($response['found']) && $response['found']) || (isset($response['result']) && $response['result'] == 'deleted')) {
$this->_conn->affected_rows++;
}
}
return $this->_conn->affected_rows;
}
}
/**
* @param string $hostPath
* @return bool
*/
function is_server_host_valid($hostPath)
{
return strpos(rtrim($hostPath, '/'), '/') === false;
}
function connect() {
$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;
}
function support($feature) {
return preg_match("~database|table|columns~", $feature);
}
function logged_user() {
$credentials = adminer()->credentials();
return $credentials[1];
}
function get_databases() {
$return = connection()->rootQuery('_aliases');
if ($return) {
$return = array_keys($return);
sort($return, SORT_STRING);
}
return $return;
}
function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
}
function collations() {
return array();
}
function db_collation($db, $collations) {
//
}
function engines() {
return array();
}
function count_tables($databases) {
$return = array();
$result = connection()->query('_stats');
if ($result && $result['indices']) {
$indices = $result['indices'];
foreach ($indices as $indice => $stats) {
$indexing = $stats['total']['indexing'];
$return[$indice] = $indexing['index_total'];
}
}
return $return;
}
function tables_list() {
if (min_version(7)) {
return array('_doc' => 'table');
}
$return = connection()->query('_mapping');
if ($return) {
$return = array_fill_keys(array_keys($return[connection()->_db]["mappings"]), 'table');
}
return $return;
}
function table_status($name = "", $fast = false) {
$search = connection()->query("_search", array(
"size" => 0,
"aggregations" => array(
"count_by_type" => array(
"terms" => array(
"field" => "_type"
)
)
)
), "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() {
return h(connection()->error);
}
function information_schema() {
}
function is_view($table_status) {
}
function indexes($table, $connection2 = null) {
return array(
array("type" => "PRIMARY", "columns" => array("_id")),
);
}
function fields($table) {
$mappings = array();
if (min_version(7)) {
$result = connection()->query("_mapping");
if ($result) {
$mappings = $result[connection()->_db]['mappings']['properties'];
}
} else {
$result = connection()->query("$table/_mapping");
if ($result) {
$mappings = $result[$table]['properties'];
if (!$mappings) {
$mappings = $result[connection()->_db]['mappings'][$table]['properties'];
}
}
}
$return = array(
"_id" => array(
"field" => "_id",
"full_type" => "_id",
"type" => "_id",
"privileges" => array("insert" => 1, "select" => 1, "where" => 1, "order" => 1),
)
);
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,
"where" => !isset($field["index"]) || $field["index"] ?: null,
"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;
}
function foreign_keys($table) {
return array();
}
function table($idf) {
return $idf;
}
function idf_escape($idf) {
return $idf;
}
function convert_field($field) {
//
}
function unconvert_field($field, $return) {
return $return;
}
function fk_support($table_status) {
//
}
function found_rows($table_status, $where) {
return null;
}
/** Create index
* @param string
* @return mixed
*/
function create_database($db) {
return connection()->rootQuery(urlencode($db), null, 'PUT');
}
/** Remove index
* @param array
* @return mixed
*/
function drop_databases($databases) {
return connection()->rootQuery(urlencode(implode(',', $databases)), null, 'DELETE');
}
/** Alter type
* @param array
* @return mixed
*/
function alter_table($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] ? $f[1][1] : "text");
$properties[$field_name] = array(
'type' => $field_type
);
}
if (!empty($properties)) {
$properties = array('properties' => $properties);
}
return connection()->query("_mapping/{$name}", $properties, 'PUT');
}
/** Drop types
* @param array
* @return bool
*/
function drop_tables($tables) {
$return = true;
foreach ($tables as $table) { //! convert to bulk api
$return = $return && connection()->query(urlencode($table), null, 'DELETE');
}
return $return;
}
function last_id() {
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, "boolean" => 1),
lang('Date and time') => array("date" => 10),
lang('Strings') => array("string" => 65535, "text" => 65535, "keyword" => 65535),
lang('Binary') => array("binary" => 255),
) as $key => $val) {
$types += $val;
$structured_types[$key] = array_keys($val);
}
return array(
'possible_drivers' => array("json + allow_url_fopen"),
'jush' => "elastic",
'operators' => array(
"must(term)", "must(match)", "must(regexp)",
"should(term)", "should(match)", "should(regexp)",
"must_not(term)", "must_not(match)", "must_not(regexp)",
),
'operator_like' => "should(match)",
'operator_regexp' => "should(regexp)",
'functions' => array(),
'grouping' => array(),
'edit_functions' => array(array("json")),
'types' => $types,
'structured_types' => $structured_types,
);
}
}