Documentation is available at migration_tools.php
- <?php
- /**
- * Tools to migrate data,
- *
- */
- // --------------------------------------------------
- // FUNCTION migrate_data FROM TABLES
- // --------------------------------------------------
- /**
- * Get source and target params to migrate data
- * Main
- *
- * @param string $from_table source table name
- * @param string $to_table target table name
- * @param array $from_to_field field definition for above tables
- * @global array $_SESSION to get source and target database
- * @global string $config_encodage define character encoding
- * @global string $spip_auteurs_source name of source spip_auteurs table to check profil value
- * @global string $spip_auteurs_target name of target spip_auteurs table to put profil value
- */
- function migrate_data($from_table, $to_table, $from_to_field) {
- global $_SESSION, $config_encodage, $spip_auteurs_source, $spip_auteurs_target;
- $source_db = $_SESSION['source_database'];
- $target_db = $_SESSION['target_database'];
- $display_progression_bar = 1;
- print "<strong>Tranfert $source_db/$from_table → $target_db/$to_table</strong> <br />";
- // --------------------------------------------------
- // clean
- // --------------------------------------------------
- db_connection('target');
- clean_table($target_db, $to_table);
- // --------------------------------------------------
- // get data from previous
- // --------------------------------------------------
- db_connection('source');
- $qa = "select * from $from_table";
- process_sql($qa, $ra, $ta);
- if ($ta == 0) {
- print "Table vide <br />";
- }
- else {
- print "$ta fiche à transférer<br />";
- if ($display_progression_bar) {
- $progress_bar = new ProgressBar('En cours...');
- $progress_bar->initialize($ta); // print the empty bar
- }
- while ($data = mysql_fetch_array($ra)) {
- $i = 1;
- if ($display_progression_bar) {$progress_bar->increase($i);}
- $value_to_insert = $field_to_insert = '';
- foreach ($from_to_field as $previous => $current) {
- switch ($config_encodage) {
- case 'iso':
- $previous_value = utf8_decode(addslashes(stripslashes($data[$previous])));
- break;
- case 'idem':
- $previous_value = addslashes(stripslashes($data[$previous]));
- break;
- default:
- $previous_value = utf8_encode(addslashes(stripslashes($data[$previous])));
- break;
- }
- // ----------------------------------------
- // patch admin level agora
- // coded in 'minirezo', should be '0minirezo'
- // ----------------------------------------
- if (ereg('minirezo', $previous_value)) {
- $previous_value = '0minirezo';
- }
- // ----------------------------------------
- $value_to_insert .= "'$previous_value', ";
- $field_to_insert .= "$current, ";
- $i++;
- }
- // remove last ,
- $field_to_insert = substr($field_to_insert, 0, -2);
- $value_to_insert = substr($value_to_insert, 0, -2);
- // --------------------------------------------------
- // insert into current
- // --------------------------------------------------
- db_connection('target');
- $qb = "insert into $to_table ($field_to_insert)
- values ($value_to_insert)";
- process_sql($qb, $rb, $tb, $target_db);
- $i++;
- }
- // ----------------------------------------
- // apply auteur patch
- // ----------------------------------------
- if ($from_table == $spip_auteurs_source) {
- patch_auteurs_statut();
- }
- print "<br />$ta fiches transférées<br /><br /><hr>";
- }
- return $ta;
- }
- // --------------------------------------------------
- // CLEAN TABLE
- // --------------------------------------------------
- function clean_table($db, $table) {
- $q = "TRUNCATE TABLE $table";
- //print $q;
- $r = mysql_query($q);
- print "Destination <em>$db/$table</em> remise à vide<br>";
- }
- // ------------------------------
- // GO_TO
- // ------------------------------
- function go_to($page) {
- Header("Location:$page");
- }
- // --------------------------------------------------
- // remove primary key in spip
- // --------------------------------------------------
- function remove_primary_key($table) {
- db_connection('target');
- // --------------------------------------------------
- // get field info
- // --------------------------------------------------
- $query="SELECT * FROM $table";
- $result = mysql_query($query);
- $fields_count = mysql_num_fields($result);
- $j = 0;
- $can_alter = 0;
- while ($j < $fields_count) {
- $field_name[$j] = mysql_field_name($result, $j);
- $field_type[$j] = mysql_field_type($result, $j);
- $len[$j] = mysql_field_len($result, $j);
- $flags[$j] = mysql_field_flags($result, $j);
- if (eregi('primary', $flags[$j])) {$can_alter = 1;}
- $j++;
- }
- if ($can_alter) {
- $qpk = "ALTER TABLE $table DROP PRIMARY KEY";
- process_sql($qpk, $rpk, $tpk);
- }
- }
- // ----------------------------------------
- // function patch_auteurs_statut()
- // ----------------------------------------
- function patch_auteurs_statut() {
- global $statut_auteur, $spip_auteurs_source, $spip_auteurs_target;
- // ----------------------------------------
- // get spip_auteurs
- // ----------------------------------------
- db_connection('source');
- $qaut = "select * from $spip_auteurs_source";
- process_sql($qaut, $raut, $taut);
- while ($obj = mysql_fetch_object($raut)) {
- $id_auteur = $obj->id_auteur;
- $new_profil = $statut_auteur[$obj->profil];
- db_connection('target');
- $qup = "update $spip_auteurs_target
- set
- statut = '$new_profil'
- where
- id_auteur = '$id_auteur'
- ";
- process_sql($qup, $rup, $tup);
- }
- }
- // --------------------------------------------------
- // FUNCTION CHECKBOX_FROM_TABLE
- // assume code form is toto!jojo!baba
- // updated by Alain : Wed Oct 17 16:54:49 CEST 2007
- // --------------------------------------------------
- function checkbox_from_array (
- $array_data,
- $code_in_string,
- $form_name_string,
- $data_collector,
- $is_submit,
- $is_correct,
- $in_table = 0,
- $max_item_by_line = 0) {
- $display_debug = 0;
- $rstr = "";
- $ctr = 0;
- $liner = 0;
- $code_in_array = explode('!', $code_in_string);
- if ($in_table) {
- $rstr .= "<tr>\r";
- $rstr .= "<td valign=\"top\">\r";
- }
- foreach ($array_data as $table_id => $item_lib) {
- if (!$is_submit or ($is_submit and $is_correct)) {
- if (in_array($table_id ,$code_in_array)) {$checked = " checked";$start_strong = "<strong>";$end_strong = "</strong>";}
- else {$checked = "";$start_strong = "";$end_strong = "";}
- }
- else {
- if ($table_id == $data_collector[$ctr]) {$checked = " checked";$start_strong = "<strong>";$end_strong = "</strong>";}
- else {$checked = "";$start_strong = "";$end_strong = "";}
- }
- $rstr .= "<input type=\"checkbox\" id=\"$form_name_string" . "$ctr\" name=\"$form_name_string" . "[$ctr]\" value=\"$table_id\" $checked />\r";
- $rstr .= "$start_strong<label for=\"$form_name_string" . "$ctr\">$item_lib</label>$end_strong<br />\r";
- $ctr++;
- $liner++;
- if ($max_item_by_line == 0) {
- $max_item_by_line = floor($query_total / 2);
- }
- if ($in_table and $liner >= $max_item_by_line) {
- $liner = 0;
- $rstr .= "</td>\r";
- $rstr .= "<td valign=\"top\">\r";
- }
- }
- if ($display_debug) {
- print '<pre>CHECK transmitted values<br />';
- print_r($data_collector);
- print '</pre>';
- }
- return $rstr;
- }
- // --------------------------------------------------
- // USEFUL SUB FUNCTION
- // --------------------------------------------------
- function format_from_checkbox($string_from_db, $init_value_array) {
- $arrayt = explode('!', $string_from_db);
- for ($i = 0; $i < count ($arrayt); $i++) {
- $arrayt[$i] = $init_value_array["$arrayt[$i]"];
- }
- return implode(', ', $arrayt);
- }
- // ===== RADIO_FROM_ARRAY ===========================================
- // last new
- // ===============================================================
- function radio_from_array ($array, $name, $value, $ending_line = '', $array_label="") {
- $str_res = "";
- $value = stripslashes($value);
- foreach ($array as $arr_value => $arr_lib) {
- if ($value != "") {
- if ($value == $arr_value) {$is_checked = "checked";}
- else {$is_checked = "";}
- }
- if ($arr_value != '') {
- $str_res .= "<input type=\"radio\" name=\"$name\" value=\"$arr_value\"";
- $str_res .= "id=\"$arr_lib\" $is_checked>\r";
- $str_res .= "<label for=\"$arr_lib\">$arr_lib</label>$space_toprint\r";
- }
- $str_res .= $ending_line;
- }
- return $str_res;
- }
- // --------------------------------------------------
- // NB RECORD
- // --------------------------------------------------
- function nb_record($any_table) {
- $display_debug = 0;
- $write_debug = 0;
- $qc = "SELECT COUNT(*) FROM $any_table";
- process_sql($qc, $rc, $tc);
- // $rc = mysql_query($qc);
- $res = mysql_fetch_row($rc);
- if ($display_debug) {
- print "Nb record checker<br />";
- print "For table $any_table => ";
- print $res[0];
- print " records <br /> <br />";
- }
- return $res[0];
- }
- // --------------------------------------------------
- //
- // --------------------------------------------------
- function db_connection($what_host) {
- global $_SESSION;
- $display_debug = 0;
- $write_debug = 0;
- $result = array();
- @mysql_close();
- if ($display_debug) {print "connection for $what_host<br /><br />";}
- switch ($what_host) {
- case 'source':
- $host = 'localhost';
- $database = $_SESSION['source_database'];
- $user = $_SESSION['source_user'];
- $password = $_SESSION['source_pw'];
- $what = "source Agora $database";
- $connection = @mysql_connect($host, $user, $password);
- break;
- case 'target':
- $host = 'localhost';
- $database = $_SESSION['target_database'];
- $user = $_SESSION['target_user'];
- $password = $_SESSION['target_pw'];
- $what = "cible Spip $database";
- $connection = @mysql_connect($host, $user, $password);
- break;
- }
- if (!$connection) {
- $connection_error = "Pas de connexion à la $what : " . mysql_error();
- }
- else {
- $db_selection = @mysql_select_db($database, $connection);
- if (!$db_selection) {
- $db_selection_error = "Pas de connexion à la $what : " . mysql_error();
- }
- }
- $result['connection'] = $connection;
- $result['connection_error'] = $connection_error;
- $result['db_selection'] = $db_selection;
- $result['db_selection_error'] = $db_selection_error;
- if ($display_debug) {
- print '<pre>CHECK \n';
- print_r($_SESSION);
- print '</pre>';
- print "<b>db_start check</b><br />";
- print "Case $what_host<br /><br />";
- }
- return $result;
- }
- // --------------------------------------------------
- // get version
- // --------------------------------------------------
- function get_version($what_host, $what_db) {
- $display_debug = 0;
- $connect_db = db_connection($what_host);
- //$db_list = mysql_list_dbs();
- $table_list = mysql_list_tables($what_db);
- // detect _meta
- while ($table = mysql_fetch_row($table_list)) {
- if (ereg('meta', $table[0])) {
- $meta_table = $table[0];
- }
- if ($display_debug) {print $table[0] . "<br />";}
- }
- // get version
- $qver = "select valeur from $meta_table
- where nom like '%version%'";
- process_sql($qver, $rver, $tver);
- $meta_data = mysql_fetch_row($rver);
- return $meta_data[0];
- }
- // ----------------------------------------
- // format numbers
- // ----------------------------------------
- function fr_format_number($value) {
- return number_format($value, 0, ',', ' ');
- }
- // ----------------------------------------
- // convert sec -> HMS
- // ----------------------------------------
- function convert_hms($total_sec, $format = 'short') {
- $res = '';
- $days = floor($total_sec/86400 );
- $partdays = fmod($total_sec, 86400);
- $hours = floor($partdays/3600 );
- $parthours = fmod($partdays, 3600);
- $min = floor($parthours/60 );
- $sec = fmod($parthours, 60);
- if ($format == 'short') {
- switch ($days) {
- case 0: break;
- default: $res .= "$days j "; break;
- }
- switch ($hours) {
- case 0: break;
- default: $res .= "$hours h ";break;
- }
- switch ($min) {
- case 0: break;
- default: $res .= "$min min ";break;
- }
- switch ($sec) {
- case 0: break;
- default: $res .= "$sec s"; break;
- }
- }
- else {
- switch ($days) {
- case 0: break;
- case 1: $res .= "$days jour "; break;
- default: $res .= "$days jours "; break;
- }
- switch ($hours) {
- case 0: break;
- case 1: $res .= "$hours heure "; break;
- default: $res .= "$hours heures "; break;
- }
- switch ($min) {
- case 0: break;
- case 1: $res .= "$min minute "; break;
- default: $res .= "$min minutes "; break;
- }
- switch ($sec) {
- case 0: break;
- case 1: $res .= "$sec seconde"; break;
- default: $res .= "$sec secondes"; break;
- }
- }
- return $res;
- }
- ?>