[pLog-svn] r3274 - in plog/trunk: . class/config class/database config templates/wizard

mark at devel.lifetype.net mark at devel.lifetype.net
Mon Apr 24 03:57:26 GMT 2006


Author: mark
Date: 2006-04-24 03:44:47 +0000 (Mon, 24 Apr 2006)
New Revision: 3274

Modified:
   plog/trunk/class/config/configfilestorage.class.php
   plog/trunk/class/database/db.class.php
   plog/trunk/config/config.properties.php
   plog/trunk/templates/wizard/intro.template
   plog/trunk/templates/wizard/step1.template
   plog/trunk/wizard.php
Log:
Add support the "set NAMES xxx" to make the mysql 4.1(+) do the right encoding in client, connection and collation.

Modified: plog/trunk/class/config/configfilestorage.class.php
===================================================================
--- plog/trunk/class/config/configfilestorage.class.php	2006-04-23 21:30:41 UTC (rev 3273)
+++ plog/trunk/class/config/configfilestorage.class.php	2006-04-24 03:44:47 UTC (rev 3274)
@@ -93,6 +93,7 @@
             $config["db_username"] = "";
             $config["db_password"] = "";
             $config["db_database"] = "";
+            $config["db_character_set"] = "default";
             $config["db_persistent"] = true;
             #
             # the database prefix will be appended to the name of each database tables in case you want

Modified: plog/trunk/class/database/db.class.php
===================================================================
--- plog/trunk/class/database/db.class.php	2006-04-23 21:30:41 UTC (rev 3273)
+++ plog/trunk/class/database/db.class.php	2006-04-24 03:44:47 UTC (rev 3274)
@@ -35,16 +35,17 @@
 				//$db = NewADOConnection('mysql');
                 $db = PDb::getDriver('mysql');
 
-                $username = $fileConfig->getValue( "db_username" );
-                $password = $fileConfig->getValue( "db_password" );
-                $host     = $fileConfig->getValue( "db_host" );
-                $dbname   = $fileConfig->getValue( "db_database" );
+                $username  = $fileConfig->getValue( "db_username" );
+                $password  = $fileConfig->getValue( "db_password" );
+                $host      = $fileConfig->getValue( "db_host" );
+                $dbname    = $fileConfig->getValue( "db_database" );
+                $dbcharset = $fileConfig->getValue( "db_character_set" );
                 $dbpersistent   = $fileConfig->getValue( "db_persistent" );
