~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to lib/tools/bin/generate-dbxml.php

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19
 
 */
20
 
ini_set('error_reporting', 2047);
21
 
if (!empty($_SERVER['SERVER_NAME'])) {
22
 
    print "You must run this from the command line\n";
23
 
    exit(1);
24
 
}
25
 
 
26
 
require_once(dirname(__FILE__) . '/XmlParser.inc');
27
 
require_once(dirname(__FILE__) . '/../../smarty/Smarty.class.php');
28
 
 
29
 
$tmpdir = dirname(__FILE__) . '/tmp_dbxml_' . rand(1, 30000);
30
 
if (file_exists($tmpdir)) {
31
 
    print "Tmp dir already exists: $tmpdir\n";
32
 
    exit(1);
33
 
}
34
 
 
35
 
if (!mkdir($tmpdir)) {
36
 
    print "Unable to make tmp dir: $tmpdir\n";
37
 
    exit(1);
38
 
}
39
 
 
40
 
$smarty = new Smarty();
41
 
$smarty->compile_dir = $tmpdir;
42
 
$smarty->error_reporting = error_reporting();
43
 
$smarty->debugging = true;
44
 
$smarty->use_sub_dirs = false;
45
 
$smarty->template_dir = dirname(__FILE__);
46
 
 
47
 
function generateEntityDbXml() {
48
 
    global $smarty;
49
 
 
50
 
    $entityXmlFiles = glob('tmp/classxml/*.xml');
51
 
    if (empty($entityXmlFiles)) {
52
 
        return;
53
 
    }
54
 
    foreach ($entityXmlFiles as $xmlFile) {
55
 
        $p =& new XmlParser();
56
 
        $root = $p->parse($xmlFile);
57
 
        $base = basename($xmlFile);
58
 
        $base = preg_replace('/\.[^\.]*$/', '', $base);
59
 
        $tmpFile = "tmp/dbxml/$base.xml";
60
 
        $origFile = "$base.xml";
61
 
 
62
 
        $membersBase = $root[0]['child'];
63
 
        $schema = array(
64
 
            'name' => $root[0]['child'][2]['child'][0]['content'],
65
 
            'major' => $root[0]['child'][2]['child'][1]['content'],
66
 
            'minor' => (!empty($root[0]['child'][2]['child'][2]['content']) ?
67
 
                        $root[0]['child'][2]['child'][2]['content'] : 0));
68
 
 
69
 
        $members = array();
70
 
        $keys = array();
71
 
        $indexes = array();
72
 
        $requiresId = false;
73
 
        foreach ($membersBase as $child) {
74
 
            switch($child['name']) {
75
 
            case 'MEMBER':
76
 
                $member = array('name' => $child['child'][0]['content'],
77
 
                                'type' => $child['child'][1]['content'],
78
 
                                'ucName' => ucfirst($child['child'][0]['content']),
79
 
                                'lcType' => strtolower($child['child'][1]['content']));
80
 
                for ($i = 2; $i < count($child['child']); $i++) {
81
 
                    switch($child['child'][$i]['name']) {
82
 
                    case 'MEMBER-SIZE':
83
 
                        $member['size'] = $child['child'][$i]['content'];
84
 
                        break;
85
 
 
86
 
                    case 'INDEXED':
87
 
                        $indexes[] = array('columns' => array($member['name']));
88
 
                        $member[strtolower($child['child'][$i]['name'])] = 1;
89
 
                        break;
90
 
 
91
 
                    case 'UNIQUE':
92
 
                        $keys[] = array('columns' => array($member['name']));
93
 
                        $member[strtolower($child['child'][$i]['name'])] = 1;
94
 
                        break;
95
 
 
96
 
                    case 'PRIMARY':
97
 
                        $keys[] = array('columns' => array($member['name']), 'primary' => 1);
98
 
                        $member['primary'] = 1;
99
 
                        break;
100
 
 
101
 
                    case 'ID':
102
 
                    case 'LINKED':
103
 
                        $member[strtolower($child['child'][$i]['name'])] = 1;
104
 
                        break;
105
 
 
106
 
                    case 'REQUIRED':
107
 
                        $member['required'] = array();
108
 
                        if (isset($child['child'][$i]['attrs']['EMPTY'])) {
109
 
                            $member['required']['empty'] = $child['child'][$i]['attrs']['EMPTY'];
110
 
                        } else {
111
 
                            $member['required']['empty'] = 'disallowed';
112
 
                        }
113
 
                        break;
114
 
 
115
 
                    case 'DEFAULT':
116
 
                        $member['default'] = $child['child'][$i]['content'];
117
 
                        break;
118
 
 
119
 
                    case 'MEMBER-EXTERNAL-ACCESS':
120
 
                        /* Not relevant for storage layer */
121
 
                        break;
122
 
 
123
 
                    default:
124
 
                        print 'Unknown member type: ' . $child['child'][$i]['name'] . '\n';
125
 
                    }
126
 
                }
127
 
 
128
 
                if (empty($member['size'])) {
129
 
                    $member['size'] = 'MEDIUM';
130
 
                }
131
 
 
132
 
                $members[] = $member;
133
 
                break;
134
 
 
135
 
            case 'KEY':
136
 
                $key = array();
137
 
                foreach ($child['child'] as $column) {
138
 
                    $key['columns'][] = $column['content'];
139
 
                }
140
 
                $key['primary'] =
141
 
                    isset($child['attrs']['PRIMARY']) && $child['attrs']['PRIMARY'] == 'true';
142
 
                $keys[] = $key;
143
 
                break;
144
 
 
145
 
            case 'INDEX':
146
 
                $index = array();
147
 
                foreach ($child['child'] as $column) {
148
 
                    $index['columns'][] = $column['content'];
149
 
                }
150
 
                $index['primary'] =
151
 
                    isset($child['attrs']['PRIMARY']) && $child['attrs']['PRIMARY'] == 'true';
152
 
 
153
 
                $indexes[] = $index;
154
 
                break;
155
 
 
156
 
            case 'REQUIRES-ID':
157
 
                $requiresId = true;
158
 
                $keys[] = array('columns' => array('id'), 'primary' => 1);
159
 
                break;
160
 
            }
161
 
        }
162
 
 
163
 
        $smarty->assign('root', $root);
164
 
        $smarty->assign('schema', $schema);
165
 
        $smarty->assign('members', $members);
166
 
        $smarty->assign('keys', $keys);
167
 
        $smarty->assign('indexes', $indexes);
168
 
        $smarty->assign('requiresId', $requiresId);
169
 
        $smarty->assign('isMap', false);
170
 
        $new = $smarty->fetch('dbxml.tpl');
171
 
 
172
 
        $fd = fopen($tmpFile, "w");
173
 
        fwrite($fd, $new);
174
 
        fclose($fd);
175
 
    }
176
 
}
177
 
 
178
 
