thomascube
2011-10-05 537c394302df13e7623a36b28b11caceeaaa75b2
commit | author | age
d48470 1 #!/usr/bin/env php
1e9cd5 2 <?php
155bbb 3 /*
A 4
5  +-----------------------------------------------------------------------+
30aa4c 6  | bin/dumpschema.sh                                                     |
155bbb 7  |                                                                       |
e019f2 8  | This file is part of the Roundcube Webmail client                     |
f5e7b3 9  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
155bbb 10  | Licensed under the GNU GPL                                            |
A 11  |                                                                       |
12  | PURPOSE:                                                              |
13  |   Dumps database schema in XML format using MDB2_Schema               |
14  |                                                                       |
15  +-----------------------------------------------------------------------+
16  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17  +-----------------------------------------------------------------------+
18
19  $Id$
20
21 */
1e9cd5 22
da402d 23 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
6cc3f5 24
A 25 require INSTALL_PATH.'program/include/clisetup.php';
1e9cd5 26
T 27 /** callback function for schema dump **/
28 function print_schema($dump)
29 {
30     foreach ((array)$dump as $part)
31         echo $dump . "\n";
32 }
33
34 $config = new rcube_config();
35
36 // don't allow public access if not in devel_mode
37 if (!$config->get('devel_mode') && $_SERVER['REMOTE_ADDR']) {
38     header("HTTP/1.0 401 Access denied");
39     die("Access denied!");
40 }
41
42 $options = array(
43     'use_transactions' => false,
44     'log_line_break' => "\n",
45     'idxname_format' => '%s',
46     'debug' => false,
47     'quote_identifier' => true,
48     'force_defaults' => false,
06c3d1 49     'portability' => false,
1e9cd5 50 );
T 51
e905db 52 $dsnw = $config->get('db_dsnw');
T 53 $dsn_array = MDB2::parseDSN($dsnw);
54
55 // set options for postgres databases
56 if ($dsn_array['phptype'] == 'pgsql') {
57     $options['disable_smart_seqname'] = true;
58     $options['seqname_format'] = '%s';
59 }
60
61 $schema =& MDB2_Schema::factory($dsnw, $options);
1e9cd5 62 $schema->db->supported['transactions'] = false;
T 63
e905db 64
1e9cd5 65 // send as text/xml when opened in browser
T 66 if ($_SERVER['REMOTE_ADDR'])
67     header('Content-Type: text/xml');
68
69
70 if (PEAR::isError($schema)) {
71     $error = $schema->getMessage() . ' ' . $schema->getUserInfo();
72 }
73 else {
74     $dump_config = array(
75         // 'output_mode' => 'file',
76         'output' => 'print_schema',
77     );
155bbb 78     
1e9cd5 79     $definition = $schema->getDefinitionFromDatabase();
e905db 80     $definition['charset'] = 'utf8';
T 81
1e9cd5 82     if (PEAR::isError($definition)) {
T 83         $error = $definition->getMessage() . ' ' . $definition->getUserInfo();
84     }
85     else {
86         $operation = $schema->dumpDatabase($definition, $dump_config, MDB2_SCHEMA_DUMP_STRUCTURE);
87         if (PEAR::isError($operation)) {
88             $error = $operation->getMessage() . ' ' . $operation->getUserInfo();
89         }
90     }
91 }
92
93 $schema->disconnect();
94
e905db 95 if ($error && !$_SERVER['REMOTE_ADDR'])
T 96     fputs(STDERR, $error);
1e9cd5 97
T 98 ?>