-              if($dbpersistent == true) {
-            	if( !$db->PConnect( $host, $username, $password, $dbname )) {
-            		die( "Fatal error: could not connect to the database!" );
+                if($dbpersistent == true) {
+	            	if( !$db->PConnect( $host, $username, $password, $dbname )) {
+	            		die( "Fatal error: could not connect to the database!" );
+	            	}
             	}
-            	}
             	else {
 	            	if( !$db->Connect( $host, $username, $password, $dbname )) {
 	            		throw( new Exception( "Fatal error: could not connect to the database!" ));
@@ -55,6 +56,11 @@
     	       	// by field number
         	   	//$db->SetFetchMode( ADODB_FETCH_ASSOC );       	
         	   	// comment out the line above if you're planning to test pdb
+	            if ( !empty( $dbcharset ) && $dbcharset != "default" )
+	            {
+	            	$sql = "SET NAMES ".$dbcharset;
+	            	$db->Execute( $sql );
+	            }
             }
             
             return $db;

Modified: plog/trunk/config/config.properties.php
===================================================================
--- plog/trunk/config/config.properties.php	2006-04-23 21:30:41 UTC (rev 3273)
+++ plog/trunk/config/config.properties.php	2006-04-24 03:44:47 UTC (rev 3274)
@@ -31,4 +31,5 @@
 $config["db_password"] = "";
 $config["db_database"] = "";
 $config["db_prefix"] = "";
+$config["db_character_set"] = "default";
 $config["db_persistent"] = true;

Modified: plog/trunk/templates/wizard/intro.template
===================================================================
--- plog/trunk/templates/wizard/intro.template	2006-04-23 21:30:41 UTC (rev 3273)
+++ plog/trunk/templates/wizard/intro.template	2006-04-24 03:44:47 UTC (rev 3274)
@@ -48,8 +48,8 @@
    <div class="field">
     <label for="dbName">Database name</label>
     <span class="required">*</span> 
+    <div class="formHelp">Name of the database where tables will be created</div>
     <input style="width:100%" type="text" id="dbName" name="dbName" value="{$dbName}"/>
-    <div class="formHelp">Name of the database where tables will be created</div>
     {include file="wizard/validate.template" field=dbName message="Database name is missing or incorrect"}    
    </div> 
    

Modified: plog/trunk/templates/wizard/step1.template
===================================================================
--- plog/trunk/templates/wizard/step1.template	2006-04-23 21:30:41 UTC (rev 3273)
+++ plog/trunk/templates/wizard/step1.template	2006-04-24 03:44:47 UTC (rev 3274)
@@ -16,35 +16,51 @@
   {/if} 
     <div class="field">
      <label for="dbServer">Database server</label>
-     <div class="fieldHelp"></div>
+     <div class="formHelp"></div>
      <span id="dbServer">{$dbServer}</span><br/>
     </div>
     <div class="field">
      <label for="dbUser">Database user</label>
-     <div class="fieldHelp"></div>     
+     <div class="formHelp"></div>     
      <span id="dbServer">{$dbUser}</span><br/>
     </div>
     <div class="field"> 
      <label for="dbPassword">Database password</label>
-     <div class="fieldHelp"></div>     
+     <div class="formHelp"></div>     
      <span id="dbPassword">{$dbPassword|stars}</span><br/>
     </div>
     <div class="field"> 
      <label for="dbName">Database table</label>
-     <div class="fieldHelp"></div>     
+     <div class="formHelp"></div>     
      <span idb="dbName">{$dbName}</span><br/>
     </div>
     <div class="field">
      <label for="dbPrefix">Database prefix</label>
-     <div class="fieldHelp"></div>     
+     <div class="formHelp"></div>     
      <span id="dbPrefix">{$dbPrefix}</span><br/>
     </div>
+
+    {if !empty($availableCharacterSets)}
+	    <div class="field">
+	     	<label for="dbCharacterSet">Database default character set</label>
+     		<span class="required"></span>
+	     	<div class="formHelp">If you plan to use UTF-8 as your default web encoding, please select <strong>utf8</strong> as your database default character set.</div>     
+	    	<select name="dbCharacterSet" id="dbCharacterSet" size="1" style="width:20%">
+	        <option value="default">default</option>
+            {foreach from=$availableCharacterSets item=availableCharacterSet}
+	        	<option value="{$availableCharacterSet}" {if $availableCharacterSet == $defaultCharacterSet} selected="selected" {/if}>{$availableCharacterSet}</option>
+	        {/foreach}
+         </select> 
+        </div> 
+    {/if}
+    
     <div class="field">
      <label for="createDatabase">Create database</label>
-     <div class="fieldHelp"></div>     
      <span class="required"></span>
-     <div class="fieldHelp"><input type="checkbox" name="createDatabase" id="createDatabase" class="checkbox" value="1" />Please check this if you want the installer to create the database.</div>
-    </div> 
+     <div class="formHelp"></div>
+     <div class="field"><input type="checkbox" name="createDatabase" id="createDatabase" class="checkbox" value="1" {if $createDatabase} checked="checked"  {/if}/>Please check this if you want the installer to create the database.</div>
+    </div>
+    
  </fieldset>
  <div class="buttons"> 
   <input type="button" name="Back" value="&laquo; Back" onclick="javascript:history.go(-1);" /> 

Modified: plog/trunk/wizard.php
===================================================================
--- plog/trunk/wizard.php	2006-04-23 21:30:41 UTC (rev 3273)
+++ plog/trunk/wizard.php	2006-04-24 03:44:47 UTC (rev 3274)
@@ -106,6 +106,13 @@
             $res = $db->Connect($config->getValue( "db_host" ), $config->getValue( "db_username" ), $config->getValue( "db_password" ));
         }
 
+        $dbcharset = $config->getValue( "db_character_set" );
+        if ( !empty( $dbcharset ) && $dbcharset != "default" )
+        {
+           	$sql = "SET NAMES ".$dbcharset;
+           	$db->Execute( $sql );
+        }
+
         if( DB_WIZARD_DEBUG )
             $db->debug = true;
 
@@ -537,6 +544,7 @@
         var $_dbPassword;
         var $_dbName;
         var $_dbPrefix;
+        var $_connection;
 
         function WizardStepOne( $actionInfo, $request )
         {
@@ -614,15 +622,97 @@
                 return( false );
             }
             else {
-                $this->_view = new WizardView( "step1" );
+                $connectionEsablished = false;
 
-                // now we better read the information from the config file to make sure that
-                // it has been correctly saved
-                $this->setCommonData( true );
+                $this->_connection = @mysql_connect( $this->_dbServer, $this->_dbUser, $this->_dbPassword );
+                if( $this->_connection ) {
+                    $connectionEsablished = true;
+                } else {
+                    $connectionEsablished = false;
+                    $message = "There was an error connecting to the database. Please check your settings.";
+                }
 
-                return true;
+                if ( !$connectionEsablished ) {
+                    $this->_view = new WizardView( "step1" );
+                    $this->_view->setErrorMessage( $message );
+                    $this->setCommonData( true );
+                    return false;
+                } else {
+                	$this->_view = new WizardView( "step1" );
+	                $availableCharacterSets = $this->getAvailableCharacterSets();
+	                $defaultCharacterSet = $this->getDatabaseCharacterSet();
+	                $createDatabase = false;
+	                if( empty( $defaultCharacterSet ) )
+	                {
+	                	$defaultCharacterSet = $this->getServerCharacterSet();
+	                	$createDatabase = true;
+	                }
+	                $this->_view->setValue( "availableCharacterSets", $availableCharacterSets );
+	                $this->_view->setValue( "defaultCharacterSet", $defaultCharacterSet );
+	                $this->_view->setValue( "createDatabase", $createDatabase );
+	                // now we better read the information from the config file to make sure that
+	                // it has been correctly saved
+	                $this->setCommonData( true );
+	                return true;
+	            }
             }
         }