function generateMapDbXml() {
179
 
    global $smarty;
180
 
 
181
 
    $xmlFile = '../Maps.xml';
182
 
    if (!file_exists($xmlFile)) {
183
 
        return;
184
 
    }
185
 
 
186
 
    $p =& new XmlParser();
187
 
    $root = $p->parse($xmlFile);
188
 
    $base = basename($xmlFile);
189
 
    $base = preg_replace('/\.[^\.]*$/', '', $base);
190
 
    $origFile = "$base.xml";
191
 
 
192
 
    foreach ($root[0]['child'] as $map) {
193
 
        $origMapName = $map['child'][0]['content'];
194
 
 
195
 
        /*
196
 
         * NOTE!  Keep this in sync with the similar block in extractClassXml.pl
197
 
         * and generate-entities.php
198
 
         */
199
 
        $mapName = $origMapName;
200
 
        $mapName = preg_replace('/^Gallery/', '', $mapName);
201
 
        /* Shorten some table names to fit Oracle's 30 char name limit.. */
202
 
        $mapName = str_replace('Preferences', 'Prefs', $mapName);
203
 
        $mapName = str_replace('Toolkit', 'Tk', $mapName);
204
 
        $mapName = str_replace('TkOperation', 'TkOperatn', $mapName);
205
 
 
206
 
        $schema = array(
207
 
            'name' => $mapName,
208
 
            'major' => $map['child'][1]['child'][0]['content'],
209
 
            'minor' => $map['child'][1]['child'][1]['content']);
210
 
        $tmpFile = "tmp/dbxml/$origMapName.xml";
211
 
 
212
 
        $members = array();
213
 
        $keys = array();
214
 
        $indexes = array();
215
 
        $requiresId = false;
216
 
        for ($j = 2; $j < count($map['child']); $j++) {
217
 
            $child = $map['child'][$j];
218
 
            switch($child['name']) {
219
 
            case 'MEMBER':
220
 
                $member = array('name' => $child['child'][0]['content'],
221
 
                                'type' => $child['child'][1]['content'],
222
 
                                'ucName' => ucfirst($child['child'][0]['content']),
223
 
                                'lcType' => strtolower($child['child'][1]['content']));
224
 
                for ($i = 2; $i < count($child['child']); $i++) {
225
 
                    switch($child['child'][$i]['name']) {
226
 
                    case 'MEMBER-SIZE':
227
 
                        $member['size'] = $child['child'][$i]['content'];
228
 
                        break;
229
 
 
230
 
                    case 'INDEXED':
231
 
                        $indexes[] = array('columns' => array($member['name']));
232
 
                        $member[strtolower($child['child'][$i]['name'])] = 1;
233
 
                        break;
234
 
 
235
 
                    case 'UNIQUE':
236
 
                        $keys[] = array('columns' => array($member['name']));
237
 
                        $member[strtolower($child['child'][$i]['name'])] = 1;
238
 
                        break;
239
 
 
240
 
                    case 'PRIMARY':
241
 
                        $keys[] = array('columns' => array($member['name']), 'primary' => 1);
242
 
                        $member['primary'] = 1;
243
 
                        break;
244
 
 
245
 
                    case 'REQUIRED':
246
 
                        $member['required'] = array();
247
 
                        if (isset($child['child'][$i]['attrs']['EMPTY'])) {
248
 
                            $member['required']['empty'] = $child['child'][$i]['attrs']['EMPTY'];
249
 
                        } else {
250
 
                            $member['required']['empty'] = 'disallowed';
251
 
                        }
252
 
                        break;
253
 
 
254
 
                    case 'DEFAULT':
255
 
                        $member['default'] = $child['child'][$i]['content'];
256
 
                        break;
257
 
 
258
 
                    case 'MEMBER-EXTERNAL-ACCESS':
259
 
                        /* Not relevant for storage layer */
260
 
                        break;
261
 
 
262
 
                    default:
263
 
                        print 'Unknown member type: ' . $child['child'][$i]['name'] . '\n';
264
 
                    }
265
 
                }
266
 
 
267
 
                if (empty($member['size'])) {
268
 
                    $member['size'] = 'MEDIUM';
269
 
                }
270
 
 
271
 
                $members[] = $member;
272
 
                break;
273
 
 
274
 
            case 'KEY':
275
 
                $key = array();
276
 
                foreach ($child['child'] as $column) {
277
 
                    $key['columns'][] = $column['content'];
278
 
                }
279
 
                $key['primary'] =
280
 
                    isset($child['attrs']['PRIMARY']) && $child['attrs']['PRIMARY'] == 'true';
281
 
                $keys[] = $key;
282
 
                break;
283
 
 
284
 
            case 'INDEX':
285
 
                $index = array();
286
 
                foreach ($child['child'] as $column) {
287
 
                    $index['columns'][] = $column['content'];
288
 
                }
289
 
                $index['primary'] =
290
 
                    isset($child['attrs']['PRIMARY']) && $child['attrs']['PRIMARY'] == 'true';
291
 
 
292
 
                $indexes[] = $index;
293
 
                break;
294
 
            }
295
 
 
296
 
            $smarty->assign('root', $root);
297
 
            $smarty->assign('schema', $schema);
298
 
            $smarty->assign('members', $members);
299
 
            $smarty->assign('keys', $keys);
300
 
            $smarty->assign('indexes', $indexes);
301
 
            $smarty->assign('requiresId', $requiresId);
302
 
            $smarty->assign('isMap', true);
303
 
            $new = $smarty->fetch('dbxml.tpl');
304
 
 
305
 
            $fd = fopen($tmpFile, "w");
306
 
            fwrite($fd, $new);
307
 
            fclose($fd);
308
 
        }
309
 
    }
310
 
}
311
 
 
312
 
generateEntityDbXml();
313
 
generateMapDbXml();
314
 
 
315
 
/* Clean up the cheap and easy way */
316
 
if (file_exists($tmpdir)) {
317
 
  system("rm -rf $tmpdir");
318
 
}
319
 
?>