aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/vobject/bin/mergeduplicates.php
blob: e6cde73dd000895fa9f32f1b7dc8772e3c5f2835 (plain) (blame)
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
#!/usr/bin/env php
<?php

namespace Sabre\VObject;

// This sucks.. we have to try to find the composer autoloader. But chances
// are, we can't find it this way. So we'll do our bestest
$paths = [
    __DIR__.'/../vendor/autoload.php',  // In case vobject is cloned directly
    __DIR__.'/../../../autoload.php',   // In case vobject is a composer dependency.
];

foreach ($paths as $path) {
    if (file_exists($path)) {
        include $path;
        break;
    }
}

if (!class_exists('Sabre\\VObject\\Version')) {
    fwrite(STDERR, "Composer autoloader could not be loaded.\n");
    die(1);
}

echo 'sabre/vobject ', Version::VERSION, " duplicate contact merge tool\n";

if ($argc < 3) {
    echo "\n";
    echo 'Usage: ', $argv[0], " input.vcf output.vcf [debug.log]\n";
    die(1);
}

$input = fopen($argv[1], 'r');
$output = fopen($argv[2], 'w');
$debug = isset($argv[3]) ? fopen($argv[3], 'w') : null;

$splitter = new Splitter\VCard($input);

// The following properties are ignored. If they appear in some vcards
// but not in others, we don't consider them for the sake of finding
// differences.
$ignoredProperties = [
    'PRODID',
    'VERSION',
    'REV',
    'UID',
    'X-ABLABEL',
];

$collectedNames = [];

$stats = [
    'Total vcards' => 0,
    'No FN property' => 0,
    'Ignored duplicates' => 0,
    'Merged values' => 0,
    'Error' => 0,
    'Unique cards' => 0,
    'Total written' => 0,
];

function writeStats()
{
    global $stats;
    foreach ($stats as $name => $value) {
        echo str_pad($name, 23, ' ', STR_PAD_RIGHT), str_pad($value, 6, ' ', STR_PAD_LEFT), "\n";
    }
    // Moving cursor back a few lines.
    echo "\033[".count($stats).'A';
}

function write($vcard)
{
    global $stats, $output;

    ++$stats['Total written'];
    fwrite($output, $vcard->serialize()."\n");
}

while ($vcard = $splitter->getNext()) {
    ++$stats['Total vcards'];
    writeStats();

    $fn = isset($vcard->FN) ? (string) $vcard->FN : null;

    if (empty($fn)) {
        // Immediately write this vcard, we don't compare it.
        ++$stats['No FN property'];
        ++$stats['Unique cards'];
        write($vcard);
        $vcard->destroy();
        continue;
    }

    if (!isset($collectedNames[$fn])) {
        $collectedNames[$fn] = $vcard;
        ++$stats['Unique cards'];
        continue;
    } else {
        // Starting comparison for all properties. We only check if properties
        // in the current vcard exactly appear in the earlier vcard as well.
        foreach ($vcard->children() as $newProp) {
            if (in_array($newProp->name, $ignoredProperties)) {
                // We don't care about properties such as UID and REV.
                continue;
            }
            $ok = false;
            foreach ($collectedNames[$fn]->select($newProp->name) as $compareProp) {
                if ($compareProp->serialize() === $newProp->serialize()) {
                    $ok = true;
                    break;
                }
            }

            if (!$ok) {
                if ('EMAIL' === $newProp->name || 'TEL' === $newProp->name) {
                    // We're going to make another attempt to find this
                    // property, this time just by value. If we find it, we
                    // consider it a success.
                    foreach ($collectedNames[$fn]->select($newProp->name) as $compareProp) {
                        if ($compareProp->getValue() === $newProp->getValue()) {
                            $ok = true;
                            break;
                        }
                    }

                    if (!$ok) {
                        // Merging the new value in the old vcard.
                        $collectedNames[$fn]->add(clone $newProp);
                        $ok = true;
                        ++$stats['Merged values'];
                    }
                }
            }

            if (!$ok) {
                // echo $newProp->serialize() . " does not appear in earlier vcard!\n";
                ++$stats['Error'];
                if ($debug) {
                    fwrite($debug, "Missing '".$newProp->name."' property in duplicate. Earlier vcard:\n".$collectedNames[$fn]->serialize()."\n\nLater:\n".$vcard->serialize()."\n\n");
                }

                $vcard->destroy();
                continue 2;
            }
        }
    }

    $vcard->destroy();
    ++$stats['Ignored duplicates'];
}

foreach ($collectedNames as $vcard) {
    // Overwriting any old PRODID
    $vcard->PRODID = '-//Sabre//Sabre VObject '.Version::VERSION.'//EN';
    write($vcard);
    writeStats();
}

echo str_repeat("\n", count($stats)), "\nDone.\n";