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);
215 * Storing all arrays in session for use when page is reloaded for each button press
217 $_SESSION['matching_tables'] = $matching_tables;
218 $_SESSION['update_array'] = $update_array;
219 $_SESSION['insert_array'] = $insert_array;
220 $_SESSION['src_db'] = $src_db;
221 $_SESSION['trg_db'] = $trg_db;
222 $_SESSION['matching_fields'] = $matching_tables_fields;
223 $_SESSION['src_uncommon_tables'] = $source_tables_uncommon;
224 $_SESSION['src_username'] = $src_username ;
225 $_SESSION['trg_username'] = $trg_username;
226 $_SESSION['src_password'] = $src_password;
227 $_SESSION['trg_password'] = $trg_password;
228 $_SESSION['trg_password'] = $trg_password;
229 $_SESSION['src_server'] = $src_server;
230 $_SESSION['trg_server'] = $trg_server;
231 $_SESSION['src_type'] = $src_type;
232 $_SESSION['trg_type'] = $trg_type;
233 $_SESSION['matching_tables_keys'] = $matching_tables_keys;
234 $_SESSION['uncommon_tables_fields'] = $uncommon_tables_fields;
235 $_SESSION['uncommon_tables_row_count'] = $row_count;
236 $_SESSION['target_tables_uncommon'] = $target_tables_uncommon;
237 $_SESSION['uncommon_tables'] = $source_tables_uncommon;
238 $_SESSION['delete_array'] = $delete_array;
239 $_SESSION['uncommon_columns'] = $uncommon_columns;
240 $_SESSION['source_columns'] = $source_columns;
241 $_SESSION['alter_str_array'] = $alter_str_array;
242 $_SESSION['target_tables_keys'] = $target_tables_keys;
243 $_SESSION['add_column_array'] = $add_column_array;
244 $_SESSION['criteria'] = $criteria;
245 $_SESSION['target_tables'] = $trg_tables;
246 $_SESSION['add_indexes_array'] = $add_indexes_array;
247 $_SESSION['alter_indexes_array'] = $alter_indexes_array;
248 $_SESSION['remove_indexes_array'] = $remove_indexes_array;
249 $_SESSION['source_indexes'] = $source_indexes;
250 $_SESSION['target_indexes'] = $target_indexes;
253 * Displays the sub-heading and icons showing Structure Synchronization and Data Synchronization
256 <form name
="synchronize_form" id
="synchronize_form" method
="post" action
="server_synchronize.php">
257 <?php
echo PMA_generate_common_hidden_inputs('', ''); ?
>
261 <img
class="icon" src
="<?php echo $pmaThemeImage; ?>new_struct.png" width
="16" height
="16" alt
="" />
262 <?php
echo __('Structure Synchronization'); ?
>
265 <img
class="icon" src
="<?php echo $pmaThemeImage; ?>new_data.png" width
="16" height
="16" alt
="" />
266 <?php
echo __('Data Synchronization'); ?
>
272 * Displays the tables containing the source tables names, their difference with the target tables and target tables names
274 PMA_syncDisplayHeaderCompare($src_db, $trg_db);
278 * Display the matching tables' names and difference, first
280 for ($i = 0; $i < count($matching_tables); $i++
) {
282 * Calculating the number of updates for each matching table
284 if (isset($update_array[$i]) && isset($update_array[$i][0])
285 && !empty($matching_tables_keys[$i][0])
286 && isset($update_array[$i][0][$matching_tables_keys[$i][0]])) {
287 $num_of_updates = sizeof($update_array[$i]);
292 * Calculating the number of insertions for each matching table
294 if (isset($insert_array[$i]) && isset($insert_array[$i][0])
295 && !empty($matching_tables_keys[$i])
296 && isset($insert_array[$i][0][$matching_tables_keys[$i][0]])) {
297 $num_of_insertions = sizeof($insert_array[$i]);
299 $num_of_insertions = 0;
303 * Calculating the number of alter columns, number of columns to be added, number of columns to be removed,
304 * number of index to be added and removed.
307 $num_insert_cols = 0;
308 $num_remove_cols = 0;
310 $num_remove_index = 0;
312 if (isset($alter_str_array[$i])) {
313 $num_alter_cols = sizeof($alter_str_array[$i]);
315 if (isset($add_column_array[$i])) {
316 $num_insert_cols = sizeof($add_column_array[$i]);
318 if (isset($uncommon_columns[$i])) {
319 $num_remove_cols = sizeof($uncommon_columns[$i]);
321 if (isset($add_indexes_array[$i])) {
322 $num_add_index = sizeof($add_indexes_array[$i]);
324 if (isset($remove_indexes_array[$i])) {
325 $num_remove_index = sizeof($remove_indexes_array[$i]);
327 if (isset($alter_indexes_array[$i])) {
328 $num_add_index +
= sizeof($alter_indexes_array[$i]);
329 $num_remove_index +
= sizeof($alter_indexes_array[$i]);
332 $btn_structure_params = null;
333 $btn_data_params = null;
336 * Display the red button of structure synchronization if there exists any structure difference or index difference.
338 if (($num_alter_cols > 0) ||
($num_insert_cols > 0) ||
($num_remove_cols > 0) ||
($num_add_index > 0) ||
($num_remove_index > 0)) {
339 $btn_structure_params = array($i, $num_alter_cols, $num_insert_cols,
340 $num_remove_cols, $num_add_index, $num_remove_index);
344 * Display the green button of data synchronization if there exists any data difference.
346 if ((isset($update_array[$i]) ||
isset($insert_array[$i])) && !empty($matching_tables_keys[$i])) {
347 if (isset($update_array[$i][0][$matching_tables_keys[$i][0]]) ||
isset($insert_array[$i][0][$matching_tables_keys[$i][0]])) {
348 $btn_data_params = array($i, $num_of_updates, $num_of_insertions, null, null, null);
353 'src_table_name' => $matching_tables[$i],
354 'dst_table_name' => $matching_tables[$i],
356 'btn_structure' => $btn_structure_params,
357 'btn_data' => $btn_data_params
361 * Displays the tables' names present in source but missing from target
363 for ($j = 0; $j < count($source_tables_uncommon); $j++
) {
365 'src_table_name' => '+ ' . $source_tables_uncommon[$j],
366 'dst_table_name' => $source_tables_uncommon[$j] . ' (' . __('not present') . ')',
368 'btn_structure' => array($j, null, null, null, null, null),
371 if ($row_count[$j] > 0) {
372 $row['btn_data'] = array($j, null, $row_count[$j], null, null, null);
376 foreach ($target_tables_uncommon as $tbl_nc_name) {
378 'src_table_name' => '',
379 'dst_table_name' => $tbl_nc_name);
382 * Displays the target tables names
384 PMA_syncDisplayDataCompare($rows);
390 * This "list" div will contain a table and each row will depict information about structure/data diffrence in tables.
391 * Rows will be generated dynamically as soon as the colored buttons "D" or "S" are clicked.
394 echo '<fieldset style="padding:0"><div id="list" style="overflow:auto; height:140px; padding:1em">
398 <tr style="width: 100%;">
399 <th id="table_name" style="width: 10%;" colspan="1">' . __('Table') . ' </th>
400 <th id="str_diff" style="width: 65%;" colspan="6">' . __('Structure Difference') . ' </th>
401 <th id="data_diff" style="width: 20%;" colspan="2">' . __('Data Difference') . '</th>
403 <tr style="width: 100%;">
404 <th style="width: 10%;">' . __('Table name') . '</th>
405 <th style="width: 10%;">' . __('Create table'). '</th>
406 <th style="width: 11%;">' . __('Add column(s)') . '</th>
407 <th style="width: 13%;">' . __('Remove column(s)') . '</th>
408 <th style="width: 11%;">' . __('Alter column(s)') . '</th>
409 <th style="width: 12%;">' . __('Remove index(s)') . '</th>
410 <th style="width: 11%;">' . __('Apply index(s)') . '</th>
411 <th style="width: 10%;">'. __('Update row(s)') . '</th>
412 <th style="width: 10%;">' . __('Insert row(s)') . '</th>
419 * This fieldset displays the checkbox to confirm deletion of previous rows from target tables
422 <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>
424 <fieldset class="tblFooters">';
425 echo '<input type="button" name="apply_changes" value="' . __('Apply Selected Changes')
426 . '" onclick ="ApplySelectedChanges(' . "'" . htmlspecialchars($_SESSION['token']) . "'" . ')" />';
427 echo '<input type="submit" name="synchronize_db" value="' . __('Synchronize Databases') . '" />' . '</fieldset>';
431 } // end if ((isset($_REQUEST['submit_connect'])))
434 * Display the page when 'Apply Selected Changes' is pressed
436 if (isset($_REQUEST['Table_ids'])) {
438 * Displays success message
440 echo '<div class="success">' . __('Selected target tables have been synchronized with source tables.') . '</div>';
442 $src_db = $_SESSION['src_db'];
443 $trg_db = $_SESSION['trg_db'];
444 $update_array = $_SESSION['update_array'];
445 $insert_array = $_SESSION['insert_array'];
446 $src_username = $_SESSION['src_username'];
447 $trg_username = $_SESSION['trg_username'];
448 $src_password = $_SESSION['src_password'];
449 $trg_password = $_SESSION['trg_password'];
450 $src_server = $_SESSION['src_server'];
451 $trg_server = $_SESSION['trg_server'];
452 $src_type = $_SESSION['src_type'];
453 $trg_type = $_SESSION['trg_type'];
454 $uncommon_tables = $_SESSION['uncommon_tables'];
455 $matching_tables = $_SESSION['matching_tables'];
456 $matching_tables_keys = $_SESSION['matching_tables_keys'];
457 $matching_tables_fields = $_SESSION['matching_fields'];
458 $source_tables_uncommon = $_SESSION['src_uncommon_tables'];
459 $uncommon_tables_fields = $_SESSION['uncommon_tables_fields'];
460 $target_tables_uncommon = $_SESSION['target_tables_uncommon'];
461 $row_count = $_SESSION['uncommon_tables_row_count'];
462 $target_tables = $_SESSION['target_tables'];
464 $delete_array = $_SESSION['delete_array'];
465 $uncommon_columns = $_SESSION['uncommon_columns'];
466 $source_columns = $_SESSION['source_columns'];
467 $alter_str_array = $_SESSION['alter_str_array'];
468 $criteria = $_SESSION['criteria'];
469 $target_tables_keys = $_SESSION['target_tables_keys'];
470 $add_column_array = $_SESSION['add_column_array'];
471 $add_indexes_array = $_SESSION['add_indexes_array'];
472 $alter_indexes_array = $_SESSION['alter_indexes_array'];
473 $remove_indexes_array = $_SESSION['remove_indexes_array'];
474 $source_indexes = $_SESSION['source_indexes'];
475 $target_indexes = $_SESSION['target_indexes'];
476 $uncommon_cols = $uncommon_columns;
479 * Creating link object for source and target databases
481 foreach ($cons as $con) {
482 if ($
{"{$con}_type"} != "cur") {
483 $
{"{$con}_link"} = PMA_DBI_connect($
{"{$con}_username"}, $
{"{$con}_password"}, $is_controluser = false, $
{"{$con}_server"});
485 $
{"{$con}_link"} = null;
486 // working on current server, so initialize this for tracking
487 // (does not work if user defined current server as a remote one)
488 $GLOBALS['db'] = $
{"{$con}_db"};
490 } // end foreach ($cons as $con)
493 * Initializing arrays to save the table ids whose data and structure difference is to be applied
495 $matching_table_data_diff = array(); //stores id of matching table having data difference
496 $matching_table_structure_diff = array(); //stores id of matching tables having structure difference
497 $uncommon_table_structure_diff = array(); //stores id of uncommon tables having structure difference
498 $uncommon_table_data_diff = array(); //stores id of uncommon tables having data difference
500 for ($i = 0; isset($_REQUEST[$i]); $i++
) {
501 if (isset($_REQUEST[$i])) {
502 $table_id = explode("US", $_REQUEST[$i]);
503 if (isset($table_id[1])) {
504 $uncommon_table_structure_diff[] = $table_id[1];
506 $table_id = explode("UD", $_REQUEST[$i]);
507 if (isset($table_id[1])) {
508 $uncommon_table_data_diff[] = $table_id[1];
510 $table_id = explode("MS", $_REQUEST[$i]);
511 if (isset($table_id[1])) {
512 $matching_table_structure_diff[] = $table_id[1];
515 $table_id = explode("MD", $_REQUEST[$i]);
516 if (isset($table_id[1])) {
517 $matching_table_data_diff[] = $table_id[1];
522 * Applying the structure difference on selected matching tables
524 for ($q = 0; $q < sizeof($matching_table_structure_diff); $q++
)
526 if (isset($alter_str_array[$matching_table_structure_diff[$q]])) {
528 PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, $source_columns, $alter_str_array, $matching_tables_fields,
529 $criteria, $matching_tables_keys, $target_tables_keys, $matching_table_structure_diff[$q], false);
531 unset($alter_str_array[$matching_table_structure_diff[$q]]);
533 if (isset($add_column_array[$matching_table_structure_diff[$q]])) {
535 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $matching_table_structure_diff[$q], $target_tables_keys,
536 $matching_tables_keys, $trg_db, $trg_link, $src_db, $src_link);
538 if (isset($delete_array[$matching_table_structure_diff[$q]])) {
540 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $matching_table_structure_diff[$q], $target_tables_keys, $delete_array, false);
542 unset($delete_array[$matching_table_structure_diff[$q]]);
544 PMA_addColumnsInTargetTable($src_db, $trg_db,$src_link, $trg_link, $matching_tables, $source_columns, $add_column_array, $matching_tables_fields,
545 $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables,$uncommon_tables_fields, $matching_table_structure_diff[$q], $uncommon_cols, false);
547 unset($add_column_array[$matching_table_structure_diff[$q]]);
549 if (isset($uncommon_columns[$matching_table_structure_diff[$q]])) {
551 PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $matching_table_structure_diff[$q], false);
553 unset($uncommon_columns[$matching_table_structure_diff[$q]]);
555 if (isset($add_indexes_array[$matching_table_structure_diff[$q]]) ||
isset($remove_indexes_array[$matching_table_structure_diff[$q]])
556 ||
isset($alter_indexes_array[$matching_table_structure_diff[$q]])) {
558 PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
559 $remove_indexes_array, $matching_table_structure_diff[$q], false);
561 unset($add_indexes_array[$matching_table_structure_diff[$q]]);
562 unset($alter_indexes_array[$matching_table_structure_diff[$q]]);
563 unset($remove_indexes_array[$matching_table_structure_diff[$q]]);
567 * Applying the data difference. First checks if structure diff is applied or not.
568 * If not, then apply structure difference first then apply data difference.
570 for ($p = 0; $p < sizeof($matching_table_data_diff); $p++
)
572 if ($_REQUEST['checked'] == 'true') {
574 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $matching_table_data_diff[$p], $target_tables_keys,
575 $matching_tables_keys, $trg_db, $trg_link, $src_db, $src_link);
577 if (isset($delete_array[$matching_table_data_diff[$p]])) {
579 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $matching_table_data_diff[$p], $target_tables_keys, $delete_array, false);
581 unset($delete_array[$matching_table_data_diff[$p]]);
584 if (isset($alter_str_array[$matching_table_data_diff[$p]])) {
586 PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, $source_columns, $alter_str_array, $matching_tables_fields,
587 $criteria, $matching_tables_keys, $target_tables_keys, $matching_table_data_diff[$p], false);
589 unset($alter_str_array[$matching_table_data_diff[$p]]);
591 if (isset($add_column_array[$matching_table_data_diff[$p]])) {
593 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $matching_table_data_diff[$p], $target_tables_keys,
594 $matching_tables_keys, $trg_db, $trg_link, $src_db, $src_link);
596 if (isset($delete_array[$matching_table_data_diff[$p]])) {
598 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $matching_table_data_diff[$p], $target_tables_keys, $delete_array, false);
600 unset($delete_array[$matching_table_data_diff[$p]]);
602 PMA_addColumnsInTargetTable($src_db, $trg_db,$src_link, $trg_link, $matching_tables, $source_columns, $add_column_array, $matching_tables_fields,
603 $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables, $uncommon_tables_fields, $matching_table_data_diff[$p], $uncommon_cols, false);
605 unset($add_column_array[$matching_table_data_diff[$p]]);
607 if (isset($uncommon_columns[$matching_table_data_diff[$p]])) {
609 PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $matching_table_data_diff[$p], false);
611 unset($uncommon_columns[$matching_table_data_diff[$p]]);
613 if ((isset($matching_table_structure_diff[$q]) && isset($add_indexes_array[$matching_table_structure_diff[$q]]))
614 ||
(isset($matching_table_structure_diff[$q]) && isset($remove_indexes_array[$matching_table_structure_diff[$q]]))
615 ||
(isset($matching_table_structure_diff[$q]) && isset($alter_indexes_array[$matching_table_structure_diff[$q]]))) {
617 PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
618 $remove_indexes_array, $matching_table_structure_diff[$q], false);
620 unset($add_indexes_array[$matching_table_structure_diff[$q]]);
621 unset($alter_indexes_array[$matching_table_structure_diff[$q]]);
622 unset($remove_indexes_array[$matching_table_structure_diff[$q]]);
625 * Applying the data difference.
627 PMA_updateTargetTables($matching_tables, $update_array, $src_db, $trg_db, $trg_link, $matching_table_data_diff[$p], $matching_tables_keys, false);
629 PMA_insertIntoTargetTable($matching_tables, $src_db, $trg_db, $src_link, $trg_link , $matching_tables_fields, $insert_array,
630 $matching_table_data_diff[$p], $matching_tables_keys, $source_columns, $add_column_array, $criteria, $target_tables_keys,
631 $uncommon_tables, $uncommon_tables_fields, $uncommon_cols, $alter_str_array, $source_indexes, $target_indexes, $add_indexes_array,
632 $alter_indexes_array, $delete_array, $update_array, false);
635 * Updating the session variables to the latest values of the arrays.
637 $_SESSION['delete_array'] = $delete_array;
638 $_SESSION['uncommon_columns'] = $uncommon_columns;
639 $_SESSION['alter_str_array'] = $alter_str_array;
640 $_SESSION['add_column_array'] = $add_column_array;
641 $_SESSION['add_indexes_array'] = $add_indexes_array;
642 $_SESSION['remove_indexes_array'] = $remove_indexes_array;
643 $_SESSION['insert_array'] = $insert_array;
644 $_SESSION['update_array'] = $update_array;
647 * Applying structure difference to selected non-matching tables (present in Source but absent from Target).
649 for ($s = 0; $s < sizeof($uncommon_table_structure_diff); $s++
)
651 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $uncommon_tables, $uncommon_table_structure_diff[$s], $uncommon_tables_fields, false);
652 $_SESSION['uncommon_tables_fields'] = $uncommon_tables_fields;
654 unset($uncommon_tables[$uncommon_table_structure_diff[$s]]);
657 * Applying data difference to selected non-matching tables (present in Source but absent from Target).
658 * Before data synchronization, structure synchronization is confirmed.
660 for ($r = 0; $r < sizeof($uncommon_table_data_diff); $r++
)
662 if (!(in_array($uncommon_table_data_diff[$r], $uncommon_table_structure_diff))) {
663 if (isset($uncommon_tables[$uncommon_table_data_diff[$r]])) {
665 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $uncommon_tables, $uncommon_table_data_diff[$r],
666 $uncommon_tables_fields, false);
667 $_SESSION['uncommon_tables_fields'] = $uncommon_tables_fields;
669 unset($uncommon_tables[$uncommon_table_data_diff[$r]]);
672 PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $uncommon_table_data_diff[$r],
673 $_SESSION['uncommon_tables_fields'], false);
675 unset($row_count[$uncommon_table_data_diff[$r]]);
678 * Again all the tables from source and target database are displayed with their differences.
679 * The differences have been removed from tables that have been synchronized
681 echo '<form name="applied_difference" id="synchronize_form" method="post" action="server_synchronize.php">'
682 . PMA_generate_common_hidden_inputs('', '');
684 PMA_syncDisplayHeaderCompare($src_db, $trg_db);
686 for ($i = 0; $i < count($matching_tables); $i++
) {
688 $num_insert_cols = 0;
689 $num_remove_cols = 0;
691 $num_remove_index = 0;
693 if (isset($alter_str_array[$i])) {
694 $num_alter_cols = sizeof($alter_str_array[$i]);
696 if (isset($add_column_array[$i])) {
697 $num_insert_cols = sizeof($add_column_array[$i]);
699 if (isset($uncommon_columns[$i])) {
700 $num_remove_cols = sizeof($uncommon_columns[$i]);
702 if (isset($add_indexes_array[$i])) {
703 $num_add_index = sizeof($add_indexes_array[$i]);
705 if (isset($remove_indexes_array[$i])) {
706 $num_remove_index = sizeof($remove_indexes_array[$i]);
709 $btn_structure_params = null;
710 $btn_data_params = null;
712 if (($num_alter_cols > 0) ||
($num_insert_cols > 0) ||
($num_remove_cols > 0) ||
($num_add_index > 0) ||
($num_remove_index > 0)) {
713 $btn_structure_params = array($i, $num_alter_cols, $num_insert_cols,
714 $num_remove_cols, $num_add_index, $num_remove_index);
716 if (!(in_array($i, $matching_table_data_diff))) {
718 if (isset($matching_tables_keys[$i][0]) && isset($update_array[$i][0][$matching_tables_keys[$i][0]])) {
719 if (isset($update_array[$i])) {
720 $num_of_updates = sizeof($update_array[$i]);
727 if (isset($matching_tables_keys[$i][0]) && isset($insert_array[$i][0][$matching_tables_keys[$i][0]])) {
728 if (isset($insert_array[$i])) {
729 $num_of_insertions = sizeof($insert_array[$i]);
731 $num_of_insertions = 0;
734 $num_of_insertions = 0;
737 if ((isset($matching_tables_keys[$i][0]) && isset($update_array[$i][0][$matching_tables_keys[$i][0]]))
738 ||
(isset($matching_tables_keys[$i][0]) && isset($insert_array[$i][0][$matching_tables_keys[$i][0]]))) {
739 $btn_data_params = array($i, $num_of_updates, $num_of_insertions,
743 unset($update_array[$i]);
744 unset($insert_array[$i]);
747 'src_table_name' => $matching_tables[$i],
748 'dst_table_name' => $matching_tables[$i],
750 'btn_structure' => $btn_structure_params,
751 'btn_data' => $btn_data_params
755 * placing updated value of arrays in session
758 $_SESSION['update_array'] = $update_array;
759 $_SESSION['insert_array'] = $insert_array;
761 for ($j = 0; $j < count($source_tables_uncommon); $j++
) {
762 $btn_structure_params = null;
763 $btn_data_params = null;
766 * Display the difference only when it has not been applied
768 if (!(in_array($j, $uncommon_table_structure_diff))) {
769 if (isset($uncommon_tables[$j])) {
770 $btn_structure_params = array($j, null, null, null, null, null);
772 $dst_table_name = $source_tables_uncommon[$j] . ' (' . __('not present') . ')';
774 unset($uncommon_tables[$j]);
775 $dst_table_name = $source_tables_uncommon[$j];
778 * Display the difference only when it has not been applied
780 if (!(in_array($j, $uncommon_table_data_diff))) {
781 if (isset($row_count[$j]) && ($row_count[$j] > 0)) {
782 $btn_data_params = array($j, null, $row_count[$j], null, null, null);
785 unset($row_count[$j]);
789 'src_table_name' => $source_tables_uncommon[$j],
790 'dst_table_name' => $dst_table_name,
792 'btn_structure' => $btn_structure_params,
793 'btn_data' => $btn_data_params
797 * placing the latest values of arrays in session
800 $_SESSION['uncommon_tables'] = $uncommon_tables;
801 $_SESSION['uncommon_tables_row_count'] = $row_count;
805 * Displaying the target database tables
807 foreach ($target_tables_uncommon as $tbl_nc_name) {
809 'src_table_name' => '',
810 'dst_table_name' => $tbl_nc_name);
812 PMA_syncDisplayDataCompare($rows);
818 * This "list" div will contain a table and each row will depict information about structure/data diffrence in tables.
819 * Rows will be generated dynamically as soon as the colored buttons "D" or "S" are clicked.
822 echo '<fieldset style="padding:0"><div id="list" style = "overflow:auto; height:140px; padding:1em">';
825 <tr style="width: 100%;">
826 <th id="table_name" style="width: 10%;" colspan="1">' . __('Table') . ' </th>
827 <th id="str_diff" style="width: 65%;" colspan="6">' . __('Structure Difference') . ' </th>
828 <th id="data_diff" style="width: 20%;" colspan="2">' . __('Data Difference') . '</th>
830 <tr style="width: 100%;">
831 <th style="width: 10%;">' . __('Table name') . '</th>
832 <th style="width: 10%;">' . __('Create table'). '</th>
833 <th style="width: 11%;">' . __('Add column(s)') . '</th>
834 <th style="width: 13%;">' . __('Remove column(s)') . '</th>
835 <th style="width: 11%;">' . __('Alter column(s)') . '</th>
836 <th style="width: 12%;">' . __('Remove index(s)') . '</th>
837 <th style="width: 11%;">' . __('Apply index(s)') . '</th>
838 <th style="width: 10%;">' . __('Update row(s)') . '</th>
839 <th style="width: 10%;">' . __('Insert row(s)') . '</th>
847 * This fieldset displays the checkbox to confirm deletion of previous rows from target tables
850 <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>
853 echo '<fieldset class="tblFooters">';
854 echo '<input type="button" name="apply_changes" value="' . __('Apply Selected Changes') . '"
855 onclick ="ApplySelectedChanges(' . "'" . htmlspecialchars($_SESSION['token']) . "'" .')" />';
856 echo '<input type="submit" name="synchronize_db" value="' . __('Synchronize Databases') . '" />'
862 * Displays the page when 'Synchronize Databases' is pressed.
865 if (isset($_REQUEST['synchronize_db'])) {
867 $src_db = $_SESSION['src_db'];
868 $trg_db = $_SESSION['trg_db'];
869 $update_array = $_SESSION['update_array'];
870 $insert_array = $_SESSION['insert_array'];
871 $src_username = $_SESSION['src_username'];
872 $trg_username = $_SESSION['trg_username'];
873 $src_password = $_SESSION['src_password'];
874 $trg_password = $_SESSION['trg_password'];
875 $matching_tables = $_SESSION['matching_tables'];
876 $matching_tables_keys = $_SESSION['matching_tables_keys'];
877 $matching_tables_fields = $_SESSION['matching_fields'];
878 $source_tables_uncommon = $_SESSION['src_uncommon_tables'];
879 $uncommon_tables_fields = $_SESSION['uncommon_tables_fields'];
880 $target_tables_uncommon = $_SESSION['target_tables_uncommon'];
881 $row_count = $_SESSION['uncommon_tables_row_count'];
882 $uncommon_tables = $_SESSION['uncommon_tables'];
883 $target_tables = $_SESSION['target_tables'];
885 $delete_array = $_SESSION['delete_array'];
886 $uncommon_columns = $_SESSION['uncommon_columns'];
887 $source_columns = $_SESSION['source_columns'];
888 $alter_str_array = $_SESSION['alter_str_array'];
889 $criteria = $_SESSION['criteria'];
890 $target_tables_keys = $_SESSION['target_tables_keys'];
891 $add_column_array = $_SESSION['add_column_array'];
892 $add_indexes_array = $_SESSION['add_indexes_array'];
893 $alter_indexes_array = $_SESSION['alter_indexes_array'];
894 $remove_indexes_array = $_SESSION['remove_indexes_array'];
895 $source_indexes = $_SESSION['source_indexes'];
896 $target_indexes = $_SESSION['target_indexes'];
897 $uncommon_cols = $uncommon_columns;
900 * Display success message.
902 echo '<div class="success">' . __('Target database has been synchronized with source database') . '</div>';
904 * Displaying all the tables of source and target database and now no difference is there.
906 PMA_syncDisplayHeaderCompare($src_db, $trg_db);
908 for ($i = 0; $i < count($matching_tables); $i++
)
911 'src_table_name' => $matching_tables[$i],
912 'dst_table_name' => $matching_tables[$i]);
914 foreach ($source_tables_uncommon as $tbl_nc_name) {
916 'src_table_name' => '+ ' . $tbl_nc_name,
917 'dst_table_name' => $tbl_nc_name);
919 foreach ($target_tables_uncommon as $tbl_nc_name) {
921 'src_table_name' => '',
922 'dst_table_name' => $tbl_nc_name);
924 PMA_syncDisplayDataCompare($rows);
930 * connecting the source and target servers
932 if ('cur' != $_SESSION['src_type']) {
933 $src_link = PMA_DBI_connect($src_username, $src_password, $is_controluser = false, $_SESSION['src_server']);
935 $src_link = $GLOBALS['userlink'];
936 // working on current server, so initialize this for tracking
937 // (does not work if user defined current server as a remote one)
938 $GLOBALS['db'] = $_SESSION['src_db'];
940 if ('cur' != $_SESSION['trg_type']) {
941 $trg_link = PMA_DBI_connect($trg_username, $trg_password, $is_controluser = false, $_SESSION['trg_server']);
943 $trg_link = $GLOBALS['userlink'];
944 // working on current server, so initialize this for tracking
945 $GLOBALS['db'] = $_SESSION['trg_db'];
949 * Displaying the queries.
951 echo '<fieldset><legend>' . __('Executed queries') . '</legend>';
953 * Applying all sorts of differences for each matching table
955 for ($p = 0; $p < sizeof($matching_tables); $p++
) {
957 * If the check box is checked for deleting previous rows from the target database tables then
958 * first find out rows to be deleted and then delete the rows.
960 if (isset($_REQUEST['delete_rows'])) {
961 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $p, $target_tables_keys, $matching_tables_keys,
962 $trg_db, $trg_link, $src_db, $src_link);
964 if (isset($delete_array[$p])) {
965 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $p, $target_tables_keys, $delete_array, true);
966 unset($delete_array[$p]);
969 if (isset($alter_str_array[$p])) {
970 PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables, $source_columns, $alter_str_array, $matching_tables_fields,
971 $criteria, $matching_tables_keys, $target_tables_keys, $p, true);
972 unset($alter_str_array[$p]);
974 if (! empty($add_column_array[$p])) {
975 PMA_findDeleteRowsFromTargetTables($delete_array, $matching_tables, $p, $target_tables_keys, $matching_tables_keys,
976 $trg_db, $trg_link, $src_db, $src_link);
978 if (isset($delete_array[$p])) {
979 PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables, $p, $target_tables_keys, $delete_array, true);
980 unset($delete_array[$p]);
982 PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link, $matching_tables, $source_columns, $add_column_array,
983 $matching_tables_fields, $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables, $uncommon_tables_fields,
984 $p, $uncommon_cols, true);
985 unset($add_column_array[$p]);
987 if (isset($uncommon_columns[$p])) {
988 PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables, $uncommon_columns, $p, true);
989 unset($uncommon_columns[$p]);
991 if (isset($matching_table_structure_diff) &&
992 (isset($add_indexes_array[$matching_table_structure_diff[$p]])
993 ||
isset($remove_indexes_array[$matching_table_structure_diff[$p]])
994 ||
isset($alter_indexes_array[$matching_table_structure_diff[$p]]))) {
995 PMA_applyIndexesDiff ($trg_db, $trg_link, $matching_tables, $source_indexes, $target_indexes, $add_indexes_array, $alter_indexes_array,
996 $remove_indexes_array, $matching_table_structure_diff[$p], true);
998 unset($add_indexes_array[$matching_table_structure_diff[$p]]);
999 unset($alter_indexes_array[$matching_table_structure_diff[$p]]);
1000 unset($remove_indexes_array[$matching_table_structure_diff[$p]]);
1003 PMA_updateTargetTables($matching_tables, $update_array, $src_db, $trg_db, $trg_link, $p, $matching_tables_keys, true);
1005 PMA_insertIntoTargetTable($matching_tables, $src_db, $trg_db, $src_link, $trg_link , $matching_tables_fields, $insert_array, $p,
1006 $matching_tables_keys, $matching_tables_keys, $source_columns, $add_column_array, $criteria, $target_tables_keys, $uncommon_tables,
1007 $uncommon_tables_fields,$uncommon_cols, $alter_str_array,$source_indexes, $target_indexes, $add_indexes_array,
1008 $alter_indexes_array, $delete_array, $update_array, true);
1012 * Creating and populating tables present in source but absent from target database.
1014 for ($q = 0; $q < sizeof($source_tables_uncommon); $q++
) {
1015 if (isset($uncommon_tables[$q])) {
1016 PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, true);
1018 if (isset($row_count[$q])) {
1019 PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link, $source_tables_uncommon, $q, $uncommon_tables_fields, true);
1026 * Displays the main page when none of the following buttons is pressed
1029 if (! isset($_REQUEST['submit_connect']) && ! isset($_REQUEST['synchronize_db']) && ! isset($_REQUEST['Table_ids']) )
1032 * Displays the sub-page heading
1034 echo '<h2>' . ($GLOBALS['cfg']['MainPageIconic']
1035 ?
'<img class="icon ic_s_sync" src="themes/dot.gif" alt="" />'
1040 echo '<div id="serverstatus">
1041 <form name="connection_form" id="connection_form" method="post" action="server_synchronize.php"
1042 >' // TODO: add check if all var. are filled in
1043 . PMA_generate_common_hidden_inputs('', '');
1045 echo '<legend>' . __('Synchronize') . '</legend>';
1047 * Displays the forms
1050 $databases = PMA_DBI_get_databases_full(null, false, null, 'SCHEMA_NAME',
1053 if ($GLOBALS['cfg']['AllowArbitraryServer'] === false) {
1054 $possibly_readonly = ' readonly="readonly"';
1056 $possibly_readonly = '';
1059 foreach ($cons as $type) {
1060 if ('src' == $type) {
1061 $database_header = __('Source database');
1063 $database_header = __('Target database');
1066 $database_header .= PMA_showHint(PMA_sanitize(sprintf('%sAllowArbitraryServer%s', '[a@./Documentation.html#AllowArbitraryServer@_blank]', '[/a]')));
1068 <table id
="serverconnection_<?php echo $type; ?>_remote" class="data noclick">
1069 <caption
class="tblHeaders"><?php
echo $database_header; ?
></caption
>
1071 <td colspan
="2" style
="text-align: center">
1072 <select name
="<?php echo $type; ?>_type" id
="<?php echo $type; ?>_type" class="server_selector">
1074 if ($GLOBALS['cfg']['AllowArbitraryServer']) {
1075 $preselected_option = 'rmt';
1076 echo '<option value="rmt" selected="selected">' . __('Enter manually') . '</option>';
1078 $preselected_option = 'cur';
1080 echo '<option value="cur"';
1081 if ('cur' == $preselected_option) {
1082 echo ' selected="selected"';
1084 echo '>' . __('Current connection') . '</option>';
1086 foreach ($GLOBALS['cfg']['Servers'] as $key => $tmp_server) {
1087 if (empty($tmp_server['host']) && empty($tmp_server['socket'])) {
1091 if (!empty($tmp_server['verbose'])) {
1092 $label = $tmp_server['verbose'];
1094 $label = $tmp_server['host'];
1095 if (!empty($tmp_server['port'])) {
1096 $label .= ':' . $tmp_server['port'];
1099 $value = $tmp_server['host'];
1101 if (empty($tmp_server['port']) && empty($tmp_server['socket'])) {
1104 $value .= $tmp_server['port'];
1107 $value .= $tmp_server['socket'];
1109 $value .= $tmp_server['user'];
1111 $value .= $tmp_server['only_db'];
1112 echo '<option value="' . $value . '" >'
1113 . sprintf(__('Configuration: %s'), htmlspecialchars($label)) . '</option>';
1119 <tr
class="even toggler remote-server">
1120 <td
><?php
echo __('Server'); ?
></td
>
1121 <td
><input type
="text" name
="<?php echo $type; ?>_host" class="server-host" <?php
echo $possibly_readonly; ?
>/></td
>
1123 <tr
class="odd toggler remote-server">
1124 <td
><?php
echo __('Port'); ?
></td
>
1125 <td
><input type
="text" name
="<?php echo $type; ?>_port" class="server-port" <?php
echo $possibly_readonly; ?
> value
="3306" maxlength
="5" size
="5" /></td
>
1127 <tr
class="even toggler remote-server">
1128 <td
><?php
echo __('Socket'); ?
></td
>
1129 <td
><input type
="text" name
="<?php echo $type; ?>_socket" class="server-socket" <?php
echo $possibly_readonly; ?
>/></td
>
1131 <tr
class="odd toggler remote-server">
1132 <td
><?php
echo __('User name'); ?
></td
>
1133 <td
><input type
="text" name
="<?php echo $type; ?>_username" class="server-user" /></td
>
1135 <tr
class="even toggler remote-server">
1136 <td
><?php
echo __('Password'); ?
></td
>
1137 <td
><input type
="password" name
="<?php echo $type; ?>_pass" class="server-pass" /> </td
>
1139 <tr
class="odd toggler remote-server">
1140 <td
><?php
echo __('Database'); ?
></td
>
1141 <td
><input type
="text" name
="<?php echo $type; ?>_db" class="server-db" /></td
>
1143 <tr
class="even toggler current-server" style
="display: none;">
1144 <td
><?php
echo __('Database'); ?
></td
>
1147 // these unset() do not complain if the elements do not exist
1148 unset($databases['mysql']);
1149 unset($databases['information_schema']);
1151 if (count($databases) == 0) {
1152 echo __('No databases');
1155 <select name="' . $type . '_db_sel">
1157 foreach ($databases as $db) {
1158 echo ' <option>' . htmlspecialchars($db['SCHEMA_NAME']) . '</option>';
1165 unset ($types, $type);
1169 <fieldset class="tblFooters">
1170 <input type="submit" name="submit_connect" value="' . __('Go') .'" id="buttonGo" />
1174 <div class="notice">' . __('Target database will be completely synchronized with source database. Source database will remain unchanged.') . '</div>';
1178 * Displays the footer
1180 require './libraries/footer.inc.php';