Source for file migration_tools.php

Documentation is available at migration_tools.php

  1. <?php
  2. /**
  3. * Tools to migrate data,
  4. *
  5. */
  6.  
  7.  
  8.  
  9. // --------------------------------------------------
  10. // FUNCTION migrate_data FROM TABLES
  11. // --------------------------------------------------
  12.  
  13. /**
  14. * Get source and target params to migrate data
  15. * Main
  16. *
  17. * @param string $from_table source table name
  18. * @param string $to_table target table name
  19. * @param array $from_to_field field definition for above tables
  20. * @global array $_SESSION to get source and target database
  21. * @global string $config_encodage define character encoding
  22. * @global string $spip_auteurs_source name of source spip_auteurs table to check profil value
  23. * @global string $spip_auteurs_target name of target spip_auteurs table to put profil value
  24. */
  25. function migrate_data($from_table, $to_table, $from_to_field) {
  26. global $_SESSION, $config_encodage, $spip_auteurs_source, $spip_auteurs_target;
  27. $source_db = $_SESSION['source_database'];
  28. $target_db = $_SESSION['target_database'];
  29. $display_progression_bar = 1;
  30.  
  31. print "<strong>Tranfert $source_db/$from_table &rarr; $target_db/$to_table</strong> <br />";
  32.  
  33. // --------------------------------------------------
  34. // clean
  35. // --------------------------------------------------
  36. db_connection('target');
  37. clean_table($target_db, $to_table);
  38.  
  39. // --------------------------------------------------
  40. // get data from previous
  41. // --------------------------------------------------
  42. db_connection('source');
  43. $qa = "select * from $from_table";
  44. process_sql($qa, $ra, $ta);
  45.  
  46. if ($ta == 0) {
  47. print "Table vide <br />";
  48. }
  49.  
  50. else {
  51. print "$ta fiche à transférer<br />";
  52. if ($display_progression_bar) {
  53. $progress_bar = new ProgressBar('En cours...');
  54. $progress_bar->initialize($ta); // print the empty bar
  55. }
  56.  
  57. while ($data = mysql_fetch_array($ra)) {
  58. $i = 1;
  59. if ($display_progression_bar) {$progress_bar->increase($i);}
  60. $value_to_insert = $field_to_insert = '';
  61.  
  62. foreach ($from_to_field as $previous => $current) {
  63.  
  64. switch ($config_encodage) {
  65. case 'iso':
  66. $previous_value = utf8_decode(addslashes(stripslashes($data[$previous])));
  67. break;
  68.  
  69. case 'idem':
  70. $previous_value = addslashes(stripslashes($data[$previous]));
  71. break;
  72.  
  73. default:
  74. $previous_value = utf8_encode(addslashes(stripslashes($data[$previous])));
  75. break;
  76. }
  77.  
  78. // ----------------------------------------
  79. // patch admin level agora
  80. // coded in 'minirezo', should be '0minirezo'
  81. // ----------------------------------------
  82. if (ereg('minirezo', $previous_value)) {
  83. $previous_value = '0minirezo';
  84. }
  85.  
  86. // ----------------------------------------
  87.  
  88. $value_to_insert .= "'$previous_value', ";
  89. $field_to_insert .= "$current, ";
  90. $i++;
  91. }
  92.  
  93. // remove last ,
  94. $field_to_insert = substr($field_to_insert, 0, -2);
  95. $value_to_insert = substr($value_to_insert, 0, -2);
  96.  
  97. // --------------------------------------------------
  98. // insert into current
  99. // --------------------------------------------------
  100. db_connection('target');
  101. $qb = "insert into $to_table ($field_to_insert)
  102. values ($value_to_insert)";
  103.  
  104. process_sql($qb, $rb, $tb, $target_db);
  105. $i++;
  106. }
  107. // ----------------------------------------
  108. // apply auteur patch
  109. // ----------------------------------------
  110. if ($from_table == $spip_auteurs_source) {
  111. patch_auteurs_statut();
  112. }
  113. print "<br />$ta fiches transférées<br /><br /><hr>";
  114. }
  115. return $ta;
  116. }
  117.  
  118.  
  119. // --------------------------------------------------
  120. // CLEAN TABLE
  121. // --------------------------------------------------
  122.  
  123. function clean_table($db, $table) {
  124. $q = "TRUNCATE TABLE $table";
  125. //print $q;
  126. $r = mysql_query($q);
  127. print "Destination <em>$db/$table</em> remise à vide<br>";
  128. }
  129.  
  130. // ------------------------------
  131. // GO_TO
  132. // ------------------------------
  133.  
  134. function go_to($page) {
  135. Header("Location:$page");
  136. }
  137.  
  138. // --------------------------------------------------
  139. // remove primary key in spip
  140. // --------------------------------------------------
  141.  
  142. function remove_primary_key($table) {
  143. db_connection('target');
  144.  
  145. // --------------------------------------------------
  146. // get field info
  147. // --------------------------------------------------
  148. $query="SELECT * FROM $table";
  149. $result = mysql_query($query);
  150. $fields_count = mysql_num_fields($result);
  151.  
  152. $j = 0;
  153. $can_alter = 0;
  154. while ($j < $fields_count) {
  155. $field_name[$j] = mysql_field_name($result, $j);
  156. $field_type[$j] = mysql_field_type($result, $j);
  157. $len[$j] = mysql_field_len($result, $j);
  158. $flags[$j] = mysql_field_flags($result, $j);
  159. if (eregi('primary', $flags[$j])) {$can_alter = 1;}
  160. $j++;
  161. }
  162.  
  163. if ($can_alter) {
  164.  
  165. $qpk = "ALTER TABLE $table DROP PRIMARY KEY";
  166. process_sql($qpk, $rpk, $tpk);
  167. }
  168. }
  169.  
  170. // ----------------------------------------
  171. // function patch_auteurs_statut()
  172. // ----------------------------------------
  173.  
  174. function patch_auteurs_statut() {
  175. global $statut_auteur, $spip_auteurs_source, $spip_auteurs_target;
  176.  
  177. // ----------------------------------------
  178. // get spip_auteurs
  179. // ----------------------------------------
  180. db_connection('source');
  181. $qaut = "select * from $spip_auteurs_source";
  182. process_sql($qaut, $raut, $taut);
  183.  
  184. while ($obj = mysql_fetch_object($raut)) {
  185. $id_auteur = $obj->id_auteur;
  186. $new_profil = $statut_auteur[$obj->profil];
  187.  
  188. db_connection('target');
  189. $qup = "update $spip_auteurs_target
  190. set
  191. statut = '$new_profil'
  192. where
  193. id_auteur = '$id_auteur'
  194. ";
  195. process_sql($qup, $rup, $tup);
  196. }
  197. }
  198.  
  199. // --------------------------------------------------
  200. // FUNCTION CHECKBOX_FROM_TABLE
  201. // assume code form is toto!jojo!baba
  202. // updated by Alain : Wed Oct 17 16:54:49 CEST 2007
  203. // --------------------------------------------------
  204.  
  205. function checkbox_from_array (
  206. $array_data,
  207. $code_in_string,
  208. $form_name_string,
  209. $data_collector,
  210. $is_submit,
  211. $is_correct,
  212. $in_table = 0,
  213. $max_item_by_line = 0) {
  214.  
  215. $display_debug = 0;
  216. $rstr = "";
  217. $ctr = 0;
  218. $liner = 0;
  219.  
  220. $code_in_array = explode('!', $code_in_string);
  221.  
  222. if ($in_table) {
  223. $rstr .= "<tr>\r";
  224. $rstr .= "<td valign=\"top\">\r";
  225. }
  226.  
  227. foreach ($array_data as $table_id => $item_lib) {
  228.  
  229. if (!$is_submit or ($is_submit and $is_correct)) {
  230. if (in_array($table_id ,$code_in_array)) {$checked = " checked";$start_strong = "<strong>";$end_strong = "</strong>";}
  231. else {$checked = "";$start_strong = "";$end_strong = "";}
  232. }
  233. else {
  234. if ($table_id == $data_collector[$ctr]) {$checked = " checked";$start_strong = "<strong>";$end_strong = "</strong>";}
  235. else {$checked = "";$start_strong = "";$end_strong = "";}
  236. }
  237.  
  238. $rstr .= "<input type=\"checkbox\" id=\"$form_name_string" . "$ctr\" name=\"$form_name_string" . "[$ctr]\" value=\"$table_id\" $checked />\r";
  239. $rstr .= "$start_strong<label for=\"$form_name_string" . "$ctr\">$item_lib</label>$end_strong<br />\r";
  240.  
  241. $ctr++;
  242. $liner++;
  243.  
  244. if ($max_item_by_line == 0) {
  245. $max_item_by_line = floor($query_total / 2);
  246. }
  247.  
  248. if ($in_table and $liner >= $max_item_by_line) {
  249. $liner = 0;
  250. $rstr .= "</td>\r";
  251. $rstr .= "<td valign=\"top\">\r";
  252. }
  253. }
  254.  
  255. if ($display_debug) {
  256. print '<pre>CHECK transmitted values<br />';
  257. print_r($data_collector);
  258. print '</pre>';
  259. }
  260.  
  261. return $rstr;
  262. }
  263.  
  264. // --------------------------------------------------
  265. // USEFUL SUB FUNCTION
  266. // --------------------------------------------------
  267.  
  268. function format_from_checkbox($string_from_db, $init_value_array) {
  269. $arrayt = explode('!', $string_from_db);
  270.  
  271. for ($i = 0; $i < count ($arrayt); $i++) {
  272. $arrayt[$i] = $init_value_array["$arrayt[$i]"];
  273. }
  274. return implode(', ', $arrayt);
  275. }
  276.  
  277.  
  278. // ===== RADIO_FROM_ARRAY ===========================================
  279. // last new
  280. // ===============================================================
  281.  
  282. function radio_from_array ($array, $name, $value, $ending_line = '', $array_label="") {
  283. $str_res = "";
  284. $value = stripslashes($value);
  285.  
  286. foreach ($array as $arr_value => $arr_lib) {
  287. if ($value != "") {
  288. if ($value == $arr_value) {$is_checked = "checked";}
  289. else {$is_checked = "";}
  290. }
  291. if ($arr_value != '') {
  292. $str_res .= "<input type=\"radio\" name=\"$name\" value=\"$arr_value\"";
  293. $str_res .= "id=\"$arr_lib\" $is_checked>\r";
  294. $str_res .= "<label for=\"$arr_lib\">$arr_lib</label>$space_toprint\r";
  295. }
  296. $str_res .= $ending_line;
  297. }
  298.  
  299. return $str_res;
  300. }
  301.  
  302. // --------------------------------------------------
  303. // NB RECORD
  304. // --------------------------------------------------
  305.  
  306. function nb_record($any_table) {
  307. $display_debug = 0;
  308. $write_debug = 0;
  309.  
  310. $qc = "SELECT COUNT(*) FROM $any_table";
  311. process_sql($qc, $rc, $tc);
  312. // $rc = mysql_query($qc);
  313. $res = mysql_fetch_row($rc);
  314.  
  315.  
  316. if ($display_debug) {
  317. print "Nb record checker<br />";
  318. print "For table $any_table => ";
  319. print $res[0];
  320. print " records <br /> <br />";
  321. }
  322. return $res[0];
  323. }
  324.  
  325. // --------------------------------------------------
  326. //
  327. // --------------------------------------------------
  328.  
  329. function db_connection($what_host) {
  330. global $_SESSION;
  331.  
  332. $display_debug = 0;
  333. $write_debug = 0;
  334. $result = array();
  335. @mysql_close();
  336.  
  337. if ($display_debug) {print "connection for $what_host<br /><br />";}
  338.  
  339. switch ($what_host) {
  340. case 'source':
  341. $host = 'localhost';
  342. $database = $_SESSION['source_database'];
  343. $user = $_SESSION['source_user'];
  344. $password = $_SESSION['source_pw'];
  345. $what = "source Agora $database";
  346. $connection = @mysql_connect($host, $user, $password);
  347. break;
  348.  
  349. case 'target':
  350. $host = 'localhost';
  351. $database = $_SESSION['target_database'];
  352. $user = $_SESSION['target_user'];
  353. $password = $_SESSION['target_pw'];
  354. $what = "cible Spip $database";
  355. $connection = @mysql_connect($host, $user, $password);
  356.  
  357. break;
  358. }
  359.  
  360. if (!$connection) {
  361. $connection_error = "Pas de connexion à la $what : " . mysql_error();
  362. }
  363. else {
  364. $db_selection = @mysql_select_db($database, $connection);
  365. if (!$db_selection) {
  366. $db_selection_error = "Pas de connexion à la $what : " . mysql_error();
  367. }
  368. }
  369.  
  370. $result['connection'] = $connection;
  371. $result['connection_error'] = $connection_error;
  372. $result['db_selection'] = $db_selection;
  373. $result['db_selection_error'] = $db_selection_error;
  374.  
  375. if ($display_debug) {
  376. print '<pre>CHECK \n';
  377. print_r($_SESSION);
  378. print '</pre>';
  379. print "<b>db_start check</b><br />";
  380. print "Case $what_host<br /><br />";
  381. }
  382.  
  383. return $result;
  384. }
  385.  
  386. // --------------------------------------------------
  387. // get version
  388. // --------------------------------------------------
  389.  
  390. function get_version($what_host, $what_db) {
  391.  
  392. $display_debug = 0;
  393.  
  394. $connect_db = db_connection($what_host);
  395. //$db_list = mysql_list_dbs();
  396. $table_list = mysql_list_tables($what_db);
  397.  
  398. // detect _meta
  399. while ($table = mysql_fetch_row($table_list)) {
  400. if (ereg('meta', $table[0])) {
  401. $meta_table = $table[0];
  402. }
  403.  
  404. if ($display_debug) {print $table[0] . "<br />";}
  405. }
  406.  
  407. // get version
  408. $qver = "select valeur from $meta_table
  409. where nom like '%version%'";
  410.  
  411. process_sql($qver, $rver, $tver);
  412. $meta_data = mysql_fetch_row($rver);
  413. return $meta_data[0];
  414. }
  415.  
  416.  
  417. // ----------------------------------------
  418. // format numbers
  419. // ----------------------------------------
  420.  
  421. function fr_format_number($value) {
  422. return number_format($value, 0, ',', ' ');
  423. }
  424.  
  425.  
  426. // ----------------------------------------
  427. // convert sec -> HMS
  428. // ----------------------------------------
  429.  
  430. function convert_hms($total_sec, $format = 'short') {
  431. $res = '';
  432. $days = floor($total_sec/86400 );
  433. $partdays = fmod($total_sec, 86400);
  434. $hours = floor($partdays/3600 );
  435. $parthours = fmod($partdays, 3600);
  436. $min = floor($parthours/60 );
  437. $sec = fmod($parthours, 60);
  438.  
  439. if ($format == 'short') {
  440. switch ($days) {
  441. case 0: break;
  442. default: $res .= "$days j "; break;
  443. }
  444.  
  445. switch ($hours) {
  446. case 0: break;
  447. default: $res .= "$hours h ";break;
  448. }
  449.  
  450. switch ($min) {
  451. case 0: break;
  452. default: $res .= "$min min ";break;
  453. }
  454.  
  455. switch ($sec) {
  456. case 0: break;
  457. default: $res .= "$sec s"; break;
  458. }
  459. }
  460. else {
  461. switch ($days) {
  462. case 0: break;
  463. case 1: $res .= "$days jour "; break;
  464. default: $res .= "$days jours "; break;
  465. }
  466.  
  467. switch ($hours) {
  468. case 0: break;
  469. case 1: $res .= "$hours heure "; break;
  470. default: $res .= "$hours heures "; break;
  471. }
  472.  
  473. switch ($min) {
  474. case 0: break;
  475. case 1: $res .= "$min minute "; break;
  476. default: $res .= "$min minutes "; break;
  477. }
  478.  
  479. switch ($sec) {
  480. case 0: break;
  481. case 1: $res .= "$sec seconde"; break;
  482. default: $res .= "$sec secondes"; break;
  483. }
  484. }
  485.  
  486. return $res;
  487. }
  488.  
  489. ?>

Documentation generated on Mon, 31 Mar 2008 18:08:51 +0200 by phpDocumentor 1.3.0RC3