CVS update: /cowiki/htdocs/setup/class/task/migrator/

[email protected] 6 May 2005 02:16:21 -0000
Newsgroups gmane.comp.php.cowiki.cvs
Message-ID <[email protected]>
User: dgorski 
Date: 2005/05/05 19:16:21

Modified:
   cowiki/htdocs/setup/class/task/migrator/class.Migrator_0.php

Log:
 Simplified

File Changes:

Directory: /cowiki/htdocs/setup/class/task/migrator/
====================================================

File [changed]: class.Migrator_0.php
Url: http://cowiki.tigris.org/source/browse/cowiki/htdocs/setup/class/task/migrator/class.Migrator_0.php?r1=1.2&r2=1.3
Delta lines:  +67 -192
----------------------
--- class.Migrator_0.php	29 Apr 2005 00:49:15 -0000	1.2
+++ class.Migrator_0.php	6 May 2005 02:16:18 -0000	1.3
@@ -2,7 +2,7 @@
 
 /**
  *
- * $Id: class.Migrator_0.php,v 1.2 2005/04/29 00:49:15 dgorski Exp $
+ * $Id: class.Migrator_0.php,v 1.3 2005/05/06 02:16:18 dgorski Exp $
  *
  * This file is part of coWiki. coWiki is free software under the terms of
  * the GNU General Public License (GPL). Read the LICENSE file. If you did
@@ -17,7 +17,7 @@
  * @author      Daniel T. Gorski, <[email protected]>
  * @copyright   (C) Daniel T. Gorski, {@link http://www.develnet.org}
  * @license     http://www.gnu.org/licenses/gpl.html
- * @version     $Revision: 1.2 $
+ * @version     $Revision: 1.3 $
  *
  */
 
