-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
executable file
·596 lines (532 loc) · 18.6 KB
/
main.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
use Elasticsearch\ClientBuilder;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class Main
{
/**
* initialize elastic search
* @return \Elasticsearch\Client
*/
const ONE_MONTH = 2678400;
private function initialize()
{
# monolog
$logger = ClientBuilder::defaultLogger('log/error.log', Logger::WARNING);
# elasticsearch
$client = ClientBuilder::create()
->setLogger($logger)
->build();
return $client;
}
/**
* initialize log
* @param string $name
* @param string $address
* @return object elastic
*/
private function log($name, $address)
{
$logging = new Logger($name);
$log = $logging->pushHandler(new StreamHandler($address));
return $log;
}
/**
* verify http method and get body
* @param string $httpMethod
* @return mixed
*/
private function getBody($httpMethod)
{
# validate http method
if ($_SERVER['REQUEST_METHOD'] != $httpMethod)
$this->result(false, null, 700, 'http method must be ' . $httpMethod);
# get request body
$body = file_get_contents("php://input");
# return parsed data
return json_decode($body, true);
}
/**
* insert to elastic search db
* @return mixed
*/
public function insert()
{
$body = $this->getBody('PUT');
$client = $this->initialize();
# set elastic query
$params = [
'index' => $body['database'],
'type' => $body['table'],
'id' => $body['key'],
'body' => $body['fields']
];
# insert
$response = $client->index($params);
# set version (version more than 1, means duplicated)
$result = ['version' => $response['_version']];
# return result
$this->result(true, $result, null, null);
}
/**
* update document in elastic search
* @return mixed
*/
public function update()
{
$body = $this->getBody('PUT');
$client = $this->initialize();
# set elastic query
$params = [
'index' => $body['database'],
'type' => $body['table'],
'id' => $body['key'],
'body' => [
'doc' => $body['fields']
]
];
# update
$response = $client->update($params);
# set version
$result = ['version' => $response['_version']];
# return result
$this->result(true, $result, null, null);
}
/**
* select just with key
* @param string $database
* @param string $table
* @param string $Key
* @return mixed
*/
public function select($database, $table, $Key)
{
$this->getBody('GET'); # just verify http method
$client = $this->initialize();
# set elastic query
$params = [
'index' => $database,
'type' => $table,
'id' => $Key,
'client' => ['ignore' => 404] # for custom exception
];
# select
$response = $client->get($params);
# check found
if (!$response['found']) {
$this->result(false, null, 701, 'not found');
}
# return result
$this->result(true, $response['_source'], null, null);
}
/**
* delete a document by key
* @param string $database
* @param string $table
* @param string $Key
* @return mixed
*/
public function deleteDocument($database, $table, $Key)
{
$this->getBody('DELETE'); # just verify http method
$client = $this->initialize();
# set elastic query
$params = [
'index' => $database,
'type' => $table,
'id' => $Key,
'client' => ['ignore' => 404] # for custom exception
];
# check existence
$get = $client->get($params);
if (!$get['found']) {
$this->result(false, null, 702, 'not found');
}
# delete
$client->delete($params);
# return result
$this->result(true, null, null, null);
}
/**
* search in elastic db
* @param string $database
* @param string $table
* @param integer $offset
* @param integer $limit
* @param string $word
* @return mixed
*/
public function search($database, $table, $offset, $limit, $word)
{
$this->getBody('GET'); # just verify http method
$client = $this->initialize();
#correction word
$word = substr($word, 1);
# for logging
$this->log('search', 'log/search.log')->info($word);
# get query params and search
$params = $this->searchParams($database, $table, $word, $offset, $limit);
$response = $client->search($params);
# check found
if (!$response['hits']['hits'])
$this->result(false, null, 703, 'not found');
# refactor result
foreach ($response['hits']['hits'] as $key => $value)
{
$result[$key]['username'] = $value['_source']['username'];
$result[$key]['displayName'] = $value['_source']['displayName'];
$result[$key]['age'] = $value['_source']['age'];
$result[$key]['city'] = $value['_source']['city'];
$result[$key]['website'] = $value['_source']['website'];
$result[$key]['email'] = $value['_source']['email'];
$result[$key]['cover'] = $value['_source']['cover'];
}
# return result
$this->result(true, $result, null, null);
}
/**
* create search parameters
* @param string $database
* @param string $table
* @param string $word
* @param integer $offset
* @param integer $limit
* @return array
*/
private function searchParams($database, $table, $word, $offset, $limit)
{
$params = [
'index' => $database,
'type' => $table,
'from' => $offset,
'size' => $limit,
'body' => [
"query" => [
'function_score' => [
'query' => [
'bool' => [
'should' => [
[
'bool' => [
'should' => [
[
"multi_match" => [
"query" => $word,
'fields' => ['name', 'name_fa'],
"fuzziness" => 'AUTO',
'boost' => 2,
],
],
[
"multi_match" => [
"query" => str_replace(' ', '', $word),
'fields' => ['name', 'name_fa'],
"fuzziness" => 'AUTO',
'boost' => 2,
],
],
]
]
],
[
'multi_match' => [
'query' => $word,
'fields' => ['category_name', 'category_name_fa'],
'type' => 'phrase',
"minimum_should_match" => "100%",
"operator" => "and",
'boost' => 50
]
],
[
'match' => [
'website_developer' => [
'query' => $word,
"minimum_should_match" => "100%",
"operator" => "and",
'boost' => 1
]
]
],
],
],
],
"field_value_factor" => [
"field" => "site_views",
"modifier" => "log1p",
"factor" => 0.1,
],
"boost_mode" => "multiply",
],
]
],
'client' => ['ignore' => 404] # for custom exception
];
return $params;
}
/**
* import from websites mysql database to elastic search database
* @return mixed
*/
public function import()
{
$body = $this->getBody('PUT');
$client = $this->initialize();
# set session for current database name and start import flag
ini_set('session.gc_maxlifetime', self::ONE_MONTH);
session_id('database');
session_start();
# for logging
$this->log('import', 'log/import.log')->info('before', $_SESSION);
# set variable
$newDatabase = (string)$body['database'];
$table = $body['table'];
$fields = $body['fields'];
$oldDatabase = isset($_SESSION['old']) ? $_SESSION['old'] : null; # get old database name
$startDatabase = isset($_SESSION['start']) ? $_SESSION['start'] : null; # get start import process
$finish = $body['finish'];
# if this is first request, create database schema
if (!$startDatabase) {
$this->createDatabase($client, $newDatabase, $table);
}
# insert data (batch)
$this->insertBatch($newDatabase, $table, $fields, $client, $finish, $oldDatabase);
# for logging
$this->log('import', 'log/import.log')->info('after', $_SESSION);
$this->result(true, null, null, null);
}
/**
* create elastic search database with schema
* @param object $client
* @param string $newDatabase
* @param string $table
*/
public function createDatabase($client, $newDatabase, $table)
{
$params = [
'index' => $newDatabase,
'body' => [
'settings' => [
'analysis' => [
'filter' => [
'filter_websitename' => [
'type' => 'synonym',
'synonyms_path' => 'synonyms_websitename.txt'
],
'filter_websitenamefa' => [
'type' => 'synonym',
'synonyms_path' => 'synonyms_websitenamefa.txt'
],
],
'analyzer' => [
'analyzer_websitename' => [
'tokenizer' => 'standard',
'filter' => ['filter_name', 'lowercase']
],
'analyzer_websitenamefa' => [
'tokenizer' => 'standard',
'filter' => ['filter_namefa']
],
]
]
],
'mwebsiteings' => [
$table => [
'properties' => [
'website_name' => [
'type' => 'string',
'analyzer' => 'analyzer_name',
],
'website_name_fa' => [
'type' => 'string',
'analyzer' => 'analyzer_namefa',
],
]
],
]
]
];
$client->indices()->create($params);
}
/**
* @param $newDatabase
* @param $table
* @param $fields
* @param $client
* @param $finish
* @param $oldDatabase
*/
private function insertBatch($newDatabase, $table, $fields, $client, $finish, $oldDatabase)
{
$ESQuery = $this->reformElasticQuery($newDatabase, $table, $fields);
$client->bulk($ESQuery);
$_SESSION['start'] = true;
# if this is last request, remove old db and change alias
if ($finish) {
if ($oldDatabase) # old db name on session is existence ===> this is first time
$this->afterImport($client, $oldDatabase, $newDatabase);
else # old db name on session is not existence ===> this is not first time
$this->afterFirstImport($client, $newDatabase);
}
}
/**
* do operations after first import from zero state
* @param object $client
* @param string $newDatabase
*/
private function afterFirstImport($client, $newDatabase)
{
# step 1 : add alias
$params['body'] = [
'actions' => [
[
'add' => [
'index' => $newDatabase,
'alias' => 'websites'
]
]
]
];
$client->indices()->updateAliases($params);
# step 2 : update session
$_SESSION['old'] = $newDatabase;
$_SESSION['start'] = false;
}
/**
* do operations after import
* @param object $client
* @param string $oldDatabase
* @param string $newDatabase
*/
private function afterImport($client, $oldDatabase, $newDatabase)
{
# step 1 : remove alias
$params['body'] = [
'actions' => [
[
'remove' => [
'index' => $oldDatabase,
'alias' => 'websites'
]
]
]
];
$client->indices()->updateAliases($params);
# step 2 : add alias
$params['body'] = [
'actions' => [
[
'add' => [
'index' => $newDatabase,
'alias' => 'websites'
]
]
]
];
$client->indices()->updateAliases($params);
# step 3 : remove index
$deleteParams = [
'index' => $oldDatabase
];
$client->indices()->delete($deleteParams);
# step 4 : update session
$_SESSION['old'] = $newDatabase;
$_SESSION['start'] = false;
}
/**
* reform mysql query to elastic query
* @param string $newDB
* @param string $table
* @param array $fields
* @return array
*/
private function reformElasticQuery($newDB, $table, $fields)
{
for ($i = 0; $i < count($fields); $i++) {
$result['body'][] = [
'index' => [
'_index' => $newDB,
'_type' => $table,
'_id' => $fields[$i]['website_id']
],
];
$result['body'][] = [
'email' => $fields[$i]['website_developer_email'],
'website' => $fields[$i]['website_developer_website'],
'video' => (int)$fields[$i]['website_video'],
'video_delete' => (int)$fields[$i]['video_delete'],
'user_display_name' => $fields[$i]['user_display_name'],
'user_email' => $fields[$i]['user_email'],
'user_website' => $fields[$i]['user_website']
];
}
return $result;
}
/**
* delete database with name
* @param string $database
* @return mixed
*/
public function deleteDatabase($database)
{
$this->getBody('DELETE');
$client = $this->initialize();
$deleteParams = [
'index' => $database
];
$dbExist = $client->indices()->exists($deleteParams);
# check database exist
if ( !$dbExist ) {
$this->result(false, null, 704, 'database don\'t exist');
}
# delete database
$response = $client->indices()->delete($deleteParams);
#check database deleted
if ( $response['acknowledged'] == 1 ) {
$this->result('true', null, null, null);
}
}
/**
* delete all session
* @return string
*/
public function reset()
{
session_id('database');
session_start();
session_unset();
if (session_destroy())
echo 'success';
else
echo 'failed';
}
/**
* get current database name on session
* @return array
*/
public function currentDB()
{
session_id('database');
session_start();
var_dump($_SESSION);
}
/**
* create result for all method
* @param string $success
* @param array, string $result
* @param integer $errorCode
* @param string $errorDescription
* @return mixed
*/
private function result($success, $result, $errorCode, $errorDescription)
{
header('Content-Type: websitelication/json');
$message = [
'success' => $success,
'result' => $result,
'error_code' => $errorCode,
'error_description' => $errorDescription,
];
echo json_encode($message);
}
}