eZ Publish  [4.0]
ezsqlinsertschema.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <12-Nov-2004 14:13:19 jb>
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 require 'autoload.php';
00033 
00034 $cli = eZCLI::instance();
00035 $script = eZScript::instance( array( 'description' => ( "eZ Publish SQL Schema insert\n\n" .
00036                                                         "Insert database schema and data to specified database\n".
00037                                                         "ezsqlinsertschema.php --type=mysql --user=root share/db_schema.dba ezp35stable" ),
00038                                      'use-session' => false,
00039                                      'use-modules' => true,
00040                                      'use-extensions' => true ) );
00041 
00042 $script->startup();
00043 
00044 $options = $script->getOptions( "[type:][user:][host:][password;][port:][socket:]" .
00045                                 "[table-type:][table-charset:]" .
00046                                 "[insert-types:][allow-multi-insert][schema-file:][clean-existing]",
00047                                 "[filename][database]",
00048                                 array( 'type' => ( "Which database type to use, can be one of:\n" .
00049                                                    "mysql, postgresql or any other supported by extensions" ),
00050                                        'host' => "Connect to host source database",
00051                                        'user' => "User for login to source database",
00052                                        'password' => "Password to use when connecting to source database",
00053                                        'port' => 'Port to connect to source database',
00054                                        'socket' => 'Socket to connect to match and source database (only for MySQL)',
00055                                        'table-type' => ( "The table storage type to use for SQL output when creating tables.\n" .
00056                                                          "MySQL: bdb, innodb and myisam\n" .
00057                                                          "PostgreSQL: \n" .
00058                                                          "Oracle: " ),
00059                                        'clean-existing' => 'Clean up existing schema (remove all database objects)',
00060                                        'table-charset' => 'Defines the charset to use on tables, the names of the charset depends on database type',
00061                                        'schema-file' => 'The schema file to use when dumping data structures, is only required when dumping from files',
00062                                        'allow-multi-insert' => ( 'Will create INSERT statements with multiple data entries (applies to data output only)' . "\n" .
00063                                                                  'Multi-inserts will only be created for databases that support it' ),
00064                                        'insert-types' => ( "A comma separated list of types to include in dump (default is schema only):\n" .
00065                                                            "schema - Table schema\n" .
00066                                                            "data - Table data\n" .
00067                                                            "all - Both table schema and data\n" .
00068                                                            "none - Insert nothing (useful if you want to clean up schema only)" )
00069                                        ) );
00070 $script->initialize();
00071 
00072 $type = $options['type'];
00073 $host = $options['host'];
00074 $user = $options['user'];
00075 $socket = $options['socket'];
00076 $password = $options['password'];
00077 $port = $options['port'];
00078 
00079 if ( !is_string( $password ) )
00080     $password = '';
00081 
00082 $includeSchema = true;
00083 $includeData = false;
00084 
00085 if ( $options['insert-types'] )
00086 {
00087     $includeSchema = false;
00088     $includeData = false;
00089     $includeTypes = explode( ',', $options['insert-types'] );
00090     foreach ( $includeTypes as $includeType )
00091     {
00092         switch ( $includeType )
00093         {
00094             case 'all':
00095             {
00096                 $includeSchema = true;
00097                 $includeData = true;
00098             } break;
00099 
00100             case 'schema':
00101             {
00102                 $includeSchema = true;
00103             } break;
00104 
00105             case 'data':
00106             {
00107                 $includeData = true;
00108             } break;
00109 
00110             case 'none':
00111             {
00112                 $includeSchema = false;
00113                 $includeData   = false;
00114             } break;
00115         }
00116     }
00117 }
00118 
00119 $onlyCleanupSchema = $options['clean-existing'] && !$includeSchema && !$includeData;
00120 
00121 switch ( count( $options['arguments'] ) )
00122 {
00123     case 0:
00124         $cli->error( "Missing filename and database" );
00125         $script->shutdown( 1 );
00126         break;
00127     case 1:
00128         if ( $onlyCleanupSchema )
00129         {
00130             $database = $options['arguments'][0];
00131             $filename  = '';
00132         }
00133         else
00134         {
00135             $cli->error( "Missing database" );
00136             $script->shutdown( 1 );
00137         }
00138         break;
00139     case 2:
00140         $filename = $options['arguments'][0];
00141         $database = $options['arguments'][1];
00142         break;
00143     case 3:
00144         $cli->error( "Too many arguments" );
00145         $script->shutdown( 1 );
00146         break;
00147 }
00148 
00149 $dbschemaParameters = array( 'schema' => $includeSchema,
00150                              'data' => $includeData,
00151                              'format' => 'local',
00152                              'table_type' => $options['table-type'],
00153                              'table_charset' => $options['table-charset'],
00154                              'allow_multi_insert' => $options['allow-multi-insert'] );
00155 
00156 
00157 if ( strlen( trim( $type ) ) == 0 )
00158 {
00159     $cli->error( "No database type chosen" );
00160     $script->shutdown( 1 );
00161 }
00162 
00163 if ( !$onlyCleanupSchema and ( !file_exists( $filename ) or !is_file( $filename ) ) )
00164 {
00165     $cli->error( "File '$filename' does not exist" );
00166     $script->shutdown( 1 );
00167 }
00168 
00169 if ( strlen( trim( $user ) ) == 0)
00170 {
00171     $cli->error( "No database user chosen" );
00172     $script->shutdown( 1 );
00173 }
00174 
00175 // Creates a displayable string for the end-user explaining
00176 // which database, host, user and password which were tried
00177 function eZTriedDatabaseString( $database, $host, $user, $password, $socket )
00178 {
00179     $msg = "'$database'";
00180     if ( strlen( $host ) > 0 )
00181     {
00182         $msg .= " at host '$host'";
00183     }
00184     else
00185     {
00186         $msg .= " locally";
00187     }
00188     if ( strlen( $user ) > 0 )
00189     {
00190         $msg .= " with user '$user'";
00191     }
00192     if ( strlen( $password ) > 0 )
00193         $msg .= " and with a password";
00194     if ( strlen( $socket ) > 0 )
00195         $msg .= " and with socket '$socket'";
00196     return $msg;
00197 }
00198 
00199 // Connect to database
00200 
00201 //include_once( 'lib/ezdb/classes/ezdb.php' );
00202 $parameters = array( 'server' => $host,
00203                      'user' => $user,
00204                      'password' => $password,
00205                      'database' => $database );
00206 if ( $socket )
00207     $parameters['socket'] = $socket;
00208 if ( $port )
00209     $parameters['port'] = $port;
00210 $db = eZDB::instance( $type,
00211                        $parameters,
00212                        true );
00213 
00214 if ( !is_object( $db ) )
00215 {
00216     $cli->error( 'Could not initialize database:' );
00217     $cli->error( '* No database handler was found for $type' );
00218     $script->shutdown( 1 );
00219 }
00220 if ( !$db or !$db->isConnected() )
00221 {
00222     $cli->error( "Could not initialize database:" );
00223     $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password, $socket ) );
00224 
00225     // Fetch the database error message if there is one
00226     // It will give more feedback to the user what is wrong
00227     $msg = $db->errorMessage();
00228     if ( $msg )
00229     {
00230         $number = $db->errorNumber();
00231         if ( $number > 0 )
00232             $msg .= '(' . $number . ')';
00233         $cli->error( '* ' . $msg );
00234     }
00235     $script->shutdown( 1 );
00236 }
00237 
00238 // Load in schema/data files
00239 
00240 //include_once( 'lib/ezdbschema/classes/ezdbschema.php' );
00241 $schemaArray = eZDbSchema::read( $filename, true );
00242 if ( $includeData and !$options['schema-file'] )
00243 {
00244     $cli->error( "Cannot insert data without a schema file, please specify with --schema-file" );
00245     $script->shutdown( 1 );
00246 }
00247 
00248 if ( $options['schema-file'] )
00249 {
00250     if ( !file_exists( $options['schema-file'] ) or !is_file( $options['schema-file'] ) )
00251     {
00252         $cli->error( "Schema file " . $options['schema-file'] . " does not exist" );
00253         $script->shutdown( 1 );
00254     }
00255     $schema = eZDbSchema::read( $options['schema-file'], false );
00256     $schemaArray['schema'] = $schema;
00257 }
00258 
00259 if ( $includeSchema and
00260      ( !isset( $schemaArray['schema'] ) or
00261        !$schemaArray['schema'] ) )
00262 {
00263     $cli->error( "No schema was found in file $filename" );
00264     $cli->error( "Specify --insert-types=data if you are interested in data only" );
00265     $script->shutdown( 1 );
00266 }
00267 
00268 if ( $schemaArray === false )
00269 {
00270     eZDebug::writeError( "Error reading schema from file $filename" );
00271     $script->shutdown( 1 );
00272 }
00273 $schemaArray['type'] = $type;
00274 
00275 // Clean elements if specified
00276 
00277 if ( $options['clean-existing'] )
00278 {
00279     //include_once( 'lib/ezdb/classes/ezdbtool.php' );
00280     $status = eZDBTool::cleanup( $db );
00281     if ( !$status )
00282     {
00283         $cli->error( "Failed cleaning up existing database elements" );
00284         $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password, $socket ) );
00285         $cli->error( "Error(" . $db->errorNumber() . "): " . $db->errorMessage() );
00286         $script->shutdown( 1 );
00287     }
00288 }
00289 
00290 // Prepare schema handler
00291 
00292 $schemaArray['instance'] =& $db;
00293 $dbSchema = eZDbSchema::instance( $schemaArray );
00294 
00295 if ( $dbSchema === false )
00296 {
00297     $cli->error( "Error instantiating the appropriate schema handler" );
00298     $script->shutdown( 1 );
00299 }
00300 
00301 // Insert schema/data by running SQL statements to database
00302 $status = ( $includeSchema or $includeData ) ? $dbSchema->insertSchema( $dbschemaParameters ) : true;
00303 if ( !$status )
00304 {
00305     $cli->error( "Failed insert schema/data to database" );
00306     $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password, $socket ) );
00307     $script->shutdown( 1 );
00308 }
00309 
00310 $script->shutdown();
00311 
00312 ?>