[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/bin/php/ -> ezsqldumpschema.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  //
   4  // Created on: <21-Apr-2004 09:51:56 kk>
   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 dump\n\n" .
  33                                                           "Dump sql schema to specified file or standard output\n".
  34                                                           "ezsqldumpschema.php --type=mysql --user=root stable33 schema.sql" ),
  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:][output-array][output-serialized][output-sql]" .
  42                                  "[diff-friendly][meta-data][table-type:][table-charset:][compatible-sql]" .
  43                                  "[format:]" .
  44                                  "[output-types:][allow-multi-insert][schema-file:]",
  45                                  "[database][filename]",
  46                                  array( 'type' => ( "Which database type to use for source, can be one of:\n" .
  47                                                            "mysql, postgresql" ),
  48                                         'host' => "Connect to host source database",
  49                                         'user' => "User for login to source database",
  50                                         'password' => "Password to use when connecting to source database",
  51                                         'socket' => 'Socket to connect to match and source database (only for MySQL)',
  52                                         'output-array' => 'Create file with array structures (Human readable)',
  53                                         'output-serialized' => 'Create file with serialized data (Saves space)',
  54                                         'output-sql' => 'Create file with SQL data (DB friendly)',
  55                                         'compatible-sql' => 'Will turn SQL to be more compatible to existing dumps',
  56                                         'table-type' => ( "The table storage type to use for SQL output when creating tables.\n" .
  57                                                           "MySQL: bdb, innodb and myisam\n" .
  58                                                           "PostgreSQL: \n" .
  59                                                           "Oracle: " ),
  60                                         'table-charset' => 'Defines the charset to use on tables, the names of the charset depends on database type',
  61                                         'schema-file' => 'The schema file to use when dumping data structures, is only required when dumping from files',
  62                                         'format' => ( "The output format (default is generic)\n" .
  63                                                       "generic - Format which suits all databases\n" .
  64                                                       "local - Format which suits only the database it was dumped from." ),
  65                                         'meta-data' => 'Will include extra meta-data information specific to the database.',
  66                                         'diff-friendly' => 'Will make the output friendlier towards the diff command (applies to SQL output only)',
  67                                         'allow-multi-insert' => ( 'Will create INSERT statements with multiple data entries (applies to data output only)' . "\n" .
  68                                                                   'Multi-inserts will only be created for databases that support it' ),
  69                                         'output-types' => ( "A comma separated list of types to include in dump (default is schema only):\n" .
  70                                                             "schema - Table schema\n" .
  71                                                             "data - Table data\n" .
  72                                                             "all - Both table schema and data" )
  73                                         ) );
  74  $script->initialize();
  75  
  76  $type = $options['type'];
  77  $host = $options['host'];
  78  $user = $options['user'];
  79  $socket = $options['socket'];
  80  $password = $options['password'];
  81  
  82  if ( !is_string( $password ) )
  83      $password = '';
  84  
  85  switch ( count( $options['arguments'] ) )
  86  {
  87      case 0:
  88          $cli->error( "Missing match database and/or filename" );
  89          $script->shutdown( 1 );
  90          break;
  91      case 1:
  92          $database = $options['arguments'][0];
  93          $filename = 'php://stdout';
  94          break;
  95      case 2:
  96          $database = $options['arguments'][0];
  97          $filename = $options['arguments'][1];
  98          break;
  99      case 3:
 100          $cli->error( "Too many arguments" );
 101          $script->shutdown( 1 );
 102          break;
 103  }
 104  
 105  $includeSchema = true;
 106  $includeData = false;
 107  
 108  if ( $options['output-types'] )
 109  {
 110      $includeSchema = false;
 111      $includeData = false;
 112      $includeTypes = explode( ',', $options['output-types'] );
 113      foreach ( $includeTypes as $includeType )
 114      {
 115          switch ( $includeType )
 116          {
 117              case 'all':
 118              {
 119                  $includeSchema = true;
 120                  $includeData = true;
 121              } break;
 122  
 123              case 'schema':
 124              {
 125                  $includeSchema = true;
 126              } break;
 127  
 128              case 'data':
 129              {
 130                  $includeData = true;
 131              } break;
 132          }
 133      }
 134  }
 135  
 136  $dbschemaParameters = array( 'schema' => $includeSchema,
 137                               'data' => $includeData,
 138                               'format' => $options['format'] ? $options['format'] : 'generic',
 139                               'meta_data' => $options['meta-data'],
 140                               'table_type' => $options['table-type'],
 141                               'table_charset' => $options['table-charset'],
 142                               'compatible_sql' => $options['compatible-sql'],
 143                               'allow_multi_insert' => $options['allow-multi-insert'],
 144                               'diff_friendly' => $options['diff-friendly'] );
 145  
 146  
 147  $outputType = 'serialized';
 148  if ( $options['output-array'] )
 149      $outputType = 'array';
 150  if ( $options['output-serialized'] )
 151      $outputType = 'serialized';
 152  if ( $options['output-sql'] )
 153      $outputType = 'sql';
 154  
 155  if ( strlen( trim( $type ) ) == 0)
 156  {
 157      $cli->error( "No database type chosen" );
 158      $script->shutdown( 1 );
 159  }
 160  
 161  // Creates a displayable string for the end-user explaining
 162  // which database, host, user and password which were tried
 163  function eZTriedDatabaseString( $database, $host, $user, $password, $socket )
 164  {
 165      $msg = "'$database'";
 166      if ( strlen( $host ) > 0 )
 167      {
 168          $msg .= " at host '$host'";
 169      }
 170      else
 171      {
 172          $msg .= " locally";
 173      }
 174      if ( strlen( $user ) > 0 )
 175      {
 176          $msg .= " with user '$user'";
 177      }
 178      if ( strlen( $password ) > 0 )
 179          $msg .= " and with a password";
 180      if ( strlen( $socket ) > 0 )
 181          $msg .= " and with socket '$socket'";
 182      return $msg;
 183  }
 184  
 185  if ( file_exists( $database ) and is_file( $database ) )
 186  {
 187      include_once ( 'lib/ezdbschema/classes/ezdbschema.php' );
 188      $schemaArray = eZDBSchema::read( $database, true );
 189  
 190      if ( $includeData and !isset( $schemaArray['data'] ) )
 191      {
 192          $cli->error( "The specified data file '$database' contains no data" );
 193          $script->shutdown( 1 );
 194      }
 195  
 196      if ( $includeData and !$options['schema-file'] )
 197      {
 198          $cli->error( "Cannot dump data without a schema file, please specify with --schema-file" );
 199          $script->shutdown( 1 );
 200      }
 201  
 202      if ( $options['schema-file'] )
 203      {
 204          if ( !file_exists( $options['schema-file'] ) or !is_file( $options['schema-file'] ) )
 205          {
 206              $cli->error( "Schema file " . $options['schema-file'] . " does not exist" );
 207              $script->shutdown( 1 );
 208          }
 209          $schema = eZDBSchema::read( $options['schema-file'], false );
 210          $schemaArray['schema'] = $schema;
 211      }
 212  
 213      if ( $includeSchema and
 214           ( !isset( $schemaArray['schema'] ) or
 215             !$schemaArray['schema'] ) )
 216      {
 217          $cli->error( "No schema was found in file $database" );
 218          $cli->error( "Specify --output-types=data if you are interested in data only" );
 219          $script->shutdown( 1 );
 220      }
 221  
 222      if ( $schemaArray === false )
 223      {
 224          eZDebug::writeError( "Error reading schema from file $database" );
 225          $script->shutdown( 1 );
 226          exit( 1 );
 227      }
 228      $schemaArray['type'] = $type;
 229      $dbSchema = eZDBSchema::instance( $schemaArray );
 230  }
 231  else
 232  {
 233      if ( strlen( trim( $user ) ) == 0)
 234      {
 235          $cli->error( "No database user chosen" );
 236          $script->shutdown( 1 );
 237      }
 238  
 239      include_once ( 'lib/ezdb/classes/ezdb.php' );
 240      $parameters = array( 'use_defaults' => false,
 241                           'server' => $host,
 242                           'user' => $user,
 243                           'password' => $password,
 244                           'database' => $database );
 245      if ( $socket )
 246          $parameters['socket'] = $socket;
 247      $db =& eZDB::instance( $type,
 248                             $parameters,
 249                             true );
 250  
 251      if ( !is_object( $db ) )
 252      {
 253          $cli->error( 'Could not initialize database:' );
 254          $cli->error( '* No database handler was found for $type' );
 255          $script->shutdown( 1 );
 256      }
 257      if ( !$db or !$db->isConnected() )
 258      {
 259          $cli->error( "Could not initialize database:" );
 260          $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password, $socket ) );
 261  
 262          // Fetch the database error message if there is one
 263          // It will give more feedback to the user what is wrong
 264          $msg = $db->errorMessage();
 265          if ( $msg )
 266          {
 267              $number = $db->errorNumber();
 268              if ( $number > 0 )
 269                  $msg .= '(' . $number . ')';
 270              $cli->error( '* ' . $msg );
 271          }
 272          $script->shutdown( 1 );
 273      }
 274  
 275      include_once ( 'lib/ezdbschema/classes/ezdbschema.php' );
 276      $dbSchema = eZDBSchema::instance( $db );
 277  }
 278  
 279  if ( $dbSchema === false )
 280  {
 281      $cli->error( "Error instantiating the appropriate schema handler" );
 282      $script->shutdown( 1 );
 283      exit( 1 );
 284  }
 285  
 286  if ( $outputType == 'serialized' )
 287  {
 288      $dbSchema->writeSerializedSchemaFile( $filename,
 289                                            $dbschemaParameters );
 290  }
 291  else if ( $outputType == 'array' )
 292  {
 293      $dbSchema->writeArraySchemaFile( $filename,
 294                                       $dbschemaParameters );
 295  }
 296  else if ( $outputType == 'sql' )
 297  {
 298      $dbSchema->writeSQLSchemaFile( $filename,
 299                                     $dbschemaParameters );
 300  }
 301  
 302  $script->shutdown();
 303  
 304  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7