]> BookStack Code Mirror - devops/blob - meta-scripts/bookstack-update-translators
Added scripts used for deploy and translation attribution
[devops] / meta-scripts / bookstack-update-translators
1 #!/usr/bin/env php
2 <?php
3
4 // Script to fetch translators from crowdin via the API
5 // and format into a BookStack attribution file.
6 $key = getenv('CROWDIN_PROJECT_KEY');
7 if (!$key) {
8     echo "Crowdin project key needs to be set on [CROWDIN_PROJECT_KEY] environment variable to run this script";
9     exit(0);
10 }
11
12 // Get the location of the attribution report.
13 $reportLocation = getcwd() . '/.github/translators.txt';
14 if (!file_exists($reportLocation)) {
15     echo "Could not find the translators file at [{$reportLocation}]";
16     echo "Are you running this script from the BookStack root folder?";
17     exit(0);
18 }
19
20 $reportDelimiter = ' :: ';
21
22 $reportMap = loadExistingReportIntoMap($reportDelimiter, $reportLocation);
23 $csvReport = exportTopMembersReport($key);
24 $csvData = csv_to_array($csvReport);
25 mergeCsvDataIntoReportMap($reportMap, $csvData, $reportDelimiter);
26 formatAndWriteOutput($reportLocation, $reportMap, $reportDelimiter);
27
28 function formatAndWriteOutput(string $reportLocation, array $reportMap, string $reportDelimiter) {
29     $output = "Name :: Languages\n";
30     foreach ($reportMap as $name => $languages) {
31         if (count($languages) === 0 || (count($languages) === 1 && empty($languages[0]))) continue;
32         if ($name === 'Dan Brown (ssddanbrown)' || $name === 'Name') continue;
33         $output .= $name . $reportDelimiter . implode('; ', $languages) . "\n";
34     }
35
36     file_put_contents($reportLocation, $output);
37 }
38
39 function mergeCsvDataIntoReportMap(array &$reportMap, array $csvData, string $reportDelimiter) {
40     foreach ($csvData as $csvLine) {
41         $name = $csvLine['Name'];
42         $name = str_replace($reportDelimiter, '', $name);
43         $languages = explode('; ', $csvLine['Languages']);
44         if (isset($reportMap[$name])) {
45             $languages = array_unique(array_merge($languages, $reportMap[$name]));
46         }
47         $reportMap[$name] = $languages;
48     }
49 }
50
51 function loadExistingReportIntoMap($reportDelimiter, $reportLocation) {
52     try {
53         $reportData = file_get_contents($reportLocation);
54     } catch (Exception $exception) {
55         $reportData = '';
56     }
57     $reportLines = explode("\n", $reportData);
58     $reportMap = [];
59     foreach ($reportLines as $reportLine) {
60         if (empty($reportLine)) continue;
61         [$name, $langs] = explode($reportDelimiter, $reportLine);
62         $splitLangs = explode('; ', $langs);
63         $reportMap[$name] = $splitLangs;
64     }
65     return $reportMap;
66 }
67
68 function exportTopMembersReport($key) {
69     $result = makeMemberExportReport($key);
70
71     $exportHash = $result->hash;
72     $csv = downloadMemberReport($exportHash, $key);
73
74     return $csv;
75 }
76
77 function makeMemberExportReport(string $key) {
78     $url = 'https://api.crowdin.com/api/project/bookstack/reports/top-members/export';
79     $postData = [
80         'date_from' => '2019-10-01',
81         'date_to' => date('Y-m-d'),
82         'format' => 'csv',
83         'json' => true,
84         'key' => $key,
85     ];
86
87     $ch = curl_init();
88     curl_setopt($ch, CURLOPT_URL, $url);
89     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
90     curl_setopt($ch, CURLOPT_TIMEOUT, 15);
91     curl_setopt($ch, CURLOPT_POST, true);
92     curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
93
94     $result = curl_exec($ch);
95     curl_close($ch);
96
97     $data = json_decode($result);
98     return $data;
99 }
100
101 function downloadMemberReport(string $exportHash, string $key) {
102     $params = [
103         'hash' => $exportHash,
104         'key' => $key
105     ];
106     $url = 'https://api.crowdin.com/api/project/bookstack/reports/top-members/download';
107     $url .= '?' . http_build_query($params);
108     $ch = curl_init();
109     curl_setopt($ch, CURLOPT_URL, $url);
110     curl_setopt($ch, CURLOPT_TIMEOUT, 15);
111     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
112
113     $result = curl_exec($ch);
114     curl_close($ch);
115
116     return $result;
117 }
118
119 /**
120  * Convert a comma separated string into an associated array.
121  * @link http://gist.github.com/385876 (Modified)
122  * @author Jay Williams <http://myd3.com/> (Modified)
123  * @copyright Copyright (c) 2010, Jay Williams (Modified)
124  * @license http://www.opensource.org/licenses/mit-license.php MIT License
125  */
126 function csv_to_array(string $csvString): array
127 {
128
129     $header = null;
130     $data = [];
131     $lines = explode("\n", trim($csvString));
132     foreach ($lines as $line) {
133         $csvLine = str_getcsv($line);
134         if (!$header) {
135             $header = $csvLine;
136         } else {
137             $data[] = array_combine($header, $csvLine);
138         }
139     }
140
141     return $data;
142 }