-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcmd-updateversiontags.php
More file actions
50 lines (40 loc) · 1.97 KB
/
Copy pathcmd-updateversiontags.php
File metadata and controls
50 lines (40 loc) · 1.97 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
<?php
chdir(dirname(__FILE__));
$config = array();
$config["basepath"] = getcwd() . '/';
$_SERVER["SERVER_NAME"] = "mods.vintagestory.at";
$_SERVER["REQUEST_URI"] = "";
define("DEBUG", 1);
include("lib/config.php");
include("lib/core.php");
if(DB_READONLY) {
http_response_code(HTTP_SERVICE_UNAVAILABLE);
exit();
}
$availableVersionsStrs = array_keys(json_decode(file_get_contents('http://api.vintagestory.at/stable-unstable.json'), true));
$availableVersions = [];
$malformedAvailableVersions = [];
foreach ($availableVersionsStrs as $versionStr) {
$compiled = compileSemanticVersion($versionStr);
if($compiled === false) $malformedAvailableVersions[] = $versionStr;
else $availableVersions[] = $compiled;
}
if($malformedAvailableVersions) {
$foldedErrors = implode(', ', array_map(fn($v) => "'$v'", $malformedAvailableVersions));
showErrorPage(HTTP_INTERNAL_ERROR, 'The following provided version strings do not parse as semantic versions: '.$foldedErrors.'. A semantic version must match /^\d+\.\d+\.\d+(-(dev|pre|rc)\.\d+)?$/.');
}
// Merge existing values with the new/current ones and recalculate the sort index.
//NOTE(Rennorb): The sort index is an ascending n+1 index that is used to find consecutive sequences of versions.
// We just use the index in the array after sorting the values for this. :VersionSortIndex
$storedVersions = array_map('intval', $con->getCol('SELECT `version` FROM gameVersions'));
$allVersions = array_unique(array_merge($storedVersions, $availableVersions));
sort($allVersions); // sort ascending so the keys are in the correct order
$foldedValues = implode(', ', array_map(fn($k, $v) => "($v, $k)", array_keys($allVersions), $allVersions));
// @security: All keys and values are numeric and therefore SQL inert.
$con->Execute(<<<SQL
INSERT INTO gameVersions (version, sortIndex)
VALUES $foldedValues
ON DUPLICATE KEY UPDATE
sortIndex = VALUES(sortIndex)
SQL);
echo count(array_diff($allVersions, $storedVersions)) . ' new versions added.';