]> git.donarmstrong.com Git - roundcube.git/blob - bin/dumpschema.sh
Imported Upstream version 0.7.1
[roundcube.git] / bin / dumpschema.sh
1 #!/usr/bin/env php
2 <?php
3 /*
4
5  +-----------------------------------------------------------------------+
6  | bin/dumpschema.sh                                                     |
7  |                                                                       |
8  | This file is part of the Roundcube Webmail client                     |
9  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
10  | Licensed under the GNU GPL                                            |
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: dumpschema.sh 4677 2011-04-20 13:10:45Z alec $
20
21 */
22
23 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
24
25 require INSTALL_PATH.'program/include/clisetup.php';
26
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,
49         'portability' => false,
50 );
51
52 $dsnw = $config->get('db_dsnw');
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);
62 $schema->db->supported['transactions'] = false;
63
64
65 // send as text/xml when opened in browser
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         );
78         
79         $definition = $schema->getDefinitionFromDatabase();
80         $definition['charset'] = 'utf8';
81
82         if (PEAR::isError($definition)) {
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
95 if ($error && !$_SERVER['REMOTE_ADDR'])
96         fputs(STDERR, $error);
97
98 ?>