Quellcode durchsuchen

Minor - cosmetic changes ctrl + alt + f in netbeans

Julio Montoya vor 12 Jahren
Ursprung
Commit
1e1d898a8b

+ 3 - 2
tests/migrate/config.dist.php

@@ -1,4 +1,5 @@
 <?php
+
 /**
  * This is the configuration file allowing you to connect to the origin
  * database. You should either fill this one in or generate your own
@@ -7,7 +8,7 @@
 /**
  * Define all connection variables
  */
-/* 
+/*
  * The database type allows you to define with database driver to use.
  * Currently allowed values are: mssql. Defaults to: mssql
  */
@@ -44,5 +45,5 @@ $db_name = 'master';
  * Load the real configuration file (except if we're already in config.php)
  */
 if ((basename(__FILE__) != 'config.php') && is_file('config.php')) {
-  include 'config.php'; 
+    include 'config.php';
 }

+ 10 - 9
tests/migrate/migrate.php

@@ -1,26 +1,27 @@
 <?php
+
 /**
  * Load required classes
  */
 require_once '../../main/inc/global.inc.php';
-require_once 'config.dist.php';
-if (is_file(dirname(__FILE__).'/migration.custom.class.php')) {
-  require_once 'migration.custom.class.php';
+require_once 'config.php';
+if (is_file(dirname(__FILE__) . '/migration.custom.class.php')) {
+    require_once 'migration.custom.class.php';
 } else {
-  die ("You need to define a custom migration class as migration.custom.class.php\n(copy it from migration.custom.class.dist.php, otherwise this migration process\n will not know what to do with the original database fields\n");
+    die("You need to define a custom migration class as migration.custom.class.php\n(copy it from migration.custom.class.dist.php, otherwise this migration process\n will not know what to do with the original database fields\n");
 }
 require_once 'migration.class.php';
 
 if (empty($db_type)) {
-  die("This script requires a DB type to work. Please update orig_db_conn.inc.php\n");
+    die("This script requires a DB type to work. Please update orig_db_conn.inc.php\n");
 }
-$file = dirname(__FILE__).'/migration.'.$db_type.'.class.php';
+$file = dirname(__FILE__) . '/migration.' . $db_type . '.class.php';
 if (!is_file($file)) {
-  die("Could not find db type file ".$file."\n");
+    die("Could not find db type file " . $file . "\n");
 }
 require_once $file;
-$class = 'Migration'.strtoupper($db_type);
-$m = new $class($db_host,$db_port,$db_user,$db_pass,$db_name);
+$class = 'Migration' . strtoupper($db_type);
+$m = new $class($db_host, $db_port, $db_user, $db_pass, $db_name);
 $m->connect();
 /**
  * Prepare the arrays of matches that will allow for the migration

+ 139 - 123
tests/migrate/migration.class.php

@@ -1,135 +1,151 @@
 <?php
+
 /**
  * Scipt defining the Migration class
  */
+
 /**
  * Migration class (ease the migration work). This class *must* be extended
  * in a database server-specific implementation as migration.[DB].class.php
  */
 class Migration {
-  /**
-   * Origin DB type holder
-   */
-  public $odbtype = '';
-  /**
-   * Origin DB host holder
-   */
-  public $odbhost = '';
-  /**
-   * Origin DB port holder
-   */
-  public $odbport = '';
-  /**
-   * Origin DB user holder
-   */
-  public $odbuser = '';
-  /**
-   * Origin DB password holder
-   */
-  public $odbpass = '';
-  /**
-   * Origin DB name holder
-   */
-  public $odbname = '';
-  /**
-   * Array holding all errors/warnings ocurring during one execution
-   */
-  public $errors_stack = array();
-  /**
-   * Temporary handler for SQL result
-   */
-  public $odbrows = null;
-  /**
-   * Temporary holder for the list of users, courses and sessions and their 
-   * data. Store values here (preferably using the same indexes as the
-   * destination database field names) until ready to insert into Chamilo.
-   */
-  public $data_list = array('users'=>array(), 'courses'=>array(), 'sessions'=>array());
-  /**
-   * The constructor assigns all database connection details to the migration
-   * object
-   * @param string The original database's host
-   * @param string The original database's port
-   * @param string The original database's user
-   * @param string The original database's password
-   * @param string The original database's name
-   * @return boolean False on error. Void on success.
-   */
-  public function __construct($dbhost, $dbport, $dbuser, $dbpass, $dbname) {
-    if (empty($dbhost) || empty($dbport) || empty($dbuser) || empty ($dbpass) || empty($dbname)) {
-      $this->errors_stack[] = 'All origin database params must be given. Received '.print_r(func_get_args(),1);
-      return false;
-    }
-    $this->odbtype = $dbtype;
-    $this->odbhost = $dbhost;
-    $this->odbport = $dbport;
-    $this->odbuser = $dbuser;
-    $this->odbpass = $dbpass;
-    $this->odbname = $dbname;
-  }
-  /**
-   * The connect method should be extended by the child class
-   */
-  public function connect() {
-    //extend in child class
-  }
-  /**
-   * The migrate method launches the migration process based on an array of
-   * tables and fields matches defined in the given array.
-   * @param array Structured array of matches (see migrate.php)
-   */
-  public function migrate($matches) {
-    $found = false;
-    $table_idx = -1;
-    foreach ($matches as $id => $table) {
-      error_log('Found table '.$table['orig_table']);
-      $build_only = false;
-      if (empty($table['dest_table'])) {
-        error_log(' ...which is just for data building');
-        $build_only = true;
-      }
-      // Process the migration of fields from the given table
-      $sql_select_fields = array();
-      foreach ($table['fields_match'] as $id_field => $details) {
-        if (empty($details['orig'])) {
-          //Ignore if the field declared in $matches doesn't exist in
-          // the original database
-          continue;
-        }
-        $sql_select_fields[$details['orig']] = $details['orig'];
-        // If there is something to alter in the SQL query, rewrite the entry
-        if (!empty($details['sql_alter'])) {
-          $func_alter = $details['sql_alter'];
-          $sql_select_fields[$details['orig']] = MigrationCustom::$func_alter($details['orig']);
-        }
-        error_log('  Found field '.$details['orig'].' to be selected as '.$sql_select_fields[$details['orig']]);
-      }
-      $this->select_all($table['orig_table'], $sql_select_fields);
-      if (count($table['fields_match']) == 0) {
-        error_log('No fields found');
-        continue;
-      }
-      while ($row = $this->fetch_row()) {
-        $dest_row = array();
-        $first_field = '';
-        foreach ($table['fields_match'] as $id_field => $details) {
-          if ($id_field == 0) { $first_field = $details['dest']; }
-          // process the fields one by one
-          if ($details['func'] == 'none') {
-            $dest_data = $row[$details['orig']];
-          } else {
-            $dest_data = MigrationCustom::$details['func']($row[$details['orig']],$this->data_list);
-          }
-          $dest_row[$details['dest']] = $dest_data;
+
+    /**
+     * Origin DB type holder
+     */
+    public $odbtype = '';
+
+    /**
+     * Origin DB host holder
+     */
+    public $odbhost = '';
+
+    /**
+     * Origin DB port holder
+     */
+    public $odbport = '';
+
+    /**
+     * Origin DB user holder
+     */
+    public $odbuser = '';
+
+    /**
+     * Origin DB password holder
+     */
+    public $odbpass = '';
+
+    /**
+     * Origin DB name holder
+     */
+    public $odbname = '';
+
+    /**
+     * Array holding all errors/warnings ocurring during one execution
+     */
+    public $errors_stack = array();
+
+    /**
+     * Temporary handler for SQL result
+     */
+    public $odbrows = null;
+
+    /**
+     * Temporary holder for the list of users, courses and sessions and their 
+     * data. Store values here (preferably using the same indexes as the
+     * destination database field names) until ready to insert into Chamilo.
+     */
+    public $data_list = array('users' => array(), 'courses' => array(), 'sessions' => array());
+
+    /**
+     * The constructor assigns all database connection details to the migration
+     * object
+     * @param string The original database's host
+     * @param string The original database's port
+     * @param string The original database's user
+     * @param string The original database's password
+     * @param string The original database's name
+     * @return boolean False on error. Void on success.
+     */
+    public function __construct($dbhost, $dbport, $dbuser, $dbpass, $dbname) {
+        if (empty($dbhost) || empty($dbport) || empty($dbuser) || empty($dbpass) || empty($dbname)) {
+            $this->errors_stack[] = 'All origin database params must be given. Received ' . print_r(func_get_args(), 1);
+            return false;
         }
-        if (!empty($table['dest_func'])) {
-          error_log('Calling MigrationCustom::'.$table['dest_func'].' on data recovered: '.print_r($dest_row,1));
-          //$table['dest_func']($dest_row);
-        } else {
-          $this->errors_stack[] = "No destination data dest_func found. Abandoning data with first field $first_field = ".$dest_row[$first_field];
+        $this->odbtype = $dbtype;
+        $this->odbhost = $dbhost;
+        $this->odbport = $dbport;
+        $this->odbuser = $dbuser;
+        $this->odbpass = $dbpass;
+        $this->odbname = $dbname;
+    }
+
+    /**
+     * The connect method should be extended by the child class
+     */
+    public function connect() {
+        //extend in child class
+    }
+
+    /**
+     * The migrate method launches the migration process based on an array of
+     * tables and fields matches defined in the given array.
+     * @param array Structured array of matches (see migrate.php)
+     */
+    public function migrate($matches) {
+        $found = false;
+        $table_idx = -1;
+        foreach ($matches as $id => $table) {
+            error_log('Found table ' . $table['orig_table']);
+            $build_only = false;
+            if (empty($table['dest_table'])) {
+                error_log(' ...which is just for data building');
+                $build_only = true;
+            }
+            // Process the migration of fields from the given table
+            $sql_select_fields = array();
+            foreach ($table['fields_match'] as $id_field => $details) {
+                if (empty($details['orig'])) {
+                    //Ignore if the field declared in $matches doesn't exist in
+                    // the original database
+                    continue;
+                }
+                $sql_select_fields[$details['orig']] = $details['orig'];
+                // If there is something to alter in the SQL query, rewrite the entry
+                if (!empty($details['sql_alter'])) {
+                    $func_alter = $details['sql_alter'];
+                    $sql_select_fields[$details['orig']] = MigrationCustom::$func_alter($details['orig']);
+                }
+                error_log('  Found field ' . $details['orig'] . ' to be selected as ' . $sql_select_fields[$details['orig']]);
+            }
+            $this->select_all($table['orig_table'], $sql_select_fields);
+            if (count($table['fields_match']) == 0) {
+                error_log('No fields found');
+                continue;
+            }
+            while ($row = $this->fetch_row()) {
+                $dest_row = array();
+                $first_field = '';
+                foreach ($table['fields_match'] as $id_field => $details) {
+                    if ($id_field == 0) {
+                        $first_field = $details['dest'];
+                    }
+                    // process the fields one by one
+                    if ($details['func'] == 'none') {
+                        $dest_data = $row[$details['orig']];
+                    } else {
+                        $dest_data = MigrationCustom::$details['func']($row[$details['orig']], $this->data_list);
+                    }
+                    $dest_row[$details['dest']] = $dest_data;
+                }
+                if (!empty($table['dest_func'])) {
+                    error_log('Calling MigrationCustom::' . $table['dest_func'] . ' on data recovered: ' . print_r($dest_row, 1));
+                    //$table['dest_func']($dest_row);
+                } else {
+                    $this->errors_stack[] = "No destination data dest_func found. Abandoning data with first field $first_field = " . $dest_row[$first_field];
+                }
+            }
+            error_log('Finished processing table ' . $table['orig_table']);
         }
-      }
-      error_log('Finished processing table '.$table['orig_table']);
     }
-  }
-}
+}

+ 43 - 36
tests/migrate/migration.custom.class.php

@@ -1,47 +1,54 @@
 <?php
+
 /**
  * This file contains the MigrationCustom class, which defines methods to
  * alter the data from the original database when importing it to the Chamilo
  * database
  */
+
 /**
  * The custom migration class allows you to define rules to process data
  * during the migration
  */
 class MigrationCustom {
-  /**
-   * The only required method is the 'none' method, which will not trigger
-   * any process at all
-   * @param mixed Data
-   * @param mixed Unaltered data
-   */
-  public function none($data) {
-    return $data;
-  }
-  /**
-   * Transform the uid identifiers from MSSQL to a string
-   * @param string Field name
-   * @return string SQL select string to include in the final select
-   */
-  public function sql_alter_unhash_50($field) {
-    return 'cast('.$field.' as varchar(50))';
-  }
-  /**
-   * Log data from the original users table
-   */
-  public function log_original_user_unique_id(&$omigrate, $data) {
-    $omigrate['users'][$data] = 0;
-  }
-  /**
-   * Log data from the original users table
-   */
-  public function log_original_course_unique_id(&$omigrate, $data) {
-    $omigrate['courses'][$data] = 0;
-  }
-  /**
-   * Log data from the original users table
-   */
-  public function log_original_session_unique_id(&$omigrate, $data) {
-    $omigrate['sessions'][$data] = 0;
-  }
-}
+
+    /**
+     * The only required method is the 'none' method, which will not trigger
+     * any process at all
+     * @param mixed Data
+     * @param mixed Unaltered data
+     */
+    public function none($data) {
+        return $data;
+    }
+
+    /**
+     * Transform the uid identifiers from MSSQL to a string
+     * @param string Field name
+     * @return string SQL select string to include in the final select
+     */
+    public function sql_alter_unhash_50($field) {
+        return 'cast(' . $field . ' as varchar(50))';
+    }
+
+    /**
+     * Log data from the original users table
+     */
+    public function log_original_user_unique_id(&$omigrate, $data) {
+        $omigrate['users'][$data] = 0;
+    }
+
+    /**
+     * Log data from the original users table
+     */
+    public function log_original_course_unique_id(&$omigrate, $data) {
+        $omigrate['courses'][$data] = 0;
+    }
+
+    /**
+     * Log data from the original users table
+     */
+    public function log_original_session_unique_id(&$omigrate, $data) {
+        $omigrate['sessions'][$data] = 0;
+    }
+}

+ 33 - 27
tests/migrate/migration.mssql.class.php

@@ -1,35 +1,41 @@
 <?php
+
 /**
  *
  */
 class MigrationMSSQL extends Migration {
-  public function __construct($dbhost, $dbport='1433', $dbuser, $dbpass, $dbname) {
-    parent::__construct($dbhost, $dbport, $dbuser, $dbpass, $dbname);
-    ini_set('display_errors',1);
-    ini_set('mssql.datetimeconvert',0);
-    $this->odbtype = 'mssql';
-  }
-  public function connect() {
-    $this->c = mssql_connect($this->odbhost,$this->odbuser,$this->odbpass,TRUE);
-    if ($this->c === false) {
-      $this->errors_stack[] = 'Could not connect. MSSQL error: '.mssql_get_last_message();
-      return false;
+
+    public function __construct($dbhost, $dbport = '1433', $dbuser, $dbpass, $dbname) {
+        parent::__construct($dbhost, $dbport, $dbuser, $dbpass, $dbname);
+        ini_set('display_errors', 1);
+        ini_set('mssql.datetimeconvert', 0);
+        $this->odbtype = 'mssql';
     }
-    mssql_select_db($dbname,$this->c);
-    return true;
-  }
-  public function select_all($table, $fields) {
-    $fields_sql = '';
-    foreach ($fields as $field) {
-      $fields_sql .= $field.', ';
+
+    public function connect() {
+        $this->c = mssql_connect($this->odbhost, $this->odbuser, $this->odbpass, TRUE);
+        if ($this->c === false) {
+            $this->errors_stack[] = 'Could not connect. MSSQL error: ' . mssql_get_last_message();
+            return false;
+        }
+        mssql_select_db($dbname, $this->c);
+        return true;
     }
-    $fields_sql = substr($fields_sql,0,-2);
-    $sql = 'SELECT '.$fields_sql.' FROM '.$table;
-    //remove
-    error_log($sql);
-    $this->rows_iterator = mssql_query($sql,$this->c);
-  }
-  public function fetch_row() {
-    return mssql_fetch_row($this->rows_iterator);
-  }
+
+    public function select_all($table, $fields) {
+        $fields_sql = '';
+        foreach ($fields as $field) {
+            $fields_sql .= $field . ', ';
+        }
+        $fields_sql = substr($fields_sql, 0, -2);
+        $sql = 'SELECT ' . $fields_sql . ' FROM ' . $table;
+        //remove
+        error_log($sql);
+        $this->rows_iterator = mssql_query($sql, $this->c);
+    }
+
+    public function fetch_row() {
+        return mssql_fetch_row($this->rows_iterator);
+    }
+
 }