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

UpdateRegionCodes.php « Commands « GeoIp2 « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 66f18aa706d053a106fe0b254133d6c5100dc83b (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
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\GeoIp2\Commands;

use Piwik\Development;
use Piwik\Http;
use Piwik\Plugin\ConsoleCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateRegionCodes extends ConsoleCommand
{
    public $source = 'https://salsa.debian.org/iso-codes-team/iso-codes/raw/master/data/iso_3166-2.json';

    protected function configure()
    {
        $this->setName('usercountry:update-region-codes');
        $this->setDescription("Updates the ISO region names");
    }

    public function isEnabled()
    {
        return Development::isEnabled();
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return void|int
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $regionsFile = __DIR__ . '/../data/isoRegionNames.php';

        $output->setDecorated(true);

        $output->writeln('Starting region codes update');

        $output->write('Fetching region codes from ' . $this->source);

        try {
            $newContent = Http::sendHttpRequest($this->source, 1000);
        } catch (\Exception $e) {
            $output->writeln(' <fg=red>X (Fetching content failed)</>');
            return 1;
        }

        $regionData = json_decode($newContent, true);

        if (empty($regionData)) {
            $output->writeln(' <fg=red>X (Content could not be parsed)</>');
            return 1;
        }

        $output->writeln(' <fg=green>✓</>');

        $newRegions = [];
        foreach ($regionData['3166-2'] as $region) {

            // some fixes of incorrect region codes
            if ($region['code'] === 'SS-EE8') {
                $region['code'] = 'SS-EE';
            }
            if ($region['code'] === 'ML-BK0') {
                $region['code'] = 'ML-BKO';
            }
            if ($region['code'] === 'IQ-SW') {
                $region['code'] = 'IQ-SU';
            }
            if ($region['code'] === 'MU-RP') {
                $region['code'] = 'MU-RR';
            }

            list($countryCode, $regionCode) = explode('-', $region['code']);
            $newRegions[$countryCode][$regionCode] = $region['name'];
        }

        $currentRegions = include $regionsFile;

        // regions for Saint Lucia missing in iso-codes
        if (empty($newRegions['LC']) && !empty($currentRegions['LC'])) {
            $newRegions['LC'] = $currentRegions['LC'];
        }

        // regions for Republic of Côte d'Ivoire still outdated in iso-codes
        $newRegions['CI'] = $currentRegions['CI'];

        // regions missing in iso-codes
        $isoCodesMissing = [
            'AR-F', 'BI-MY', 'DO-31', 'DO-32', 'DO-33', 'DO-34', 'DO-35', 'DO-36', 'DO-37', 'DO-38', 'DO-39', 'DO-40', 'DO-41', 'DO-42',
            'EG-LX', 'HT-NI', 'IQ-KI', 'IR-32', 'KG-GO', 'KZ-BAY', 'LR-GP', 'LR-RG', 'MK-85', 'QA-SH', 'SD-GK', 'SI-212', 'SI-213',
            'TH-38', 'TJ-DU', 'TJ-RA', 'TT-MRC', 'TT-TOB', 'YE-HU'
        ];

        foreach ($isoCodesMissing as $isoCode) {
            list($countryCode, $regionCode) = explode('-', $isoCode);

            if (!empty($newRegions[$countryCode][$regionCode])) {
                continue; // skip if it was already icnluded
            }

            $newRegions[$countryCode][$regionCode] = $currentRegions[$countryCode][$regionCode];
            ksort($newRegions[$countryCode], SORT_NATURAL);
        }

        ksort($newRegions);

        if (json_encode($newRegions) === json_encode($currentRegions)) {
            $output->writeln('Everything already up to date <fg=green>✓</>');
            return 0;
        }

        $content = <<<CONTENT
<?php
// Generated file containing all ISO region codes and names
return 
CONTENT;

        $content .= var_export($newRegions, true) . ';';

        file_put_contents($regionsFile, $content);

        $output->writeln('File successfully updated <fg=green>✓</>');
        return 0;

    }


}