eZ Publish  [4.0]
ezsqldumpschema.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <21-Apr-2004 09:51:56 kk>
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 //include_once( 'lib/ezutils/classes/ezcli.php' );
00031 //include_once( 'kernel/classes/ezscript.php' );
00032 
00033 require 'autoload.php';
00034 
00035 $cli = eZCLI::instance();
00036 $script = eZScript::instance( array( 'description' => ( "eZ Publish SQL Schema dump\n\n" .
00037                                                         "Dump sql schema to specified file or standard output\n".
00038                                                         "ezsqldumpschema.php --type=mysql --user=root stable33 schema.sql" ),
00039                                      'use-session' => false,
00040                                      'use-modules' => true,
00041                                      'use-extensions' => true ) );
00042 
00043 $script->startup();
00044 
00045 $options = $script->getOptions( "[type:][user:][host:][password;][port:][socket:][output-array][output-serialized][output-sql]" .
00046                                 "[diff-friendly][meta-data][table-type:][table-charset:][compatible-sql]" .
00047                                 "[format:]" .
00048                                 "[output-types:][allow-multi-insert][schema-file:]",
00049                                 "[database][filename]",
00050                                 array( 'type' => ( "Which database type to use for source, can be one of:\n" .
00051                                                           "mysql, postgresql" ),
00052                                        'host' => "Connect to host source database",
00053                                        'user' => "User for login to source database",
00054                                        'password' => "Password to use when connecting to source database",
00055                                        'port' => 'Port to connect to source database',
00056                                        'socket' => 'Socket to connect to match and source database (only for MySQL)',
00057                                        'output-array' => 'Create file with array structures (Human readable)',
00058                                        'output-serialized' => 'Create file with serialized data (Saves space)',
00059                                        'output-sql' => 'Create file with SQL data (DB friendly)',
00060                                        'compatible-sql' => 'Will turn SQL to be more compatible to existing dumps',
00061                                        'table-type' => ( "The table storage type to use for SQL output when creating tables.\n" .
00062                                                          "MySQL: bdb, innodb and myisam\n" .
00063                                                          "PostgreSQL: \n" .
00064                                                          "Oracle: " ),
00065                                        'table-charset' => 'Defines the charset to use on tables, the names of the charset depends on database type',
00066                                        'schema-file' => 'The schema file to use when dumping data structures, is only required when dumping from files',
00067                                        'format' => ( "The output format (default is generic)\n" .
00068                                                      "generic - Format which suits all databases\n" .
00069                                                      "local - Format which suits only the database it was dumped from." ),
00070                                        'meta-data' => 'Will include extra meta-data information specific to the database.',
00071                                        'diff-friendly' => 'Will make the output friendlier towards the diff command (applies to SQL output only)',
00072                                        'allow-multi-insert' => ( 'Will create INSERT statements with multiple data entries (applies to data output only)' . "\n" .
00073                                                                  'Multi-inserts will only be created for databases that support it' ),
00074                                        'output-types' => ( "A comma separated list of types to include in dump (default is schema only):\n" .
00075                                                            "schema - Table schema\n" .
00076                                                            "data - Table data\n" .
00077                                                            "all - Both table schema and data" )
00078                                        ) );
00079 $script->initialize();
00080 
00081 $type = $options['type'];
00082 $host = $options['host'];
00083 $user = $options['user'];
00084 $port = $options['port'];
00085 $socket = $options['socket'];
00086 $password = $options['password'];
00087 
00088 if ( !is_string( $password ) )
00089     $password = '';
00090 
00091 switch ( count( $options['arguments'] ) )
00092 {
00093     case 0:
00094         $cli->error( "Missing match database and/or filename" );
00095         $script->shutdown( 1 );
00096         break;
00097     case 1:
00098         $database = $options['arguments'][0];
00099         $filename = 'php://stdout';
00100         break;
00101     case 2:
00102         $database = $options['arguments'][0];
00103         $filename = $options['arguments'][1];
00104         break;
00105     case 3:
00106         $cli->error( "Too many arguments" );
00107         $script->shutdown( 1 );
00108         break;
00109 }
00110 
00111 $includeSchema = true;
00112 $includeData = false;
00113 
00114 if ( $options['output-types'] )
00115 {
00116     $includeSchema = false;
00117     $includeData = false;
00118     $includeTypes = explode( ',', $options['output-types'] );
00119     foreach ( $includeTypes as $includeType )
00120     {
00121         switch ( $includeType )
00122         {
00123             case 'all':
00124             {
00125                 $includeSchema = true;
00126                 $includeData = true;
00127             } break;
00128 
00129             case 'schema':
00130             {
00131                 $includeSchema = true;
00132             } break;
00133 
00134             case 'data':
00135             {
00136                 $includeData = true;
00137             } break;
00138         }
00139     }
00140 }
00141 
00142 $dbschemaParameters = array( 'schema' => $includeSchema,
00143                              'data' => $includeData,
00144                              'format' => $options['format'] ? $options['format'] : 'generic',
00145                              'meta_data' => $options['meta-data'],
00146                              'table_type' => $options['table-type'],
00147                              'table_charset' => $options['table-charset'],
00148                              'compatible_sql' => $options['compatible-sql'],
00149                              'allow_multi_insert' => $options['allow-multi-insert'],
00150                              'diff_friendly' => $options['diff-friendly'] );
00151 
00152 
00153 $outputType = 'serialized';
00154 if ( $options['output-array'] )
00155     $outputType = 'array';
00156 if ( $options['output-serialized'] )
00157     $outputType = 'serialized';
00158 if ( $options['output-sql'] )
00159     $outputType = 'sql';
00160 
00161 if ( strlen( trim( $type ) ) == 0)
00162 {
00163     $cli->error( "No database type chosen" );
00164     $script->shutdown( 1 );
00165 }
00166 
00167 // Creates a displayable string for the end-user explaining
00168 // which database, host, user and password which were tried
00169 function eZTriedDatabaseString( $database, $host, $user, $password, $socket )
00170 {
00171     $msg = "'$database'";
00172     if ( strlen( $host ) > 0 )
00173     {
00174         $msg .= " at host '$host'";
00175     }
00176     else
00177     {
00178         $msg .= " locally";
00179     }
00180     if ( strlen( $user ) > 0 )
00181     {
00182         $msg .= " with user '$user'";
00183     }
00184     if ( strlen( $password ) > 0 )
00185         $msg .= " and with a password";
00186     if ( strlen( $socket ) > 0 )
00187         $msg .= " and with socket '$socket'";
00188     return $msg;
00189 }
00190 
00191 if ( file_exists( $database ) and is_file( $database ) )
00192 {
00193     //include_once( 'lib/ezdbschema/classes/ezdbschema.php' );
00194     $schemaArray = eZDbSchema::read( $database, true );
00195 
00196     if ( $includeData and !isset( $schemaArray['data'] ) )
00197     {
00198         $cli->error( "The specified data file '$database' contains no data" );
00199         $script->shutdown( 1 );
00200     }
00201 
00202     if ( $includeData and !$options['schema-file'] )
00203     {
00204         $cli->error( "Cannot dump data without a schema file, please specify with --schema-file" );
00205         $script->shutdown( 1 );
00206     }
00207 
00208     if ( $options['schema-file'] )
00209     {
00210         if ( !file_exists( $options['schema-file'] ) or !is_file( $options['schema-file'] ) )
00211         {
00212             $cli->error( "Schema file " . $options['schema-file'] . " does not exist" );
00213             $script->shutdown( 1 );
00214         }
00215         $schema = eZDbSchema::read( $options['schema-file'], false );
00216         $schemaArray['schema'] = $schema;
00217     }
00218 
00219     if ( $includeSchema and
00220          ( !isset( $schemaArray['schema'] ) or
00221            !$schemaArray['schema'] ) )
00222     {
00223         $cli->error( "No schema was found in file $database" );
00224         $cli->error( "Specify --output-types=data if you are interested in data only" );
00225         $script->shutdown( 1 );
00226     }
00227 
00228     if ( $schemaArray === false )
00229     {
00230         eZDebug::writeError( "Error reading schema from file $database" );
00231         $script->shutdown( 1 );
00232     }
00233     $schemaArray['type'] = $type;
00234     $dbSchema = eZDbSchema::instance( $schemaArray );
00235 }
00236 else
00237 {
00238     if ( strlen( trim( $user ) ) == 0)
00239     {
00240         $cli->error( "No database user chosen" );
00241         $script->shutdown( 1 );
00242     }
00243 
00244     //include_once( 'lib/ezdb/classes/ezdb.php' );
00245     $parameters = array( 'use_defaults' => false,
00246                          'server' => $host,
00247                          'user' => $user,
00248                          'password' => $password,
00249                          'database' => $database );
00250     if ( $socket )
00251         $parameters['socket'] = $socket;
00252     if ( $port )
00253         $parameters['port'] = $port;
00254     $db = eZDB::instance( $type,
00255                            $parameters,
00256                            true );
00257 
00258     if ( !is_object( $db ) )
00259     {
00260         $cli->error( 'Could not initialize database:' );
00261         $cli->error( '* No database handler was found for $type' );
00262         $script->shutdown( 1 );
00263     }
00264     if ( !$db or !$db->isConnected() )
00265     {
00266         $cli->error( "Could not initialize database:" );
00267         $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password, $socket ) );
00268 
00269         // Fetch the database error message if there is one
00270         // It will give more feedback to the user what is wrong
00271         $msg = $db->errorMessage();
00272         if ( $msg )
00273         {
00274             $number = $db->errorNumber();
00275             if ( $number > 0 )
00276                 $msg .= '(' . $number . ')';
00277             $cli->error( '* ' . $msg );
00278         }
00279         $script->shutdown( 1 );
00280     }
00281 
00282     //include_once( 'lib/ezdbschema/classes/ezdbschema.php' );
00283     $dbSchema = eZDbSchema::instance( $db );
00284 }
00285 
00286 if ( $dbSchema === false )
00287 {
00288     $cli->error( "Error instantiating the appropriate schema handler" );
00289     $script->shutdown( 1 );
00290 }
00291 
00292 if ( $outputType == 'serialized' )
00293 {
00294     $dbSchema->writeSerializedSchemaFile( $filename,
00295                                           $dbschemaParameters );
00296 }
00297 else if ( $outputType == 'array' )
00298 {
00299     $dbSchema->writeArraySchemaFile( $filename,
00300                                      $dbschemaParameters );
00301 }
00302 else if ( $outputType == 'sql' )
00303 {
00304     $dbSchema->writeSQLSchemaFile( $filename,
00305                                    $dbschemaParameters );
00306 }
00307 
00308 $script->shutdown();
00309 
00310 ?>