php8
[web.mtrack] / MTrack / DBSchema.php
1 <?php
2
3 require_once 'MTrack/DBSchema/SQLite.php';
4 require_once 'MTrack/DBSchema/pgsql.php';
5 require_once 'MTrack/DBSchema/mysql.php';
6
7 require_once 'MTrack/DBSchema/Table.php';
8
9 class MTrackDBSchema {
10   var $tables;
11   var $version;
12   var $post;
13
14   function __construct($filename) {
15     $s = simplexml_load_file($filename);
16
17     $this->version = (int)$s['version'];
18
19     /* fabricate a table to hold the schema info */
20     $table = new MTrackDBSchema_Table;
21     $table->name = 'mtrack_schema';
22     $f = new stdclass;
23     $f->name = 'version';
24     $f->type = 'integer';
25     $f->nullable = '0';
26     $table->fields[$f->name] = $f;
27     $this->tables[$table->name] = $table;
28
29     foreach ($s->table as $t) {
30       $table = new MTrackDBSchema_Table;
31       $table->name = (string)$t['name'];
32
33       foreach ($t->field as $f) {
34         $F = new stdclass;
35         foreach ($f->attributes() as $k => $v) {
36           $F->{(string)$k} = (string)$v;
37         }
38         if (isset($f->comment)) {
39           $F->comment = (string)$f->comment;
40         }
41         $table->fields[$F->name] = $F;
42       }
43       foreach ($t->key as $k) {
44         $K = new stdclass;
45         $K->fields = array();
46         if (isset($k['type'])) {
47           $K->type = (string)$k['type'];
48         } else {
49           $K->type = 'primary';
50         }
51         foreach ($k->field as $f) {
52           $K->fields[] = (string)$f;
53         }
54         if (isset($k['name'])) {
55           $K->name = (string)$k['name'];
56         } else {
57           $K->name = sprintf("idx_%s_%s", $table->name, join('_', $K->fields));
58         }
59         $table->keys[$K->name] = $K;
60       }
61
62       $this->tables[$table->name] = $table;
63     }
64     foreach ($s->post as $p) {
65       $this->post[(string)$p['driver']] = (string)$p;
66     }
67
68     /* apply custom ticket fields */
69     if (isset($this->tables['tickets'])) {
70       $table = $this->tables['tickets'];
71       $custom = MTrackTicket_CustomFields::getInstance();
72       foreach ($custom->fields as $field) {
73         $f = new stdclass;
74         $f->name = $field->name;
75         $f->type = 'text';
76         $table->fields[$f->name] = $f;
77       }
78     }
79   }
80 }