/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to classes/request.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
$DEFAULT_PROPS = array (
 
4
        "db_mask" => 0,
 
5
        "experiment" => "0-0",
 
6
        "window" => "0"
 
7
);
 
8
 
 
9
 
 
10
class REQUESTList implements Iterator {
 
11
 var $props;
 
12
 var $list;
 
13
 var $cl;
 
14
 
 
15
 var $cur;
 
16
 
 
17
 function __construct(&$props, &$list, $cl = NULL) {
 
18
    $this->props = &$props;
 
19
    $this->list = &$list;
 
20
 
 
21
    $this->cur = false;
 
22
    
 
23
    if ($cl) $this->cl = &$cl;
 
24
    else $this->cl = "REQUEST";
 
25
 }
 
26
  
 
27
 function CreateDataRequest() {
 
28
    return new DATARequest($this->props);
 
29
 }
 
30
 
 
31
 function rewind() {
 
32
    reset($this->list);
 
33
    $this->cur = current($this->list);
 
34
 }
 
35
 
 
36
 function current() {
 
37
    $props = $this->props;
 
38
    foreach ($this->cur as $key => $value) {
 
39
        $props[$key] = $value;
 
40
    }
 
41
    return new $this->cl($props);
 
42
 }
 
43
 
 
44
 function key() {
 
45
    return key($this->list);
 
46
 }
 
47
 
 
48
 function next() {
 
49
    $this->cur = next($this->list);
 
50
 }
 
51
 
 
52
 function valid() {
 
53
    return $this->cur?true:false;
 
54
 }
 
55
}
 
56
 
 
57
class BASICRequest {
 
58
 var $props;
 
59
 
 
60
 function __construct(&$props = NULL) {
 
61
/*    $f = fopen("/tmp/xxx.props", "a+");
 
62
    fwrite($f, print_r($_GET, true));
 
63
    fwrite($f, print_r($_POST, true));
 
64
    fclose($f);*/
 
65
    
 
66
    if ($props) $this->props = $props;
 
67
    else {
 
68
        if (isset($_GET["db_server"])) $this->props = $_GET;
 
69
        else if (isset($_POST["db_server"])) $this->props = $_POST;
 
70
        else if (isset($_POST["props"])) return $this->props = json_decode(stripslashes($_POST['props']), true);
 
71
        else $this->props = array();
 
72
    }
 
73
 
 
74
/*              
 
75
    foreach ($DEFAULT_PROPS as $prop => $value) {
 
76
        if (!isset($this->props[$prop]))
 
77
            $this->props[$prop] = $value;
 
78
    }   
 
79
*/
 
80
 }
 
81
 
 
82
}
 
