123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?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;
- error_log("\n".'------------ Migration->migrate function called ------------'."\n");
-
- $extra_fields = array();
-
- foreach ($matches as $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;
- }
-
- //Creating extra fields
- if (isset($table['extra_fields']) && in_array($table['dest_table'], array('course', 'user', 'session'))) {
- error_log('Inserting (if exists) extra fields for : ' . $table['dest_table']." \n");
-
- foreach ($table['extra_fields'] as $extra_field) {
- $options = $extra_field['options'];
- unset($extra_field['options']);
-
- $extra_field_obj = new ExtraField($table['dest_table']);
- $extra_field_id = $extra_field_obj->save($extra_field);
-
- $selected_fields = self::prepare_field_match($options);
- //error_log('$selected_fields: ' . print_r($selected_fields, 1));
-
- //Adding options
- if (!empty($options)) {
- $extra_field_option_obj = new ExtraFieldOption($table['dest_table']);
- $this->select_all($options['orig_table'], $selected_fields);
- $num_rows = $this->num_rows();
- if ($num_rows) {
- $data_to_insert = array();
- $data_to_insert['field_id'] = $extra_field_id;
- while ($row = $this->fetch_array()) {
- $data = self::execute_field_match($options, $row);
- $data_to_insert = array_merge($data_to_insert, $data);
- $extra_field_option_obj->save_one_item($data_to_insert, false, false);
- //error_log(print_r($extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']], 1));
- $extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['options'][] = $data_to_insert;
- $extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['field_type'] = $extra_field['field_type'];
- }
- //$extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']]['selected_option'] =
- //error_log('$data: ' . print_r($data_to_insert, 1));
- }
- } else {
- $extra_fields[$table['dest_table']]['extra_field_'.$extra_field['field_variable']] = $extra_field_id;
- }
- }
- }
-
- // Process the migration of fields from the given table
- $sql_select_fields = self::prepare_field_match($table);
- $this->select_all($table['orig_table'], $sql_select_fields, $table);
-
- if (count($table['fields_match']) == 0) {
- error_log('No fields found');
- continue;
- }
- $num_rows = $this->num_rows();
-
- if ($num_rows) {
- error_log('Records found: '.$num_rows);
- $item = 1;
- while ($row = $this->fetch_array()) {
- //error_log('Loading: ');error_log(print_r($row, 1));
- self::execute_field_match($table, $row, $extra_fields);
- $percentage = $item / $num_rows*100;
- if (round($percentage) % 10 == 0) {
- $percentage = round($percentage, 3);
- error_log("Processing item {$table['orig_table']} #$item $percentage%") ;
- }
- $item++;
- }
- error_log('Finished processing table ' . $table['orig_table']." \n\n");
- } else {
- error_log('No records found');
- }
-
- //Stop here (only for tests)
- if ($table['orig_table'] == 'Matricula') {
- exit;
- }
- }
- }
-
- function prepare_field_match($table) {
- $sql_select_fields = array();
- foreach ($table['fields_match'] as $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']]);
- }
- return $sql_select_fields;
- }
-
- function execute_field_match($table, $row, $extra_fields) {
- //error_log('execute_field_match');
- $dest_row = array();
- $first_field = '';
- $my_extra_fields = isset($extra_fields[$table['dest_table']]) ? $extra_fields[$table['dest_table']] : null;
- $extra_field_obj = null;
- $extra_field_value_obj = null;
-
- if (!empty($table['dest_table'])) {
- $extra_field_obj = new Extrafield($table['dest_table']);
- $extra_field_value_obj = new ExtraFieldValue($table['dest_table']);
- }
- $extra_fields_to_insert = array();
-
- foreach ($table['fields_match'] as $id_field => $details) {
- if ($id_field == 0) {
- $first_field = $details['dest'];
- }
-
- if (isset($details['orig'])) {
- $field_exploded = explode('.', $details['orig']);
- if (isset($field_exploded[1])) {
- $details['orig'] = $field_exploded[1];
- }
- }
-
- // process the fields one by one
- if ($details['func'] == 'none' || empty($details['func'])) {
- $dest_data = $row[$details['orig']];
- } else {
- $dest_data = MigrationCustom::$details['func']($row[$details['orig']], $this->data_list, $row);
- }
-
- if (isset($dest_row[$details['dest']])) {
- $dest_row[$details['dest']] .= ' '.$dest_data;
- } else {
- $dest_row[$details['dest']] = $dest_data;
- }
-
- //Extra field values
- $extra_field = isset($my_extra_fields) && isset($my_extra_fields[$details['dest']]) ? $my_extra_fields[$details['dest']] : null;
- //error_log('-----');
- //error_log(print_r($extra_field, 1));
- if (!empty($extra_field) && $extra_field_obj) {
- if (isset($extra_field['options'])) {
- $options = $extra_field['options'];
- $field_type = $extra_field['field_type'];
-
- if (!empty($options)) {
- foreach ($options as $option) {
- foreach ($option as $key => $value) {
- //error_log("$key $value --> {$dest_row[$details['dest']]} ");
- if ($key == 'option_value' && $value == $dest_row[$details['dest']]) {
- $value = $option['option_display_text'];
- if ($field_type == Extrafield::FIELD_TYPE_SELECT) {
- $value = $option['option_display_text'];
- }
- $params = array(
- 'field_id' => $option['field_id'],
- 'field_value' => $value,
- );
- break(2);
- }
- }
- }
- }
- } else {
- $params = array(
- 'field_id' => $extra_field,
- 'field_value' => $dest_row[$details['dest']],
- );
- }
- if (!empty($params)) {
- $extra_fields_to_insert[] = $params;
- }
- unset($dest_row[$details['dest']]);
- }
- }
-
- if (!empty($table['dest_func'])) {
- //error_log('Calling '.$table['dest_func'].' on data recovered: '.print_r($dest_row, 1));
- $dest_row['return_item_if_already_exists'] = true;
-
- $item_result = call_user_func_array($table['dest_func'], array($dest_row, $this->data_list));
-
- if ($table['show_in_error_log'] == false) {
-
- } else {
- error_log('Result of calling ' . $table['dest_func'] . ': ' . print_r($item_result, 1));
- }
- //error_log('Result of calling ' . $table['dest_func'] . ': ' . print_r($item_result, 1));
- //After the function was executed fill the $this->data_list array
- switch ($table['dest_table']) {
- case 'course':
- //Saving courses in array
- if ($item_result) {
- $this->data_list['courses'][$dest_row['uidIdCurso']] = $item_result;
- } else {
- error_log('Course Not FOUND');
- error_log(print_r($item_result, 1));
- exit;
- }
- $handler_id = $item_result['code'];
- break;
- case 'user':
- if (!empty($item_result)) {
- $handler_id = $item_result['user_id'];
- //error_log($dest_row['email'].' '.$dest_row['uidIdPersona']);
- if (isset($dest_row['uidIdAlumno'])) {
- $this->data_list['users_alumno'][$dest_row['uidIdAlumno']]['extra'] = $item_result;
- }
- if (isset($dest_row['uidIdEmpleado'])) {
- //print_r($dest_row['uidIdEmpleado']);exit;
- $this->data_list['users_empleado'][$dest_row['uidIdEmpleado']]['extra'] = $item_result;
- }
- } else {
- global $api_failureList;
- error_log(print_r($api_failureList, 1));
- }
- break;
- case 'session':
- $this->data_list['sessions'][$dest_row['uidIdPrograma']] = $item_result;
- $handler_id = $item_result; //session_id
- break;
- }
-
- //Saving extra fields of the element
- if (!empty($extra_fields_to_insert)) {
- foreach ($extra_fields_to_insert as $params) {
- $params[$extra_field_value_obj->handler_id] = $handler_id;
- $extra_field_value_obj->save($params);
- }
- }
- } else {
- $this->errors_stack[] = "No destination data dest_func found. Abandoning data with first field $first_field = " . $dest_row[$first_field];
- }
- return $dest_row;
- }
- }
|