[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 #!/usr/bin/env php 2 <?php 3 // 4 // Created on: <12-Nov-2004 14:13:19 jb> 5 // 6 // SOFTWARE NAME: eZ publish 7 // SOFTWARE RELEASE: 3.9.0 8 // BUILD VERSION: 17785 9 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 10 // SOFTWARE LICENSE: GNU General Public License v2.0 11 // NOTICE: > 12 // This program is free software; you can redistribute it and/or 13 // modify it under the terms of version 2.0 of the GNU General 14 // Public License as published by the Free Software Foundation. 15 // 16 // This program is distributed in the hope that it will be useful, 17 // but WITHOUT ANY WARRANTY; without even the implied warranty of 18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 // GNU General Public License for more details. 20 // 21 // You should have received a copy of version 2.0 of the GNU General 22 // Public License along with this program; if not, write to the Free 23 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 24 // MA 02110-1301, USA. 25 // 26 // 27 28 include_once ( 'lib/ezutils/classes/ezcli.php' ); 29 include_once ( 'kernel/classes/ezscript.php' ); 30 31 $cli =& eZCLI::instance(); 32 $script =& eZScript::instance( array( 'description' => ( "eZ publish SQL Schema insert\n\n" . 33 "Insert database schema and data to specified database\n". 34 "ezsqlinsertschema.php --type=mysql --user=root share/db_schema.dba ezp35stable" ), 35 'use-session' => false, 36 'use-modules' => true, 37 'use-extensions' => true ) ); 38 39 $script->startup(); 40 41 $options = $script->getOptions( "[type:][user:][host:][password;][socket:]" . 42 "[table-type:][table-charset:]" . 43 "[insert-types:][allow-multi-insert][schema-file:][clean-existing]", 44 "[filename][database]", 45 array( 'type' => ( "Which database type to use, can be one of:\n" . 46 "mysql, postgresql or any other supported by extensions" ), 47 'host' => "Connect to host source database", 48 'user' => "User for login to source database", 49 'password' => "Password to use when connecting to source database", 50 'socket' => 'Socket to connect to match and source database (only for MySQL)', 51 'table-type' => ( "The table storage type to use for SQL output when creating tables.\n" . 52 "MySQL: bdb, innodb and myisam\n" . 53 "PostgreSQL: \n" . 54 "Oracle: " ), 55 'clean-existing' => 'Clean up existing schema (remove all database objects)', 56 'table-charset' => 'Defines the charset to use on tables, the names of the charset depends on database type', 57 'schema-file' => 'The schema file to use when dumping data structures, is only required when dumping from files', 58 'allow-multi-insert' => ( 'Will create INSERT statements with multiple data entries (applies to data output only)' . "\n" . 59 'Multi-inserts will only be created for databases that support it' ), 60 'insert-types' => ( "A comma separated list of types to include in dump (default is schema only):\n" . 61 "schema - Table schema\n" . 62 "data - Table data\n" . 63 "all - Both table schema and data\n" . 64 "none - Insert nothing (useful if you want to clean up schema only)" ) 65 ) ); 66 $script->initialize(); 67 68 $type = $options['type']; 69 $host = $options['host']; 70 $user = $options['user']; 71 $socket = $options['socket']; 72 $password = $options['password']; 73 74 if ( !is_string( $password ) ) 75 $password = ''; 76 77 $includeSchema = true; 78 $includeData = false; 79 80 if ( $options['insert-types'] ) 81 { 82 $includeSchema = false; 83 $includeData = false; 84 $includeTypes = explode( ',', $options['insert-types'] ); 85 foreach ( $includeTypes as $includeType ) 86 { 87 switch ( $includeType ) 88 { 89 case 'all': 90 { 91 $includeSchema = true; 92 $includeData = true; 93 } break; 94 95 case 'schema': 96 { 97 $includeSchema = true; 98 } break; 99 100 case 'data': 101 { 102 $includeData = true; 103 } break; 104 105 case 'none': 106 { 107 $includeSchema = false; 108 $includeData = false; 109 } break; 110 } 111 } 112 } 113 114 $onlyCleanupSchema = $options['clean-existing'] && !$includeSchema && !$includeData; 115 116 switch ( count( $options['arguments'] ) ) 117 { 118 case 0: 119 $cli->error( "Missing filename and database" ); 120 $script->shutdown( 1 ); 121 break; 122 case 1: 123 if ( $onlyCleanupSchema ) 124 { 125 $database = $options['arguments'][0]; 126 $filename = ''; 127 } 128 else 129 { 130 $cli->error( "Missing database" ); 131 $script->shutdown( 1 ); 132 } 133 break; 134 case 2: 135 $filename = $options['arguments'][0]; 136 $database = $options['arguments'][1]; 137 break; 138 case 3: 139 $cli->error( "Too many arguments" ); 140 $script->shutdown( 1 ); 141 break; 142 } 143 144 $dbschemaParameters = array( 'schema' => $includeSchema, 145 'data' => $includeData, 146 'format' => 'local', 147 'table_type' => $options['table-type'], 148 'table_charset' => $options['table-charset'], 149 'allow_multi_insert' => $options['allow-multi-insert'] ); 150 151 152 if ( strlen( trim( $type ) ) == 0 ) 153 { 154 $cli->error( "No database type chosen" ); 155 $script->shutdown( 1 ); 156 } 157 158 if ( !$onlyCleanupSchema and ( !file_exists( $filename ) or !is_file( $filename ) ) ) 159 { 160 $cli->error( "File '$filename' does not exist" ); 161 $script->shutdown( 1 ); 162 } 163 164 if ( strlen( trim( $user ) ) == 0) 165 { 166 $cli->error( "No database user chosen" ); 167 $script->shutdown( 1 ); 168 } 169 170 // Creates a displayable string for the end-user explaining 171 // which database, host, user and password which were tried 172 function eZTriedDatabaseString( $database, $host, $user, $password, $socket ) 173 { 174 $msg = "'$database'"; 175 if ( strlen( $host ) > 0 ) 176 { 177 $msg .= " at host '$host'"; 178 } 179 else 180 { 181 $msg .= " locally"; 182 } 183 if ( strlen( $user ) > 0 ) 184 { 185 $msg .= " with user '$user'"; 186 } 187 if ( strlen( $password ) > 0 ) 188 $msg .= " and with a password"; 189 if ( strlen( $socket ) > 0 ) 190 $msg .= " and with socket '$socket'"; 191 return $msg; 192 } 193 194 // Connect to database 195 196 include_once ( 'lib/ezdb/classes/ezdb.php' ); 197 $parameters = array( 'server' => $host, 198 'user' => $user, 199 'password' => $password, 200 'database' => $database ); 201 if ( $socket ) 202 $parameters['socket'] = $socket; 203 $db =& eZDB::instance( $type, 204 $parameters, 205 true ); 206 207 if ( !is_object( $db ) ) 208 { 209 $cli->error( 'Could not initialize database:' ); 210 $cli->error( '* No database handler was found for $type' ); 211 $script->shutdown( 1 ); 212 } 213 if ( !$db or !$db->isConnected() ) 214 { 215 $cli->error( "Could not initialize database:" ); 216 $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password, $socket ) ); 217 218 // Fetch the database error message if there is one 219 // It will give more feedback to the user what is wrong 220 $msg = $db->errorMessage(); 221 if ( $msg ) 222 { 223 $number = $db->errorNumber(); 224 if ( $number > 0 ) 225 $msg .= '(' . $number . ')'; 226 $cli->error( '* ' . $msg ); 227 } 228 $script->shutdown( 1 ); 229 } 230 231 // Load in schema/data files 232 233 include_once ( 'lib/ezdbschema/classes/ezdbschema.php' ); 234 $schemaArray = eZDBSchema::read( $filename, true ); 235 if ( $includeData and !$options['schema-file'] ) 236 { 237 $cli->error( "Cannot insert data without a schema file, please specify with --schema-file" ); 238 $script->shutdown( 1 ); 239 } 240 241 if ( $options['schema-file'] ) 242 { 243 if ( !file_exists( $options['schema-file'] ) or !is_file( $options['schema-file'] ) ) 244 { 245 $cli->error( "Schema file " . $options['schema-file'] . " does not exist" ); 246 $script->shutdown( 1 ); 247 } 248 $schema = eZDBSchema::read( $options['schema-file'], false ); 249 $schemaArray['schema'] = $schema; 250 } 251 252 if ( $includeSchema and 253 ( !isset( $schemaArray['schema'] ) or 254 !$schemaArray['schema'] ) ) 255 { 256 $cli->error( "No schema was found in file $filename" ); 257 $cli->error( "Specify --insert-types=data if you are interested in data only" ); 258 $script->shutdown( 1 ); 259 } 260 261 if ( $schemaArray === false ) 262 { 263 eZDebug::writeError( "Error reading schema from file $filename" ); 264 $script->shutdown( 1 ); 265 exit( 1 ); 266 } 267 $schemaArray['type'] = $type; 268 269 // Clean elements if specified 270 271 if ( $options['clean-existing'] ) 272 { 273 include_once ( 'lib/ezdb/classes/ezdbtool.php' ); 274 $status = eZDBTool::cleanup( $db ); 275 if ( !$status ) 276 { 277 $cli->error( "Failed cleaning up existing database elements" ); 278 $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password, $socket ) ); 279 $cli->error( "Error(" . $db->errorNumber() . "): " . $db->errorMessage() ); 280 $script->shutdown( 1 ); 281 } 282 } 283 284 // Prepare schema handler 285 286 $schemaArray['instance'] =& $db; 287 $dbSchema = eZDBSchema::instance( $schemaArray ); 288 289 if ( $dbSchema === false ) 290 { 291 $cli->error( "Error instantiating the appropriate schema handler" ); 292 $script->shutdown( 1 ); 293 exit( 1 ); 294 } 295 296 // Insert schema/data by running SQL statements to database 297 $status = ( $includeSchema or $includeData ) ? $dbSchema->insertSchema( $dbschemaParameters ) : true; 298 if ( !$status ) 299 { 300 $cli->error( "Failed insert schema/data to database" ); 301 $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password, $socket ) ); 302 $script->shutdown( 1 ); 303 } 304 305 $script->shutdown(); 306 307 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |