Welcome to mirror list, hosted at ThFree Co, Russian Federation.

server_synchronize.lib.php « libraries - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1349ef74c064ac10b4bce6a779d9a81a0d32efa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Server synchronisation functions.
 *
 * @package PhpMyAdmin
 */
if (! defined('PHPMYADMIN')) {
    exit;
}

/**
 * Places matching tables in source and target databases in $matching_tables
 * array whereas $uncommon_source_tables array gets the tables present in
 * source database but are absent from target database.  Criterion for
 * matching tables is just comparing their names.
 *
 * @param array $trg_tables              array of target database table names,
 * @param array $src_tables              array of source database table names,
 * @param array &$matching_tables        empty array passed by reference to save
 *                                       names of matching tables,
 * @param array &$uncommon_source_tables empty array passed by reference to save
 *                                       names of tables present in source database
 *                                       but absent from target database
 *
 * @return nothing
 */
function PMA_getMatchingTables($trg_tables, $src_tables, &$matching_tables,
    &$uncommon_source_tables
) {
    for ($k=0; $k< count($src_tables); $k++) {
        $present_in_target = false;
        for ($l=0; $l < count($trg_tables); $l++) {
            if ($src_tables[$k] === $trg_tables[$l]) {
                $present_in_target = true;
                $matching_tables[] = $src_tables[$k];
            }
        }
        if ($present_in_target === false) {
            $uncommon_source_tables[] = $src_tables[$k];
        }
    }
}

/**
 * Places tables present in target database but are absent from source database
 *
 * @param array $trg_tables              array of target database table names,
 * @param array $matching_tables         matching tables array containing names
 *                                       of matching tables,
 * @param array &$uncommon_target_tables empty array passed by reference to save
 *                                       names of tables presnet in target database
 *                                       but absent from source database
 *
 * @return nothing
 */
function PMA_getNonMatchingTargetTables($trg_tables, $matching_tables,
    &$uncommon_target_tables
) {
    for ($c=0; $c<count($trg_tables); $c++) {
        $match = false;
        for ($d=0; $d < count($matching_tables); $d++) {
            if ($trg_tables[$c] === $matching_tables[$d]) {
                $match=true;
            }
        }
        if ($match === false) {
            $uncommon_target_tables[] = $trg_tables[$c];
        }
    }
}

/**
 * Finds the difference in source and target matching tables by
 * first comparing source table's primary key entries with target table enteries.
 * It gets the field names for the matching table also for comparisons.
 * If the entry is found in target table also then it is checked for the remaining
 * field values also, in order to check whether update is required or not.
 * If update is required, it is placed in $update_array
 * Otherwise that entry is placed in the $insert_array.
 *
 * @param string  $src_db                  name of source database
 * @param string  $trg_db                  name of target database
 * @param db_link $src_link                connection established with source server
 * @param db_link $trg_link                connection established with target server
 * @param array   &$matching_table         array containing matching table names
 * @param array   &$matching_tables_fields A two dimensional array passed by
 *                                         reference to contain names of fields for
 *                                         each matching table
 * @param array   &$update_array           A three dimensional array passed by
 *                                         reference to contain updates required
 *                                         for each matching table
 * @param array   &$insert_array           A three dimensional array passed by
 *                                         reference to contain inserts required
 *                                         for each matching table
 * @param array   &$delete_array           Unused
 * @param array   &$fields_num             A two dimensional array passed by
 *                                         reference to contain number of fields
 *                                         for each matching table
 * @param int     $matching_table_index    Index of a table from $matching_table
 *                                         array
 * @param array   &$matching_tables_keys   A two dimensional array passed by
 *                                         reference to contain names of keys for
 *                                         each matching table
 *
 * @return nothing
 */
function PMA_dataDiffInTables($src_db, $trg_db, $src_link, $trg_link,
    &$matching_table, &$matching_tables_fields, &$update_array, &$insert_array,
    &$delete_array, &$fields_num, $matching_table_index, &$matching_tables_keys
) {
    if (isset($matching_table[$matching_table_index])) {
        $fld = array();
        $fld_results = PMA_DBI_get_columns(
            $src_db, $matching_table[$matching_table_index], null, true, $src_link
        );
        $is_key = array();
        if (isset($fld_results)) {
            foreach ($fld_results as $each_field) {
                $field_name = $each_field['Field'];
                if ($each_field['Key'] == 'PRI') {
                    $is_key[] = $field_name;
                }
                $fld[] = $field_name;
            }
        }
        $matching_tables_fields[$matching_table_index] = $fld;
        $fields_num[$matching_table_index] = count($fld);
        $matching_tables_keys[$matching_table_index] = $is_key;

        $source_result_set = PMA_getColumnValues(
            $src_db, $matching_table[$matching_table_index], $is_key, $src_link
        );
        $source_size = count($source_result_set);

        $trg_fld_results = PMA_DBI_get_columns(
            $trg_db, $matching_table[$matching_table_index], null, true, $trg_link
        );
        $all_keys_match = true;
        $trg_keys = array();

        if (isset($trg_fld_results)) {
            foreach ($trg_fld_results as $each_field) {
                if ($each_field['Key'] == 'PRI') {
                    $trg_keys[] = $each_field['Field'];
                    if (! (in_array($each_field['Field'], $is_key))) {
                        $all_keys_match = false;
                    }
                }
            }
        }
        $update_row = 0;
        $insert_row = 0;

        for ($j = 0; $j < $source_size; $j++) {
            $starting_index = 0;
            $update_field = 0;

            if (isset($source_result_set[$j]) && ($all_keys_match)) {

                // Query the target server to see which rows already exist
                $trg_select_query = "SELECT * FROM " . PMA_backquote($trg_db) . "."
                    . PMA_backquote($matching_table[$matching_table_index])
                    . " WHERE ";

                if (count($is_key) == 1) {
                    $trg_select_query .= PMA_backquote($is_key[0])
                        . "='" . $source_result_set[$j] . "'";
                } elseif (count($is_key) > 1) {
                    for ($k=0; $k < count($is_key); $k++) {
                        $trg_select_query .= PMA_backquote($is_key[$k])
                            . "='" . $source_result_set[$j][$is_key[$k]] . "'";
                        if ($k < (count($is_key)-1)) {
                            $trg_select_query .= " AND ";
                        }
                    }
                }

                $target_result_set = PMA_DBI_fetch_result(
                    $trg_select_query,
                    null,
                    null,
                    $trg_link
                );
                if ($target_result_set) {

                    // Fetch the row from the source server to do a comparison
                    $src_select_query = "SELECT * FROM "
                        . PMA_backquote($src_db) . "."
                        . PMA_backquote($matching_table[$matching_table_index])
                        . " WHERE ";

                    if (count($is_key) == 1) {
                        $src_select_query .= PMA_backquote($is_key[0])
                            . "='" . $source_result_set[$j] . "'";
                    } elseif (count($is_key) > 1) {
                        for ($k=0; $k< count($is_key); $k++) {
                            $src_select_query .= PMA_backquote($is_key[$k])
                                . "='" . $source_result_set[$j][$is_key[$k]] . "'";
                            if ($k < (count($is_key) - 1)) {
                                $src_select_query .= " AND ";
                            }
                        }
                    }

                    $src_result_set = PMA_DBI_fetch_result(
                        $src_select_query,
                        null,
                        null,
                        $src_link
                    );

                    /**
                    * Comparing each corresponding field of the source and target
                    * matching rows. Placing the primary key, value of primary
                    * key, field to be updated, and the new value of field to
                    * be updated in each row of the update array.
                    */
                    for ($m = 0; ($m < $fields_num[$matching_table_index]) && ($starting_index == 0) ; $m++) {
                        if (isset($src_result_set[0][$fld[$m]])) {
                            if (isset($target_result_set[0][$fld[$m]])) {
                                if (($src_result_set[0][$fld[$m]] != $target_result_set[0][$fld[$m]]) && (! (in_array($fld[$m], $is_key)))) {
                                    if (count($is_key) == 1) {
                                        if ($source_result_set[$j]) {
                                            $update_array[$matching_table_index][$update_row][$is_key[0]] = $source_result_set[$j];
                                        }
                                    } elseif (count($is_key) > 1) {
                                        for ($n=0; $n < count($is_key); $n++) {
                                            if (isset($src_result_set[0][$is_key[$n]])) {
                                                $update_array[$matching_table_index][$update_row][$is_key[$n]] = $src_result_set[0][$is_key[$n]];
                                            }
                                        }
                                    }

                                    $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];

                                    $update_field++;
                                    if (isset($src_result_set[0][$fld[$m]])) {
                                        $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
                                        $update_field++;
                                    }
                                    $starting_index = $m;
                                    $update_row++;
                                }
                            } else {
                                if (count($is_key) == 1) {
                                    if ($source_result_set[$j]) {
                                        $update_array[$matching_table_index][$update_row][$is_key[0]] = $source_result_set[$j];
                                    }
                                } elseif (count($is_key) > 1) {
                                    for ($n = 0; $n < count($is_key); $n++) {
                                        if (isset($src_result_set[0][$is_key[$n]])) {
                                            $update_array[$matching_table_index][$update_row][$is_key[$n]] = $src_result_set[0][$is_key[$n]];
                                        }
                                    }
                                }

                                $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];

                                $update_field++;
                                if (isset($src_result_set[0][$fld[$m]])) {
                                    $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
                                    $update_field++;
                                }
                                $starting_index = $m;
                                $update_row++;
                            }
                        }
                    }
                    for ($m = $starting_index + 1; $m < $fields_num[$matching_table_index] ; $m++) {
                        if (isset($src_result_set[0][$fld[$m]])) {
                            if (isset($target_result_set[0][$fld[$m]])) {
                                if (($src_result_set[0][$fld[$m]] != $target_result_set[0][$fld[$m]]) && (!(in_array($fld[$m], $is_key)))) {
                                    $update_row--;
                                    $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
                                    $update_field++;
                                    if ($src_result_set[0][$fld[$m]]) {
                                        $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
                                        $update_field++;
                                    }
                                    $update_row++;
                                }
                            } else {
                                $update_row--;
                                $update_array[$matching_table_index][$update_row][$update_field] = $fld[$m];
                                $update_field++;
                                if ($src_result_set[0][$fld[$m]]) {
                                    $update_array[$matching_table_index][$update_row][$update_field] = $src_result_set[0][$fld[$m]];
                                    $update_field++;
                                }
                                $update_row++;
                            }
                        }
                    }
                } else {
                    /**
                     * Placing the primary key, and the value of primary key of the
                     * row that is to be inserted in the target table
                     */
                    if (count($is_key) == 1) {
                        if (isset($source_result_set[$j])) {
                            $insert_array[$matching_table_index][$insert_row][$is_key[0]] = $source_result_set[$j];
                        }
                    } elseif (count($is_key) > 1) {
                        for ($l = 0; $l < count($is_key); $l++) {
                            if (isset($source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]])) {
                                $insert_array[$matching_table_index][$insert_row][$is_key[$l]] = $source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]];
                            }
                        }
                    }
                    $insert_row++;
                }
            } else {
                /**
                 * Placing the primary key, and the value of primary key of the
                 * row that is to be inserted in the target table. This
                 * condition is met when there is an additional column in the
                 * source table
                 */
                if (count($is_key) == 1) {
                    if (isset($source_result_set[$j])) {
                        $insert_array[$matching_table_index][$insert_row][$is_key[0]] = $source_result_set[$j];
                    }
                } elseif (count($is_key) > 1) {
                    for ($l = 0; $l < count($is_key); $l++) {
                        if (isset($source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]])) {
                            $insert_array[$matching_table_index][$insert_row][$is_key[$l]] = $source_result_set[$j][$matching_tables_fields[$matching_table_index][$l]];
                        }
                    }
                }
                $insert_row++;
            }
        } // for loop ends
    }
}

/**
 * Finds the rows which are to be deleted from target table.
 *
 * @param array   &$delete_array        array containing rows that are to be deleted
 * @param array   $matching_table       array containing matching table names
 * @param int     $matching_table_index index of a table from $matching_table array
 * @param array   $trg_keys             array of target table keys
 * @param array   $src_keys             array of source table keys
 * @param string  $trg_db               name of target database
 * @param db_link $trg_link             connection established with target server
 * @param string  $src_db               name of source database
 * @param db_link $src_link             connection established with source server
 *
 * @return nothing
 */
function PMA_findDeleteRowsFromTargetTables(&$delete_array, $matching_table,
    $matching_table_index, $trg_keys, $src_keys, $trg_db, $trg_link, $src_db, $src_link
) {
    if (isset($trg_keys[$matching_table_index])) {
        $target_key_values = PMA_getColumnValues($trg_db, $matching_table[$matching_table_index], $trg_keys[$matching_table_index], $trg_link);
    }
    if (isset($src_keys[$matching_table_index])) {
        $source_key_values = PMA_getColumnValues($src_db, $matching_table[$matching_table_index], $src_keys[$matching_table_index], $src_link);
    }
    $all_keys_match = 1;
    for ($a = 0; $a < count($trg_keys[$matching_table_index]); $a++) {
        if (isset($trg_keys[$matching_table_index][$a])) {
            if (! (in_array($trg_keys[$matching_table_index][$a], $src_keys[$matching_table_index]))) {
                $all_keys_match = 0;
            }
        }
    }
    if (! ($all_keys_match)) {
        if (isset($target_key_values)) {
            $delete_array[$matching_table_index] = $target_key_values;
        }
    }
    if (isset($trg_keys[$matching_table_index])) {
        if ((count($trg_keys[$matching_table_index]) == 1) && $all_keys_match) {
            $row = 0;
            if (isset($target_key_values)) {
                for ($i = 0; $i < count($target_key_values); $i++) {
                    if (! (in_array($target_key_values[$i], $source_key_values))) {
                        $delete_array[$matching_table_index][$row] = $target_key_values[$i];
                        $row++;
                    }
                }
            }
        } elseif ((count($trg_keys[$matching_table_index]) > 1) && $all_keys_match) {
            $row = 0;
            if (isset($target_key_values)) {
                for ($i = 0; $i < count($target_key_values); $i++) {
                    $is_present = false;
                    for ($j = 0; $j < count($source_key_values) && ($is_present == false) ; $j++) {
                        $check = true;
                        for ($k = 0; $k < count($trg_keys[$matching_table_index]); $k++) {
                            if ($target_key_values[$i][$trg_keys[$matching_table_index][$k]] != $source_key_values[$j][$trg_keys[$matching_table_index][$k]]) {
                                $check = false;
                            }
                        }
                        if ($check) {
                            $is_present = true;
                        }
                    }
                    if (! ($is_present)) {
                        for ($l = 0; $l < count($trg_keys[$matching_table_index]); $l++) {
                            $delete_array[$matching_table_index][$row][$trg_keys[$matching_table_index][$l]] = $target_key_values[$i][$trg_keys[$matching_table_index][$l]];
                        }
                        $row++;
                    }
                }
            }
        }
    }
}

/**
 * PMA_dataDiffInUncommonTables() finds the data difference in
 * $source_tables_uncommon
 *
 * @param array  $source_tables_uncommon table names that are in source db and not
 *                                       in target db
 * @param string $src_db                 name of source database
 * @param mixed  $src_link               connection established with source server
 * @param int    $index                  index of a table from $matching_table array
 * @param array  &$row_count             number of rows
 *
 * @return void
 */
function PMA_dataDiffInUncommonTables($source_tables_uncommon, $src_db, $src_link, $index, &$row_count)
{
    $query = "SELECT COUNT(*) FROM " . PMA_backquote($src_db) . "."
        . PMA_backquote($source_tables_uncommon[$index]);
    $rows  = PMA_DBI_fetch_result($query, null, null, $src_link);
    $row_count[$index] = $rows[0];
}

/**
 * PMA_updateTargetTables() sets the updated field values to
 * target table rows using $update_array[$matching_table_index]
 *
 * @param array  $table                Matching tables' names
 * @param array  $update_array         A three dimensional array containing field
 *                                     value updates required for each matching
 *                                     table
 * @param string $src_db               Name of source database
 * @param string $trg_db               Name of target database
 * @param mixed  $trg_link             Connection established with target server
 * @param int    $matching_table_index index of matching table in
 *                                     matching_table_array
 * @param array  $matching_table_keys  matching keys for table
 * @param bool   $display              whether to display query
 *
 * @return nothing
 */
function PMA_updateTargetTables(
    $table, $update_array, $src_db, $trg_db,
    $trg_link, $matching_table_index, $matching_table_keys, $display
) {
    if (isset($update_array[$matching_table_index])) {
        if (count($update_array[$matching_table_index])) {

            for ($update_row = 0; $update_row < count($update_array[$matching_table_index]); $update_row++) {

                if (isset($update_array[$matching_table_index][$update_row])) {
                    $update_fields_num = count($update_array[$matching_table_index][$update_row])-count($matching_table_keys[$matching_table_index]);
                    if ($update_fields_num > 0) {
                        $query = "UPDATE " . PMA_backquote($trg_db) . "." .PMA_backquote($table[$matching_table_index]) . " SET ";

                        for ($update_field = 0; $update_field < $update_fields_num; $update_field = $update_field+2) {
                            if (isset($update_array[$matching_table_index][$update_row][$update_field]) && isset($update_array[$matching_table_index][$update_row][$update_field+1])) {
                                $query .= PMA_backquote($update_array[$matching_table_index][$update_row][$update_field]) . "='" . $update_array[$matching_table_index][$update_row][$update_field+1] . "'";
                            }
                            if ($update_field < ($update_fields_num - 2)) {
                                $query .= ", ";
                            }
                        }
                        $query .= " WHERE ";
                        if (isset($matching_table_keys[$matching_table_index])) {
                            for ($key = 0; $key < count($matching_table_keys[$matching_table_index]); $key++) {
                                if (isset($matching_table_keys[$matching_table_index][$key])) {
                                    $query .= PMA_backquote($matching_table_keys[$matching_table_index][$key]) . "='" . $update_array[$matching_table_index][$update_row][$matching_table_keys[$matching_table_index][$key]] . "'";
                                }
                                if ($key < (count($matching_table_keys[$matching_table_index]) - 1)) {
                                    $query .= " AND ";
                                }
                            }
                        }
                        $query .= ';';
                        if ($display == true) {
                            echo "<p>" . $query . "</p>";
                        }
                        PMA_DBI_try_query($query, $trg_link, 0);
                    }
                }
            }
        }
    }
}

/**
 * PMA_insertIntoTargetTable() inserts missing rows in the target table
 * using $array_insert[$matching_table_index]
 *
 * @todo this function uses undefined variables and is possibly broken:
 * $matching_tables, $matching_tables_fields, $remove_indexes_array,
 * $matching_table_keys
 *
 * @param array  $matching_table          matching table names
 * @param string $src_db                  name of source database
 * @param string $trg_db                  name of target database
 * @param mixed  $src_link                connection established with source server
 * @param mixed  $trg_link                connection established with target server
 * @param array  $table_fields            field names of a table
 * @param array  &$array_insert
 * @param int    $matching_table_index    index of matching table in
 *                                        matching_table_array
 * @param array  $matching_tables_keys    field names that are keys in the matching
 *                                        table
 * @param array  $source_columns          source column information
 * @param array  &$add_column_array       column names that are to be added in
 *                                        target table
 * @param array  $criteria                criteria like type, null, collation,
 *                                        default etc
 * @param array  $target_tables_keys      field names that are keys in the target
 *                                        table
 * @param array  $uncommon_tables         table names that are present in source db
 *                                        but not in targt db
 * @param array  &$uncommon_tables_fields field names of the uncommon tables
 * @param array  $uncommon_cols           column names that are present in target
 *                                        table and not in source table
 * @param array  &$alter_str_array        column names that are to be altered
 * @param array  &$source_indexes         column names on which indexes are made
 *                                        in source table
 * @param array  &$target_indexes         column names on which indexes are made
 *                                        in target table
 * @param array  &$add_indexes_array      column names on which index is to be
 *                                        added in target table
 * @param array  &$alter_indexes_array    column names whose indexes are to be
 *                                        altered. Only index name and uniqueness
 *                                        of an index can be changed
 * @param array  &$delete_array           rows that are to be deleted
 * @param array  &$update_array           rows that are to be updated in target
 * @param bool   $display                 whether to display query
 *
 * @return nothing
 */
function PMA_insertIntoTargetTable($matching_table, $src_db, $trg_db, $src_link,
    $trg_link, $table_fields, &$array_insert, $matching_table_index,
    $matching_tables_keys, $source_columns, &$add_column_array, $criteria,
    $target_tables_keys, $uncommon_tables, &$uncommon_tables_fields, $uncommon_cols,
    &$alter_str_array, &$source_indexes, &$target_indexes, &$add_indexes_array,
    &$alter_indexes_array, &$delete_array, &$update_array, $display
) {
    if (isset($array_insert[$matching_table_index])) {
        if (count($array_insert[$matching_table_index])) {
            for ($insert_row = 0; $insert_row< count($array_insert[$matching_table_index]); $insert_row++) {
                if (isset($array_insert[$matching_table_index][$insert_row][$matching_tables_keys[$matching_table_index][0]])) {

                    $select_query = "SELECT * FROM " . PMA_backquote($src_db) . "." . PMA_backquote($matching_table[$matching_table_index]) . " WHERE ";
                    for ($i = 0; $i < count($matching_tables_keys[$matching_table_index]); $i++) {
                        $select_query .= $matching_tables_keys[$matching_table_index][$i] . "='";
                        $select_query .= $array_insert[$matching_table_index][$insert_row][$matching_tables_keys[$matching_table_index][$i]] . "'" ;

                        if ($i < (count($matching_tables_keys[$matching_table_index]) - 1)) {
                            $select_query.= " AND ";
                        }
                    }
                    $select_query .= "; ";
                    $result = PMA_DBI_fetch_result($select_query, null, null, $src_link);
                    $insert_query = "INSERT INTO " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_table[$matching_table_index]) ." (";

                    for ($field_index = 0; $field_index < count($table_fields[$matching_table_index]); $field_index++) {
                        $insert_query .=  PMA_backquote($table_fields[$matching_table_index][$field_index]);

                        $is_fk_query = "SELECT * FROM  information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db ."'
                                         AND TABLE_NAME = '" . $matching_table[$matching_table_index]. "'AND COLUMN_NAME = '" .
                                         $table_fields[$matching_table_index][$field_index] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;" ;

                        $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $trg_link);
                        if (count($is_fk_result) > 0) {
                            for ($j = 0; $j < count($is_fk_result); $j++) {
                                $table_index = array_keys($matching_table, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);

                                if (isset($alter_str_array[$table_index[0]])) {
                                    PMA_alterTargetTableStructure(
                                        $trg_db, $trg_link, $matching_tables,
                                        $source_columns, $alter_str_array,
                                        $matching_tables_fields,
                                        $criteria, $matching_tables_keys,
                                        $target_tables_keys, $table_index[0],
                                        $display
                                    );
                                    unset($alter_str_array[$table_index[0]]);
                                }
                                if (isset($uncommon_columns[$table_index[0]])) {
                                    PMA_removeColumnsFromTargetTable(
                                        $trg_db, $trg_link, $matching_tables,
                                        $uncommon_columns, $table_index[0], $display
                                    );
                                    unset($uncommon_columns[$table_index[0]]);
                                }
                                if (isset($add_column_array[$table_index[0]])) {
                                    PMA_findDeleteRowsFromTargetTables(
                                        $delete_array, $matching_tables,
                                        $table_index[0], $target_tables_keys,
                                        $matching_tables_keys, $trg_db,
                                        $trg_link, $src_db, $src_link
                                    );

                                    if (isset($delete_array[$table_index[0]])) {
                                        PMA_deleteFromTargetTable(
                                            $trg_db, $trg_link, $matching_tables,
                                            $table_index[0], $target_tables_keys,
                                            $delete_array, $display
                                        );
                                        unset($delete_array[$table_index[0]]);
                                    }
                                    PMA_addColumnsInTargetTable(
                                        $src_db, $trg_db, $src_link, $trg_link,
                                        $matching_tables, $source_columns,
                                        $add_column_array, $matching_tables_fields,
                                        $criteria, $matching_tables_keys,
                                        $target_tables_keys, $uncommon_tables,
                                        $uncommon_tables_fields, $table_index[0],
                                        $uncommon_cols, $display
                                    );
                                    unset($add_column_array[$table_index[0]]);
                                }
                                if (isset($add_indexes_array[$table_index[0]])
                                    || isset($remove_indexes_array[$table_index[0]])
                                    || isset($alter_indexes_array[$table_index[0]])
                                ) {
                                    PMA_applyIndexesDiff(
                                        $trg_db, $trg_link, $matching_tables,
                                        $source_indexes, $target_indexes,
                                        $add_indexes_array, $alter_indexes_array,
                                        $remove_indexes_array, $table_index[0],
                                        $display
                                    );

                                    unset($add_indexes_array[$table_index[0]]);
                                    unset($alter_indexes_array[$table_index[0]]);
                                    unset($remove_indexes_array[$table_index[0]]);
                                }
                                if (isset($update_array[$table_index[0]])) {
                                    PMA_updateTargetTables(
                                        $matching_tables, $update_array,
                                        $src_db, $trg_db, $trg_link,
                                        $table_index[0], $matching_table_keys,
                                        $display
                                    );
                                    unset($update_array[$table_index[0]]);
                                }
                                if (isset($array_insert[$table_index[0]])) {
                                     PMA_insertIntoTargetTable(
                                         $matching_table, $src_db, $trg_db,
                                         $src_link, $trg_link, $table_fields,
                                         $array_insert, $table_index[0],
                                         $matching_tables_keys, $source_columns,
                                         $add_column_array, $criteria,
                                         $target_tables_keys, $uncommon_tables,
                                         $uncommon_tables_fields, $uncommon_cols,
                                         $alter_str_array, $source_indexes,
                                         $target_indexes, $add_indexes_array,
                                         $alter_indexes_array, $delete_array,
                                         $update_array, $display
                                     );
                                     unset($array_insert[$table_index[0]]);
                                }
                            }
                        }
                        if ($field_index < count($table_fields[$matching_table_index])-1) {
                            $insert_query .= ", ";
                        }
                    }
                    $insert_query .= ") VALUES(";
                    if (count($table_fields[$matching_table_index]) == 1) {
                         $insert_query .= "'" . PMA_sqlAddSlashes($result[0]) . "'";
                    } else {
                        for ($field_index = 0; $field_index < count($table_fields[$matching_table_index]); $field_index++) {
                            if (isset($result[0][$table_fields[$matching_table_index][$field_index]])) {
                                $insert_query .= "'" . PMA_sqlAddSlashes($result[0][$table_fields[$matching_table_index][$field_index]]) . "'";
                            } else {
                                $insert_query .= "'NULL'";
                            }
                            if ($field_index < (count($table_fields[$matching_table_index])) - 1) {
                                    $insert_query .= " ," ;
                            }
                        }
                    }
                    $insert_query .= ");";
                    if ($display == true) {
                        PMA_displayQuery($insert_query);
                    }
                    PMA_DBI_try_query($insert_query, $trg_link, 0);
                }
            }
        }
    }
}

/**
 * PMA_createTargetTables() Create the missing table $uncommon_table in
 * target database
 *
 * @param string $src_db                  name of source database
 * @param string $trg_db                  name of target database
 * @param mixed  $src_link                connection established with source server
 * @param mixed  $trg_link                connection established with target server
 * @param array  &$uncommon_tables        names of tables present in source but
 *                                        not in target
 * @param int    $table_index             index of table in $uncommon_tables array
 * @param array  &$uncommon_tables_fields field names of the uncommon table
 * @param bool   $display                 whether to display query
 *
 * @return nothing
 */
function PMA_createTargetTables($src_db, $trg_db, $src_link, $trg_link,
    &$uncommon_tables, $table_index, &$uncommon_tables_fields, $display
) {
    if (isset($uncommon_tables[$table_index])) {
        $fields_result = PMA_DBI_get_columns(
            $src_db,
            $uncommon_tables[$table_index],
            null,
            true,
            $src_link
        );
        $fields = array();
        foreach ($fields_result as $each_field) {
            $field_name = $each_field['Field'];
            $fields[] = $field_name;
        }
        $uncommon_tables_fields[$table_index] = $fields;

        $Create_Query = PMA_DBI_fetch_value(
            "SHOW CREATE TABLE " . PMA_backquote($src_db) . '.'
            . PMA_backquote($uncommon_tables[$table_index]),
            0,
            1,
            $src_link
        );

        // Replace the src table name with a `dbname`.`tablename`
        $Create_Table_Query = preg_replace(
            '/' . preg_quote(PMA_backquote($uncommon_tables[$table_index]), '/') . '/',
            PMA_backquote($trg_db) . '.' . PMA_backquote($uncommon_tables[$table_index]),
            $Create_Query,
            $limit = 1
        );

        $is_fk_query = "SELECT * FROM  information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $src_db . "'
                        AND TABLE_NAME = '" . $uncommon_tables[$table_index] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;" ;

        $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
        if (count($is_fk_result) > 0) {
            for ($j = 0; $j < count($is_fk_result); $j++) {
                if (in_array($is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
                    $table_index = array_keys($uncommon_tables, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
                    PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields, $display);
                    unset($uncommon_tables[$table_index[0]]);
                }
            }
        }
        $Create_Table_Query .= ';';
        if ($display == true) {
            echo '<p>' . $Create_Table_Query . '</p>';
        }
        PMA_DBI_try_query($Create_Table_Query, $trg_link, 0);
    }
}
/**
 * PMA_populateTargetTables() inserts data into uncommon tables after they have
 * been created
 *
 * @param string $src_db                 name of source database
 * @param string $trg_db                 name of target database
 * @param mixed  $src_link               connection established with source server
 * @param mixed  $trg_link               connection established with target server
 * @param array  $uncommon_tables        uncommon table names (table names that
 *                                       are present in source but not in target db)
 * @param int    $table_index            index of table in matching_table_array
 * @param array  $uncommon_tables_fields field names of the uncommon table
 * @param bool   $display                whether to display query
 *
 * @return nothing
 *
 * @todo This turns NULL values into '' (empty string)
 */
function PMA_populateTargetTables($src_db, $trg_db, $src_link, $trg_link,
    $uncommon_tables, $table_index, $uncommon_tables_fields, $display
) {
    // @todo: maybe display some of the queries if they are not too numerous
    $display = false;
    $unbuffered_result = PMA_DBI_try_query(
        'SELECT * FROM ' . PMA_backquote($src_db) . '.'
        . PMA_backquote($uncommon_tables[$table_index]),
        $src_link,
        PMA_DBI_QUERY_UNBUFFERED
    );
    if (false !== $unbuffered_result) {
        $insert_query = 'INSERT INTO ' . PMA_backquote($trg_db) . '.'
            . PMA_backquote($uncommon_tables[$table_index]) . ' VALUES';
        while ($one_row = PMA_DBI_fetch_row($unbuffered_result)) {
            $insert_query .= '(';
            $key_of_last_value = count($one_row) - 1;
            foreach ($one_row as $key => $value) {
                $insert_query .= "'" . PMA_sqlAddSlashes($value) . "'";
                if ($key < $key_of_last_value) {
                    $insert_query .= ",";
                }
            }
            $insert_query .= '),';
        }
        $insert_query = substr($insert_query, 0, -1);
        $insert_query .= ';';
        if ($display == true) {
            PMA_displayQuery($insert_query);
        }
        PMA_DBI_try_query($insert_query, $trg_link, 0);
    }
}

/**
 * Deletes rows from target table
 *
 * @param string $trg_db             name of target database
 * @param mixed  $trg_link           connection established with target server
 * @param array  $matching_tables    matching table names
 * @param int    $table_index        index of table in matching_table_array
 * @param array  $target_tables_keys primary key names of the target tables
 * @param array  $delete_array       key values of rows that are to be deleted
 * @param bool   $display            whether to display query
 *
 * @return nothing
 */
function PMA_deleteFromTargetTable($trg_db, $trg_link, $matching_tables,
    $table_index, $target_tables_keys, $delete_array, $display
) {
    for ($i = 0; $i < count($delete_array[$table_index]); $i++) {
        if (isset($target_tables_keys[$table_index])) {
            $delete_query = 'DELETE FROM ' . PMA_backquote($trg_db) . '.' .PMA_backquote($matching_tables[$table_index]) . ' WHERE ';
            for ($y = 0; $y < count($target_tables_keys[$table_index]); $y++) {
                $delete_query .= PMA_backquote($target_tables_keys[$table_index][$y]) . " = '";

                if (count($target_tables_keys[$table_index]) == 1) {
                    $delete_query .= $delete_array[$table_index][$i] . "'";
                } elseif (count($target_tables_keys[$table_index]) > 1) {
                    $delete_query .= $delete_array[$table_index][$i][$target_tables_keys[$table_index][$y]] . "'";
                }
                if ($y < (count($target_tables_keys[$table_index]) - 1)) {
                    $delete_query .= ' AND ';
                }
                $pk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . $trg_db . "'
                            AND REFERENCED_TABLE_NAME = '" . $matching_tables[$table_index]."' AND REFERENCED_COLUMN_NAME = '"
                           . $target_tables_keys[$table_index][$y] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";

                $pk_query_result = PMA_DBI_fetch_result($pk_query, null, null, $trg_link);
                $result_size = count($pk_query_result);

                if ($result_size > 0) {
                    for ($b = 0; $b < $result_size; $b++) {
                        $drop_pk_query = "DELETE FROM "
                            . PMA_backquote($pk_query_result[$b]['TABLE_SCHEMA'])
                            . "."
                            . PMA_backquote($pk_query_result[$b]['TABLE_NAME'])
                            . " WHERE "
                            . PMA_backquote($pk_query_result[$b]['COLUMN_NAME'])
                            . " = " . $target_tables_keys[$table_index][$y] . ";";
                        PMA_DBI_try_query($drop_pk_query, $trg_link, 0);
                    }
                }
            }
        }
        if ($display == true) {
            echo '<p>' . $delete_query . '</p>';
        }
        PMA_DBI_try_query($delete_query, $trg_link, 0);
    }
}

/**
 * Gets all the column information for source and target table.
 * Compare columns on their names.
 * If column exists in target then compare Type, Null, Collation, Key, Default and
 * Comment for that column.
 * If column does not exist in target table then it is placed in  $add_column_array.
 * If column exists in target table but criteria is different then it is palced
 * in $alter_str_array.
 * If column does not exist in source table but is present in target table then
 * it is placed in  $uncommon_columns.
 * Keys for all the source tables that have a corresponding target table are
 * placed in $matching_tables_keys.
 * Keys for all the target tables that have a corresponding source table are
 * placed in $target_tables_keys.
 *
 * @param string $src_db               name of source database
 * @param string $trg_db               name of target database
 * @param mixed  $src_link             connection established with source server
 * @param mixed  $trg_link             connection established with target server
 * @param array  $matching_tables      names of matching tables
 * @param array  &$source_columns      columns information of the source tables
 * @param array  &$target_columns      columns information of the target tables
 * @param array  &$alter_str_array     three dimensional associative array first
 *                                     index being the matching table index,
 *                                     second index being column name for which
 *                                     target column have some criteria different
 *                                     and third index containing the criteria
 *                                     which is different.
 * @param array  &$add_column_array    two dimensional associative array, first
 *                                     index of the array contain the matching
 *                                     table number and second index contain the
 *                                     column name which is to be added in the
 *                                     target table
 * @param array  &$uncommon_columns    columns that are present in the target table
 *                                     but not in the source table
 * @param array  $criteria             criteria which are to be checked for field
 *                                     that is present in source table and
 *                                     target table
 * @param array  &$target_tables_keys  field names which is key in the target table
 * @param int    $matching_table_index number of the matching table
 *
 * @return nothing
 */
function PMA_structureDiffInTables($src_db, $trg_db, $src_link, $trg_link,
    $matching_tables, &$source_columns, &$target_columns, &$alter_str_array,
    &$add_column_array, &$uncommon_columns, $criteria, &$target_tables_keys,
    $matching_table_index
) {
    //Gets column information for source and target table
    $source_columns[$matching_table_index] = PMA_DBI_get_columns_full(
        $src_db,
        $matching_tables[$matching_table_index],
        null,
        $src_link
    );
    $target_columns[$matching_table_index] = PMA_DBI_get_columns_full(
        $trg_db,
        $matching_tables[$matching_table_index],
        null,
        $trg_link
    );
    foreach ($source_columns[$matching_table_index] as $column_name => $each_column) {
        if (isset($target_columns[$matching_table_index][$column_name]['Field'])) {
            //If column exists in target table then matches criteria like type,
            // null, collation, key, default, comment of the column
            for ($i = 0; $i < count($criteria); $i++) {
                if ($source_columns[$matching_table_index][$column_name][$criteria[$i]] != $target_columns[$matching_table_index][$column_name][$criteria[$i]]) {
                    if (($criteria[$i] == 'Default') && ($source_columns[$matching_table_index][$column_name][$criteria[$i]] == '' )) {
                        $alter_str_array[$matching_table_index][$column_name][$criteria[$i]] = 'None';
                    } else {
                        if (! (($criteria[$i] == 'Key') && (($source_columns[$matching_table_index][$column_name][$criteria[$i]] == 'MUL')
                            || ($target_columns[$matching_table_index][$column_name][$criteria[$i]] == 'MUL')
                            || ($source_columns[$matching_table_index][$column_name][$criteria[$i]] == 'UNI')
                            || ($target_columns[$matching_table_index][$column_name][$criteria[$i]] == 'UNI')))
                        ) {
                            $alter_str_array[$matching_table_index][$column_name][$criteria[$i]] = $source_columns[$matching_table_index][$column_name][$criteria[$i]];
                        }
                    }
                }
            }
        } else {
            $add_column_array[$matching_table_index][$column_name]= $column_name;
        }
    }
    //Finds column names that are present in target table but not in source table
    foreach ($target_columns[$matching_table_index] as $fld_name => $each_column) {
        if (! (isset($source_columns[$matching_table_index][$fld_name]['Field']))) {
            $fields_uncommon[] = $fld_name;
        }
        if ($target_columns[$matching_table_index][$fld_name]['Key'] == 'PRI') {
            $keys[] = $fld_name;
        }
    }
    if (isset($fields_uncommon)) {
        $uncommon_columns[$matching_table_index] = $fields_uncommon;
    }
    if (isset($keys)) {
        $target_tables_keys[$matching_table_index] = $keys;
    }
}
/**
 * Adds column that are present in source table but not in target table
 *
 * @param string $src_db                  name of source database
 * @param string $trg_db                  name of target database
 * @param mixed  $src_link                connection established with source server
 * @param mixed  $trg_link                connection established with target server
 * @param array  $matching_tables         names of matching tables
 * @param array  $source_columns          columns information of the source tables
 * @param array  &$add_column_array       the names of the column(field) that are
 *                                        to be added in the target
 * @param array  $matching_tables_fields
 * @param array  $criteria                criteria
 * @param array  $matching_tables_keys    field names which is key in the source
 *                                        table
 * @param array  $target_tables_keys      field names which is key in the target
 *                                        table
 * @param array  $uncommon_tables         table names that are present in source
 *                                        db and not in target db
 * @param array  &$uncommon_tables_fields names of the fields of the uncommon tables
 * @param int    $table_counter           number of the matching table
 * @param array  $uncommon_cols
 * @param bool   $display                 whether to display query
 *
 * @return nothing
 */
function PMA_addColumnsInTargetTable($src_db, $trg_db, $src_link, $trg_link,
    $matching_tables, $source_columns, &$add_column_array, $matching_tables_fields,
    $criteria, $matching_tables_keys, $target_tables_keys, $uncommon_tables,
    &$uncommon_tables_fields, $table_counter, $uncommon_cols, $display
) {
    for ($i = 0; $i < count($matching_tables_fields[$table_counter]); $i++) {
        if (isset($add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]])) {
            $query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$table_counter]). " ADD COLUMN " .
            PMA_backquote($add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]]) . " " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Type'];

            if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Null'] == 'NO') {
                $query .= ' Not Null ';
            } elseif ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Null'] == 'YES') {
                $query .= ' Null ';
            }
            if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Collation'] != '') {
                $query .= ' COLLATE ' . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Collation'];
            }
            if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Default'] != '') {
                $query .= " DEFAULT " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Default'];
            }
            if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Comment'] != '') {
                $query .= " COMMENT " . $source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Comment'];
            }
            if ($source_columns[$table_counter][$matching_tables_fields[$table_counter][$i]]['Key'] == 'PRI' ) {
                $trg_key_size = count($target_tables_keys[$table_counter]);
                if ($trg_key_size) {
                    $check = true;
                    for ($a = 0; ($a < $trg_key_size) && ($check); $a++) {
                        if (! (in_array($target_tables_keys[$table_counter], $uncommon_cols))) {
                             $check = false;
                        }
                    }
                    if (! $check) {
                        $query .= " ,DROP PRIMARY KEY " ;
                    }
                }
                $query .= " , ADD PRIMARY KEY (";
                for ($t = 0; $t < count($matching_tables_keys[$table_counter]); $t++) {
                    $query .= PMA_backquote($matching_tables_keys[$table_counter][$t]);
                    if ($t < (count($matching_tables_keys[$table_counter]) - 1)) {
                        $query .= " , " ;
                    }
                }
                $query .= ")";
            }

            $query .= ";";
            if ($display == true) {
                echo '<p>' . $query . '</p>';
            }
            PMA_DBI_try_query($query, $trg_link, 0);

            // Checks if column to be added is a foreign key or not
            $is_fk_query = "SELECT * FROM  information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db . "' AND TABLE_NAME = '"
            . $matching_tables[$table_counter] . "' AND COLUMN_NAME ='" . $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] .
            "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";

            $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);

            // If column is a foreign key then it is checked that referenced table
            // exist in target db. If referenced table does not exist in target
            // db then it is created first.
            if (isset($is_fk_result)) {
                if (in_array($is_fk_result[0]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
                    $table_index = array_keys($uncommon_tables, $is_fk_result[0]['REFERENCED_TABLE_NAME']);
                    PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link, $is_fk_result[0]['REFERENCED_TABLE_NAME'], $uncommon_tables, $uncommon_tables_fields, $display);
                    PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields, $display);
                    unset($uncommon_tables[$table_index[0]]);
                }
                $fk_query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$table_counter]) .
                            "ADD CONSTRAINT FOREIGN KEY " . PMA_backquote($add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]]) . "
                            (" . $add_column_array[$table_counter][$matching_tables_fields[$table_counter][$i]] . ") REFERENCES " . PMA_backquote($trg_db) .
                             '.' . PMA_backquote($is_fk_result[0]['REFERENCED_TABLE_NAME']) . " (" . $is_fk_result[0]['REFERENCED_COLUMN_NAME'] . ");";

                PMA_DBI_try_query($fk_query, $trg_link, null);
            }
        }
    }
}
/**
 * Checks if the referenced table have foreign keys.
 * uses    PMA_createTargetTables()
 *
 * @param string $src_db                  name of source database
 * @param mixed  $src_link                connection established with source server
 * @param string $trg_db                  name of target database
 * @param mixed  $trg_link                connection established with target server
 * @param string $referenced_table        table whose column is a foreign key in
 *                                        another table
 * @param array  &$uncommon_tables        names that are uncommon
 * @param array  &$uncommon_tables_fields field names of the uncommon table
 * @param bool   $display                 whether to display query
 *
 * @return nothing
 */
function PMA_checkForeignKeys($src_db, $src_link, $trg_db, $trg_link,
    $referenced_table, &$uncommon_tables, &$uncommon_tables_fields, $display
) {
    $is_fk_query = "SELECT * FROM  information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $src_db . "'
                    AND TABLE_NAME = '" . $referenced_table . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";

    $is_fk_result = PMA_DBI_fetch_result($is_fk_query, null, null, $src_link);
    if (count($is_fk_result) > 0) {
        for ($j = 0; $j < count($is_fk_result); $j++) {
            if (in_array($is_fk_result[$j]['REFERENCED_TABLE_NAME'], $uncommon_tables)) {
                $table_index = array_keys($uncommon_tables, $is_fk_result[$j]['REFERENCED_TABLE_NAME']);
                PMA_checkForeignKeys(
                    $src_db, $src_link, $trg_db, $trg_link, $is_fk_result[$j]['REFERENCED_TABLE_NAME'],
                    $uncommon_tables, $uncommon_tables_fields, $display
                );
                PMA_createTargetTables($src_db, $trg_db, $trg_link, $src_link, $uncommon_tables, $table_index[0], $uncommon_tables_fields, $display);
                unset($uncommon_tables[$table_index[0]]);
            }
        }
    }
}
/**
 * Alters structure of the target table using $alter_str_array
 *
 * @param string $trg_db                 name of target database
 * @param mixed  $trg_link               connection established with target server
 * @param array  $matching_tables        names of matching tables
 * @param array  &$source_columns        columns information of the source table
 * @param array  &$alter_str_array       column name and criteria which is to be
 *                                       altered for the targert table
 * @param array  $matching_tables_fields name of the fields for the matching table
 * @param array  $criteria               criteria
 * @param array  &$matching_tables_keys  field names which is key in the source
 *                                       table
 * @param array  &$target_tables_keys    field names which is key in the target
 *                                       table
 * @param int    $matching_table_index   number of the matching table
 * @param bool   $display                whether to display query
 *
 * @return nothing
 */