83
 
 
84
class REQUEST extends BASICRequest {
 
85
 var $opts = false;
 
86
 var $opts_custom = false;
 
87
    
 
88
 const LIST_ALL = 1;
 
89
 const LIST_WILDCARDED = 2;
 
90
 
 
91
 const READER_FORBID_CACHEREADER = 1;
 
92
 
 
93
 function __construct(&$props = NULL) {
 
94
    global $DEFAULT_PROPS;
 
95
 
 
96
    parent::__construct($props);
 
97
 }
 
98
 
 
99
 function GetServerConfig() {
 
100
    global $READER_DB;
 
101
    
 
102
    if (isset($this->props['db_server'])) {
 
103
        $srvid = $this->props['db_server'];
 
104
        if (isset($READER_DB[$srvid]))
 
105
            return $READER_DB[$srvid];
 
106
        else
 
107
            throw new ADEIException(translate("Invalid server name is specified: \"%s\"", $srvid));    
 
108
    } else
 
109
        throw new ADEIException(translate("The data source server is not specified"));
 
110
 }
 
111
 
 
112
 function GetSourceList($flags = 0) {
 
113
    global $READER_DB;
 
114
    
 
115
    if ((($flags&REQUEST::LIST_ALL)==0)&&(isset($this->props['db_server']))) {
 
116
        $srvid = $this->props['db_server'];
 
117
        if (isset($READER_DB[$srvid])) {
 
118
            $srvlist = array();
 
119
            $srvlist[$srvid] = &$READER_DB[$srvid];
 
120
        } else throw new ADEIException(translate("Invalid server name is specified: \"%s\"", $srvid));
 
121
    } else {
 
122
        $srvlist = &$READER_DB;
 
123
    }
 
124
    
 
125
    $list = array();
 
126
    foreach ($srvlist as $srvid => &$srv) {
 
127
        if ((($flags&REQUEST::LIST_ALL)==0)&&(isset($this->props['db_name']))) {
 
128
            $db = $this->props['db_name'];
 
129
            $list[$srvid . "__" . $db] = array(
 
130
                'db_server' => $srvid,
 
131
                'db_name' => $db
 
132
            );
 
133
        } else {
 
134
            if ($flags&REQUEST::LIST_WILDCARDED) {
 
135
                if ($reader) $reader = &$rdr;
 
136
                else $reader = $this->CreateReader();
 
137
                $list = $reader->GetDatabaseList();
 
138
                foreach (array_keys($list) as $db) {
 
139
                    $list[$srvid . "__" . $db] = array(
 
140
                        'db_server' => $srvid,
 
141
                        'db_name' => $db
 
142
                    );
 
143
                }
 
144
                unset($reader);
 
145
            } else {
 
146
                foreach ($srv['database'] as $db) {
 
147
                    $list[$srvid . "__" . $db] = array(
 
148
                        'db_server' => $srvid,
 
149
                        'db_name' => $db
 
150
                    );
 
151
                }
 
152
            }
 
153
        }
 
154
    }
 
155
    
 
156
    return new REQUESTList($this->props, $list, "SOURCERequest");
 
157
 }
 
158
 
 
159
 function GetOptions() {
 
160
    if ($this->opts) return $this->opts;
 
161
    else {
 
162
        $this->opts = new OPTIONS($this);
 
163
        return $this->opts;
 
164
    }
 
165
 } 
 
166
 
 
167
 function GetGroupOptions(LOGGROUP &$grp = NULL) {
 
168
    if (($grp)&&($grp->gid != $this->props['db_group'])) {
 
169
        return GetCustomOptions($this->props['db_server'], $this->props['db_name'], $grp->gid);
 
170
    }
 
171
    
 
172
    if (!$this->opts) {
 
173
        $this->opts = new OPTIONS($this);
 
174
    }
 
175
    
 
176
    return $this->opts;
 
177
 }
 
178
 
 
179
 function GetCustomOptions($server = false, $db = false, $group = false) {
 
180
    $id .= $server . "__" . $db . "__" . $group;
 
181
    
 
182
    if (!$this->opts_custom) $this->opts_custom = array();
 
183
    else if (isset($this->opts_custom[$id])) return $this->opts_custom[$id];
 
184
 
 
185
        
 
186
    $req = array();
 
187
    if ($server !== false) $req["db_server"] = $server;
 
188
    if ($db !== false) $req["db_name"] = $db;
 
189
    if ($group !== false) $req["db_group"] = $group;
 
190
    
 
191
    $this->opts_custom[$id] = new OPTIONS($req);
 
192
    return $this->opts_custom[$id];
 
193
 }
 
194
 
 
195
 function GetOption($prop) {
 
196
    $opts = $this->GetOptions();
 
197
    return $opts->Get($prop);
 
198
 }
 
199
 
 
200
 function GetGroupOption($prop, LOGGROUP &$grp = NULL) {
 
201
    $opts = $this->GetGroupOptions($grp);
 
202
    return $opts->Get($prop);
 
203
 }
 
204
 
 
205
 function GetCustomOption($prop, $server = false, $db = false, $group = false) {
 
206
    $opts = $this->GetCustomOptions($server, $db, $group);
 
207
    return $opts->Get($prop);
 
208
 }
 
209
 
 
210
 function GetQueryString(array &$ext = NULL) {
 
211
    $first = 1;
 
212
    $query = "";
 
213
    
 
214
    foreach (($ext?array_merge($this->props,$ext):$this->props) as $name => $value) {
 
215
        if ($first) $first = 0;
 
216
        else $query .= "&";
 
217
        
 
218
        $query .= $name . "=" . urlencode($value);
 
219
    }
 
220
    
 
221
    return $query;
 
222
 }
 
223
 
 
224
 function CheckServer() {
 
225
    if (!isset($this->props['db_server'])) return false;
 
226
    return true;
 
227
 }
 
228
 function CheckSource() {
 
229
    if (!$this->CheckServer()) return false;
 
230
    if (!isset($this->props['db_name'])) return false;
 
231
    return true;
 
232
 }
 
233
 function CheckGroup() {
 
234
    if (!$this->CheckSource()) return false;
 
235
    if (!isset($this->props['db_group'])) return false;
 
236
    return true;
 
237
 }
 
238
 function CheckData() {
 
239
    if (!$this->CheckGroup()) return false;
 
240
//    if (!isset($this->props['db_mask'])) return false;
 
241
    return true;
 
242
 }
 
243
 
 
244
 function CreateServerRequest() {
 
245
    return new SERVERRequest($this->props);
 
246
 }
 
247
 function CreateSourceRequest() {
 
248
    return new SOURCERequest($this->props);
 
249
 }
 
250
 function CreateGroupRequest() {
 
251
    return new GROUPRequest($this->props);
 
252
 }
 
253
 function CreateDataRequest() {
 
254
    return new DATARequest($this->props);
 
255
 }
 
256
}
 
