-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.php
78 lines (71 loc) · 2.12 KB
/
generate.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
<?php
require './config/settings.php';
require './vendor/autoload.php';
use Checker\DrupalOrgClient;
global $conf;
$is_header = TRUE;
$header = [];
$data_path = __DIR__ . '/data/';
$dest_path = __DIR__ . '/dest/';
if (($handle = fopen($data_path . 'projects.csv', 'r')) !== FALSE) {
$options = [
'http_errors' => TRUE,
'timeout' => 120,
'verify' => FALSE,
];
/** @var \GuzzleHttp\Command\Guzzle\Description $description */
$description = DrupalOrgClient::getDescription();
/** @var Checker\DrupalOrgClient $client */
$client = DrupalOrgClient::create($options, $description);
while (($line = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($is_header) {
$is_header = FALSE;
$header = $line;
continue;
}
$row = [];
foreach ($header as $index => $column) {
$key = str_replace(' ', '_', strtolower($column));
$row[$key] = isset($line[$index]) ? $line[$index] : NULL;
}
$ts = time();
try {
$client->getAdditionalData($row, $conf['core']);
}
catch (\Exception $e) {
// Additional field for prod URL.
console_log("Error while processing !project project (!time)\n", ['!project' => $row['project'], '!time' => (time() - $ts) . 's']);
continue;
}
// Additional field for prod URL.
console_log("Processed !project project (!time)\n", ['!project' => $row['project'], '!time' => (time() - $ts) . 's']);
$rows[] = $row;
}
fclose($handle);
$data = [
'updated' => date('c'),
'metadata' => $rows
];
console_log("Generated metadata at !time.\n", ['!time' => $data['updated']]);
}
if (isset($data) && ($handle = fopen($dest_path . 'project-metadata.json', 'w+')) !== FALSE) {
if (!fwrite($handle, json_encode($data) . "\n")) {
console_log('Error while writing project-metadata.json');
}
fclose($handle);
}
else {
console_log('Error while opening projects.json to write');
exit(1);
}
/**
* Helper to log messages.
*
* @param string $message
*
* @param array $options
* An array of replacement string and translation.
*/
function console_log($message, array $options = []) {
print strtr($message, $options);
}