function PMA_alterTargetTableStructure($trg_db, $trg_link, $matching_tables,
    &$source_columns, &$alter_str_array, $matching_tables_fields, $criteria,
    &$matching_tables_keys, &$target_tables_keys, $matching_table_index, $display
) {
    $check = true;
    $sql_query = '';
    $found = false;

    //Checks if the criteria to be altered is primary key
    for ($v = 0; $v < count($matching_tables_fields[$matching_table_index]); $v++) {
        if (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$v]]['Key'])) {
            if ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$v]]['Key'] == 'PRI' ) {
                $check = false;
            }
        }
    }

    $pri_query = null;
    if (! $check) {
        $pri_query = "ALTER TABLE " . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]);
        if (count($target_tables_keys[$matching_table_index]) > 0) {
            $pri_query .= "  DROP PRIMARY KEY ," ;
        }
        $pri_query .= "  ADD PRIMARY KEY (";
        for ($z = 0; $z < count($matching_tables_keys[$matching_table_index]); $z++) {
            $pri_query .= PMA_backquote($matching_tables_keys[$matching_table_index][$z]);
            if ($z < (count($matching_tables_keys[$matching_table_index]) - 1)) {
                $pri_query .= " , " ;
            }
        }
        $pri_query .= ");";
    }

    if (isset($pri_query)) {
        if ($display == true) {
            echo '<p>' . $pri_query . '</p>';
        }
        PMA_DBI_try_query($pri_query, $trg_link, 0);
    }
    for ($t = 0; $t < count($matching_tables_fields[$matching_table_index]); $t++) {
        if ((isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]])) && (count($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]) > 0)) {
            $sql_query = 'ALTER TABLE ' . PMA_backquote($trg_db) . '.' . PMA_backquote($matching_tables[$matching_table_index]) . ' MODIFY ' .
            PMA_backquote($matching_tables_fields[$matching_table_index][$t]) . ' ' . $source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'];
            $found = false;
            for ($i = 0; $i < count($criteria); $i++) {
                if (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]]) && $criteria[$i] != 'Key') {
                    $found = true;
                    if (($criteria[$i] == 'Type') && (! isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i+1]]))) {
                        if ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i + 1]] == 'NO') {
                            $sql_query .= " Not Null" ;
                        } elseif ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i + 1]] == 'YES') {
                            $sql_query .= " Null" ;
                        }
                    }
                    if (($criteria[$i] == 'Null') && ( $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'NO')) {
                        $sql_query .= " Not Null "  ;
                    } elseif (($criteria[$i] == 'Null') && ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'YES')) {
                        $sql_query .= " Null "  ;
                    }
                    if ($criteria[$i] == 'Collation') {
                        if ( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
                            $sql_query .= " Not Null " ;
                        }
                        $sql_query .=  " COLLATE " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] ;
                    }
                    if (($criteria[$i] == 'Default') && ($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] == 'None')) {
                        if ( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
                            $sql_query .= " Not Null " ;
                        }
                    } elseif ($criteria[$i] == 'Default') {
                        if (! (isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
                            $sql_query .= " Not Null " ;
                        }
                        if (is_string($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]])) {
                            if ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'] != 'timestamp') {
                                $sql_query .=  " DEFAULT '" . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] . "'";
                            } elseif ($source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]]['Type'] == 'timestamp') {
                                $sql_query .=  " DEFAULT " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]];
                            }
                        } elseif (is_numeric($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]])) {
                            $sql_query .=  " DEFAULT " . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]];
                        }
                    }
                    if ($criteria[$i] == 'Comment') {
                        if ( !(isset($alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[2]]))) {
                            $sql_query .= " Not Null " ;
                        }
                        $sql_query .=  " COMMENT '" . $alter_str_array[$matching_table_index][$matching_tables_fields[$matching_table_index][$t]][$criteria[$i]] . "'" ;
                    }
                }
            }
        }
        $sql_query .= ";";
        if ($found) {
            if ($display == true) {
                echo '<p>' . $sql_query . '</p>';
            }
            PMA_DBI_try_query($sql_query, $trg_link, 0);
        }
    }
    $check = false;
    $query = "ALTER TABLE " . PMA_backquote($trg_db) . '.'
        . PMA_backquote($matching_tables[$matching_table_index]);
    for ($p = 0; $p < count($matching_tables_keys[$matching_table_index]); $p++) {
        if ((isset($alter_str_array[$matching_table_index][$matching_tables_keys[$matching_table_index][$p]]['Key']))) {
            $check = true;
            $query .= ' MODIFY ' . PMA_backquote($matching_tables_keys[$matching_table_index][$p]) . ' '
            . $source_columns[$matching_table_index][$matching_tables_fields[$matching_table_index][$p]]['Type'] . ' Not Null ';
            if ($p < (count($matching_tables_keys[$matching_table_index]) - 1)) {
                $query .= ', ';
            }
        }
    }
    $query .= ';';
    if ($check) {
        if ($display == true) {
            echo '<p>' . $query . '</p>';
        }
        PMA_DBI_try_query($query, $trg_link, 0);
    }
}

