HCE Project PHP language client API bindings  1.5.1
Hierarchical Cluster Engine PHP Client Interface API
 All Classes Namespaces Files Functions Variables Pages
kvdb_import.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
15  @ini_set('max_execution_time', 120);
16  @ini_set('memory_limit', '1024M');
17 
18  @error_reporting(E_ALL);
19  @ini_set('display_errors', 1);
20 
21  require_once 'acn_cli_api.inc.php';
22  require_once 'acn_kvdb_api.inc.php';
23 
24  //Check cli runtime
25  if(php_sapi_name()!=='cli' || !defined('STDIN')){
26  echo 'Only cli execution mode supported'.PHP_EOL;
27  exit(1);
28  }
29 
30  //Parse cli arguments
32 
33  //Check mandatory arguments
34  if(isset($args['h']) || isset($args['help']) || !isset($args[ACN_KVDB_CA_URL], $args['document'])){
35  echo 'Usage: '.$argv[0].' --url=kvdb_manager_url> --document=<document_file> --format=<document_format{0-xml,1-json}> '.
36  '[--t=<http_requet_timeout_sec>] [--l=<log_mode{0-Off,1-On}>]'.PHP_EOL;
37 
38  exit(1);
39  }
40 
41  //Set default values
42  if(!isset($args[ACN_KVDB_CA_TIMEOUT]) || empty($args[ACN_KVDB_CA_TIMEOUT])){
43  $args[ACN_KVDB_CA_TIMEOUT]=ACN_KVDB_TIMEOUT;
44  }
45  if(!isset($args['l']) || $args['l']===''){
46  $args['l']=1;
47  }
48  if(!isset($args['format']) || $args['format']===''){
49  $args['format']=0;
50  }
51 
52  //Prepare request array
54  if(!isset($args['document'])){
55  $aborted='--document=<document_file> ';
56  }else{
57  if(!file_exists($args['document'])){
58  $aborted.='file '.$args['document'].' ';
59  }else{
60  //Get documents array
61  if($args['format']==ACN_KVDB_IMPORT_TYPE_XML){
62  $documents=ACN_KVDB_import_from_xml_file($args['document']);
63  }else{
64  $documents=ACN_KVDB_import_from_json_file($args['document']);
65  }
66  //Prepare documents array to add it to the request array
67  foreach($documents as $id=>$document){
68  if(JSON_USE_BASE64){
69  $documents[$id]=base64_encode(json_encode($document));
70  }else{
71  $documents[$id]=json_encode($document);
72  }
73  }
74 //var_dump($documents);
75  $request_array=ACN_KVDB_prepare_request_array(ACN_KVDB_CMD_PUT, $documents);
76  }
77  }
78 
79  //Execute request
80  if($aborted==''){
81  $r=ACN_KVDB_request($request_array, array(ACN_KVDB_CA_URL=>$args[ACN_KVDB_CA_URL], ACN_KVDB_CA_TIMEOUT=>$args[ACN_KVDB_CA_TIMEOUT]));
82  if($r['error']==ACN_KVDB_ERROR_OK){
83  $r=ACN_KVDB_parse_response($r['response']);
84  if($args['l']){
85  var_dump($r); echo PHP_EOL;
86  }
87  if($r[ACN_KVDB_FIELD_ERROR_CODE]==ACN_KVDB_ERROR_OK){
88  echo cli_prettyPrintJson($r[ACN_KVDB_FIELD_DATA], '');
89  }else{
90  if($args['l']){
91  echo 'Response error '.$r[ACN_KVDB_FIELD_ERROR_CODE].' : '.$r[ACN_KVDB_FIELD_ERROR_MSG];
92  }
93  }
94  }else{
95  $r[ACN_KVDB_FIELD_TIME]=0;
96  if($args['l']){
97  echo 'Request error '.$r['error'].' : '.$r['error_msg'].PHP_EOL;
98  }
99  }
100  }else if($aborted!==''){
101  $r[ACN_KVDB_FIELD_TIME]=0;
102  if($args['l']){
103  echo $aborted.' required'.PHP_EOL;
104  }
105  }
106 
107  //Out stat info
108  if($args['l']){
109  echo PHP_EOL.'Finished, time '.$r[ACN_KVDB_FIELD_TIME].PHP_EOL;
110  }
111 
112  exit();
113 
114 ?>