57fec67b59b69102b94ea90a0af2154bcde3ec4b
[metaproxy-moved-to-github.git] / etc / 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
13 print("DONE\n");
14 exit(0);
15
16
17 class Command {
18   private $options;
19   private $command;
20   private $short_format_config = 'hc:s:t:';
21   private $syntax_config = array('ccl', 'cql', 'pqf');
22
23
24   public function __construct() {
25     $args = Console_Getopt::readPHPArgv();
26
27     if ( PEAR::isError($args) ) {
28       fwrite(STDERR,$args->getMessage()."\n");
29       exit(1);
30     }
31     
32     // Compatibility between "php script.php" and "./script.php"
33     if ( realpath($_SERVER['argv'][0]) == __FILE__ ) {
34       $this->options 
35         = Console_Getopt::getOpt($args, $this->short_format_config);
36     } else {
37       $this->options 
38         = Console_Getopt::getOpt2($args, $this->short_format_config);
39     }
40     
41     // Check for invalid options
42     if ( PEAR::isError($this->options) ) {
43       fwrite(STDERR, $this->options->getMessage()."\n");
44       $this->help();
45     }
46     
47     $this->command = array();
48     
49     // Loop through the user provided options
50     foreach ( $this->options[0] as $option ) {
51       switch ( $option[0] ) {
52       case 'h':
53         help();
54         break;
55       case 's':
56         $this->command['syntax'] = $option[1];
57         break;
58       case 't':
59         $this->command['transform'] = $option[1];
60         break;
61      case 'c':
62        $this->command['config'] = $option[1];
63        break;
64       }
65     }
66     
67     // Loop through the user provided options
68     foreach ($this->options[1] as $argument ) {
69       $this->command['query'] .= ' ' . $argument;
70     }
71   }
72
73   
74   public function help() {
75     fwrite(STDERR, "  Usage:\n");
76     fwrite(STDERR, "  ./experiment-query-config-translate.php -s syntax -t transform -c config.xml query\n");
77     fwrite(STDERR, "  Experiment with general query configuration syntax and transformations.\n");
78     fwrite(STDERR, "  -c config.xml XML config file\n");
79     fwrite(STDERR, "  -s syntax     Syntax of source query language, 'ccl', 'cql', 'pqf'\n");
80     fwrite(STDERR, "  -t transform  Syntax of transformed query language, 'ccl', 'cql', 'pqf'\n");
81     fwrite(STDERR, "  -h            Display help\n");
82     fwrite(STDERR, "  query         Valid query in specified syntax\n");  
83     exit(0);
84   }
85
86   public function command() {
87     return $this->command;
88   }
89   
90   public function syntax() {
91     return $this->command['syntax'];
92   }
93   
94   public function transform() {
95     return $this->command['transform'];
96   }
97   
98   public function config() {
99     return $this->command['config'];
100   }
101
102   public function query() {
103     return $this->commamd['query'];
104   }
105   
106 }
107
108 class Config {
109   private $xml_conf;
110
111   public function load($xml_file){
112     $this->xml_conf = @simplexml_load_file($xml_file) 
113       or die("Unable to load XML config file '" . $xml_file ."'\n"); 
114     $this->xml_conf->registerXPathNamespace('iq', 
115                                             'http://indexdata.com/query');
116   }
117
118   public function parse(){
119     //foreach ($this->xml_conf->xpath('//desc') as $desc) {
120     //echo "$desc\n";
121
122     $namespaces =  $this->xml_conf->getNamespaces(true);
123     foreach ($namespaces as $ns){
124       print("namespace '" . $ns . "'\n");
125     } 
126
127
128     foreach ($this->xml_conf->xpath('//iq:syntax') as $syntax){
129       print("syntax '" . $syntax['name']  . "'\n");
130     }
131    
132     
133     
134   }
135   
136 }
137