-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySQLtoes.php
320 lines (270 loc) · 11.4 KB
/
mySQLtoes.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
<?php
/* Author: Mahdi Hazaveh <mahdi@hazaveh.net> */
require_once 'medoo.min.php';
class convertAgent
{
// Initializing default variables.
public $idCol = 1;
public $indexName;
public $tableName;
public $database;
public $elasticSearch;
public $colors;
public $resume;
public $offset = 0;
public $logger;
public function __construct()
{
global $argv;
$this->colors = new Colors();
if (file_exists('resume.lock') && $argv[1] != 'resume') {
$this->resume();
}
if (isset($argv[1]) && $argv[1] == 'resume') {
echo PHP_EOL . 'Resuming from previous state' . PHP_EOL;
if (file_exists('resume.lock')) {
$this->offset = (int) file_get_contents('resume.lock');
} else {
echo PHP_EOL . 'resume.lock file does not exist. Exiting...';
die(PHP_EOL);
}
}
if (strlen(DB_TABLE) == 0) {
echo PHP_EOL . $this->colors->
getColoredString("Database table is not defined. Please check the Config.php file.",
'red') . PHP_EOL;
die();
}
try {
$this->database = new medoo([
'database_type' => 'mysql',
'database_name' => DB_NAME,
'server' => DB_HOST,
'username' => DB_USER,
'password' => DB_PASS,
'charset' => 'utf8',
'port' => DB_PORT,
]);
} catch (Exception $e) {
echo PHP_EOL . $this->colors->
getColoredString("Error While Connection to mySQL Database,\nPlease check your Database connection settings in config.php",
'red') . PHP_EOL;
die();
}
if (USE_DB_COLUMN == TRUE) {
$this->idCol = DB_COL;
}
global $ES_HOST;
$this->logger = \Elasticsearch\ClientBuilder::defaultLogger('mySQLtoes.log');
$this->elasticSearch = \Elasticsearch\ClientBuilder::create()->setLogger($this->logger)->setHosts($ES_HOST)->build();
}
// This function understands the Table Status and number of rows.
public function understand()
{
echo PHP_EOL . $this->colors->
getColoredString('mySQLtoes 1.2', 'green')
. PHP_EOL;
sleep(1);
echo $this->colors->
getColoredString('I\'m looking for the number of rows in the MySQL Table.', "green")
. PHP_EOL;
sleep(1);
$totalRrows = $this->database->count(DB_TABLE);
echo PHP_EOL . $this->colors->
getColoredString('I found ' . $totalRrows . ' items in ' . DB_TABLE . ' table!', 'yellow')
. PHP_EOL;
sleep(1);
echo PHP_EOL . $this->colors->
getColoredString('You set a limit of ' . Q_LIMIT . ' per select query in your config file.', 'black', 'yellow')
. PHP_EOL . PHP_EOL;
echo $this->colors->getColoredString('Calculating number of queries to be executed.', 'green');
sleep(1);
echo $this->colors->getColoredString('.', 'green');
sleep(1);
echo $this->colors->getColoredString('.', 'green') . PHP_EOL . PHP_EOL;
$this->Querying($totalRrows);
}
public function execute($x)
{
$startTime = microtime(true);
$jpnResult = $this->database->query($x)->fetchAll(PDO::FETCH_ASSOC);
echo PHP_EOL . 'Successful query.' . PHP_EOL;
$params = ['body' => []];
echo $this->colors->
getColoredString('Getting ' . Q_LIMIT . ' records ready to insert.', 'yellow') . PHP_EOL;
if ($this->idCol == DB_COL) {
foreach ($jpnResult as $column => $content) {
$params['body'][] = [
'index' => [
'_index' => ES_INDEX,
'_type' => ES_TYPE,
'_id' => $content[$this->idCol],
]
];
$params['body'][] = [$column => $content];
}
} else {
foreach ($jpnResult as $column => $content) {
$params['body'][] = [
'index' => [
'_index' => ES_INDEX,
'_type' => ES_TYPE,
'_id' => $this->idCol,
]
];
$params['body'][] = [$column => $content];
$this->idCol++;
}
}
try {
$responses = $this->elasticSearch->bulk($params);
} catch (Exception $e) {
echo PHP_EOL .
$this->colors->getColoredString("Error while connection to Elasticsearch! Please check your\nconnection details in config.php and validate the settings.", 'red')
. PHP_EOL;
die();
}
echo $this->colors->
getColoredString('Successfully inserted ' . Q_LIMIT . ' records to Elasticsearch, moving on...', 'green') . PHP_EOL;
$endTime = microtime(true);
$totalTime = ($endTime - $startTime) / 60;
echo 'Batch Finished.' . PHP_EOL;
echo 'Total Time Elapsed: ' . $totalTime . " Minutes.";
}
public function currentStatus($offset) {
$file = fopen('resume.lock' , 'w');
fwrite($file, $offset);
fclose($file);
}
public function Querying($a, $offset = 0)
{
global $ES_HOST;
// How many queries should I be running:
$toDoQueries = (int)($a / Q_LIMIT) + 1;
echo $this->colors->
getColoredString('Based on number of your rows I have to run ' . $toDoQueries .
' queries.', 'yellow') . PHP_EOL . PHP_EOL;
echo $this->colors->
getColoredString('
.i;;;;i.
iYcviii;vXY:
.YXi .i1c.
.YC. . in7.
.vc. ...... ;1c.
i7, .. .;1;
i7, .. ... .Y1i
,7v .6MMM@; .YX,
.7;. ..IMMMMMM1 :t7.
.;Y. ;$MMMMMM9. :tc.
vY. .. .nMMM@MMU. ;1v.
i7i ... .#MM@M@C. .....:71i
it: .... $MMM@9;.,i;;;i,;tti
:t7. ..... CREATED,iii:::,,;St.
.nC. ..... IMMMQ..,::::::,.,czX.
.ct: ....... .ZMMMI..,:::::::,,:76Y.
c2: ......,i..Y$M@t..:::::::,,..inZY
vov ......:ii..c$MBc..,,,,,,,,,,..iI9i
i9Y ......iii:..7@MA,..,,,,,,,,,....;AA:
iIS. ......:ii::..;@MI....,............;Ez.
.I9. ......:i::::...8M1..................C0z.
.z9; ......:i::::,.. .i:...................zWX.
vbv ......,i::::,,. ................. :AQY
c6Y. .,...,::::,,..:..BY... ................ :8bi
:6S. ..,,...,:::,,,....MAHDI.. ............... .;bZ,
:6o, .,,,,..:::,,,..i#HAZAVEH#................. YW2.
.n8i ..,,,,,,,::,,,,.. tMMMMM@C:.................. .1Wn
7Uc. .:::,,,,,::,,,,.. i1t;,..................... .UEi
7C...::::::::::::,,,,.. .................... vSi.
;1;...,,::::::,......... .................. Yz:
v97,......... .voC.
izAotX7777777777777777777777777777777777777777Y7n92:
.;CoIIIIIUAA666666699999ZZZZZZZZZZZZZZZZZZZZ6ov.
WARNING: System is about to start inserting to elasticsearch in 30 seconds.
if you wish to cancel this process hit CTRL+C now.
', 'red');
echo
$this->colors->getColoredString('
Database Name: ' . DB_NAME . '
Database Host: ' . DB_HOST . '
Database User: ' . DB_USER . '
Database Password: HIDDEN
Database Table: ' . DB_TABLE . '
Elasticsarch Index: ' . ES_INDEX . '
Elasticsearch Host: ' . $ES_HOST[0] . '
', 'yellow');
sleep(30);
echo 'Starting to query...' . PHP_EOL;
for ($x = 0; $x <= $toDoQueries; $x++) {
$query = 'SELECT * FROM ' . DB_TABLE . ' LIMIT ' . Q_LIMIT . ' OFFSET ' . $this->offset . PHP_EOL;
$this->execute($query);
$this->colors->getColoredString('Finished ' . $this->offset, 'green' . PHP_EOL);
$this->currentStatus($this->offset);
$this->offset = $this->offset + Q_LIMIT;
}
}
public function resume() {
echo $this->colors->getColoredString('
Error: the resume.lock file exists in the directory. this means that
this script has been stopped unexpectedly last time it was running.
if you wish to continue from the last batch please supply "resume"
switch to the script, else remove the resume.lock file to start over inserting the data.
', 'red');
die(PHP_EOL);
}
}
// Coloring the CLI output for Sake of prettiness.
class Colors {
private $foreground_colors = array();
private $background_colors = array();
public function __construct() {
// Set up shell colors
$this->foreground_colors['black'] = '0;30';
$this->foreground_colors['dark_gray'] = '1;30';
$this->foreground_colors['blue'] = '0;34';
$this->foreground_colors['light_blue'] = '1;34';
$this->foreground_colors['green'] = '0;32';
$this->foreground_colors['light_green'] = '1;32';
$this->foreground_colors['cyan'] = '0;36';
$this->foreground_colors['light_cyan'] = '1;36';
$this->foreground_colors['red'] = '0;31';
$this->foreground_colors['light_red'] = '1;31';
$this->foreground_colors['purple'] = '0;35';
$this->foreground_colors['light_purple'] = '1;35';
$this->foreground_colors['brown'] = '0;33';
$this->foreground_colors['yellow'] = '1;33';
$this->foreground_colors['light_gray'] = '0;37';
$this->foreground_colors['white'] = '1;37';
$this->background_colors['black'] = '40';
$this->background_colors['red'] = '41';
$this->background_colors['green'] = '42';
$this->background_colors['yellow'] = '43';
$this->background_colors['blue'] = '44';
$this->background_colors['magenta'] = '45';
$this->background_colors['cyan'] = '46';
$this->background_colors['light_gray'] = '47';
}
// Returns colored string
public function getColoredString($string, $foreground_color = null, $background_color = null) {
$colored_string = "";
// Check if given foreground color found
if (isset($this->foreground_colors[$foreground_color])) {
$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
}
// Check if given background color found
if (isset($this->background_colors[$background_color])) {
$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
// Returns all foreground color names
public function getForegroundColors() {
return array_keys($this->foreground_colors);
}
// Returns all background color names
public function getBackgroundColors() {
return array_keys($this->background_colors);
}
}
?>