3 /* vim: set expandtab sw=4 ts=4 sts=4: */
12 require_once './libraries/common.inc.php';
15 * Does the common work
17 $GLOBALS['js_include'][] = 'server_synchronize.js';
18 require_once './libraries/server_common.inc.php';
21 * Contains all the functions specific to synchronization
23 require './libraries/server_synchronize.lib.php';
26 * Increases the time limit up to the configured maximum
28 @set_time_limit
($cfg['ExecTimeLimit']);
33 require './libraries/server_links.inc.php';
36 * Enables warnings on the page
38 //$cfg['Error_Handler']['display'] = true;
39 //$cfg['Error_Handler']['gather'] = true;
42 * Save the value of token generated for this page
44 if (isset($_REQUEST['token'])) {
45 $_SESSION['token'] = $_REQUEST['token'];
48 // variable for code saving
49 $cons = array ("src", "trg");
52 * Displays the page when 'Go' is pressed
55 if ((isset($_REQUEST['submit_connect']))) {
56 foreach ($cons as $con) {
57 $
{"{$con}_host"} = $_REQUEST[$con . '_host'];
58 $
{"{$con}_username"} = $_REQUEST[$con . '_username'];
59 $
{"{$con}_password"} = $_REQUEST[$con . '_pass'];
60 $
{"{$con}_port"} = $_REQUEST[$con . '_port'];
61 $
{"{$con}_socket"} = $_REQUEST[$con . '_socket'];
62 $
{"{$con}_db"} = $_REQUEST[$con . '_db'];
63 $
{"{$con}_type"} = $_REQUEST[$con . '_type'];
65 if ($
{"{$con}_type"} == 'cur') {
66 $
{"{$con}_connection"} = null;
67 $
{"{$con}_server"} = null;
68 $
{"{$con}_db"} = $_REQUEST[$con . '_db_sel'];
72 if (isset($
{"{$con}_socket"}) && ! empty($
{"{$con}_socket"})) {
73 $
{"{$con}_server"}['socket'] = $
{"{$con}_socket"};
75 $
{"{$con}_server"}['host'] = $
{"{$con}_host"};
76 if (isset($
{"{$con}_port"}) && ! empty($
{"{$con}_port"}) && ((int)$
{"{$con}_port"} * 1) > 0) {
77 $
{"{$con}_server"}['port'] = (int)$
{"{$con}_port"};
81 $
{"{$con}_connection"} = PMA_DBI_connect($
{"{$con}_username"}, $
{"{$con}_password"}, $is_controluser = false, $
{"{$con}_server"}, $auxiliary_connection = true);
82 } // end foreach ($cons as $con)
84 if ((! $src_connection && $src_type != 'cur') ||
(! $trg_connection && $trg_type != 'cur')) {
86 * Displays the connection error string if
87 * connections are not established
90 echo '<div class="error">';
91 if (! $src_connection && $src_type != 'cur') {
92 echo __('Could not connect to the source') . '<br />';
94 if (! $trg_connection && $trg_type != 'cur') {
95 echo __('Could not connect to the target');
98 unset($_REQUEST['submit_connect']);
102 * Creating the link object for both source and target databases and
103 * selecting the source and target databases using these links
105 foreach ($cons as $con) {
106 if ($
{"{$con}_connection"} != null) {
107 $
{"{$con}_link"} = PMA_DBI_connect($
{"{$con}_username"}, $
{"{$con}_password"}, $is_controluser = false, $
{"{$con}_server"});
109 $
{"{$con}_link"} = null;
111 $
{"{$con}_db_selected"} = PMA_DBI_select_db($
{"{$con}_db"}, $
{"{$con}_link"});
112 } // end foreach ($cons as $con)
114 if (($src_db_selected != 1) ||
($trg_db_selected != 1)) {
116 * Displays error string if the database(s) did not exist
118 echo '<div class="error">';
119 if ($src_db_selected != 1) {
120 echo sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($src_db));
122 if ($trg_db_selected != 1) {
123 echo sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($trg_db));
126 unset($_REQUEST['submit_connect']);
128 } else if (($src_db_selected == 1) && ($trg_db_selected == 1)) {
131 * Using PMA_DBI_get_tables() to get all the tables
132 * from target and source databases.
134 $src_tables = PMA_DBI_get_tables($src_db, $src_link);
135 $source_tables_num = sizeof($src_tables);
137 $trg_tables = PMA_DBI_get_tables($trg_db, $trg_link);
138 $target_tables_num = sizeof($trg_tables);
141 * initializing arrays to save matching and non-matching
142 * table names from target and source databases.
144 $unmatched_num_src = 0;
145 $source_tables_uncommon = array();
146 $unmatched_num_trg = 0;
147 $target_tables_uncommon = array();
148 $matching_tables = array();
149 $matching_tables_num = 0;
152 * Using PMA_getMatchingTables to find which of the tables' names match
153 * in target and source database.
155 PMA_getMatchingTables($trg_tables, $src_tables, $matching_tables, $source_tables_uncommon);
157 * Finding the uncommon tables for the target database
158 * using function PMA_getNonMatchingTargetTables()
160 PMA_getNonMatchingTargetTables($trg_tables, $matching_tables, $target_tables_uncommon);
163 * Initializing several arrays to save the data and structure
164 * difference between the source and target databases.
166 $row_count = array(); //number of rows in source table that needs to be created in target database
167 $fields_num = array(); //number of fields in each matching table
168 $delete_array = array(); //stores the primary key values for target tables that have excessive rows than corresponding source tables.
169 $insert_array = array(array(array()));// stores the primary key values for the rows in each source table that are not present in target tables.
170 $update_array = array(array(array())); //stores the primary key values, name of field to be updated, value of the field to be updated for
171 // each row of matching table.
172 $matching_tables_fields = array(); //contains the fields' names for each matching table
173 $matching_tables_keys = array(); //contains the primary keys' names for each matching table
174 $uncommon_tables_fields = array(); //coantains the fields for all the source tables that are not present in target
175 $matching_tables_num = sizeof($matching_tables);
177 $source_columns = array(); //contains the full columns' information for all the source tables' columns
178 $target_columns = array(); //contains the full columns' information for all the target tables' columns
179 $uncommon_columns = array(); //contains names of columns present in source table but absent from the corresponding target table
180 $source_indexes = array(); //contains indexes on all the source tables
181 $target_indexes = array(); //contains indexes on all the target tables
182 $add_indexes_array = array(); //contains the indexes name present in source but absent from target tables
183 $target_tables_keys = array(); //contains the keys of all the target tables
184 $alter_indexes_array = array(); //contains the names of all the indexes for each table that need to be altered in target database
185 $remove_indexes_array = array(); //contains the names of indexes that are excessive in target tables
186 $alter_str_array = array(array()); //contains the criteria for each column that needs to be altered in target tables
187 $add_column_array = array(array()); //contains the name of columns that need to be added in target tables
189 * The criteria array contains all the criteria against which columns are compared for differences.
191 $criteria = array('Field', 'Type', 'Null', 'Collation', 'Key', 'Default', 'Comment');
193 for($i = 0; $i < sizeof($matching_tables); $i++
) {
195 * Finding out all the differences structure, data and index diff for all the matching tables only
197 PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $matching_tables_fields, $update_array, $insert_array,
198 $delete_array, $fields_num, $i, $matching_tables_keys);
200 PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns,
201 $target_columns, $alter_str_array, $add_column_array, $uncommon_columns, $criteria, $target_tables_keys, $i);
203 PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_indexes, $target_indexes,
204 $add_indexes_array, $alter_indexes_array, $remove_indexes_array, $i);
207 for($j = 0; $j < sizeof($source_tables_uncommon); $j++
) {
209 * Finding out the number of rows to be added in tables that need to be added in target database
211 PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $j, $row_count);
214 * Storing all arrays in session for use when page is reloaded for each button press
216 $_SESSION['matching_tables'] = $matching_tables;
217 $_SESSION['update_array'] = $update_array;
218 $_SESSION['insert_array'] = $insert_array;
219 $_SESSION['src_db'] = $src_db;
220 $_SESSION['trg_db'] = $trg_db;
221 $_SESSION['matching_fields'] = $matching_tables_fields;
222 $_SESSION['src_uncommon_tables'] = $source_tables_uncommon;
223 $_SESSION['src_username'] = $src_username ;
224 $_SESSION['trg_username'] = $trg_username;
225 $_SESSION['src_password'] = $src_password;
226 $_SESSION['trg_password'] = $trg_password;
227 $_SESSION['trg_password'] = $trg_password;
228 $_SESSION['src_server'] = $src_server;
229 $_SESSION['trg_server'] = $trg_server;
230 $_SESSION['src_type'] = $src_type;
231 $_SESSION['trg_type'] = $trg_type;
232 $_SESSION['matching_tables_keys'] = $matching_tables_keys;
233 $_SESSION['uncommon_tables_fields'] = $uncommon_tables_fields;
234 $_SESSION['uncommon_tables_row_count'] = $row_count;
235 $_SESSION['target_tables_uncommon'] = $target_tables_uncommon;
236 $_SESSION['uncommon_tables'] = $source_tables_uncommon;
237 $_SESSION['delete_array'] = $delete_array;
238 $_SESSION['uncommon_columns'] = $uncommon_columns;
239 $_SESSION['source_columns'] = $source_columns;
240 $_SESSION['alter_str_array'] = $alter_str_array;
241 $_SESSION['target_tables_keys'] = $target_tables_keys;
242 $_SESSION['add_column_array'] = $add_column_array;
243 $_SESSION['criteria'] = $criteria;
244 $_SESSION['target_tables'] = $trg_tables;
245 $_SESSION['add_indexes_array'] = $add_indexes_array;
246 $_SESSION['alter_indexes_array'] = $alter_indexes_array;
247 $_SESSION['remove_indexes_array'] = $remove_indexes_array;
248 $_SESSION['source_indexes'] = $source_indexes;
249 $_SESSION['target_indexes'] = $target_indexes;
252 * Displays the sub-heading and icons showing Structure Synchronization and Data Synchronization
255 <form name
="synchronize_form" id
="synchronize_form" method
="post" action
="server_synchronize.php">
256 <?php
PMA_generate_common_hidden_inputs('', ''); ?
>
260 <img
class="icon" src
="<?php echo $pmaThemeImage; ?>new_struct.png" width
="16" height
="16" alt
="" />
261 <?php
echo __('Structure Synchronization'); ?
>
264 <img
class="icon" src
="<?php echo $pmaThemeImage; ?>new_data.png" width
="16" height
="16" alt
="" />
265 <?php
echo __('Data Synchronization'); ?
>
271 * Displays the tables containing the source tables names, their difference with the target tables and target tables names
273 PMA_syncDisplayHeaderCompare($src_db, $trg_db);
277 * Display the matching tables' names and difference, first
279 for ($i = 0; $i < count($matching_tables); $i++
) {
281 * Calculating the number of updates for each matching table
283 if (isset($update_array[$i]) && isset($update_array[$i][0]) &&
284 isset($update_array[$i][0][$matching_tables_keys[$i][0]])) {
285 $num_of_updates = sizeof($update_array[$i]);
290 * Calculating the number of insertions for each matching table
292 if (isset($insert_array[$i]) && isset($insert_array[$i][0]) &&
293 isset($insert_array[$i][0][$matching_tables_keys[$i][0]])) {
294 $num_of_insertions = sizeof($insert_array[$i]);
296 $num_of_insertions = 0;
300 * Calculating the number of alter columns, number of columns to be added, number of columns to be removed,
301 * number of index to be added and removed.
304 $num_insert_cols = 0;
305 $num_remove_cols = 0;
307 $num_remove_index = 0;
309 if (isset($alter_str_array[$i])) {
310 $num_alter_cols = sizeof($alter_str_array[$i]);
312 if (isset($add_column_array[$i])) {
313 $num_insert_cols = sizeof($add_column_array[$i]);
315 if (isset($uncommon_columns[$i])) {
316 $num_remove_cols = sizeof($uncommon_columns[$i]);
318 if (isset($add_indexes_array[$i])) {
319 $num_add_index = sizeof($add_indexes_array[$i]);
321 if (isset($remove_indexes_array[$i])) {
322 $num_remove_index = sizeof($remove_indexes_array[$i]);
324 if (isset($alter_indexes_array[$i])) {
325 $num_add_index +
= sizeof($alter_indexes_array[$i]);
326 $num_remove_index +
= sizeof($alter_indexes_array[$i]);
329 $btn_structure_params = null;
330 $btn_data_params = null;
333 * Display the red button of structure synchronization if there exists any structure difference or index difference.
335 if (($num_alter_cols > 0) ||
($num_insert_cols > 0) ||
($num_remove_cols > 0) ||
($num_add_index > 0) ||
($num_remove_index > 0)) {
336 $btn_structure_params = array($i, $num_alter_cols, $num_insert_cols,
337 $num_remove_cols, $num_add_index, $num_remove_index);
341 * Display the green button of data synchronization if there exists any data difference.
343 if (isset($update_array[$i]) ||
isset($insert_array[$i])) {
344 if (isset($update_array[$i][0][$matching_tables_keys[$i][0]]) ||
isset($insert_array[$i][0][$matching_tables_keys[$i][0]])) {
345 $btn_data_params = array($i, $num_of_updates, $num_of_insertions, null, null, null);
350 'src_table_name' => $matching_tables[$i],
351 'dst_table_name' => $matching_tables[$i],
353 'btn_structure' => $btn_structure_params,
354 'btn_data' => $btn_data_params
358 * Displays the tables' names present in source but missing from target
360 for ($j = 0; $j < count($source_tables_uncommon); $j++
) {
362 'src_table_name' => '+ ' . $source_tables_uncommon[$j],
363 'dst_table_name' => $source_tables_uncommon[$j] . ' (' . __('not present') . ')',
365 'btn_structure' => array($j, null, null, null, null, null),
368 if ($row_count[$j] > 0) {
369 $row['btn_data'] = array($j, null, $row_count[$j], null, null, null);
373 foreach ($target_tables_uncommon as $tbl_nc_name) {
375 'src_table_name' => '',
376 'dst_table_name' => $tbl_nc_name);
379 * Displays the target tables names
381 PMA_syncDisplayDataCompare($rows);
387 * This "list" div will contain a table and each row will depict information about structure/data diffrence in tables.
388 * Rows will be generated dynamically as soon as the colored buttons "D" or "S" are clicked.
391 echo '<fieldset style="padding:0"><div id="list" style="overflow:auto; height:140px; padding:1em">
395 <tr style="width: 100%;">
396 <th id="table_name" style="width: 10%;" colspan="1">' . __('Table') . ' </th>
397 <th id="str_diff" style="width: 65%;" colspan="6">' . __('Structure Difference') . ' </th>
398 <th id="data_diff" style="width: 20%;" colspan="2">' . __('Data Difference') . '</th>
400 <tr style="width: 100%;">
401 <th style="width: 10%;">' . __('Table name') . '</th>
402 <th style="width: 10%;">' . __('Create table'). '</th>
403 <th style="width: 11%;">' . __('Add column(s)') . '</th>
404 <th style="width: 13%;">' . __('Remove column(s)') . '</th>
405 <th style="width: 11%;">' . __('Alter column(s)') . '</th>
406 <th style="width: 12%;">' . __('Remove index(s)') . '</th>
407 <th style="width: 11%;">' . __('Apply index(s)') . '</th>
408 <th style="width: 10%;">'. __('Update row(s)') . '</th>
409 <th style="width: 10%;">' . __('Insert row(s)') . '</th>
416 * This fieldset displays the checkbox to confirm deletion of previous rows from target tables
419 <p><input type= "checkbox" name="delete_rows" id ="delete_rows" /><label for="delete_rows">' . __('Would you like to delete all the previous rows from target tables?') . '</label> </p>
421 <fieldset class="tblFooters">';
422 echo '<input type="button" name="apply_changes" value="' . __('Apply Selected Changes')
423 . '" onclick ="ApplySelectedChanges(' . "'" . htmlspecialchars($_SESSION['token']) . "'" . ')" />';
424 echo '<input type="submit" name="synchronize_db" value="' . __('Synchronize Databases') . '" />' . '</fieldset>';
428 } // end if ((isset($_REQUEST['submit_connect'])))
431 * Display the page when 'Apply Selected Changes' is pressed
433 if (isset($_REQUEST['Table_ids'])) {
435 * Displays success message
437 echo '<div class="success">' . __('Selected target tables have been synchronized with source tables.') . '</div>';
439 $src_db = $_SESSION['src_db'];
440 $trg_db = $_SESSION['trg_db'];
441 $update_array = $_SESSION['update_array'];
442 $insert_array = $_SESSION['insert_array'];
443 $src_username = $_SESSION['src_username'];
444 $trg_username = $_SESSION['trg_username'];
445 $src_password = $_SESSION['src_password'];
446 $trg_password = $_SESSION['trg_password'];
447 $src_server = $_SESSION['src_server'];
448 $trg_server = $_SESSION['trg_server'];
449 $src_type = $_SESSION['src_type'];
450 $trg_type = $_SESSION['trg_type'];
451 $uncommon_tables = $_SESSION['uncommon_tables'];
452 $matching_tables = $_SESSION['matching_tables'];
453 $matching_tables_keys = $_SESSION['matching_tables_keys'];
454 $matching_tables_fields = $_SESSION['matching_fields'];
455 $source_tables_uncommon = $_SESSION['src_uncommon_tables'];
456 $uncommon_tables_fields = $_SESSION['uncommon_tables_fields'];
457 $target_tables_uncommon = $_SESSION['target_tables_uncommon'];
458 $row_count = $_SESSION['uncommon_tables_row_count'];
459 $target_tables = $_SESSION['target_tables'];
461 $delete_array = $_SESSION['delete_array'];
462 $uncommon_columns = $_SESSION['uncommon_columns'];
463 $source_columns = $_SESSION['source_columns'];
464 $alter_str_array = $_SESSION['alter_str_array'];
465 $criteria = $_SESSION['criteria'];
466 $target_tables_keys = $_SESSION['target_tables_keys'];
467 $add_column_array = $_SESSION['add_column_array'];
468 $add_indexes_array = $_SESSION['add_indexes_array'];
469 $alter_indexes_array = $_SESSION['alter_indexes_array'];
470 $remove_indexes_array = $_SESSION['remove_indexes_array'];
471 $source_indexes = $_SESSION['source_indexes'];
472 $target_indexes = $_SESSION['target_indexes'];
473 $uncommon_cols = $uncommon_columns;
476 * Creating link object for source and target databases
478 foreach ($cons as $con) {
479 if ($
{"{$con}_type"} != "cur") {
480 $
{"{$con}_link"} = PMA_DBI_connect($
{"{$con}_username"}, $
{"{$con}_password"}, $is_controluser = false, $
{"{$con}_server"});
482 $
{"{$con}_link"} = null;
483 // working on current server, so initialize this for tracking
484 // (does not work if user defined current server as a remote one)
485 $GLOBALS['db'] = $
{"{$con}_db"};
487 } // end foreach ($cons as $con)
490 * Initializing arrays to save the table ids whose data and structure difference is to be applied
492 $matching_table_data_diff = array(); //stores id of matching table having data difference
493 $matching_table_structure_diff = array(); //stores id of matching tables having structure difference
494 $uncommon_table_structure_diff = array(); //stores id of uncommon tables having structure difference
495 $uncommon_table_data_diff = array(); //stores id of uncommon tables having data difference
497 for ($i = 0; isset($_REQUEST[$i]); $i++
) {
498 if (isset($_REQUEST[$i])) {
499 $table_id = explode("US", $_REQUEST[$i]);
500 if (isset($table_id[1])) {
501 $uncommon_table_structure_diff[] = $table_id[1];
503 $table_id = explode("UD", $_REQUEST[$i]);
504 if (isset($table_id[1])) {
505 $uncommon_table_data_diff[] = $table_id[1];
507 $table_id = explode("MS", $_REQUEST[$i]);
508 if (isset($table_id[1])) {
509 $matching_table_structure_diff[] = $table_id[1];
512 $table_id = explode("MD", $_REQUEST[$i]);
513 if (isset($table_id[1])) {
514 $matching_table_data_diff[] = $table_id[1];
519 * Applying the structure difference on selected matching tables
521 for($q = 0; $q < sizeof($matching_table_structure_diff); $q++
)
523 if (isset($alter_str_array[$matching_table_structure_diff[$q]])) {
525 PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, $source_columns, $alter_str_array, $matching_tables_fields,
526 $criteria, $matching_tables_keys, $target_tables_keys, $matching_table_structure_diff[$q], false);
528 unset($alter_str_array[$matching_table_structure_diff[$q]]);
530 if (isset($add_column_array[$matching_table_structure_diff[$q]])) {
532 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $matching_table_structure_diff[$q], $target_tables_keys,
533 $matching_tables_keys, $trg_db, $trg_link, $src_db, $src_link);
535 if (isset($delete_array[$matching_table_structure_diff[$q]])) {
537 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $matching_table_structure_diff[$q], $target_tables_keys, $delete_array, false);
539 unset($delete_array[$matching_table_structure_diff[$q]]);
541 PMA_addColumnsInTargetTable($src_db, $trg_db,$src_link, $trg_link, $matching_tables, $source_columns, $add_column_array, $matching_tables_fields,
542 $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables,$uncommon_tables_fields, $matching_table_structure_diff[$q], $uncommon_cols, false);
544 unset($add_column_array[$matching_table_structure_diff[$q]]);
546 if (isset($uncommon_columns[$matching_table_structure_diff[$q]])) {
548 PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $matching_table_structure_diff[$q], false);
550 unset($uncommon_columns[$matching_table_structure_diff[$q]]);
552 if (isset($add_indexes_array[$matching_table_structure_diff[$q]]) ||
isset($remove_indexes_array[$matching_table_structure_diff[$q]])
553 ||
isset($alter_indexes_array[$matching_table_structure_diff[$q]])) {
555 PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
556 $remove_indexes_array, $matching_table_structure_diff[$q], false);
558 unset($add_indexes_array[$matching_table_structure_diff[$q]]);
559 unset($alter_indexes_array[$matching_table_structure_diff[$q]]);
560 unset($remove_indexes_array[$matching_table_structure_diff[$q]]);
564 * Applying the data difference. First checks if structure diff is applied or not.
565 * If not, then apply structure difference first then apply data difference.
567 for($p = 0; $p < sizeof($matching_table_data_diff); $p++
)
569 if ($_REQUEST['checked'] == 'true') {
571 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $matching_table_data_diff[$p], $target_tables_keys,
572 $matching_tables_keys, $trg_db, $trg_link, $src_db, $src_link);
574 if (isset($delete_array[$matching_table_data_diff[$p]])) {
576 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $matching_table_data_diff[$p], $target_tables_keys, $delete_array, false);
578 unset($delete_array[$matching_table_data_diff[$p]]);
581 if (isset($alter_str_array[$matching_table_data_diff[$p]])) {
583 PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, $source_columns, $alter_str_array, $matching_tables_fields,
584 $criteria, $matching_tables_keys, $target_tables_keys, $matching_table_data_diff[$p], false);
586 unset($alter_str_array[$matching_table_data_diff[$p]]);
588 if (isset($add_column_array[$matching_table_data_diff[$p]])) {
590 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $matching_table_data_diff[$p], $target_tables_keys,
591 $matching_tables_keys, $trg_db, $trg_link, $src_db, $src_link);
593 if (isset($delete_array[$matching_table_data_diff[$p]])) {
595 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $matching_table_data_diff[$p], $target_tables_keys, $delete_array, false);
597 unset($delete_array[$matching_table_data_diff[$p]]);
599 PMA_addColumnsInTargetTable($src_db, $trg_db,$src_link, $trg_link, $matching_tables, $source_columns, $add_column_array, $matching_tables_fields,
600 $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables, $uncommon_tables_fields, $matching_table_data_diff[$p], $uncommon_cols, false);
602 unset($add_column_array[$matching_table_data_diff[$p]]);
604 if (isset($uncommon_columns[$matching_table_data_diff[$p]])) {
606 PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $matching_table_data_diff[$p], false);
608 unset($uncommon_columns[$matching_table_data_diff[$p]]);
610 if ((isset($matching_table_structure_diff[$q]) && isset($add_indexes_array[$matching_table_structure_diff[$q]]))
611 ||
(isset($matching_table_structure_diff[$q]) && isset($remove_indexes_array[$matching_table_structure_diff[$q]]))
612 ||
(isset($matching_table_structure_diff[$q]) && isset($alter_indexes_array[$matching_table_structure_diff[$q]]))) {
614 PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
615 $remove_indexes_array, $matching_table_structure_diff[$q], false);
617 unset($add_indexes_array[$matching_table_structure_diff[$q]]);
618 unset($alter_indexes_array[$matching_table_structure_diff[$q]]);
619 unset($remove_indexes_array[$matching_table_structure_diff[$q]]);
622 * Applying the data difference.
624 PMA_updateTargetTables($matching_tables, $update_array, $src_db, $trg_db, $trg_link, $matching_table_data_diff[$p], $matching_tables_keys, false);
626 PMA_insertIntoTargetTable($matching_tables, $src_db, $trg_db, $src_link, $trg_link , $matching_tables_fields, $insert_array,
627 $matching_table_data_diff[$p], $matching_tables_keys, $source_columns, $add_column_array, $criteria, $target_tables_keys,
628 $uncommon_tables, $uncommon_tables_fields, $uncommon_cols, $alter_str_array, $source_indexes, $target_indexes, $add_indexes_array,
629 $alter_indexes_array, $delete_array, $update_array, false);
632 * Updating the session variables to the latest values of the arrays.
634 $_SESSION['delete_array'] = $delete_array;
635 $_SESSION['uncommon_columns'] = $uncommon_columns;
636 $_SESSION['alter_str_array'] = $alter_str_array;
637 $_SESSION['add_column_array'] = $add_column_array;
638 $_SESSION['add_indexes_array'] = $add_indexes_array;
639 $_SESSION['remove_indexes_array'] = $remove_indexes_array;
640 $_SESSION['insert_array'] = $insert_array;
641 $_SESSION['update_array'] = $update_array;
644 * Applying structure difference to selected non-matching tables (present in Source but absent from Target).
646 for($s = 0; $s < sizeof($uncommon_table_structure_diff); $s++
)
648 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $uncommon_tables, $uncommon_table_structure_diff[$s], $uncommon_tables_fields, false);
649 $_SESSION['uncommon_tables_fields'] = $uncommon_tables_fields;
651 unset($uncommon_tables[$uncommon_table_structure_diff[$s]]);
654 * Applying data difference to selected non-matching tables (present in Source but absent from Target).
655 * Before data synchronization, structure synchronization is confirmed.
657 for($r = 0; $r < sizeof($uncommon_table_data_diff); $r++
)
659 if (!(in_array($uncommon_table_data_diff[$r], $uncommon_table_structure_diff))) {
660 if (isset($uncommon_tables[$uncommon_table_data_diff[$r]])) {
662 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $uncommon_tables, $uncommon_table_data_diff[$r],
663 $uncommon_tables_fields, false);
664 $_SESSION['uncommon_tables_fields'] = $uncommon_tables_fields;
666 unset($uncommon_tables[$uncommon_table_data_diff[$r]]);
669 PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $uncommon_table_data_diff[$r],
670 $_SESSION['uncommon_tables_fields'], false);
672 unset($row_count[$uncommon_table_data_diff[$r]]);
675 * Again all the tables from source and target database are displayed with their differences.
676 * The differences have been removed from tables that have been synchronized
678 echo '<form name="applied_difference" id="synchronize_form" method="post" action="server_synchronize.php">'
679 . PMA_generate_common_hidden_inputs('', '');
681 PMA_syncDisplayHeaderCompare($src_db, $trg_db);
683 for($i = 0; $i < count($matching_tables); $i++
) {
685 $num_insert_cols = 0;
686 $num_remove_cols = 0;
688 $num_remove_index = 0;
690 if (isset($alter_str_array[$i])) {
691 $num_alter_cols = sizeof($alter_str_array[$i]);
693 if (isset($add_column_array[$i])) {
694 $num_insert_cols = sizeof($add_column_array[$i]);
696 if (isset($uncommon_columns[$i])) {
697 $num_remove_cols = sizeof($uncommon_columns[$i]);
699 if (isset($add_indexes_array[$i])) {
700 $num_add_index = sizeof($add_indexes_array[$i]);
702 if (isset($remove_indexes_array[$i])) {
703 $num_remove_index = sizeof($remove_indexes_array[$i]);
706 $btn_structure_params = null;
707 $btn_data_params = null;
709 if (($num_alter_cols > 0) ||
($num_insert_cols > 0) ||
($num_remove_cols > 0) ||
($num_add_index > 0) ||
($num_remove_index > 0)) {
710 $btn_structure_params = array($i, $num_alter_cols, $num_insert_cols,
711 $num_remove_cols, $num_add_index, $num_remove_index);
713 if (!(in_array($i, $matching_table_data_diff))) {
715 if (isset($matching_tables_keys[$i][0]) && isset($update_array[$i][0][$matching_tables_keys[$i][0]])) {
716 if (isset($update_array[$i])) {
717 $num_of_updates = sizeof($update_array[$i]);
724 if (isset($matching_tables_keys[$i][0]) && isset($insert_array[$i][0][$matching_tables_keys[$i][0]])) {
725 if (isset($insert_array[$i])) {
726 $num_of_insertions = sizeof($insert_array[$i]);
728 $num_of_insertions = 0;
731 $num_of_insertions = 0;
734 if ((isset($matching_tables_keys[$i][0]) && isset($update_array[$i][0][$matching_tables_keys[$i][0]]))
735 ||
(isset($matching_tables_keys[$i][0]) && isset($insert_array[$i][0][$matching_tables_keys[$i][0]]))) {
736 $btn_data_params = array($i, $num_of_updates, $num_of_insertions,
740 unset($update_array[$i]);
741 unset($insert_array[$i]);
744 'src_table_name' => $matching_tables[$i],
746 'btn_structure' => $btn_structure_params,
747 'btn_data' => $btn_data_params
751 * placing updated value of arrays in session
754 $_SESSION['update_array'] = $update_array;
755 $_SESSION['insert_array'] = $insert_array;
757 for ($j = 0; $j < count($source_tables_uncommon); $j++
) {
758 $btn_structure_params = null;
759 $btn_data_params = null;
762 * Display the difference only when it has not been applied
764 if (!(in_array($j, $uncommon_table_structure_diff))) {
765 if (isset($uncommon_tables[$j])) {
766 $btn_structure_params = array($j, null, null, null, null, null);
768 $dst_table_name = $source_tables_uncommon[$j] . ' (' . __('not present') . ')';
770 unset($uncommon_tables[$j]);
771 $dst_table_name = $source_tables_uncommon[$j];
774 * Display the difference only when it has not been applied
776 if (!(in_array($j, $uncommon_table_data_diff))) {
777 if (isset($row_count[$j]) && ($row_count[$j] > 0)) {
778 $btn_data_params = array($j, null, $row_count[$j], null, null, null);
781 unset($row_count[$j]);
785 'src_table_name' => $source_tables_uncommon[$j],
786 'dst_table_name' => $dst_table_name,
788 'btn_structure' => $btn_structure_params,
789 'btn_data' => $btn_data_params
793 * placing the latest values of arrays in session
796 $_SESSION['uncommon_tables'] = $uncommon_tables;
797 $_SESSION['uncommon_tables_row_count'] = $row_count;
801 * Displaying the target database tables
803 foreach ($target_tables_uncommon as $tbl_nc_name) {
805 'src_table_name' => '',
806 'dst_table_name' => $tbl_nc_name);
808 PMA_syncDisplayDataCompare($rows);
814 * This "list" div will contain a table and each row will depict information about structure/data diffrence in tables.
815 * Rows will be generated dynamically as soon as the colored buttons "D" or "S" are clicked.
818 echo '<fieldset style="padding:0"><div id="list" style = "overflow:auto; height:140px; padding:1em">';
821 <tr style="width: 100%;">
822 <th id="table_name" style="width: 10%;" colspan="1">' . __('Table') . ' </th>
823 <th id="str_diff" style="width: 65%;" colspan="6">' . __('Structure Difference') . ' </th>
824 <th id="data_diff" style="width: 20%;" colspan="2">' . __('Data Difference') . '</th>
826 <tr style="width: 100%;">
827 <th style="width: 10%;">' . __('Table name') . '</th>
828 <th style="width: 10%;">' . __('Create table'). '</th>
829 <th style="width: 11%;">' . __('Add column(s)') . '</th>
830 <th style="width: 13%;">' . __('Remove column(s)') . '</th>
831 <th style="width: 11%;">' . __('Alter column(s)') . '</th>
832 <th style="width: 12%;">' . __('Remove index(s)') . '</th>
833 <th style="width: 11%;">' . __('Apply index(s)') . '</th>
834 <th style="width: 10%;">' . __('Update row(s)') . '</th>
835 <th style="width: 10%;">' . __('Insert row(s)') . '</th>
843 * This fieldset displays the checkbox to confirm deletion of previous rows from target tables
846 <p><input type="checkbox" name="delete_rows" id ="delete_rows" /><label for="delete_rows">' . __('Would you like to delete all the previous rows from target tables?') . '</label> </p>
849 echo '<fieldset class="tblFooters">';
850 echo '<input type="button" name="apply_changes" value="' . __('Apply Selected Changes') . '"
851 onclick ="ApplySelectedChanges(' . "'" . htmlspecialchars($_SESSION['token']) . "'" .')" />';
852 echo '<input type="submit" name="synchronize_db" value="' . __('Synchronize Databases') . '" />'
858 * Displays the page when 'Synchronize Databases' is pressed.
861 if (isset($_REQUEST['synchronize_db'])) {
863 $src_db = $_SESSION['src_db'];
864 $trg_db = $_SESSION['trg_db'];
865 $update_array = $_SESSION['update_array'];
866 $insert_array = $_SESSION['insert_array'];
867 $src_username = $_SESSION['src_username'];
868 $trg_username = $_SESSION['trg_username'];
869 $src_password = $_SESSION['src_password'];
870 $trg_password = $_SESSION['trg_password'];
871 $matching_tables = $_SESSION['matching_tables'];
872 $matching_tables_keys = $_SESSION['matching_tables_keys'];
873 $matching_tables_fields = $_SESSION['matching_fields'];
874 $source_tables_uncommon = $_SESSION['src_uncommon_tables'];
875 $uncommon_tables_fields = $_SESSION['uncommon_tables_fields'];
876 $target_tables_uncommon = $_SESSION['target_tables_uncommon'];
877 $row_count = $_SESSION['uncommon_tables_row_count'];
878 $uncommon_tables = $_SESSION['uncommon_tables'];
879 $target_tables = $_SESSION['target_tables'];
881 $delete_array = $_SESSION['delete_array'];
882 $uncommon_columns = $_SESSION['uncommon_columns'];
883 $source_columns = $_SESSION['source_columns'];
884 $alter_str_array = $_SESSION['alter_str_array'];
885 $criteria = $_SESSION['criteria'];
886 $target_tables_keys = $_SESSION['target_tables_keys'];
887 $add_column_array = $_SESSION['add_column_array'];
888 $add_indexes_array = $_SESSION['add_indexes_array'];
889 $alter_indexes_array = $_SESSION['alter_indexes_array'];
890 $remove_indexes_array = $_SESSION['remove_indexes_array'];
891 $source_indexes = $_SESSION['source_indexes'];
892 $target_indexes = $_SESSION['target_indexes'];
893 $uncommon_cols = $uncommon_columns;
896 * Display success message.
898 echo '<div class="success">' . __('Target database has been synchronized with source database') . '</div>';
900 * Displaying all the tables of source and target database and now no difference is there.
902 PMA_syncDisplayHeaderCompare($src_db, $trg_db);
904 for ($i = 0; $i < count($matching_tables); $i++
)
907 'src_table_name' => $matching_tables[$i],
908 'dst_table_name' => $matching_tables[$i]);
910 foreach ($source_tables_uncommon as $tbl_nc_name) {
912 'src_table_name' => '+ ' +
$tbl_nc_name,
913 'dst_table_name' => $tbl_nc_name);
915 foreach ($target_tables_uncommon as $tbl_nc_name) {
917 'src_table_name' => '',
918 'dst_table_name' => $tbl_nc_name);
920 PMA_syncDisplayDataCompare($rows);
926 * connecting the source and target servers
928 if ('cur' != $_SESSION['src_type']) {
929 $src_link = PMA_DBI_connect($src_username, $src_password, $is_controluser = false, $_SESSION['src_server']);
931 $src_link = $GLOBALS['userlink'];
932 // working on current server, so initialize this for tracking
933 // (does not work if user defined current server as a remote one)
934 $GLOBALS['db'] = $_SESSION['src_db'];
936 if ('cur' != $_SESSION['trg_type']) {
937 $trg_link = PMA_DBI_connect($trg_username, $trg_password, $is_controluser = false, $_SESSION['trg_server']);
939 $trg_link = $GLOBALS['userlink'];
940 // working on current server, so initialize this for tracking
941 $GLOBALS['db'] = $_SESSION['trg_db'];
945 * Displaying the queries.
947 echo '<h5>' . __('The following queries have been executed:') . '</h5>';
948 echo '<div id="serverstatus" style = "overflow: auto; width: 1050px; height: 180px;
949 border-left: 1px gray solid; border-bottom: 1px gray solid; padding: 0px; margin: 0px"> ';
951 * Applying all sorts of differences for each matching table
953 for($p = 0; $p < sizeof($matching_tables); $p++
) {
955 * If the check box is checked for deleting previous rows from the target database tables then
956 * first find out rows to be deleted and then delete the rows.
958 if (isset($_REQUEST['delete_rows'])) {
959 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $p, $target_tables_keys, $matching_tables_keys,
960 $trg_db, $trg_link, $src_db, $src_link);
962 if (isset($delete_array[$p])) {
963 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $p, $target_tables_keys, $delete_array, true);
964 unset($delete_array[$p]);
967 if (isset($alter_str_array[$p])) {
968 PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, $source_columns, $alter_str_array, $matching_tables_fields,
969 $criteria, $matching_tables_keys, $target_tables_keys, $p, true);
970 unset($alter_str_array[$p]);
972 if (! empty($add_column_array[$p])) {
973 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $p, $target_tables_keys, $matching_tables_keys,
974 $trg_db, $trg_link, $src_db, $src_link);
976 if (isset($delete_array[$p])) {
977 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $p, $target_tables_keys, $delete_array, true);
978 unset($delete_array[$p]);
980 PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns, $add_column_array,
981 $matching_tables_fields, $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables, $uncommon_tables_fields,
982 $p, $uncommon_cols, true);
983 unset($add_column_array[$p]);
985 if (isset($uncommon_columns[$p])) {
986 PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $p, true);
987 unset($uncommon_columns[$p]);
989 if (isset($matching_table_structure_diff) &&
990 (isset($add_indexes_array[$matching_table_structure_diff[$p]])
991 ||
isset($remove_indexes_array[$matching_table_structure_diff[$p]])
992 ||
isset($alter_indexes_array[$matching_table_structure_diff[$p]]))) {
993 PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
994 $remove_indexes_array, $matching_table_structure_diff[$p], true);
996 unset($add_indexes_array[$matching_table_structure_diff[$p]]);
997 unset($alter_indexes_array[$matching_table_structure_diff[$p]]);
998 unset($remove_indexes_array[$matching_table_structure_diff[$p]]);
1001 PMA_updateTargetTables($matching_tables, $update_array, $src_db, $trg_db, $trg_link, $p, $matching_tables_keys, true);
1003 PMA_insertIntoTargetTable($matching_tables, $src_db, $trg_db, $src_link, $trg_link , $matching_tables_fields, $insert_array, $p,
1004 $matching_tables_keys, $matching_tables_keys, $source_columns, $add_column_array, $criteria, $target_tables_keys, $uncommon_tables,
1005 $uncommon_tables_fields,$uncommon_cols, $alter_str_array,$source_indexes, $target_indexes, $add_indexes_array,
1006 $alter_indexes_array, $delete_array, $update_array, true);
1010 * Creating and populating tables present in source but absent from target database.
1012 for($q = 0; $q < sizeof($source_tables_uncommon); $q++
) {
1013 if (isset($uncommon_tables[$q])) {
1014 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, true);
1016 if (isset($row_count[$q])) {
1017 PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, true);
1024 * Displays the main page when none of the following buttons is pressed
1027 if (! isset($_REQUEST['submit_connect']) && ! isset($_REQUEST['synchronize_db']) && ! isset($_REQUEST['Table_ids']) )
1030 * Displays the sub-page heading
1032 echo '<h2>' . ($GLOBALS['cfg']['MainPageIconic']
1033 ?
'<img class="icon ic_s_sync" src="themes/dot.gif" alt="" />'
1038 echo '<div id="serverstatus">
1039 <form name="connection_form" id="connection_form" method="post" action="server_synchronize.php"
1040 >' // TODO: add check if all var. are filled in
1041 . PMA_generate_common_hidden_inputs('', '');
1043 echo '<legend>' . __('Synchronize') . '</legend>';
1045 * Displays the forms
1048 $databases = PMA_DBI_get_databases_full(null, false, null, 'SCHEMA_NAME',
1051 if ($GLOBALS['cfg']['AllowArbitraryServer'] === false) {
1052 $possibly_readonly = ' readonly="readonly"';
1054 $possibly_readonly = '';
1057 foreach ($cons as $type) {
1058 if ('src' == $type) {
1059 $database_header = __('Source database');
1061 $database_header = __('Target database');
1064 $database_header .= PMA_showHint(PMA_sanitize(sprintf('%sAllowArbitraryServer%s', '[a@./Documentation.html#AllowArbitraryServer@_blank]', '[/a]')));
1066 <table id
="serverconnection_<?php echo $type; ?>_remote" class="data noclick">
1067 <caption
class="tblHeaders"><?php
echo $database_header; ?
></caption
>
1069 <td colspan
="2" style
="text-align: center">
1070 <select name
="<?php echo $type; ?>_type" id
="<?php echo $type; ?>_type" class="server_selector">
1072 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
1073 $preselected_option = 'rmt';
1074 echo '<option value="rmt" selected="selected">' . __('Enter manually') . '</option>';
1076 $preselected_option = 'cur';
1078 echo '<option value="cur"';
1079 if ('cur' == $preselected_option) {
1080 echo ' selected="selected"';
1082 echo '>' . __('Current connection') . '</option>';
1084 foreach ($GLOBALS['cfg']['Servers'] as $key => $tmp_server) {
1085 if (empty($tmp_server['host'])) {
1089 if (!empty($tmp_server['verbose'])) {
1090 $label = $tmp_server['verbose'];
1092 $label = $tmp_server['host'];
1093 if (!empty($tmp_server['port'])) {
1094 $label .= ':' . $tmp_server['port'];
1097 $value = $tmp_server['host'];
1099 if (empty($tmp_server['port']) && empty($tmp_server['socket'])) {
1102 $value .= $tmp_server['port'];
1105 $value .= $tmp_server['socket'];
1107 $value .= $tmp_server['user'];
1109 $value .= $tmp_server['only_db'];
1110 echo '<option value="' . $value . '" >'
1111 . htmlspecialchars(sprintf(__('Configuration: %s'), $label)) . '</option>';
1117 <tr
class="even toggler remote-server">
1118 <td
><?php
echo __('Server'); ?
></td
>
1119 <td
><input type
="text" name
="<?php echo $type; ?>_host" class="server-host" <?php
echo $possibly_readonly; ?
>/></td
>
1121 <tr
class="odd toggler remote-server">
1122 <td
><?php
echo __('Port'); ?
></td
>
1123 <td
><input type
="text" name
="<?php echo $type; ?>_port" class="server-port" <?php
echo $possibly_readonly; ?
> value
="3306" maxlength
="5" size
="5" /></td
>
1125 <tr
class="even toggler remote-server">
1126 <td
><?php
echo __('Socket'); ?
></td
>
1127 <td
><input type
="text" name
="<?php echo $type; ?>_socket" class="server-socket" <?php
echo $possibly_readonly; ?
>/></td
>
1129 <tr
class="odd toggler remote-server">
1130 <td
><?php
echo __('User name'); ?
></td
>
1131 <td
><input type
="text" name
="<?php echo $type; ?>_username" class="server-user" /></td
>
1133 <tr
class="even toggler remote-server">
1134 <td
><?php
echo __('Password'); ?
></td
>
1135 <td
><input type
="password" name
="<?php echo $type; ?>_pass" class="server-pass" /> </td
>
1137 <tr
class="odd toggler remote-server">
1138 <td
><?php
echo __('Database'); ?
></td
>
1139 <td
><input type
="text" name
="<?php echo $type; ?>_db" class="server-db" /></td
>
1141 <tr
class="even toggler current-server" style
="display: none;">
1142 <td
><?php
echo __('Database'); ?
></td
>
1145 // these unset() do not complain if the elements do not exist
1146 unset($databases['mysql']);
1147 unset($databases['information_schema']);
1149 if (count($databases) == 0) {
1150 echo __('No databases');
1153 <select name="' . $type . '_db_sel">
1155 foreach ($databases as $db) {
1156 echo ' <option>' . htmlspecialchars($db['SCHEMA_NAME']) . '</option>';
1163 unset ($types, $type);
1167 <fieldset class="tblFooters">
1168 <input type="submit" name="submit_connect" value="' . __('Go') .'" id="buttonGo" />
1172 <div class="notice">' . __('Target database will be completely synchronized with source database. Source database will remain unchanged.') . '</div>';
1176 * Displays the footer
1178 require './libraries/footer.inc.php';