| 1 | #!/usr/bin/env php |
|---|
| 2 | <?php |
|---|
| 3 | /* |
|---|
| 4 | |
|---|
| 5 | +-----------------------------------------------------------------------+ |
|---|
| 6 | | bin/dumpschema.php | |
|---|
| 7 | | | |
|---|
| 8 | | This file is part of the RoundCube Webmail client | |
|---|
| 9 | | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland | |
|---|
| 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$ |
|---|
| 20 | |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | if (php_sapi_name() != 'cli') { |
|---|
| 24 | die('Not on the "shell" (php-cli).'); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' ); |
|---|
| 28 | require INSTALL_PATH.'program/include/iniset.php'; |
|---|
| 29 | |
|---|
| 30 | /** callback function for schema dump **/ |
|---|
| 31 | function print_schema($dump) |
|---|
| 32 | { |
|---|
| 33 | foreach ((array)$dump as $part) |
|---|
| 34 | echo $dump . "\n"; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | $config = new rcube_config(); |
|---|
| 38 | |
|---|
| 39 | // don't allow public access if not in devel_mode |
|---|
| 40 | if (!$config->get('devel_mode') && $_SERVER['REMOTE_ADDR']) { |
|---|
| 41 | header("HTTP/1.0 401 Access denied"); |
|---|
| 42 | die("Access denied!"); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | $options = array( |
|---|
| 46 | 'use_transactions' => false, |
|---|
| 47 | 'log_line_break' => "\n", |
|---|
| 48 | 'idxname_format' => '%s', |
|---|
| 49 | 'debug' => false, |
|---|
| 50 | 'quote_identifier' => true, |
|---|
| 51 | 'force_defaults' => false, |
|---|
| 52 | 'portability' => false, |
|---|
| 53 | ); |
|---|
| 54 | |
|---|
| 55 | $dsnw = $config->get('db_dsnw'); |
|---|
| 56 | $dsn_array = MDB2::parseDSN($dsnw); |
|---|
| 57 | |
|---|
| 58 | // set options for postgres databases |
|---|
| 59 | if ($dsn_array['phptype'] == 'pgsql') { |
|---|
| 60 | $options['disable_smart_seqname'] = true; |
|---|
| 61 | $options['seqname_format'] = '%s'; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | $schema =& MDB2_Schema::factory($dsnw, $options); |
|---|
| 65 | $schema->db->supported['transactions'] = false; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | // send as text/xml when opened in browser |
|---|
| 69 | if ($_SERVER['REMOTE_ADDR']) |
|---|
| 70 | header('Content-Type: text/xml'); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | if (PEAR::isError($schema)) { |
|---|
| 74 | $error = $schema->getMessage() . ' ' . $schema->getUserInfo(); |
|---|
| 75 | } |
|---|
| 76 | else { |
|---|
| 77 | $dump_config = array( |
|---|
| 78 | // 'output_mode' => 'file', |
|---|
| 79 | 'output' => 'print_schema', |
|---|
| 80 | ); |
|---|
| 81 | |
|---|
| 82 | $definition = $schema->getDefinitionFromDatabase(); |
|---|
| 83 | $definition['charset'] = 'utf8'; |
|---|
| 84 | |
|---|
| 85 | if (PEAR::isError($definition)) { |
|---|
| 86 | $error = $definition->getMessage() . ' ' . $definition->getUserInfo(); |
|---|
| 87 | } |
|---|
| 88 | else { |
|---|
| 89 | $operation = $schema->dumpDatabase($definition, $dump_config, MDB2_SCHEMA_DUMP_STRUCTURE); |
|---|
| 90 | if (PEAR::isError($operation)) { |
|---|
| 91 | $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | $schema->disconnect(); |
|---|
| 97 | |
|---|
| 98 | if ($error && !$_SERVER['REMOTE_ADDR']) |
|---|
| 99 | fputs(STDERR, $error); |
|---|
| 100 | |
|---|
| 101 | ?> |
|---|