forked from reisba/CsvDiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_diff.php
87 lines (82 loc) · 2.42 KB
/
csv_diff.php
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
<?php
if (count($argv) !== 4){
$filename = __FILE__;
echo "Run: {$filename} {existing_source_csv} {new_source_csv} {shared_unique_csv_cell_title}";
echo "\n";
echo "Example: {$filename} existing_data.csv current_production_data.csv ID";
echo "\n";
exit();
}
ini_set('auto_detect_line_endings',TRUE);
$existingTranslations = $argv[1];
$currentTranslationData = $argv[2];
$sharedUniqueId = $argv[3];
$handle = fopen($argv[1], 'r');
$uniqueIds = [];
$loop = 1;
$uniqueIdRowIndex = null;
while ($row = fgetcsv($handle, 0, ';')) {
if ($loop === 1){
$uniqueIdRowIndex = getIndexOf($sharedUniqueId, $row);
if (is_null($uniqueIdRowIndex)){
dumpRow($row);
throw new \LogicException("Cannot find unique header {$sharedUniqueId} in {$existingTranslations}");
}
}
if ($loop > 1){
$uniqueIds[] = $row[$uniqueIdRowIndex];
}
$loop++;
}
fclose($handle);
$handle = fopen($currentTranslationData, 'r');
$loop = 1;
$uniqueIdRowIndex = null;
$diffedRows = array();
$cSkipped = 0;
while ($row = fgetcsv($handle, 0, ';')) {
if ($loop === 1){
$uniqueIdRowIndex = getIndexOf($sharedUniqueId, $row);
if (is_null($uniqueIdRowIndex)){
dumpRow($row);
throw new \LogicException("Cannot find unique header {$sharedUniqueId} in {$currentTranslationData}");
}
// Title cells
$diffedRows[] = $row;
}
if ($loop > 1){
$id = $row[$uniqueIdRowIndex];
if (in_array($id, $uniqueIds)){
echo "Skipped (same unique id found): {$id} \n";
$cSkipped++;
} else {
$diffedRows[] = $row;
}
}
$loop++;
}
fclose($handle);
ini_set('auto_detect_line_endings',FALSE);
$filename = $currentTranslationData . ".diffed.csv";
$handle = fopen($filename, 'w');
foreach ($diffedRows as $item){
fputcsv($handle, $item, ';');
}
fclose($handle);
echo "Total rows skipped: {$cSkipped}"; echo "\n";
echo "New rows in {$currentTranslationData}: " . count($diffedRows); echo "\n";
echo "Diff written in: {$filename}"; echo "\n";
function getIndexOf($value, $arrayData) {
foreach ($arrayData as $index => $itemValue){
if ($itemValue === $value){
return $index;
}
}
return null;
}
function dumpRow($row){
echo "Content of the row:"; echo "\n";
foreach ($row as $index => $value){
echo "[{$index}] {$value}"; echo "\n";
}
}