@@ -62,20 +62,32 @@
 
     public function perform() {
 
-        // Read, create database connector user and schema
-        switch ($this->State->getDocStorageScheme()) {
+        // Connect with root credentials and dispatch
+        if ($this->State->mustCreateNewConnectorUser()
+            || $this->State->mustCreateNewDocumentDatabase()) {
+
+            // Connect with root user
+            $sResource = $this->State->getDocumentStorageRootDSN();
+
+            $Storage = StorageFactory::getInstance()
+                          ->createDocumentStorage($sResource);
+
+            // Dispatch due to the different access schemes
+            $UriInfo = new UriInfo($sResource);
+
+            switch ($UriInfo->get('scheme')) {
             case 'mysql':
             case 'mysql+innodb':
-                $this->createMySqlScheme('mysql');
-                break;
-
             case 'mysqli':
             case 'mysqli+innodb':
-                $this->createMySqlScheme('mysqli');
+                    $this->createMySqlScheme($Storage);
                 break;
 
             default:
-                throw new MigratorException('Unsupported storage container.');
+                    throw new MigratorException(
+                        'Unsupported access scheme: '.$UriInfo->get('scheme')
+                    );
+            }
         }
 
         return true;
@@ -83,125 +95,55 @@
 
     // --------------------------------------------------------------------
 
-    // We have to differentiate between "mysql" and "mysqli" here. Both
-    // access variants are possible.
-    private function createMySqlScheme($sScheme) {
-
-        $sRtUsr = $this->State->getDocStorageAdminUser();
-        $sRtPass = $this->State->getDocStorageAdminPassword();
-
-        $sUser = $this->State->getDocStorageConnectorUser();
-        $sPass = $this->State->getDocStorageConnectorPassword();
+    private function createMySqlScheme($Storage) {
 
-        $sHost = $this->State->getDocHost();
-        $sPort = $this->State->getDocHostPort();
-        if ($sPort != '') { $sHost = $sHost . ':' . $sPort; }
-
-        $sDatabase = $this->State->getDocDatabaseName();
+        // Connector user data (not root user!)
+        $sResource = $this->State->getDocumentStorageDSN();
+        $UriInfo = new UriInfo($sResource);
+
+        $sUser = $UriInfo->get('user');
+        $sPass = $UriInfo->get('pass');
+        $sHost = $UriInfo->get('host');
+        $sDB   = $UriInfo->get('basepath');
 
         // If necessary, create a connector user
         if ($this->State->mustCreateNewConnectorUser()) {
 
-            // MYSQL - Try to connect to with given *root user* credentials
-            if ($sScheme == 'mysql') {
-                if (!($rLink = @mysql_connect($sHost, $sRtUsr, $sRtPass))) {
-                    throw new MigratorException(
-                        'MySQL connection error: '.mysql_error()
-                    );
-                }
-            }
-
-            // MYSQLi - Try to connect to with given *root user* credentials
-            if ($sScheme == 'mysqli') {
-                if (!($rLink = @mysqli_connect($sHost, $sRtUsr, $sRtPass))) {
-                    throw new MigratorException(
-                        'MySQLi connection error: '.mysqli_connect_error()
-                    );
-                }
-            }
-
-            // ---
-
             // Insert new user
             $sQuery = " INSERT INTO mysql.user
-                           SET Host     = '".$sHost."',
-                               User     = '".$sUser."',
+                           SET host     = '".$sHost."',
+                               user     = '".$sUser."',
                                password = PASSWORD('".$sPass."')";
 
-            // MYSQL
-            if ($sScheme == 'mysql') {
-                if (!@mysql_query($sQuery, $rLink)) {
-                    // Swallow, user already exists
-                }
-            }
-
-            // MYSQLi
-            if ($sScheme == 'mysqli') {
-                if (!@mysqli_query($rLink, $sQuery)) {
-                   // Swallow, user already exists
-                }
-            }
+            $Storage->query($sQuery);
 
             // ---
 
             // Define database and set privileges
             $sQuery = " INSERT INTO mysql.db
-                           SET Host                   = '".$sHost."',
-                               Db                     = '".$sDatabase."',
-                               User                   = '".$sUser."',
-                               Select_priv            = 'Y',
-                               Insert_priv            = 'Y',
-                               Update_priv            = 'Y',
-                               Delete_priv            = 'Y',
-                               Create_priv            = 'Y',
-                               Drop_priv              = 'Y',
-                               Index_priv             = 'Y',
-                               Alter_priv             = 'Y',
-                               Create_tmp_table_priv  = 'Y',
-                               Lock_tables_priv       = 'Y'";
-
-            // MYSQL
-            if ($sScheme == 'mysql') {
-                if (!@mysql_query($sQuery, $rLink)) {
-                    // Swallow
-                }
-
-                // Reload MySQL / Flush grant tables
-                if (!@mysql_query('FLUSH PRIVILEGES', $rLink)) {
-                    throw new MigratorException(
-                        'MySQL error: '.mysql_error()
-                    );
-                }
-
-                @mysql_close();
-
-                // Done
-                $this->State->setMustCreateNewConnectorUser(false);
-            }
-
-            // MYSQLi
-            if ($sScheme == 'mysqli') {
-                if (!@mysqli_query($rLink, $sQuery)) {
-                    // Swallow
-                }
-
-                // Reload MySQL / Flush grant tables
-                if (!@mysqli_query($rLink, 'FLUSH PRIVILEGES')) {
-                    throw new MigratorException(
-                        'MySQLi error: '.mysqli_error($rLink)
-                    );
-                }
+                           SET host                   = '".$sHost."',
+                               db                     = '".$sDB."',
+                               user                   = '".$sUser."',
+                               select_priv            = 'Y',
+                               insert_priv            = 'Y',
+                               update_priv            = 'Y',
+                               delete_priv            = 'Y',
+                               create_priv            = 'Y',
+                               drop_priv              = 'Y',
+                               index_priv             = 'Y',
+                               alter_priv             = 'Y',
+                               create_tmp_table_priv  = 'Y',
+                               lock_tables_priv       = 'Y'";
 
-                @mysqli_close($rLink);
+            $Storage->query($sQuery);
 
                 // Done
                 $this->State->setMustCreateNewConnectorUser(false);
-            }
+            $Storage->reload();
         }
 
-        // ---
+        // ----------------------------------------------------------------
 
-        // If necessary, create scheme
         if ($this->State->mustCreateNewDocumentDatabase()) {
 
             $sPath = $this->State->getMiscPath();
@@ -232,83 +174,16 @@
 
             // ---
 
-            // MYSQL - Try to connect to with given *connector user*
-            // credentials
-            if ($sScheme == 'mysql') {
-
-                if (!($rLink = @mysql_connect($sHost, $sUser, $sPass))) {
-                    throw new MigratorException(
-                        'MySQL error: '.mysql_error()
-                    );
-                }
-
-                // Create database
-                if (!@mysql_query('CREATE DATABASE '.$sDatabase, $rLink)) {
-                    throw new MigratorException(
-                        'MySQL error: '.mysql_error()
-                    );
-                }
-
-                // Select the database
-                if (!@mysql_select_db($sDatabase, $rLink)) {
-                    throw new MigratorException(
-                        'MySQL error: '.mysql_error()
-                    );
-                }
-
-                // Create schema
-                foreach ($aContent as $sQuery) {
-                    if (!@mysql_query($sQuery, $rLink)) {
-                        throw new MigratorException(
-                            'MySQL error: '.mysql_error()
-                        );
-                    }
-                }
-
-                @mysql_close();
+            $Storage->query("CREATE DATABASE ".$sDB);
+            $Storage->useDatabase($sDB);
 
-                // Done
-                $this->State->setMustCreateNewDocumentDatabase(false);
-            }
-
-            // MYSQLi - Try to connect to with given *connector user*
-            // credentials
-            if ($sScheme == 'mysqli') {
-
-                if (!($rLink = @mysqli_connect($sHost, $sUser, $sPass))) {
-                    throw new MigratorException(
-                        'MySQLi error: '.mysqli_connect_error()
-                    );
-                }
-
-                // Create database
-                if (!@mysqli_query($rLink, 'CREATE DATABASE '.$sDatabase)) {
-                    throw new MigratorException(
-                        'MySQLi error: '.mysqli_error($rLink)
-                    );
-                }
-
-                // Select the database
-                if (!@mysqli_select_db($rLink, $sDatabase)) {
-                    throw new MigratorException(
-                        'MySQLi error: '.mysqli_error($rLink)
-                    );
-                }
-
-                // Create schema
+            // Create basic schema
                 foreach ($aContent as $sQuery) {
-                    if (!@mysqli_query($rLink, $sQuery)) {
-                        throw new MigratorException(
-                            'MySQLi error: '.mysqli_error($rLink)
-                        );
-                    }
+                $Storage->query($sQuery);
                 }
 
-                @mysqli_close($rLink);
-
                 // Done
                 $this->State->setMustCreateNewDocumentDatabase(false);
-            }
         }
     }