+
+	    function getAvailableCharacterSets()
+	    {
+	        // check mysql version first. Version lower than 4.1 doesn't support utf8
+	        $serverVersion = mysql_get_server_info( $this->_connection );
+	        $version = explode('.', $serverVersion);
+	        if ($version[0] < 4) return false;
+	        if ( ($version[0] == 4) && ($version[1] == 0) ) return false;
+	        
+	        // check if utf8 support was compiled in
+	        $result = mysql_query( "SHOW CHARACTER SET", $this->_connection );
+	        if (mysql_num_rows($result) > 0) {
+	            // iterate through resultset
+	            $availableCharacterSets = array();
+	            while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
+	            {
+					array_push( $availableCharacterSets, $row['Charset'] );
+	            }
+	            return $availableCharacterSets;
+	
+	        }
+	        return false;
+	    }
+	    
+	    function getDatabaseCharacterSet()
+	    {
+			if (!@mysql_select_db( $this->_dbName, $this->_connection )) {
+				return false;
+			}
+	        
+	        // We use a SHOW CREATE DATABASE command to show the original
+	        // SQL character set when DB was created.
+	        $result = mysql_query("SHOW CREATE DATABASE `".$this->_dbName."`", $this->_connection);
+	        if (mysql_num_rows($result) < 0 ) {
+	            // The specified db name is wrong!
+	            return false;
+	        }
+	        $dbInfo = mysql_fetch_row($result);
+	        $pattern = '/40100 DEFAULT CHARACTER SET (\w+) /';
+	        if ( (preg_match($pattern, $dbInfo[1], $match) > 0) ) {
+	            return $match[1];
+	        }
+	        return false;
+	    }
+	
+	    function getServerCharacterSet(){
+	        // We use a SHOW CREATE DATABASE command to show the original
+	        // SQL character set when DB was created.
+	        $result = mysql_query("SHOW VARIABLES LIKE 'character_set_server'", $this->_connection);
+	        if (mysql_num_rows($result) > 0 ) {
+	            $row = mysql_fetch_array( $result, MYSQL_ASSOC );
+	            return $row['Value'];
+	        }
+	
+	        return false;
+	    }
     }
 
     /**
@@ -635,6 +725,7 @@
 
         var $_db;
         var $_database;
+        var $_dbCharacterSet;
         var $_createDatabase;
 
         function setDbConfigValues( &$view )
@@ -646,6 +737,7 @@
             $view->setValue( "dbServer", $configFile->getValue( "db_host" ));
             $view->setValue( "dbName", $configFile->getValue( "db_database" ));
             $view->setValue( "dbPrefix", $configFile->getValue( "db_prefix" ));
+            $view->setValue( "dbCharacterSet", $configFile->getValue( "db_character_set" ));
             return true;
         }
 
@@ -654,6 +746,30 @@
             global $Tables;
             global $Inserts;
 
+			$this->_dbCharacterSet = $this->_request->getValue( "dbCharacterSet" );
+            $configFile = new ConfigFileStorage();
+			$configFileName = $configFile->getConfigFileName();
+            
+            if( File::exists( $configFileName ) && !File::isWritable( $configFileName )) {
+                $this->_view = new WizardView( "step1" );
+                $message = "Please make sure that the file $configFileName can be written by this script during
+                            the installation process. It is needed to store the database configuration settings. Once the
+                            installation is complete, please revert the permissions to no writing possible.";
+                $this->_view->setErrorMessage( $message );
+                $this->setCommonData( true );
+                return false;
+            }
+
+            // continue if everything went fine
+            if( !$configFile->saveValue( "db_character_set", $this->_dbCharacterSet ) ) {
+                $message = "Could not save values to the configuration file. Please make sure it is available and
+                            that has write permissions for the user under your web server is running.";
+                $this->_view = new WizardView( "step1" );
+                $this->_view->setErrorMessage( $message );
+
+                return false;
+            }
+            
             $createDb = $this->_request->getValue( "createDatabase" );
             $message = '';
 



More information about the pLog-svn mailing list