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

Oscar Renalias oscar at renalias.net
Mon Apr 24 05:11:22 GMT 2006


Mark,

this looks very good to me but I think the configuration of the
character encoding for the database is in the wrong class
(db.class.php is definitely not the right place)

As I suggested yesterday, this is what I would do:

1) Change the PDbDriverBase::Connect and PDbDriverBase::PConnect
methods to accept a new parameter, the character encoding (defaulting
to "do nothing"):

  function Connect( $host, $username, $password, $dbname = null,
$charset = null )

2) In the body of Connect and PConnect, then the driver should set the
character set according to the database that it implements so the
PDbMySQLDriver class should send the query "SET NAMES ..."

3) Since the Db class is the only one that has access to the
configuration file, it is the class that should provide the database
driver with the right encoding:

                $username = $fileConfig->getValue( "db_username" );
                $password = $fileConfig->getValue( "db_password" );
                $host     = $fileConfig->getValue( "db_host" );
                $dbname   = $fileConfig->getValue( "db_database" );
                $dbpersistent   = $fileConfig->getValue( "db_persistent" );
                $charset = $fileConfig->getValue( "db_character_set" );
              if($dbpersistent == true) {
            	if( !$db->PConnect( $host, $username, $password, $dbname,
$charset )) {
            		die( "Fatal error: could not connect to the database!" );
            	}
            	}
            	else {
	            	if( !$db->Connect( $host, $username, $password, $dbname,
$charset )) {
	            		throw( new Exception( "Fatal error: could not connect
to the database!" ));
	                	die();
	            	}
            	}

I think this should work.

Oscar

On 4/24/06, Mark Wu <markplace at gmail.com> wrote:
> Hi Oscar:
>
> It is a terrible commit, it takes 40 minuts to commit this change ...
> Weird.... :( Are you generating the testig data now?
>
> BTW, here comes the changes that I made:
>
> 1. I change the wizard to check the mysql charset (only availiable for mysql
> 4.1+ )
>
> - I will check the db that user assigned is exist or not, if yes, I will use
> the encoding from the database information
>
> - If the db, user assign is not exist, I will use the character_set_server
> that user specifiy in their mysql installation.
>
> - If both of these information does not exist, I will use the "default"
> encoding, that means "do nothing
>
> 2. I change the db.class.php
>
> - It will use the charset setting in the config file. If the
> db_character_set does not exist or eqaul to "default", it will do nothing.
>
> - Or, it will execute "SET NAMES $encoding" in every connection.
>
> 3. I also enchance the wizard. It will change the db user assigned is exist
> or not. It not, it will check the "create database" in default ..
>
> ** Especially take a look at db.class.php, I put the  "SET NAMES $encoding"
> query there.... I think it is not a good place ... But, it it the only one
> place that I think can do that kind of change. (Because Pdb::Driver does not
> check the config values..)
>
> So, maybe we need a new config value: db_type, and we can specify "mysql" in
> default ..
>
> So, the code, could change to :
>
> If ($db_type == "mysql" ) {
>    blah blah; ...
> }
>
> Mark
>
> > -----Original Message-----
> > From: plog-svn-bounces at devel.lifetype.net
> > [mailto:plog-svn-bounces at devel.lifetype.net] On Behalf Of
> > mark at devel.lifetype.net
> > Sent: Monday, April 24, 2006 11:57 AM
> > To: plog-svn at devel.lifetype.net
> > Subject: [pLog-svn] r3274 - in plog/trunk: . class/config
> > class/databaseconfig templates/wizard
> >
> > 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 = '';
> >
> >
> > _______________________________________________
> > pLog-svn mailing list
> > pLog-svn at devel.lifetype.net
> > http://devel.lifetype.net/mailman/listinfo/plog-svn
>
> _______________________________________________
> pLog-svn mailing list
> pLog-svn at devel.lifetype.net
> http://devel.lifetype.net/mailman/listinfo/plog-svn
>


More information about the pLog-svn mailing list