/**
 * Removes the columns which are present in target table but not in source table.
 *
 * @param string $trg_db           name of target database
 * @param mixed  $trg_link         connection established with target server
 * @param array  $matching_tables  names of matching tables
 * @param array  $uncommon_columns array containing the names of the column which
 *                                 are to be dropped from the target table
 * @param int    $table_counter    index of the matching table as in
 *                                 $matchiing_tables array
 * @param bool   $display          whether to display query
 *
 * @return nothing
 */
function PMA_removeColumnsFromTargetTable($trg_db, $trg_link, $matching_tables,
    $uncommon_columns, $table_counter, $display
) {
    if (isset($uncommon_columns[$table_counter])) {
        $drop_query = "ALTER TABLE " . PMA_backquote($trg_db) . "."
            . PMA_backquote($matching_tables[$table_counter]);
        for ($a = 0; $a < count($uncommon_columns[$table_counter]); $a++) {
            //Checks if column to be removed is a foreign key in any table
            $pk_query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . $trg_db . "'
                         AND REFERENCED_TABLE_NAME = '" . $matching_tables[$table_counter]."' AND REFERENCED_COLUMN_NAME = '"
                         . $uncommon_columns[$table_counter][$a] . "' AND TABLE_NAME <> REFERENCED_TABLE_NAME;";

            $pk_query_result = PMA_DBI_fetch_result($pk_query, null, null, $trg_link);
            $result_size = count($pk_query_result);

            if ($result_size > 0) {
                for ($b = 0; $b < $result_size; $b++) {
                    $drop_pk_query = "ALTER TABLE " . PMA_backquote($pk_query_result[$b]['TABLE_SCHEMA']) . "." . PMA_backquote($pk_query_result[$b]['TABLE_NAME']) . "
                                      DROP FOREIGN KEY " . PMA_backquote($pk_query_result[$b]['CONSTRAINT_NAME']) . ", DROP COLUMN " . PMA_backquote($pk_query_result[$b]['COLUMN_NAME']) . ";";
                    PMA_DBI_try_query($drop_pk_query, $trg_link, 0);
                }
            }
            $query = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '" . $trg_db . "' AND TABLE_NAME = '"
                     . $matching_tables[$table_counter]. "' AND COLUMN_NAME = '" . $uncommon_columns[$table_counter][$a] . "'
                      AND TABLE_NAME <> REFERENCED_TABLE_NAME;";

            $result = PMA_DBI_fetch_result($query, null, null, $trg_link);

            if (count($result) > 0) {
                $drop_query .= " DROP FOREIGN KEY " . PMA_backquote($result[0]['CONSTRAINT_NAME']) . ",";
            }
            $drop_query .=  " DROP COLUMN " . PMA_backquote($uncommon_columns[$table_counter][$a]);
            if ($a < (count($uncommon_columns[$table_counter]) - 1)) {
                $drop_query .= " , " ;
            }
        }
        $drop_query .= ";" ;

        if ($display == true) {
            echo '<p>' . $drop_query . '</p>';
        }
        PMA_DBI_try_query($drop_query, $trg_link, 0);
    }
}

/**
 * PMA_indexesDiffInTables() compares the source table indexes with target
 * table indexes and keep the indexes to be added in target table in
 * $add_indexes_array indexes to be altered in $alter_indexes_array and indexes
 * to be removed from target table in $remove_indexes_array.  Only keyname and
 * uniqueness characteristic of the indexes are altered.
 *
 * @param string $src_db                name of source database
 * @param string $trg_db                name of target database
 * @param mixed  $src_link              connection established with source server
 * @param mixed  $trg_link              connection established with target server
 * @param array  $matching_tables       matching tables name
 * @param array  &$source_indexes       indexes of the source table
 * @param array  &$target_indexes       indexes of the target table
 * @param array  &$add_indexes_array    name of the column on which the index
 *                                      is to be added in the target table
 * @param array  &$alter_indexes_array  key name which needs to be altered
 * @param array  &$remove_indexes_array key name of the index which is to be
 *                                      removed from the target table
 * @param int    $table_counter         number of the matching table
 *
 * @return nothing
 */
function PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link,
    $matching_tables, &$source_indexes, &$target_indexes, &$add_indexes_array,
    &$alter_indexes_array, &$remove_indexes_array, $table_counter
) {
    //Gets indexes information for source and target table
    $source_indexes[$table_counter] = PMA_DBI_get_table_indexes($src_db, $matching_tables[$table_counter], $src_link);
    $target_indexes[$table_counter] = PMA_DBI_get_table_indexes($trg_db, $matching_tables[$table_counter], $trg_link);
    for ($a = 0; $a < count($source_indexes[$table_counter]); $a++) {
        $found = false;
        $z = 0;
        //Compares key name and non_unique characteristic of source indexes with target indexes
        /*
         * @todo compare the length of each sub part
         */
        while (($z <= count($target_indexes[$table_counter])) && ($found == false)) {
            if (isset($source_indexes[$table_counter][$a]) && isset($target_indexes[$table_counter][$z]) && $source_indexes[$table_counter][$a]['Key_name'] == $target_indexes[$table_counter][$z]['Key_name']) {
                $found = true;
                if (($source_indexes[$table_counter][$a]['Column_name'] != $target_indexes[$table_counter][$z]['Column_name']) || ($source_indexes[$table_counter][$a]['Non_unique'] != $target_indexes[$table_counter][$z]['Non_unique'])) {
                    if (! (($source_indexes[$table_counter][$a]['Key_name'] == "PRIMARY") || ($target_indexes[$table_counter][$z]['Key_name'] == 'PRIMARY'))) {
                        $alter_indexes_array[$table_counter][] = $source_indexes[$table_counter][$a]['Key_name'];
                    }
                }
            }
            $z++;
        }
        if ($found === false) {
            if (! ($source_indexes[$table_counter][$a]['Key_name'] == 'PRIMARY')) {
                $add_indexes_array [$table_counter][] = $source_indexes[$table_counter][$a]['Column_name'];
            }
        }
    }

    //Finds indexes that exist on target table but not on source table
    for ($b = 0; $b < count($target_indexes[$table_counter]); $b++) {
        $found = false;
        $c = 0;
        while (($c <= count($source_indexes[$table_counter])) && ($found == false)) {
            if ($target_indexes[$table_counter][$b]['Column_name'] == $source_indexes[$table_counter][$c]['Column_name']) {
                $found = true;
            }
            $c++;
        }
        if ($found === false) {
            $remove_indexes_array[$table_counter][] = $target_indexes[$table_counter][$b]['Key_name'];
        }
    }
}

/**
 * PMA_applyIndexesDiff() create indexes, alters indexes and remove indexes.
 *
 * @param string $trg_db               name of target database
 * @param mixed  $trg_link             connection established with target server
 * @param array  $matching_tables      matching tables name
 * @param array  $source_indexes       indexes of the source table
 * @param array  $target_indexes       indexes of the target table
 * @param array  $add_indexes_array    column names on which indexes are to be
 *                                     created in target table
 * @param array  $alter_indexes_array  column names for which indexes are to be
 *                                     altered
 * @param array  $remove_indexes_array key name of the indexes which are to be
 *                                     removed from the target table
 * @param int    $table_counter        number of the matching table
 * @param bool   $display              whether to display query
 *
 * @return nothing
 */
