root/trunk/classes/request.php

Revision trunk,175, 26.9 kB (checked in by Suren A. Chilingaryan <csa@dside.dyndns.org>, 8 months ago)

Minor extension of control service: access to the last stored data and non-verified set

Line 
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
20     $this->list = &$list;
21
22     $this->cur = false;
23     
24     if ($cl) $this->cl = &$cl;
25     else $this->cl = "REQUEST";
26  }
27  
28  function IsEmpty() {
29     if ($this->list) return false;
30     return true;
31  }
32
33     // false - is not known
34  function GetSize() {
35     return sizeof($this->list);
36  }
37  
38  function CreateDataRequest() {
39     return new DATARequest($this->props);
40  }
41  
42  function rewind() {
43     reset($this->list);
44     $this->cur = current($this->list);
45  }
46  
47  function current() {
48     $props = $this->props;
49
50     // To avoid confusing
51     if (isset($this->cur['db_group'])) {
52     unset($props['db_mask']);
53     } elseif (isset($this->cur['db_name'])) {
54     unset($props['db_mask']);
55     unset($props['db_group']);
56     } elseif (isset($this->cur['db_server'])) {
57     unset($props['db_mask']);
58     unset($props['db_group']);
59     unset($props['db_name']);
60     }
61     
62     foreach ($this->cur as $key => $value) {
63     $props[$key] = $value;
64     }
65
66     return new $this->cl($props);
67  }
68  
69  function key() {
70     return key($this->list);
71  }
72  
73  function next() {
74     $this->cur = next($this->list);
75  }
76
77  function valid() {
78     return $this->cur?true:false;
79  }
80 }
81
82 class REQUESTListConstructor implements Iterator {
83  var $list;
84  var $func;
85  
86  function __construct(REQUESTList $list, $func) {
87     $this->list = $list;
88     $this->func = $func;
89  }
90  
91  function rewind() {
92     $this->list->rewind();
93  }
94  
95  function current() {
96     $req = $this->list->current();
97     return call_user_func(array($req, $this->func));
98  }
99  
100  function key() {
101     return $this->list->key();
102  }
103  
104  function next() {
105     $this->list->next();
106  }
107
108  function valid() {
109     return $this->list->valid();
110  }
111 }
112
113
114
115 class BASICRequest {
116  var $props;
117
118  const ENCODING_DEFAULT = 0;
119  const ENCODING_XML = 1;
120  const ENCODING_JSON = 2;
121  const ENCODING_LABVIEW = 3;
122
123  function __construct(&$props = NULL) {
124 /*    $f = fopen("/tmp/xxx.props", "a+");
125     fwrite($f, print_r($_GET, true));
126     fwrite($f, print_r($_POST, true));
127     fclose($f);*/
128     
129     if ($props === NULL) {
130     if (isset($_GET["db_server"])) $this->props = $_GET;
131     else if (isset($_POST["db_server"])) $this->props = $_POST;
132     else if (isset($_POST["props"])) return $this->props = json_decode(stripslashes($_POST['props']), true);
133     else if (isset($_GET["module"])||isset($_GET["setup"])) $this->props = $_GET;
134     else if (isset($_POST["module"])||isset($_POST["setup"])) $this->props = $_POST;
135     else if ($_GET) $this->props = $_GET;
136     else if ($_POST) $this->props = $_POST;
137     else $this->props = array();
138     } else {
139     if (is_array($props)) $this->props = $props;
140     else $this->props = array();
141     }
142
143 /*       
144     foreach ($DEFAULT_PROPS as $prop => $value) {
145     if (!isset($this->props[$prop]))
146         $this->props[$prop] = $value;
147     }   
148 */
149  }
150  
151  static function GetResponseEncoding($default = false) {
152     switch (strtolower($_REQUEST['encoding'])) {
153     case "labview":
154         return BASICRequest::ENCODING_LABVIEW;
155     case "xml":
156         return BASICRequest::ENCODING_XML;
157     case "json":
158         return BASICRequest::ENCODING_JSON;
159     default:
160         if ($default === false)
161             return BASICRequest::ENCODING_DEFAULT;
162         return $default;
163     }
164  }
165
166  static function GetGlobalOption($opt, $default = false) {
167 //    $opt = strtoupper($opt);
168     if (isset($GLOBALS[$opt])) return $GLOBALS[$opt];
169     return $default;
170  }
171  
172  function GetProp($prop, $default = false) {
173     if (isset($this->props[$prop])) return $this->props[$prop];
174     return $default;
175  }
176  
177  function SetProp($prop, $value = false) {
178     $this->props[$prop] = $value;
179  }
180  
181  function ParseProps($props) {
182     if ($props) {
183       if (!is_array($props)) {
184     $props = preg_split("/(&amp;|\s|&)/", $props);
185     $override = array();
186     foreach ($props as $prop) {
187         if (preg_match("/^([^=]+)=(.*)$/", $prop, $m)) {
188             $override[$m[1]] = $m[2];
189         }
190     }
191     return $override;
192       } else {
193     return $props;
194       }
195     }
196     
197     return array();
198  }
199
200  function CombineProps($props, $override = NULL, $override2 = NULL) {
201     if ($override) {
202     $override = $this->ParseProps($override);
203     $override2 = $this->ParseProps($override2);
204
205     return array_merge($props, $override, $override2);
206     }
207     return $props;
208  }
209
210  function GenerateQueryString($props) {
211     global $ADEI_SETUP;
212     
213     $props['setup'] = $ADEI_SETUP;
214     $first = 1;
215     $query = "";
216
217     foreach ($props as $name => $value) {
218     if ($first) $first = 0;
219     else $query .= "&";
220     
221     $query .= $name . "=" . urlencode($value);
222     }
223     
224     return $query;
225  }
226  
227  function GetFilteredProps(array $filter, $exclude = false) {
228     if ($exclude) {
229     $props = $this->props;
230     foreach ($filter as $i) {
231         unset($props[$i]);
232     }
233     } else {
234     $props = array();
235     foreach ($filter as $i) {
236         if (isset($this->props[$i])) {
237         $props[$i] = $this->props[$i];
238         }
239     }
240     }
241
242     return $props;
243  }
244
245  
246  function GetProps($override = NULL, $override2 = NULL) {
247     return $this->CombineProps($this->props, $override, $override2);
248  }
249  
250  function GetGroupProps($override = NULL, $override2 = NULL) {
251     return $this->CombineProps(
252     $this->GetFilteredProps(array("db_server", "db_name", "db_group")),
253     $override, $override2
254     );
255  }
256  
257  function GetQueryString($ext = NULL, $ext2 = NULL) {
258     $props = $this->GetProps($ext, $ext2);
259     return $this->GenerateQueryString($props);
260  }
261
262  function GetGroupQueryString($ext = NULL, $ext2 = NULL) {
263     $props = $this->GetGroupProps($ext, $ext2);
264     return $this->GenerateQueryString($props);
265  }
266  
267  function GetTimeFormat() {
268     $format = $this->GetProp("time_format", "unix");
269     switch ($format) {
270     case "unix":
271         return false;
272     break;
273     case "iso":
274         return "c";
275     break;
276     case "text":
277         return "d M Y, H:i:s";
278     break;
279     }
280     return $format;
281  }
282  
283  function CreateImageHelper() {
284     global $ADEI;
285     $ADEI->RequireClass("welcome", true);
286     return new WELCOME($this);
287  }
288  
289  function CreateTextPlotter() {
290     global $ADEI;
291     $ADEI->RequireClass("drawtext");
292     return new DRAWText($this);
293  }
294  
295  function CreateSearcher() {
296     global $ADEI;
297     $ADEI->RequireClass("search");
298     return new SEARCH($this);
299  }
300  
301  function CreateServerRequest() {
302     return new SERVERRequest();
303  }
304  
305  function CreateSourceRequest() {
306     return new SOURCERequest();
307  }
308  
309  function CreateGroupRequest() {
310     return new GROUPRequest();
311  }
312
313  function CreateControlGroupRequest() {
314     return new CONTROLGroupRequest();
315  }
316  
317  function CreateDataRequest() {
318     return new DATARequest();
319  }
320
321  function CreateResponse(array &$result = NULL, $error = false) {
322     $encoding = $this->GetResponseEncoding(REQUEST::ENCODING_XML);
323     
324     switch ($encoding) {
325      case REQUEST::ENCODING_JSON:
326     if ($error) {
327         echo json_encode(array("error" => $error));
328     } else {
329         echo json_encode(array_merge(array("error" => 0), $result));
330     }
331      break;
332      default:
333         $xslt = $this->GetProp('xslt');
334     $time_format = $this->GetTimeFormat();
335     return ADEI::EncodeToStandardXML($error, $result, $xslt, array(
336         'time_format' => $time_format
337     ));
338     }
339     
340     return 0;
341  }
342 }
343
344 class REQUEST extends BASICRequest {
345  var $opts = false;
346  var $opts_custom = false;
347  
348  const LIST_ALL = 0x0001;
349  const LIST_WILDCARDED = 0x0002;
350  const NEED_INFO = 0x0004;
351  const NEED_COUNT = 0x0008;
352  const NEED_ITEMINFO = 0x0010;
353  const NEED_AXISINFO = 0x0020;
354  const ONLY_AXISINFO = 0x0040;
355  const LIST_VIRTUAL = 0x0100;
356  const LIST_COMPLEX = 0x0200;
357  const SKIP_UNCACHED = 0x0400;
358  const SKIP_CHECKS = 0x0800;
359  
360  const CONTROL = 0x1000;
361  
362  const FLAG_MASK = 0xFFFF;
363
364  const READER_FORBID_CACHEREADER = 1;
365  
366  function __construct(&$props = NULL) {
367     global $DEFAULT_PROPS;
368
369     parent::__construct($props);
370  }
371
372  function GetServerConfig() {
373     global $READER_DB;
374     global $ADEI_VIRTUAL_READERS;
375
376     if (isset($this->props['db_server'])) {
377     $srvid = $this->props['db_server'];
378     if (isset($READER_DB[$srvid]))
379         return $READER_DB[$srvid];
380     elseif (isset($ADEI_VIRTUAL_READERS[$srvid]))
381         return $ADEI_VIRTUAL_READERS[$srvid];
382         throw new ADEIException(translate("Invalid server name is specified: \"%s\"", $srvid));   
383     } else
384     throw new ADEIException(translate("The data source server is not specified"));
385  }
386
387  function GetSources($flags = 0) {
388     global $READER_DB;
389
390     $srvlist = $this->GetServerList($flags);
391     
392     $list = array();
393     foreach ($srvlist as $srvid => &$srv) {
394     if ((($flags&REQUEST::LIST_ALL)==0)&&(isset($this->props['db_name']))) {
395         // Check database existence (DS)
396         $db = $this->props['db_name'];
397         $list[$srvid . "__" . $db] = array(
398         'db_server' => $srvid,
399         'db_name' => $db
400         );
401     } else {
402         try {
403         $req = new SERVERRequest($srv);
404         $dblist = $req->GetDatabaseList($flags);
405 //        $reader = $req->CreateReader();
406 //        $dblist = $reader->GetDatabaseList($flags/*&REQUEST::LIST_WILDCARDED*/);
407
408         foreach (array_keys($dblist) as $db) {
409             $list[$srvid . "__" . $db] = array(
410             'db_server' => $srvid,
411             'db_name' => $db
412             );
413         }
414
415 //        unset($reader);
416         } catch (ADEIException $ae) {
417         // Just skipping source
418 #        throw $ae;
419         }
420     }
421     }
422
423     return new REQUESTList($this->props, $list, "SOURCERequest");
424  }
425  
426  function GetGroups($flags = 0) {
427     $list = array();
428
429     if ((($flags&REQUEST::LIST_ALL)==0)&&(isset($this->props['db_group'])))
430     $filter = $this->props['db_group'];
431     else
432     $filter = false;
433
434     $slist = $this->GetSources($flags);
435     foreach ($slist as $sid => $sreq) {
436
437     $glist = $sreq->GetGroups(NULL, $flags&~REQUEST::LIST_ALL);
438     foreach ($glist as $gid => $greq) {
439         if (($filter)&&($gid != $filter)) continue;
440         
441         $list[$sid . "__" . $gid] = $greq->props;
442     }
443     }
444
445     return new REQUESTList($this->props, $list, "GROUPRequest");
446  }
447  
448  function ComposeGroupName($srvname, $dbname, $grname) {
449     return "$srvname -- $dbname -- $grname";
450  }
451
452  function GetServerList($flags = 0) {
453     global $READER_DB;
454     global $ADEI_VIRTUAL_READERS;
455     
456     if (!is_array($READER_DB))
457     throw new ADEIException(translate("No data sources is configured"));
458     
459     $list = array();
460
461     foreach ($READER_DB as $db_id => &$db) {
462     if ($flags&REQUEST::NEED_INFO) {
463             $list[$db_id] = $db;
464     } else {
465             $list[$db_id] = array();
466     }
467         $list[$db_id]["name"] = $db["title"];
468         $list[$db_id]["db_server_name"] = $db["title"];
469         $list[$db_id]["db_server"] = $db_id;
470     }
471
472     /* Accepting all virtuals for now */
473     if ($flags&REQUEST::SKIP_UNCACHED) {
474     $cache = new CACHEDB();
475     $cached_servers = $cache->ListCachedServers();
476
477         foreach (array_keys($list) as $key) {
478         if (!in_array($list[$key]['db_server'], $cached_servers)) {
479         unset($list[$key]);
480         }
481     }
482     }
483
484     
485     if (($flags&REQUEST::LIST_VIRTUAL)&&(is_array($ADEI_VIRTUAL_READERS))) {
486     foreach ($ADEI_VIRTUAL_READERS as $db_id => &$db) {
487         if ($flags&REQUEST::NEED_INFO) {
488             $list[$db_id] = $db;
489         } else {
490             $list[$db_id] = array();
491         }
492         
493         $list[$db_id]["name"] = $db["title"];
494             $list[$db_id]["db_server_name"] = $db["title"];
495             $list[$db_id]["db_server"] = $db_id;
496         $list[$db_id]["virtual"] = true;
497     }
498     }
499     
500     return $list;
501  }
502  
503  function GetGroupList($flags = 0) {
504     $list = array();
505
506     if ((($flags&REQUEST::LIST_ALL)==0)&&(isset($this->props['db_group'])))
507     $filter = $this->props['db_group'];
508     else
509     $filter = false;
510
511     $slist = $this->GetSources($flags);
512     foreach ($slist as $sid => $sreq) {
513     $sinfo = $sreq->GetSourceInfo();
514     
515     $glist = $sreq->GetGroupList($flags&~REQUEST::LIST_ALL);
516     
517     foreach ($glist as $gid => $ginfo) {
518         if (($filter)&&($gid != $filter)) continue;
519         
520         $list[$sid . "__" . $gid] = array_merge($sinfo, $ginfo, array(
521         'name' => $this->ComposeGroupName(
522             $sinfo['db_server_name'],
523             $sinfo['db_name_name'],
524             $ginfo['db_group_name']
525         )
526         ));
527     }
528     }
529     
530     return $list;   
531  }
532  
533  function GetOptions() {
534     if ($this->opts) return $this->opts;
535     else {
536     $this->opts = new OPTIONS($this);
537     return $this->opts;
538     }
539  }
540
541  function GetGroupOptions(LOGGROUP &$grp = NULL) {
542     if (($grp)&&($grp->gid != $this->props['db_group'])) {
543     return $this->GetCustomOptions($this->props['db_server'], $this->props['db_name'], $grp->gid);
544     }
545     
546     if (!$this->opts) {
547     $this->opts = new OPTIONS($this);
548     }
549     
550     return $this->opts;
551  }
552
553  function GetCustomOptions($server = false, $db = false, $group = false) {
554     $id = $server . "__" . $db . "__" . $group;
555     
556     if (!$this->opts_custom) $this->opts_custom = array();
557     else if (isset($this->opts_custom[$id])) return $this->opts_custom[$id];
558
559     
560     $req = array();
561     if ($server !== false) $req["db_server"] = $server;
562     if ($db !== false) $req["db_name"] = $db;
563     if ($group !== false) $req["db_group"] = $group;
564     
565     $this->opts_custom[$id] = new OPTIONS($req);
566     return $this->opts_custom[$id];
567  }
568
569  function GetOption($prop, $default = NULL) {
570     $opts = $this->GetOptions();
571     return $opts->Get($prop, $default);
572  }
573  
574  function GetGroupOption($prop, LOGGROUP $grp = NULL, $default = NULL) {
575     $opts = $this->GetGroupOptions($grp);
576     return $opts->Get($prop, $default);
577  }
578
579  function GetCustomOption($prop, $server = false, $db = false, $group = false, $default = NULL) {
580     $opts = $this->GetCustomOptions($server, $db, $group);
581     return $opts->Get($prop, $default);
582  }
583  
584  function LimitInterval(INTERVAL $ivl, LOGGROUP $grp = NULL) {
585     $opts = $this->GetGroupOptions($grp);
586     $limits = $opts->GetDateLimit();
587     $ivl->Limit($limits[0], $limits[1]);
588  }
589  
590  
591  function CheckServer() {
592     if (!isset($this->props['db_server'])) return false;
593     return true;
594  }
595  function CheckSource() {
596     if (!$this->CheckServer()) return false;
597     if (!isset($this->props['db_name'])) return false;
598     return true;
599  }
600  function CheckGroup() {
601     if (!$this->CheckSource()) return false;
602     if (!isset($this->props['db_group'])) return false;
603     return true;
604  }
605  function CheckData() {
606     if (!$this->CheckGroup()) return false;
607 //    if (!isset($this->props['db_mask'])) return false;
608     return true;
609  }
610  
611  function CreateServerRequest() {
612     return new SERVERRequest($this->props);
613  }
614  function CreateSourceRequest() {
615     return new SOURCERequest($this->props);
616  }
617  function CreateControlGroupRequest() {
618     return new CONTROLGroupRequest($this->props);
619  }
620  function CreateGroupRequest() {
621     return new GROUPRequest($this->props);
622  }
623  function CreateDataRequest() {
624     return new DATARequest($this->props);
625  }
626
627  function GetLocationString() {
628     if ($this->props['db_server']) {
629     $res = "Server: \"" . $this->props['db_server'] . "\"";
630     if ($this->props['db_name']) {
631         $res .= ", Database: \"" . $this->props['db_name'] . "\"";
632         if ($this->props['db_group']) {
633         $res .= ", Group: \"" . $this->props['db_group'] . "\"";
634         }
635     }
636     return $res;
637     }
638     
639     return "";
640  }
641  
642  function GetAxisInfo($aid = false) {
643     return $this->props;
644  }
645  
646  function GetAxis($aid) {
647     global $ADEI_AXES;
648     
649     if ($aid) {
650     $aprop = $aid . "_axis";
651     if (isset($this->props[$aprop. "_name"])) {
652         $info = $this->GetAxisInfo($aid);
653         return new DRAWAxis($info, $aid);
654     }
655     
656     // UI Axes resolver should go here (only if UI already included)
657     
658     if (isset($ADEI_AXES[$aid])) {
659         return new DRAWAxis($ADEI_AXES[$aid]);
660     }
661     } else {
662     $info = $this->GetAxisInfo();
663     return new DRAWAxis($info);
664     }
665  }
666  
667  
668  function GetAxes($aids) {
669     $res = array();
670     foreach ($aids as $aid) {
671     $res[$aid] = $this->GetAxis($aid);
672     }
673     return $res;
674  }
675
676 }
677
678 class SERVERRequest extends REQUEST {
679  var $srv;
680
681  function __construct(&$props = NULL) {
682     global $READER_DB;
683     global $ADEI_VIRTUAL_READERS;
684
685     parent::__construct($props);
686     
687     if (isset($this->props["db_server"]))
688     $srvid = $this->props["db_server"];
689     else
690     throw new ADEIException(translate("The data source server should be specified"));
691
692     if (is_array($READER_DB[$srvid])) {
693     $this->srv = $READER_DB[$srvid];
694     } else if (is_array($ADEI_VIRTUAL_READERS[$srvid])) {
695     $this->srv = $ADEI_VIRTUAL_READERS[$srvid];
696     $this->srv['virtual'] = 1;
697     } else {
698     throw new ADEIException(translate("Invalid server identificator is supplied: \"%s\"", $srvid));
699     }
700  }
701  
702  function IsVirtual() {
703     if ($this->srv['virtual']) return true;
704     return false;
705  }
706
707  function GetServerInfo($flags = 0) {
708     if ($flags&REQUEST::NEED_INFO) {
709     $server = $this->srv;
710     if (isset($this->props['db_name'])) $server['database'] = $this->props['db_name'];
711     else unset($server['database']);
712     } else {
713     $server = array();
714     }
715
716     $server['db_server'] = $this->props['db_server'];
717     $server['db_server_name'] = $this->srv['title'];
718     if ($this->srv['virtual']) $server['virtual'] = true;
719     
720     return $server;
721  }
722  
723  function GetServerTitle($flags = 0) {
724     return $this->srv['title'];
725  }
726  
727  function GetDatabaseList($flags = 0) {
728     $reader = $this->CreateReader();
729     $dblist = $reader->GetDatabaseList($flags);
730     
731     foreach ($dblist as $did => &$dinfo) {
732     $dinfo['db_name'] = $did;
733     $dinfo['db_name_name'] = $dinfo['name'];
734     }
735
736     if (($flags&REQUEST::SKIP_UNCACHED)&&(!$this->srv['virtual'])) {
737     $cache = new CACHEDB();
738     $cached_databases = $cache->ListCachedDatabases($this->props['db_server']);
739
740         foreach (array_keys($dblist) as $key) {
741         if (!in_array($dblist[$key]['db_name'], $cached_databases)) {
742         unset($dblist[$key]);
743         }
744     }
745     }
746     
747     return $dblist;
748  }
749  
750  function CreateReader($flags = 0) {
751     global $ADEI;
752     
753     if ($this->srv['disconnected']) {
754     if ($flags&REQUEST::READER_FORBID_CACHEREADER)
755         throw new ADEIException(translate("The data reader(%s) is disconnected", $this->srv['reader']), ADEIException::DISCONNECTED);
756
757     $rdr = $this->CreateCacheReader();
758     $rdr->DisableReaderAccess();
759     return $rdr;
760     } else if (($opts = $this->GetOptions())&&($opts->Get('use_cache_reader'))&&(!($flags&REQUEST::READER_FORBID_CACHEREADER))) {
761     return $this->CreateCacheReader();
762     } else {
763     $reader = $this->srv['reader'];
764     
765     try {
766         $ADEI->RequireClass("readers/$reader", true);
767     } catch (ADEIException $ae) {
768         if ($this->srv['reader'])
769         throw new ADEIException(translate("Unsupported data reader is configured: \"%s\"", $this->srv['reader']));
770         else
771         throw new ADEIException(translate("The data reader is not configured"));
772     }
773     }
774
775     try {
776     $rdr = new $reader($this);
777     } catch (ADEIException $ae) {
778     if ($flags&REQUEST::READER_FORBID_CACHEREADER) throw $ae;
779
780     if ($opts->Get('overcome_reader_faults')) {
781         $rdr = $this->CreateCacheReader();
782         $rdr->DisableReaderAccess();
783     } else throw $ae;   
784     }
785     
786     return $rdr;
787  }
788  
789  function CreateCacheReader(CACHEDB &$cache = NULL) {
790     if ($this->srv['virtual']) {
791     $reader = $this->srv['reader'];
792     
793     try {
794         ADEI::RequireClass("readers/$reader", true);
795     } catch (ADEIException $ae) {
796         if ($this->srv['reader'])
797         throw new ADEIException(translate("Unsupported data reader is configured: \"%s\"", $this->srv['reader']));
798         else
799         throw new ADEIException(translate("The data reader is not configured"));
800     }
801     if (method_exists($reader, "ConvertToCacheReader")) {
802         $rdr = new $reader($this);
803         $rdr->ConvertToCacheReader($cache);
804         return $rdr;
805     }
806     if (method_exists($reader, "CreateCacheReader")) {
807         $rdr = new $reader($this);
808         return $rdr->CreateCacheReader($cache);
809     }
810     }
811     
812     return new CACHEReader($this, $cache);
813  }
814
815  function GetLocationString() {
816     return "Server: \"" . $this->props['db_server'] . "\"";
817  }
818 }
819
820
821 class SOURCERequest extends SERVERRequest {
822  function __construct(&$props = NULL) {
823     parent::__construct($props);
824
825     if (!isset($this->props["db_name"]))
826     throw new ADEIException(translate("The database should be specified"));
827  }
828
829
830  function GetDatabaseInfo($flags = 0) {
831     return array(
832     'db_name' => $this->props['db_name'],
833     'db_name_name' => $this->props['db_name']
834     );
835  }
836
837  function GetDatabaseTitle($flags = 0) {
838     return $this->props['db_name'];
839  }
840  
841  function GetSourceTitle($flags = 0) {
842     return $this->GetServerTitle($flags) . " -- " . $this->GetDatabaseTitle($flags);
843  }
844
845  function GetSourceInfo($flags = 0) {
846     return array_merge($this->GetServerInfo($flags), $this->GetDatabaseInfo($flags));
847  }
848  
849  function GetGroups(READER $rdr = NULL, $flags = 0) {
850     if ($flags&REQUEST::LIST_ALL) return REQUEST::GetGroups($flags);
851
852     $list = array();
853     if (isset($this->props['db_group'])) {
854     $gid = $this->props['db_group'];
855     $list[$gid] = array(
856         'db_group' => $gid,
857     );
858     } else {
859     if ($rdr) $reader = $rdr;
860     else $reader = $this->CreateReader();
861     $list = $reader->GetGroupList($flags);
862     foreach (array_keys($list) as $gid) {
863         $list[$gid] = array(
864         'db_group' => $gid,
865         );
866     }
867     }
868     return new REQUESTList($this->props, $list, "GROUPRequest");
869  }
870
871  function GetGroupList($flags = 0) {
872     if ($flags&REQUEST::LIST_ALL) return REQUEST::GetGroupList($flags);
873
874     $list = array();
875     
876     if (isset($this->props['db_group'])) {
877     if ($rdr) $reader = &$rdr;
878     else $reader = $this->CreateReader();
879
880     $grp = $reader->CreateGroup($req = array(
881         'db_group' => $this->props['db_group']
882     ));
883     
884     $list = $reader->GetGroupInfo($grp, $flags);
885     } else {
886     if ($rdr) $reader = &$rdr;
887     else $reader = $this->CreateReader();
888     
889     $list = $reader->GetGroupList($flags);
890     }
891     
892     if (($flags&REQUEST::SKIP_UNCACHED)&&(!$this->srv['virtual'])) {
893     $cache = new CACHEDB();
894     $cached_groups = $cache->ListCachedGroups($this->props['db_name'], $this->props['db_server']);
895
896 #    print_r($list);
897 #    print_r($cached_groups);
898         foreach (array_keys($list) as $key) {
899         if (!in_array($list[$key]['gid'], $cached_groups)) {
900         unset($list[$key]);
901         }
902     }
903     }
904
905     if ($list) {
906     foreach ($list as &$gr) {
907         $gr['db_group'] = $gr['gid'];
908         $gr['db_group_name'] = $gr['name'];
909     }
910     }
911     
912     
913     return $list;
914  }
915
916  function GetAlarmList($flags = 0) {
917     $reader = $this->CreateReader();
918     $list = $reader->GetAlarmList(NULL, NULL, $flags);
919     return $list;
920  }
921  
922  function GetLocationString() {
923     return "Server: \"" . $this->props['db_server'] . "\" Database: \"" . $this->props['db_name'] . "\"";
924  }
925
926  function CreateControlGroupRequest(LOGGROUP $grp = NULL) {
927     if ($grp) {
928     $props = $this->props;
929     $props['conntrol_group'] = $grp->gid;
930         return new CONTROLGroupRequest($props);
931     }
932
933     return parent::CreateControlGroupRequest();
934  }
935
936  function CreateGroupRequest(LOGGROUP $grp = NULL) {
937     if ($grp) {
938     $props = $this->props;
939     $props['db_group'] = $grp->gid;
940         return new GROUPRequest($props);
941     }
942
943     return parent::CreateGroupRequest();
944  }
945 }
946
947 abstract class ITEMGroupRequest extends SOURCERequest {
948  function __construct(&$props = NULL) {
949     parent::__construct($props);
950  }
951
952  function GetGroupInfo($flags = 0) {
953     if ($flags&REQUEST::CONTROL) {
954     if (!isset($this->props["control_group"]))
955         throw new ADEIException(translate("The Control Group is not specified"));
956
957     return array(
958         'control_group' => $this->props['control_group'],
959         'control_group_name' => $this->props['control_group']
960     );
961     } else {
962     if (!isset($this->props["db_group"]))
963         throw new ADEIException(translate("The Logging Group is not specified"));
964
965     return array(
966         'db_group' => $this->props['db_group'],
967         'db_group_name' => $this->props['db_group']
968     );
969     }
970  }
971
972  function GetItemList($flags = 0) {
973     $reader = $this->CreateReader();
974     $list = $reader->GetItemList(NULL, NULL, $flags);
975     return $list;
976  }
977  
978  function GetMaskList($flags = 0) {
979     $reader = $this->CreateReader();
980     $list = $reader->GetMaskList($gr=NULL, $flags);
981     return $list;
982  }
983  
984  function GetMaskInfo($flags = 0) {
985     return $this->props;
986  }
987  
988  function CreateGroup(READER $rdr = NULL, $flags = 0) {
989     if (!$rdr) $rdr = $this->CreateReader();
990
991     $ginfo = $this->GetGroupInfo($flags);
992     return $rdr->CreateGroup($ginfo, $flags);
993  }
994
995  function GetLocationString($flags = 0) {
996     if ($flags&REQUEST::CONTROL) {
997     return "Server: \"" . $this->props['db_server'] . "\" Database: " . $this->props['db_name'] . "\" Group: \"" . $this->props['control_group'] . "\"";
998     } else {
999     return "Server: \"" . $this->props['db_server'] . "\" Database: " . $this->props['db_name'] . "\" Group: \"" . $this->props['db_group'] . "\"";
1000     }
1001  }
1002 }
1003
1004 class CONTROLGroupRequest extends ITEMGroupRequest {
1005  function __construct(&$props = NULL) {
1006     parent::__construct($props);
1007
1008     if (!isset($this->props["control_group"]))
1009     throw new ADEIException(translate("The Control Group should be specified"));
1010  }
1011 }
1012
1013 class GROUPRequest extends ITEMGroupRequest {
1014  function __construct(&$props = NULL) {
1015     parent::__construct($props);
1016
1017     if (!isset($this->props["db_group"]))
1018     throw new ADEIException(translate("The Logging Group should be specified"));
1019  }
1020
1021
1022  function CreateCache(READER $rdr = NULL) {
1023     if ($this->srv['virtual']) {
1024     if (!$rdr) $rdr = $this->CreateReader();
1025     return $rdr->CreateCache();
1026     }
1027
1028     return new CACHE($this, $rdr);
1029  }
1030  
1031  function CreateSimpleCacheSet(MASK $mask = NULL, CACHE $cache = NULL) {
1032     if (!$cache) $cache = $this->CreateCache();
1033     return new SIMPLECacheSet($cache, $mask);
1034  }
1035  
1036  function CreateCacheSet(REQUESTList $list) {
1037     return new REQUESTListCacheSet($list);
1038  }
1039  
1040  function CreateSimpleRequestSet(MASK $mask = NULL, LOGGROUP $grp = NULL, $type = "GROUPRequest") {
1041     $props = array();
1042     if ($grp) $props['db_group'] = $grp->GetProp();
1043     if ($mask) $props['db_mask'] = $mask->GetProp();
1044     return new REQUESTList($this->props, $list = array($props), $type);
1045  }
1046  
1047  function CreateRequestSet(REQUESTList $list) {
1048     return $list;
1049  }
1050  
1051  function CreateCacheUpdater(READER $rdr = NULL) {
1052     if ($this->srv['virtual']) {
1053     if (!$rdr) $rdr = $this->CreateReader();
1054     return $rdr->CreateCache(NULL, CACHE::CREATE_UPDATER);
1055     }
1056
1057     return new CACHE($this, $rdr, CACHE::CREATE_UPDATER);
1058  }
1059
1060  function GetCachePostfix() {
1061     $cache = new CACHE($this);
1062     return $cache->GetCachePostfix();
1063  }
1064 }
1065
1066 class DATARequest extends GROUPRequest {
1067  function __construct(&$props = NULL) {
1068     parent::__construct($props);
1069  }
1070
1071  function GetIntervalInfo() {
1072     return $this->props;
1073  }
1074  
1075  function CreateInterval(READER &$rdr = NULL, LOGGROUP &$grp = NULL, $flags = 0) {
1076     $iinfo = $this->GetIntervalInfo();
1077     return new INTERVAL($iinfo, $rdr, $grp, $flags);
1078  }
1079  
1080  function CreatePlotter() {
1081     global $ADEI;
1082     $ADEI->RequireClass("draw");
1083
1084     return new DRAW($this);
1085  }
1086  
1087  function GetFormatInfo() {
1088     global $ADEI_SYSTEM_FORMATS;
1089     global $EXPORT_FORMATS;
1090     global $EXPORT_DEFAULT_FORMAT;
1091     
1092     $format = $this->props['format'];
1093     if ($format) {
1094     if ($EXPORT_FORMATS[$format])
1095         return $EXPORT_FORMATS[$format];
1096     else if ($ADEI_SYSTEM_FORMATS[$format])
1097         return $ADEI_SYSTEM_FORMATS[$format];
1098     else
1099         throw new ADEIException(translate("Unsupported export fromat (%s) is specified", $format));
1100     } else if ($EXPORT_FORMATS[$EXPORT_DEFAULT_FORMAT])
1101     return $EXPORT_FORMATS[$EXPORT_DEFAULT_FORMAT];
1102     else
1103     return array(
1104             title => "CSV",
1105         extension => "csv"
1106     );
1107  }
1108  
1109  function CreateExporter(STREAMObjectInterface &$h = NULL, &$format = NULL) {
1110     global $ADEI;
1111     $ADEI->RequireClass("export");
1112
1113     return new EXPORT($this, $h, $format);
1114  }
1115 }
1116
1117
1118 function adeiCreateExporter(STREAMHandler &$h = NULL) {
1119     $req = new DATARequest();
1120     return $req->CreateExporter($h);
1121 }
1122
1123 ?>
Note: See TracBrowser for help on using the browser.