Fix Metaproxy stops logging after check config failed MP-590
[metaproxy-moved-to-github.git] / xml / experiments / experiment-query-config-translate.php
1 #!/usr/bin/php
2 <?php
3 # Include PEAR::Console_Getopt
4 require_once 'Console/Getopt.php';
5
6 $command = new Command;
7 print_r($command->command());
8
9 $config = new Config;
10 $config->load($command->config());
11 $config->parse();
12 $config->cql_check_boolean("and");
13 $config->cql_check_boolean("notexist");
14 $config->cql_check_apt("cql", "all", "=");
15 $config->cql_check_apt("cql", "all", "notexist");
16 $config->cql_check_apt("cql", "notexist", "=");
17 $config->cql_check_apt("notexist", "all", "<>");
18
19
20 print("DONE\n");
21 exit(0);
22
23
24 class Command {
25   private $options;
26   private $command;
27   private $short_format_config = 'hc:s:t:';
28   private $syntax_config = array('ccl', 'cql', 'pqf');
29
30
31   public function __construct() {
32     $args = Console_Getopt::readPHPArgv();
33
34     if ( PEAR::isError($args) ) {
35       fwrite(STDERR,$args->getMessage()."\n");
36       exit(1);
37     }
38     
39     // Compatibility between "php script.php" and "./script.php"
40     if ( realpath($_SERVER['argv'][0]) == __FILE__ ) {
41       $this->options 
42         = Console_Getopt::getOpt($args, $this->short_format_config);
43     } else {
44       $this->options 
45         = Console_Getopt::getOpt2($args, $this->short_format_config);
46     }
47     
48     // Check for invalid options
49     if ( PEAR::isError($this->options) ) {
50       fwrite(STDERR, $this->options->getMessage()."\n");
51       $this->help();
52     }
53     
54     $this->command = array();
55     
56     // Loop through the user provided options
57     foreach ( $this->options[0] as $option ) {
58       switch ( $option[0] ) {
59       case 'h':
60         help();
61         break;
62       case 's':
63         $this->command['syntax'] = $option[1];
64         break;
65       case 't':
66         $this->command['transform'] = $option[1];
67         break;
68      case 'c':
69        $this->command['config'] = $option[1];
70        break;
71       }
72     }
73     
74     // Loop through the user provided options
75     foreach ($this->options[1] as $argument ) {
76       $this->command['query'] .= ' ' . $argument;
77     }
78   }
79
80   
81   public function help() {
82     fwrite(STDERR, "  Usage:\n");
83     fwrite(STDERR, "  ./experiment-query-config-translate.php -s syntax -t transform -c config.xml query\n");
84     fwrite(STDERR, "  Experiment with general query configuration syntax and transformations.\n");
85     fwrite(STDERR, "  -c config.xml XML config file\n");
86     fwrite(STDERR, "  -s syntax     Syntax of source query language, 'ccl', 'cql', 'pqf'\n");
87     fwrite(STDERR, "  -t transform  Syntax of transformed query language, 'ccl', 'cql', 'pqf'\n");
88     fwrite(STDERR, "  -h            Display help\n");
89     fwrite(STDERR, "  query         Valid query in specified syntax\n");  
90     exit(0);
91   }
92
93   public function command() {
94     return $this->command;
95   }
96   
97   public function syntax() {
98     return $this->command['syntax'];
99   }
100   
101   public function transform() {
102     return $this->command['transform'];
103   }
104   
105   public function config() {
106     return $this->command['config'];
107   }
108
109   public function query() {
110     return $this->commamd['query'];
111   }
112   
113 }
114
115 class Config {
116   private $xml_conf;
117
118   public function load($xml_file){
119     $this->xml_conf = @simplexml_load_file($xml_file) 
120       or die("Unable to load XML config file '" . $xml_file ."'\n"); 
121     $this->xml_conf->registerXPathNamespace('iq', 
122                                             'http://indexdata.com/query');
123   }
124
125   public function parse(){
126     //foreach ($this->xml_conf->xpath('//desc') as $desc) {
127     //echo "$desc\n";
128
129     $namespaces =  $this->xml_conf->getNamespaces(true);
130     foreach ($namespaces as $ns){
131       print("namespace '" . $ns . "'\n");
132     }
133
134     foreach ($this->xml_conf->xpath('//iq:syntax') as $syntax){
135       print("syntax '" . $syntax['name']  . "'\n");
136     }    
137     
138   }
139   
140   public function cql_check_boolean($boolean){
141     foreach ($this->xml_conf->xpath("//iq:syntax[@name='cql']//iq:boolean") 
142              as $b){
143       if ($b['name'] == $boolean ){
144         print("CQL boolean '" . $boolean . "' exists\n");
145         return;
146       }    
147     }
148     print("CQL boolean '" . $boolean . "' error\n");
149   }
150
151   public function cql_check_apt($set, $index, $relation){
152         print("CQL APT set'" . $set . "' index '" . $index 
153               . "' relation '" . $relation . "' check\n");
154     
155    foreach ($this->xml_conf->xpath("//iq:syntax[@name='cql']//iq:set") 
156              as $s){
157       if ($s['name'] == $set ){
158         print("CQL APT set'" . $set . "' OK\n");
159         foreach ($set->xpath("//iq:index") as $i){
160           
161         print("CQL APT set'" . $set . "' OK index '" . $index . "' ERROR\n");
162         return;
163         }
164         
165         print("CQL APT set'" . $set . "' OK index '" . $index . "' ERROR\n");
166         return;
167       }    
168     }
169     print("CQL APT set'" . $set . "' ERROR\n");
170   }
171   
172 }
173