function PMA_applyIndexesDiff($trg_db, $trg_link, $matching_tables, $source_indexes,
    $target_indexes, $add_indexes_array, $alter_indexes_array,
    $remove_indexes_array, $table_counter, $display
) {
    //Adds indexes on target table
    if (isset($add_indexes_array[$table_counter])) {
        $sql = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]) . " ADD" ;
        for ($a = 0; $a < count($source_indexes[$table_counter]); $a++) {
            if (isset($add_indexes_array[$table_counter][$a])) {
                for ($b = 0; $b < count($source_indexes[$table_counter]); $b++) {
                    if ($source_indexes[$table_counter][$b]['Column_name'] == $add_indexes_array[$table_counter][$a]) {
                        if ($source_indexes[$table_counter][$b]['Non_unique'] == '0') {
                            $sql .= " UNIQUE ";
                        }
                        $sql .= " INDEX " . PMA_backquote($source_indexes[$table_counter][$b]['Key_name']) . " (" . $add_indexes_array[$table_counter][$a] . " );";
                        if ($display == true) {
                            echo '<p>' . $sql . '</p>';
                        }
                        PMA_DBI_try_query($sql, $trg_link, 0);
                    }
                }
            }
        }
    }
    //Alter indexes of target table

    if (isset($alter_indexes_array[$table_counter])) {
        $query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
        for ($a = 0; $a < count($alter_indexes_array[$table_counter]); $a++) {
            if (isset($alter_indexes_array[$table_counter][$a])) {
                $query .= ' DROP INDEX ' . PMA_backquote($alter_indexes_array[$table_counter][$a]) . " , ADD ";
                $got_first_index_column = false;
                for ($z = 0; $z < count($source_indexes[$table_counter]); $z++) {
                    if ($source_indexes[$table_counter][$z]['Key_name'] == $alter_indexes_array[$table_counter][$a]) {
                        if (! $got_first_index_column) {
                            if ($source_indexes[$table_counter][$z]['Non_unique'] == '0') {
                                $query .= " UNIQUE ";
                            }
                            $query .= " INDEX " . PMA_backquote($source_indexes[$table_counter][$z]['Key_name']) . " (" . PMA_backquote($source_indexes[$table_counter][$z]['Column_name']);
                            $got_first_index_column = true;
                        } else {
                            // another column for this index
                            $query .= ', ' . PMA_backquote($source_indexes[$table_counter][$z]['Column_name']);
                        }
                    }
                }
                $query .= " )";
            }
        }
        $query .= ';';
        if ($display == true) {
            echo '<p>' . $query . '</p>';
        }
        PMA_DBI_try_query($query, $trg_link, 0);
    }
    //Removes indexes from target table
    if (isset($remove_indexes_array[$table_counter])) {
        $drop_index_query = "ALTER TABLE " . PMA_backquote($trg_db) . "." . PMA_backquote($matching_tables[$table_counter]);
        for ($a = 0; $a < count($target_indexes[$table_counter]); $a++) {
            if (isset($remove_indexes_array[$table_counter][$a])) {
                $drop_index_query .= " DROP INDEX " . PMA_backquote($remove_indexes_array[$table_counter][$a]);
            }
            if ($a < (count($remove_indexes_array[$table_counter]) - 1)) {
                $drop_index_query .= " , " ;
            }
        }
        $drop_index_query .= " ; " ;
        if ($display == true) {
            echo '<p>' . $drop_index_query . '</p>';
        }
        PMA_DBI_try_query($drop_index_query, $trg_link, 0);
    }
}

/**
 * PMA_displayQuery() displays a query, taking the maximum display size
 * into account
 *
 * @param string $query the query to display
 *
 * @return void
 */
function PMA_displayQuery($query)
{
    if (strlen($query) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
        $query = substr($query, 0, $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) . '[...]';
    }
    echo '<p>' . htmlspecialchars($query) . '</p>';
}

/**
 * PMA_syncDisplayHeaderCompare() shows the header for source database
 *
 * @param string $src_db source db name
 * @param string $trg_db target db name
 *
 * @return void
 */
function PMA_syncDisplayHeaderCompare($src_db, $trg_db)
{
    echo '<fieldset style="padding:0"><div style="padding:1.5em; overflow:auto; height:220px">';

    echo '<table class="data">';
    echo '<tr>';
    echo '<th>' . __('Source database') . ':  '
        . htmlspecialchars($src_db) . '<br />(';
    if ('cur' == $_SESSION['src_type']) {
        echo __('Current server');
    } else {
        echo __('Remote server') . ' '
            . htmlspecialchars($_SESSION['src_server']['host']);
    }
    echo ')</th>';
    echo '<th>' . __('Difference') . '</th>';
    echo '<th>' . __('Target database') . ':  '
        . htmlspecialchars($trg_db) . '<br />(';
    if ('cur' == $_SESSION['trg_type']) {
        echo __('Current server');
    } else {
        echo __('Remote server') . ' '
            . htmlspecialchars($_SESSION['trg_server']['host']);
    }
    echo ')</th>';
    echo '</tr>';
}

/**
 * Prints table row
 *
 * $rows contains following keys:
 * - src_table_name - source server table name
 * - dst_table_name - target server table name
 * - btn_type - 'M' or 'U'
 * - btn_structure - null or arguments for showDetails in server_synchronize.js
 *           (without img_obj and table_name):
 *           i, update_size, insert_size, remove_size, insert_index, remove_index
 *
 * @param array $rows
 *
 * @return nothing
 */
function PMA_syncDisplayDataCompare($rows)
{
    global $pmaThemeImage;

    $odd_row = true;
    foreach ($rows as $row) {
        echo '<tr class=" ' . ($odd_row ? 'odd' : 'even') . '">';
        echo '<td>' . htmlspecialchars($row['src_table_name']) . '</td><td style="text-align:center">';
        if (isset($row['btn_structure']) && $row['btn_structure']) {
            // parameters: i, update_size, insert_size, remove_size, insert_index, remove_index
            $p = $row['btn_structure'];
            $p[0] = $row['btn_type'] . 'S' . $p[0];
            echo '<img class="icon struct_img" src="' . $pmaThemeImage . 'new_struct.png" width="16" height="16"
                 alt="Structure" title="' . __('Click to select') . '" style="cursor:pointer" onclick="showDetails('
                 . "'" . implode($p, "','") . "'"
                 . ', this, ' . "'" . PMA_escapeJsString(htmlspecialchars($row['src_table_name'])) . "'" . ')" /> ';
        }
        if (isset($row['btn_data']) && $row['btn_data']) {
            // parameters: i, update_size, insert_size, remove_size, insert_index, remove_index
            $p = $row['btn_data'];
            $p[0] = $row['btn_type'] . 'D' . $p[0];
            echo '<img class="icon data_img" src="' . $pmaThemeImage . 'new_data.png" width="16" height="16"
                alt="Data" title="' . __('Click to select') . '" style="cursor:pointer" onclick="showDetails('
                . "'" . implode($p, "','") . "'"
                . ', this, ' . "'" . PMA_escapeJsString(htmlspecialchars($row['src_table_name'])) . "'" . ')" />';
        }
        echo '</td><td>' . htmlspecialchars($row['dst_table_name']) . '</td></tr>';
        $odd_row = !$odd_row;
    }
}

/**
 * Fetches column values
 *
 * @param string $database name of database
 * @param string $table    name of table to retrieve columns from
 * @param string $column   name of the column to retrieve data from
 * @param mixed  $link     mysql link resource
 *
 * @return array $field_values
 */
function PMA_getColumnValues($database, $table, $column, $link = null)
{
    $query = 'SELECT ';
    for ($i = 0; $i < count($column); $i++) {
        $query.= PMA_backquote($column[$i]);
        if ($i < (count($column)-1)) {
            $query.= ', ';
        }
    }
    $query.= ' FROM ' . PMA_backquote($database) . '.' . PMA_backquote($table);
    $field_values = PMA_DBI_fetch_result($query, null, null, $link);

    if (! is_array($field_values) || count($field_values) < 1) {
        return false;
    }
    return $field_values;
}
?>