[ Index ]
 

Code source de eGroupWare 1.2.106-2

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

title

Body

[fermer]

/setup/ -> fix_mysql_charset.php (source)

   1  <?php
   2  /**************************************************************************\
   3  * eGroupWare - Setup - fixes a mysql DB to match our system_charset        *
   4  * http://www.eGroupWare.org                                                *
   5  * Written by RalfBecker@outdoor-training.de                                *
   6  * --------------------------------------------                             *
   7  *  This program is free software; you can redistribute it and/or modify it *
   8  *  under the terms of the GNU General Public License as published by the   *
   9  *  Free Software Foundation; either version 2 of the License, or (at your  *
  10  *  option) any later version.                                              *
  11  \**************************************************************************/
  12  
  13  /* $Id: fix_mysql_charset.php 20295 2006-02-15 12:31:25Z  $ */
  14  
  15  // if we are NOT called as part of an update script, behave like a regular setup script
  16  if (!isset($GLOBALS['egw_setup']) || !is_object($GLOBALS['egw_setup']))    
  17  {
  18      $diagnostics = 1;    // can be set to 0=non, 1=some (default for now), 2=all
  19  
  20      $GLOBALS['egw_info'] = array(
  21          'flags' => array(
  22              'noheader' => True,
  23              'nonavbar' => True,
  24              'currentapp' => 'home',
  25              'noapi' => True
  26      ));
  27      include ('./inc/functions.inc.php');
  28      // Authorize the user to use setup app and load the database
  29      // Does not return unless user is authorized
  30      if (!$GLOBALS['egw_setup']->auth('Config') || @$_POST['cancel'])
  31      {
  32          Header('Location: index.php');
  33          exit;
  34      }
  35      $GLOBALS['egw_setup']->loaddb();
  36  
  37      $tpl_root = $GLOBALS['egw_setup']->html->setup_tpl_dir('setup');
  38      $setup_tpl = CreateObject('phpgwapi.Template',$tpl_root);
  39      $setup_tpl->set_file(array(
  40          'T_head' => 'head.tpl',
  41          'T_footer' => 'footer.tpl',
  42      ));
  43      $GLOBALS['egw_setup']->html->show_header('',False,'config',$GLOBALS['egw_setup']->ConfigDomain . '(' . $GLOBALS['egw_domain'][$GLOBALS['egw_setup']->ConfigDomain]['db_type'] . ')');
  44      echo '<h3>'.'Fix mysql DB to match the eGroupWare system_charset'."</h3>\n";
  45      $running_standalone = true;
  46  }
  47  $db =& $GLOBALS['egw_setup']->db;
  48  $charset2mysql =& $GLOBALS['egw_setup']->db->Link_ID->charset2mysql;
  49  $mysql2charset = array_flip($charset2mysql);
  50  
  51  $ServerInfo = $db->Link_ID->ServerInfo();
  52  $db_version = (float) $ServerInfo['version'];
  53  
  54  if ($running_standalone || $_REQUEST['debug']) echo "<p>DB-Type='<b>{$GLOBALS['egw_setup']->db->Type}</b>', DB-Version=<b>$db_version</b> ($ServerInfo[description]), eGroupWare system_charset='<b>{$GLOBALS['egw_setup']->system_charset}</b>', DB-connection charset was '<b>{$GLOBALS['egw_setup']->db_charset_was}</b>'</p>\n";
  55  
  56  $mysql_system_charset = isset($charset2mysql[$GLOBALS['egw_setup']->system_charset]) ? 
  57      $charset2mysql[$GLOBALS['egw_setup']->system_charset] : $GLOBALS['egw_setup']->system_charset;
  58  
  59  if (substr($db->Type,0,5) == 'mysql' && $db_version >= 4.1 && $GLOBALS['egw_setup']->system_charset && $GLOBALS['egw_setup']->db_charset_was &&
  60      $GLOBALS['egw_setup']->system_charset != $GLOBALS['egw_setup']->db_charset_was)
  61  {
  62      $tables_modified = 'no';
  63  
  64      $tables = array();
  65      $db->query("SHOW TABLE STATUS",__LINE__,__FILE__);
  66      while (($row = $db->row(true)))
  67      {
  68          $tables[$row['Name']] = $row['Collation'];
  69      }
  70      foreach($tables as $table => $collation)
  71      {
  72          $columns = array();
  73          $db->query("SHOW FULL FIELDS FROM `$table`",__LINE__,__FILE__);
  74          while(($row = $db->row(true)))
  75          {
  76              $columns[] = $row;
  77          }
  78          //echo $table; _debug_array($columns);
  79          $fulltext = $fulltext_back = array();
  80          $db->query("SHOW KEYS FROM `$table`",__LINE__,__FILE__);
  81          while(($row = $db->row(true)))
  82          {
  83              if ($row['Index_type'] == 'FULLTEXT')
  84              {
  85                  $fulltext[$row['Column_name']] = $row['Key_name'];
  86              }
  87          }
  88          
  89          $alter_table = $alter_table_back = array();
  90          foreach($columns as $column)
  91          {
  92              if ($column['Collation'] && preg_match('/^(char|varchar|.*text)\(?([0-9]*)\)?$/i',$column['Type'],$matches))
  93              {
  94                  list(,$type,$size) = $matches;
  95                  list($charset) = explode('_',$column['Collation']);
  96                  
  97                  if (isset($mysql2charset[$charset])) $charset = $mysql2charset[$charset];
  98                  
  99                  if ($charset != $GLOBALS['egw_setup']->system_charset)
 100                  {
 101                      $col = $column['Field'];
 102                      
 103                      if ($type == 'varchar' || $type == 'char')    // old schema_proc (pre 1.0.1) used also char
 104                      {
 105                          $type = 'varchar('.$size.')';
 106                          $bintype = 'varbinary('.$size.')';
 107                      }
 108                      else
 109                      {
 110                          $bintype = str_replace('text','blob',$type);
 111                      }
 112                      //echo "<p>$table.$col $type CHARACTER SET $charset $default $null</p>\n";
 113                      
 114                      $default = !is_null($column['Default']) ? "DEFAULT '".$column['Default']."'" : '';
 115                      $null = $column['Null'] ? 'NULL' : 'NOT NULL';
 116                      
 117                      if (isset($fulltext[$col]))
 118                      {
 119                          $idx_name = $fulltext[$col];
 120                          $idx_cols = array();
 121                          foreach($fulltext as $c => $i)
 122                          {
 123                              if ($i == $idx_name)
 124                              {
 125                                  $idx_cols[] = $c;
 126                                  unset($fulltext[$c]);
 127                              }
 128                          }
 129                          $fulltext_back[$idx_name] = $idx_cols;
 130                          $alter_table[] = " DROP INDEX `$idx_name`"; 
 131                      }
 132                      $alter_table[] = " CHANGE `$col` `$col` $bintype $default $null";
 133                      $alter_table_back[] = " CHANGE `$col` `$col` $type CHARACTER SET $mysql_system_charset $default $null";
 134                  }
 135              }
 136          }        
 137          list($charset) = explode('_',$collation);
 138          if (isset($mysql2charset[$charset])) $charset = $mysql2charset[$charset];
 139          if ($charset != $GLOBALS['egw_setup']->system_charset)
 140          {
 141              $alter_table[] = " DEFAULT CHARACTER SET $mysql_system_charset";
 142          }
 143          if (count($alter_table))
 144          {
 145              $alter_table = "ALTER TABLE $table\n".implode(",\n",$alter_table);
 146              
 147              if ($running_standalone || $_REQUEST['debug']) echo '<p>'.nl2br($alter_table)."</p>\n";
 148              if (!$db->query($alter_table,__LINE__,__FILE__))
 149              {
 150                  echo "<p>SQL Error: ".nl2br($alter_table)."</p>\n";
 151                  echo "<b>{$db->Type} Error</b>: {$db->Errno} ({$db->Error})</p>\n";
 152                  echo "<p>continuing ...</p>\n";
 153                  continue;
 154              }
 155              foreach($fulltext_back as $idx_name => $idx_cols)
 156              {
 157                  $alter_table_back[] = " ADD FULLTEXT `$idx_name` (`".implode('`,`',$idx_cols)."`)";
 158              }
 159              if (count($alter_table_back))
 160              {
 161                  $alter_table_back = "ALTER TABLE $table\n".implode(",\n",$alter_table_back);
 162                  
 163                  if ($running_standalone || $_REQUEST['debug']) echo '<p>'.nl2br($alter_table_back)."</p>\n";
 164                  if (!$db->query($alter_table_back,__LINE__,__FILE__))
 165                  {
 166                      echo "<p><b>SQL Error</b>: ".nl2br($alter_table_back)."</p>\n";
 167                      echo "<b>{$db->Type} Error</b>: {$db->Errno} ({$db->Error})</p>\n";
 168                      echo "<p>continuing ...</p>\n";
 169                      continue;
 170                  }
 171              }
 172              ++$tables_modified;
 173          }
 174      }
 175      // change the default charset of the DB
 176      $db->query("SHOW CREATE DATABASE `$db->Database`",__LINE__,__FILE__);
 177      $create_db = $db->next_record() ? $db->f(1) : '';
 178      if (preg_match('/CHARACTER SET ([a-z1-9_-]+) /i',$create_db,$matches) && $matches[1] != $mysql_system_charset)
 179      {
 180          $alter_db = "ALTER DATABASE `$db->Database` DEFAULT CHARACTER SET $mysql_system_charset";
 181          if ($running_standalone || $_REQUEST['debug']) echo '<p>'.$alter_db."</p>\n";
 182          $db->query($alter_db,__LINE__,__FILE__);
 183      }
 184  }
 185  if ($running_standalone || $_REQUEST['debug'])
 186  {
 187      echo "<p>$tables_modified tables changed to our system_charset {$GLOBALS['egw_setup']->system_charset}($mysql_system_charset)</p>\n";
 188  
 189      if ($running_standalone) $GLOBALS['egw_setup']->html->show_footer();
 190  }


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7