257
 
 
258
class SERVERRequest extends REQUEST {
 
259
 var $srv;
 
260
 
 
261
 function __construct(&$props = NULL) {
 
262
    global $READER_DB;
 
263
 
 
264
    parent::__construct($props);
 
265
    
 
266
    if (isset($this->props["db_server"]))
 
267
        $srvid = $this->props["db_server"];
 
268
    else
 
269
        throw new ADEIException(translate("The data source server should be specified"));
 
270
 
 
271
    if (is_array($READER_DB[$srvid]))
 
272
        $this->srv = &$READER_DB[$srvid];
 
273
    else
 
274
        throw new ADEIException(translate("Invalid server identificator is supplied: \"%s\"", $srvid));
 
275
 }
 
276
 
 
277
 function GetServerInfo() {
 
278
    $server = $this->srv;
 
279
    if (isset($this->props['db_name'])) $server['database'] = $this->props['db_name'];
 
280
    else unset($server['database']);
 
281
    return $server;
 
282
 }
 
283
 
 
284
 function CreateReader($flags = 0) {
 
285
    if ($this->srv['disconnected']) {
 
286
        if ($flags&REQUEST::READER_FORBID_CACHEREADER)
 
287
            throw new ADEIException(translate("The data reader(%s) is disconnected", $this->srv['reader']));
 
288
 
 
289
        return $this->CreateCacheReader();
 
290
    } else if (($opts = $this->GetOptions())&&($opts->Get('use_cache_reader'))&&(!($flags&REQUEST::READER_FORBID_CACHEREADER))) {
 
291
        return $this->CreateCacheReader();
 
292
    } else {
 
293
        $reader = $this->srv['reader'];
 
294
    
 
295
        if (!include_once("readers/" . strtolower($reader) . '.php')) {
 
296
            if ($this->srv['reader'])
 
297
                throw new ADEIException(translate("Unsupported data reader is configured: \"%s\"", $this->srv['reader']));
 
298
            else
 
299
                throw new ADEIException(translate("The data reader is not configured"));
 
300
        }
 
301
    }
 
302
 
 
303
    try {
 
304
        $rdr = new $reader($this);
 
305
    } catch (ADEIException $ae) {
 
306
        if ($flags&REQUEST::READER_FORBID_CACHEREADER) throw $ae;
 
307
 
 
308
        if ($opts->Get('overcome_reader_faults')) {
 
309
            $rdr = $this->CreateCacheReader();
 
310
        } else throw $ae;    
 
311
    }
 
312
    
 
313
    return $rdr;
 
314
 }
 
315
 
 
316
 function CreateCacheReader(CACHEDB &$cache = NULL) {
 
317
    return new CACHEReader($this, $cache);
 
318
 }
 
319
 
 
320
 function GetLocationString() {
 
321
    return "Server: \"" . $this->props['db_server'] . "\"";
 
322
 }
 
323
}
 
324
 
 
325
 
 
326
class SOURCERequest extends SERVERRequest {
 
327
 function __construct(&$props = NULL) {
 
328
    parent::__construct($props);
 
329
 
 
330
    if (!isset($this->props["db_name"]))
 
331
        throw new ADEIException(translate("The database should be specified"));
 
332
 }
 
333
 
 
334
 function GetGroupList(READER &$rdr = NULL, $flags = 0) {
 
335
    $list = array();
 
336
    
 
337
    if (isset($this->props['db_group'])) {
 
338
        $gid = $this->props['db_group'];
 
339
        $list[$gid] = array(
 
340
            'db_group' => $gid,
 
341
        );
 
342
    } else {
 
343
        if ($rdr) $reader = &$rdr;
 
344
        else $reader = $this->CreateReader();
 
345
        $list = $reader->GetGroupList();
 
346
        foreach (array_keys($list) as $gid) {
 
347
            $list[$gid] = array(
 
348
                'db_group' => $gid,
 
349
            );
 
350
        }
 
351
    }
 
352
    return new REQUESTList($this->props, $list, "GROUPRequest");
 
353
 }
 
354
 function GetLocationString() {
 
355
    return "Server: \"" . $this->props['db_server'] . "\" Database: \"" . $this->props['db_name'] . "\"";
 
356
 }
 
357
}
 
