Beispiel #1
0
function mongoQuery($config, $request)
{
    $result = false;
    $mongo_request = array();
    if (!isset($request['mode'])) {
        $request['mode'] = 'SELECT';
    }
    if (!isset($request['limit'])) {
        $request['limit'] = array(0, 25);
    }
    if ($GLOBALS['debug'] == true) {
        print_r($request);
    }
    createLog(prettyJson(json_encode($request)));
    // Execute Mongo query:
    if (isset($request)) {
        $result = mongoExecute($config, $request);
        //			print_r($mongo_result);
    } else {
        $result = array('query' => null, 'result' => array('records' => array(0 => null), 'response' => array(0 => array('error' => 'ERROR - incorrect array supplied'))));
    }
    // Print query output to command line:
    if ($GLOBALS['debug']) {
        echo chr(10) . 'Mongo DB============================================================================================' . chr(10) . $result['query'] . chr(10) . chr(10);
        print_r($result['result']);
        echo chr(10) . chr(10);
        echo '====================================================================================================' . chr(10);
    }
    return $result;
}
Beispiel #2
0
                    $val = empty($data[$c]) ? null : $data[$c];
                    // push data in first line to array
                    array_push($result, $val);
                }
            }
            if ($row > 1) {
                for ($d = 0; $d < $num; $d++) {
                    $val = empty($data[$d]) ? null : $data[$d];
                    // push data in first line to array
                    array_push($lastResult, $val);
                }
            }
            $row++;
        }
        fclose($handle);
        $lastResult = array_chunk($lastResult, (int) $num);
        $hasil = [];
        for ($i = 0; $i < count($lastResult); $i++) {
            // combine key and value
            $arr = array_combine($result, $lastResult[$i]);
            // add created_at
            $create = ['created_at' => "date('Y-m-d')"];
            // result is array
            array_push($hasil, $arr + $create);
        }
        // replace character string
        $json = json_encode($hasil);
        // return with beauty json format
        $hasilAkhir = prettyJson($json, $tableName);
    }
}
Beispiel #3
0
function resetRoutes($app_paths, $routes, $section)
{
    unset($routes[$section]);
    $file = fopen($app_paths['routes'], "w");
    if (fwrite($file, prettyJson(json_encode($routes)))) {
        $report = 'routes.json: Section removed';
    } else {
        $report = false;
    }
    fclose($file);
    return array('result' => array('response' => $report));
}
Beispiel #4
0
function esExecute($mode, $method, $url, $query)
{
    $result = array('records' => array());
    $time = 0;
    $headers = array('Accept: application/json', 'Content-Type: application/json');
    $ch = curl_init();
    //		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    //		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // return response as string
    foreach ($query as $num => $query_string) {
        //			print_r($query_string);
        switch ($method) {
            case 'GET':
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
                break;
            case 'POST':
                try {
                    $json = json_encode($query_string);
                    $echo_query[$num] = preg_replace('@\\t@', '', prettyJson($json));
                    //						echo($echo_query[$num]);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
                } catch (ElasticsearchParseException $e) {
                }
                break;
            case 'DELETE':
                $echo_query[$num] = $url . '/' . $query_string['_id'];
                curl_setopt($ch, CURLOPT_URL, $url . '/' . $query_string['_id']);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
            case 'DROP':
                $echo_query[$num] = $url;
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
        }
        $es_results = json_decode(curl_exec($ch), true);
        //			print_r($es_results);
        if (!isset($es_results['error'])) {
            switch ($mode) {
                case 'SELECT':
                case 'SELECT COUNT':
                    //						echo($es_results['hits']['total']);
                    foreach ($es_results['hits']['hits'] as $row) {
                        $result['records'][$row['_id']] = $row['_source'];
                        $result['records'][$row['_id']]['_type'] = $row['_type'];
                    }
                    $result['response'] = count($es_results['hits']['hits']) . ' record(s) retrieved';
                    if ($mode == 'SELECT COUNT') {
                        $result['record_count'] = $es_results['hits']['total'];
                    }
                    $time = $es_results['took'];
                    break;
                case 'CREATE':
                    //						$echo_query = '';
                    $result['records'][$num]['_id'] = $es_results['_id'];
                    $result['response'][$num] = $es_results;
                    break;
                case 'DELETE':
                    $echo_query = '';
                    $result['records'][$num]['_id'] = $query_string['_id'];
                    $result['response'][$num] = $es_results;
                    break;
                case 'DROP':
                    $echo_query = '';
                    $result['response'][$num] = $es_results;
                    break;
            }
        } else {
            $result['response'][$num] = $es_results;
            $result['record_count'] = 0;
            //			die();
        }
    }
    return array('query' => $echo_query, 'result' => $result, 'execcution_time' => $time);
    curl_close($ch);
}
                    $val = empty($data[$c]) ? null : $data[$c];
                    // push data in first line to array
                    array_push($result, $val);
                }
            }
            if ($row > 1) {
                for ($d = 0; $d < $num; $d++) {
                    $val = empty($data[$d]) ? null : $data[$d];
                    // push data in first line to array
                    array_push($lastResult, $val);
                }
            }
            $row++;
        }
        fclose($handle);
        $lastResult = array_chunk($lastResult, (int) $num);
        $hasil = [];
        for ($i = 0; $i < count($lastResult); $i++) {
            // combine key and value
            $arr = array_combine($result, $lastResult[$i]);
            // add created_at
            $create = ['created_at' => "date('Y-m-d')"];
            // result is array
            array_push($hasil, $arr + $create);
        }
        // replace character string
        $json = json_encode($hasil);
        // return with beauty json format
        echo prettyJson($json, $tableName);
    }
}