[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/bin/php/ -> ezconvertmysqltabletype.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 Database Converter\n\n" .
  33                                                           "Convert the database to the given type\n".
  34                                                           "ezconvertmysqltabletype.php [--host=VALUE --user=VALUE --database=VALUE [--password=VALUE]] [--list] [--newtype=TYPE] [--usecopy]" ),
  35                                        'use-session' => false,
  36                                        'use-modules' => false,
  37                                        'use-extensions' => false ) );
  38  
  39  $script->startup();
  40  
  41  $options = $script->getOptions( "[host:][user:][password:][database:][list][newtype:][usecopy]",
  42                                  "",
  43                                  array(
  44                                         'list' => "List the table types",
  45                                         'host' => "Connect to host database",
  46                                         'user' => "User for login to the database",
  47                                         'password' => "Password to use when connecting to the database",
  48                                         'newtype' => "Convert the database to the given type.\nType can either be: myisam or innodb\n".
  49                                                      "Make sure that you have made a BACKUP UP of YOUR DATABASE!",
  50                                         'usecopy' => "To convert the table we rename the original table and copy the data to the new table structure.\n".
  51                                                      "This conversion method is much slower and has a higher risk to corrupt the data in the database.\n".
  52                                                      "However this option may circumvent the MySQL crash on the ALTER query." )
  53                                );
  54  $script->initialize();
  55  
  56  $host = $options['host'];
  57  $user = $options['user'];
  58  
  59  $password = is_string( $options['password'] ) ? $options['password'] : "";
  60  $database = $options['database'];
  61  $listMode = $options['list'];
  62  $newType = $options["newtype"];
  63  $usecopy = $options["usecopy"];
  64  
  65  checkParameters( $cli, $script, $options, $host, $user, $password, $database, $listMode, $newType );
  66  $db =& connectToDatabase( $cli, $script, $host, $user, $password, $database );
  67  
  68  // If the listMode parameter is set or no newType is assigned then show the list.
  69  if ( $listMode || !isset( $newType ) )
  70  {
  71      listTypes( $cli, $db );
  72  }
  73  else
  74  {
  75      setNewType( $cli, $db, $newType, $usecopy );
  76  }
  77  
  78  /**
  79   *  Check whether the parameters are correctly set.
  80  **/
  81  function checkParameters( $cli, $script, $options, $host, $user, $password, $database, $listMode, $newType )
  82  {
  83      // Extra parameters are not tolerated.
  84      if ( count ( $options['arguments'] ) != 0 )
  85      {
  86              $cli->error( "Unknown parameters" );
  87              $script->shutdown( 1 );
  88      }
  89  
  90      // Host, User, and database are like the three musketeers.
  91      // Either the three parameters must be set or none.
  92      if ( isset( $host ) || isset( $user ) || isset( $database ) )
  93      {
  94          if ( !isset( $host ) || !isset( $user ) || !isset( $database ) )
  95          {
  96              $cli->error( "Use the host, user, database, and optionally a password together." );
  97              $script->shutdown( 1 );
  98          }
  99      }
 100  
 101      // If the newType is set, check whether the given type exist.
 102      if ( $newType )
 103      {
 104          switch ( strtolower( $newType ) )
 105          {
 106              case "innodb": break;
 107              case "myisam": break;
 108  
 109              default: $cli->error( "New table type not supported." );
 110                       $script->shutDown( 1 );
 111          }
 112      }
 113  }
 114  
 115  /**
 116   * Connect to the database
 117  **/
 118  function &connectToDatabase( $cli, $script, $host, $user, $password, $database )
 119  {
 120      include_once ( 'lib/ezdb/classes/ezdb.php' );
 121  
 122      if ( $user )
 123      {
 124          $db =& eZDB::instance( "mysql",
 125                             array( 'server' => $host,
 126                                    'user' => $user,
 127                                    'password' => $password,
 128                                    'database' => $database ) );
 129      } else
 130      {
 131           $db =& eZDB::instance();
 132           if ( $db->databaseName() != "mysql" )
 133           {
 134              $cli->error( 'This script can only show and convert mysql databases.' );
 135              $script->shutdown( 1 );
 136           }
 137      }
 138  
 139      if ( !is_object( $db ) )
 140      {
 141          $cli->error( 'Could not initialize database:' );
 142          $cli->error( '* No database handler was found for mysql' );
 143          $script->shutdown( 1 );
 144      }
 145      if ( !$db or !$db->isConnected() )
 146      {
 147          $cli->error( "Could not initialize database:" );
 148          $cli->error( "* Tried database " . eZTriedDatabaseString( $database, $host, $user, $password ) );
 149  
 150          // Fetch the database error message if there is one
 151          // It will give more feedback to the user what is wrong
 152          $msg = $db->errorMessage();
 153          if ( $msg )
 154          {
 155              $number = $db->errorNumber();
 156              if ( $number > 0 )
 157                  $msg .= '(' . $number . ')';
 158              $cli->error( '* ' . $msg );
 159          }
 160          $script->shutdown( 1 );
 161      }
 162  
 163      return $db;
 164  }
 165  
 166  function getTableType( $db, $tableName )
 167  {
 168      $res = $db->arrayQuery( "SHOW CREATE TABLE `$tableName`" );
 169      preg_match( '/(?:TYPE|ENGINE)=(\w*)/', $res[0]["Create Table"], $grep );
 170      return $grep[1];
 171  }
 172  
 173  function listTypes( $cli, $db )
 174  {
 175      $tables = $db->arrayQuery( "show tables" );
 176  
 177      $spaces = str_pad ( ' ', 35 );
 178      $cli->notice( "Table $spaces Type" );
 179      $cli->notice( "----- $spaces ----" );
 180      foreach ( $tables as $table )
 181      {
 182          $tableName = current( $table );
 183          $tableType = getTableType( $db, $tableName );
 184  
 185          $spaces = str_pad(' ', 40 - strlen( $tableName ) );
 186          $eZpublishTable = strncmp( $tableName, "ez", 2 ) == 0 ? "" : "(non eZ publish)";
 187          $cli->notice( "$tableName $spaces $tableType $eZpublishTable" );
 188      }
 189  }
 190  
 191  function alterType( $db, $tableName, $newType )
 192  {
 193       $db->query( "ALTER TABLE $tableName TYPE=$newType" );
 194  }
 195  
 196  function renameTable( $db, $tableFrom, $tableTo )
 197  {
 198      $db->query( "ALTER TABLE $tableFrom RENAME $tableTo" );
 199  }
 200  
 201  function copyTable( $db, $tableFrom, $tableTo )
 202  {
 203      $db->query( "INSERT INTO $tableTo SELECT * FROM $tableFrom" );
 204  }
 205  
 206  function createTableStructure( $db, $tableFrom, $tableTo, $newType )
 207  {
 208      $res = $db->arrayQuery( "SHOW CREATE TABLE `$tableFrom`" );
 209  
 210      $pattern = array( "/TYPE=(\w*)/", "/TABLE `$tableFrom`/" );
 211      $replacement = array( "TYPE=$newType", "TABLE `$tableTo`" );
 212      $structure = preg_replace( $pattern, $replacement, $res[0]["Create Table"] );
 213  
 214      $db->query( $structure );
 215  }
 216  
 217  function dropTable( $db, $tableName )
 218  {
 219      $db->query( "DROP TABLE $tableName" );
 220  }
 221  
 222  function setNewType( $cli, $db, $newType, $usecopy )
 223  {
 224      $tables = $db->arrayQuery( "show tables" );
 225  
 226      foreach ( $tables as $table )
 227      {
 228          $tableName = current( $table );
 229  
 230          // Checking if it is necessary to convert the table.
 231          if ( strncmp( $tableName, "ez", 2 ) != 0 )
 232          {
 233              $cli->notice( "Skipping table $tableName because it is not an eZ publish table" );
 234          }
 235          else if ( strcasecmp( getTableType( $db, $tableName ), $newType ) == 0 )
 236          {
 237              $cli->notice( "Skipping table $tableName because it has already the $newType type" );
 238          }
 239          else
 240          {
 241              // Yes, convert.
 242              $cli->notice( "Converting table $tableName ... " );
 243  
 244              if ( !$usecopy )
 245              {
 246                  // The simple one
 247                  alterType( $db, $tableName, $newType );
 248              }
 249              else
 250              {
 251                  renameTable( $db, $tableName, "eztemp__$tableName" );
 252                  createTableStructure( $db, "eztemp__$tableName", $tableName, $newType );
 253                  copyTable( $db, "eztemp__$tableName", $tableName );
 254                  dropTable( $db, "eztemp__$tableName" );
 255              }
 256          }
 257      }
 258  }
 259  
 260  $script->shutdown();
 261  ?>


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