358
 
 
359
class GROUPRequest extends SOURCERequest {
 
360
 function __construct(&$props = NULL) {
 
361
    parent::__construct($props);
 
362
 
 
363
    if (!isset($this->props["db_group"]))
 
364
        throw new ADEIException(translate("The Logging Group should be specified"));
 
365
 }
 
366
 
 
367
 function GetGroupInfo() {
 
368
    return $this->props;
 
369
 }
 
370
 
 
371
 function GetMaskInfo() {
 
372
    return $this->props;
 
373
 }
 
374
 
 
375
 function CreateGroup(READER &$rdr) {
 
376
    $ginfo = $this->GetGroupInfo();
 
377
    return $rdr->CreateGroup($ginfo);
 
378
 }
 
379
 
 
380
 function CreateCache(READER &$rdr = NULL) {
 
381
    return new CACHE($this, $rdr);
 
382
 }
 
383
 
 
384
 function CreateCacheUpdater(READER &$rdr = NULL) {
 
385
    return new CACHE($this, $rdr, CACHE::CREATE_UPDATER);
 
386
 }
 
387
 
 
388
 function GetLocationString() {
 
389
    return "Server: \"" . $this->props['db_server'] . "\" Database: " . $this->props['db_name'] . "\" Group: \"" . $this->props['db_group'] . "\"";
 
390
 }
 
391
}
 
392
 
 
393
class DATARequest extends GROUPRequest {
 
394
 function __construct(&$props = NULL) {
 
395
    parent::__construct($props);
 
396
 }
 
397
 
 
398
 function GetIntervalInfo() {
 
399
    return $this->props;
 
400
 }
 
401
 
 
402
 function CreateInterval(READER &$rdr = NULL, LOGGROUP &$grp = NULL, $flags = 0) {
 
403
    $iinfo = $this->GetIntervalInfo();
 
404
    return new INTERVAL($iinfo, $rdr, $grp, $flags);
 
405
 }
 
406
 
 
407
 function GetFormatInfo() {
 
408
    global $EXPORT_FORMATS;
 
409
    global $EXPORT_DEFAULT_FORMAT;
 
410
    
 
411
    $format = $this->props['format'];
 
412
    if ($format) {
 
413
        if ($EXPORT_FORMATS[$format])
 
414
            return $EXPORT_FORMATS[$format];
 
415
        else
 
416
            throw new ADEIException(translate("Unsupported export fromat (%s) is specified", $format));
 
417
    } else if ($EXPORT_FORMATS[$EXPORT_DEFAULT_FORMAT]) 
 
418
        return $EXPORT_FORMATS[$EXPORT_DEFAULT_FORMAT];
 
419
    else 
 
420
        return array(
 
421
            title => "CSV",
 
422
            extension => "csv"
 
423
        );
 
424
 }
 
425
 
 
426
 function CreateExporter(STREAMHandler &$h = NULL) {
 
427
    if (!$h) $h = new STREAMHandler();
 
428
 
 
429
    $format = $this->GetFormatInfo();
 
430
    switch($format['type']) {
 
431
        case "handler":
 
432
            if ($format['handler']) {
 
433
                if (!include_once("handlers/" . strtolower($format['handler']) . '.php')) {
 
434
                    throw new ADEIException(translate("Unsupported data handler is configured: \"%s\"", $format['handler']));
 
435
                }
 
436
                $handler = $format['handler'] . "Handler";
 
437
            } else $handler = "CSVHandler";
 
438
            
 
439
            $handler = new $handler($h, $format['opts']);
 
440
 
 
441
            return HANDLERExport($this, $handler, $format);
 
442
        break;
 
443
        case "filter":
 
444
            return FILTERExport($this, $h, $format);
 
445
        break;
 
446
        default:
 
447
            throw new ADEIException(translate("Unknown format type (%s)", $format['type']));
 
448
        
 
449
    }
 
450
 
 
451
    return new EXPORT($this, $h, $format);
 
452
 }
 
453
}
 
454
 
 
455
 
 
456
function adeiCreateExporter(STREAMHandler &$h = NULL) {
 
457
    $req = new DATARequest();
 
458
    return $req->CreateExporter($h);
 
459
}
 
460
 
 
461
 
 
462
 
 
463
/*
 
464
 
 
465
 
 
466
 function GetGroupList($flags) {
 
467
 }
 
468
 function GetGroupInfo() {
 
469
 }
 
470
 
 
471
 function GetIntervalInfo() {
 
472
 }
 
473
 
 
474
 function CreateCache() {
 
475
 }
 
476
 
 
477
 
 
478
 
 
479
*/
 
480
?>
 
 
b'\\ No newline at end of file'