-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.php
74 lines (61 loc) · 1.83 KB
/
process.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
<?php
include 'Export.class.php';
#Include PHP-Elasticsearch sdk's 'autoload.php' here
require '../dependency/elasticsearch-sdk/vendor/autoload.php';
$op = array("host:", "index:", "fields:", "csvfile:", "type:", "size:", "query:", "stm:", "logfile:", "async:", "help::");
$option = getopt(null,$op);
$requiredFields = array("host","index","fields","csvfile");
if(array_key_exists('help',$option)){
echo file_get_contents('help.txt');
exit;
}
#Validating manadotary fields
foreach($requiredFields as $key => $value){
if(array_key_exists($value,$option)){
if(empty($option[$value])){
echo "Please provide argument --".$value."\n";exit;
}
}else{
echo "Please provide argument --".$value."\n";exit;
}
}
$option['fields'] = '_id,'.$option['fields'];
$e = new Export;
foreach($option as $key => $value){
$e->$key = $value;
}
$logfile = null;
if(!empty($option['logfile'])){
$logfile = fopen($option['logfile'],"w");
$e->logfile = $option['logfile'];
if(!$logfile){
echo "Check with your logfile."; exit;
}
}
$csvfile = fopen($option['csvfile'],"w");
if($csvfile){
$e->csvFileObject = $csvfile;
fputcsv($csvfile,explode(',',$option['fields']));
}else{
echo "Check with your csvfile";
}
$e->fields = preg_replace('/\s+/', '', $option['fields']);;
$e->connect();
$e->queryParam();
if(array_key_exists('async', $option)){
if(!empty($option['async'])){
$max = $option['async'];
for($i = 0; $i < $max; $i++){
$pid = pcntl_fork();
if ($pid == -1) {
exit("Error forking...\n");
}
else if ($pid == 0) {
$e->fetchDataWriteCSV($i, $max);
exit();
}
}
}else{ echo "Kindly provide --async argument."; }
}else{
$e->fetchDataWriteCSV();
}