-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathDataUtils.php
More file actions
188 lines (171 loc) · 6.48 KB
/
Copy pathDataUtils.php
File metadata and controls
188 lines (171 loc) · 6.48 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php // phpcs:ignore PSR1.Files.SideEffects.FoundWithSymbols -- Script directly executed
namespace ScummVM;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../orm/config.php';
require_once __DIR__ . '/../include/Constants.php';
use League\Csv\Reader;
use League\Csv\Statement;
use Symfony\Component\Yaml\Yaml;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use GuzzleHttp\Psr7\Response;
use Propel\Runtime\Propel;
use Propel\Runtime\Connection\Exception\RollbackException;
use Propel\Runtime\Map\TableMap;
/**
* DataUtils
* This class pulls down the latest data from the ScummVM Data spreadsheet
* and converts it to YAML files used to power the site.
*/
class DataUtils
{
// phpcs:ignore Generic.Files.LineLength
const SHEET_URL = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQamumX0p-DYQa5Umi3RxX-pHM6RZhAj1qvUP0jTmaqutN9FwzyriRSXlO9rq6kR60pGIuPvCDzZL3s/pub?output=tsv';
// filename => sheet id
const SHEET_IDS = [
'platforms' => '1061029686',
'compatibility' => '1989596967',
'games' => '1775285192',
'engines' => '0',
'companies' => '226191984',
'versions' => '1225902887',
'game_demos' => '1303420306',
'series' => '1095671818',
'screenshots' => '168506355',
'scummvm_downloads' => '1057392663',
'game_downloads' => '810295288',
'director_demos' => '1256563740',
];
const OBJECT_NAMES = [
'platforms' => 'Platform',
'engines' => 'Engine',
'companies' => 'Company',
'versions' => 'Version',
'series' => 'Series',
'games' => 'Game',
'compatibility' => 'Compatibility',
'game_demos' => 'Demo',
'screenshots' => 'Screenshot',
'scummvm_downloads' => 'Download',
'game_downloads' => 'GameDownload',
'director_demos' => 'DirectorDemo',
];
/**
* Gets the TSV representation from sheets and converts it to YAML on file
*
* @return bool True if update is successful
*/
public static function updateData(): bool
{
$client = new Client();
$promises = [];
foreach (self::SHEET_IDS as $name => $gid) {
$url = self::SHEET_URL . "&gid=" . $gid;
$promises[$name] = $client->getAsync($url);
$promises[$name]->then(function ($response) use ($name) {
self::doUpdateData($name, $response);
});
}
Promise\Utils::unwrap($promises);
$ret = DataUtils::convertYamlToOrm();
// Clear the cache at the end of all data operations
\file_put_contents('.clear-cache', '');
return $ret;
}
private static function doUpdateData(string $name, Response $response): void
{
$tsv = $response->getBody();
$reader = Reader::fromString($tsv);
$reader->setDelimiter("\t");
$reader->setHeaderOffset(0);
$stmt = new Statement();
$records = $stmt->process($reader);
// Convert to JSON because records are serializable
// and cannot be converted directly to yaml
$json = \json_encode($records);
if ($json === false) {
throw new \Exception("Can't encode data as JSON");
}
$data = \json_decode($json, true);
// Convert TRUE/FALSE strings to Booleans
foreach ($data as $objKey => $obj) {
foreach ($obj as $key => $val) {
if ($val === 'TRUE') {
$data[$objKey][$key] = true;
} elseif ($val === 'FALSE') {
$data[$objKey][$key] = false;
}
}
}
// Convert to YAML
$yaml = Yaml::dump($data);
$yaml = "# This is a generated file, please do not edit manually\n" . $yaml;
$outFile = DIR_DATA . "/" . DEFAULT_LOCALE . "/$name.yaml";
echo("Writing $name data to $outFile\n");
\file_put_contents($outFile, $yaml);
}
private static function convertYamlToOrm(): bool
{
$ret = true;
foreach (self::OBJECT_NAMES as $name => $object) {
$failures = array();
$query = "ScummVM\\OrmObjects\\{$object}Query";
if ($query::create()->count() > 0) {
continue;
}
$file = DIR_DATA . "/" . DEFAULT_LOCALE . "/$name.yaml";
$data = Yaml::parseFile($file);
$data = array_values($data);
echo "Writing $object data to database\n";
$class = "ScummVM\\OrmObjects\\$object";
$mapClass = $class::TABLE_MAP;
$con = Propel::getConnection($mapClass::DATABASE_NAME);
do {
$newFailures = array();
// Use a transation to speed up writes
$con->beginTransaction();
foreach ($data as $i => $item) {
if (in_array($i, $failures)) {
continue;
}
foreach ($item as $key => $val) {
if ($val === '') {
unset($item[$key]);
}
}
$dbItem = new $class();
// TODO: Rename platform to platform_id
if ($object === 'Demo' || $object === 'DirectorDemo') {
$item['platform_id'] = $item['platform'];
}
try {
$dbItem->fromArray($item, TableMap::TYPE_FIELDNAME);
$dbItem->save($con);
} catch (\Exception $ex) {
$newFailures[] = $i;
echo json_encode($item) . "\n";
echo $ex->getMessage() . "\n";
$prev = $ex->getPrevious();
if ($prev) {
echo " > " . $prev->getMessage() . "\n";
}
}
}
try {
$con->commit();
// If we could commit, we are done
break;
} catch (RollbackException) {
$con->rollback();
}
$failures = array_merge($failures, $newFailures);
// If there are no new failure, we will get an infinite loop
} while (count($newFailures));
if (count($failures)) {
$ret = false;
}
}
return $ret;
}
}
exit(DataUtils::updateData() ? 0 : 1);