Skip to content

Commit de80c58

Browse files
committed
Remove duplication
1 parent 378db01 commit de80c58

File tree

3 files changed

+20
-93
lines changed

3 files changed

+20
-93
lines changed

webapp/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@
111111
"twig/extra-bundle": "^3.5",
112112
"twig/markdown-extra": "^3.5",
113113
"twig/string-extra": "^3.5",
114-
"twig/twig": "^3.6",
115-
"z4kn4fein/php-semver": "^3.0"
114+
"twig/twig": "^3.6"
116115
},
117116
"require-dev": {
118117
"ext-dom": "*",

webapp/composer.lock

Lines changed: 2 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/src/Service/DOMJudgeService.php

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
6565
use Symfony\Component\Security\Core\User\UserInterface;
6666
use Twig\Environment;
67-
use z4kn4fein\SemVer\Version;
6867
use ZipArchive;
6968

7069
class DOMJudgeService
@@ -1679,54 +1678,37 @@ public function getAllowedLanguagesForContest(?Contest $contest) : array {
16791678
public function checkNewVersion(): string|false {
16801679
if (!$this->config->get('check_new_version')) {
16811680
return false;
1682-
}
1683-
$versionLocalString = explode("/", str_replace("DEV", "-prerelease", $this->domjudgeVersion))[0];
1684-
$versionLocal = Version::parse($versionLocalString, false);
1681+
}
1682+
$versionLocalString = explode("/", $this->domjudgeVersion)[0];
1683+
$patch = "/" . substr($this->domjudgeVersion, 0, strrpos($this->domjudgeVersion, '.')) . ".\d/";
1684+
$minor = "/" . substr($this->domjudgeVersion, 0, strpos($this->domjudgeVersion, '.')) . ".\d.\d/";
1685+
$major = "/\d.\d.\d/";
1686+
16851687
$versionUrl = 'https://versions.domjudge.org';
16861688
$options = ['http' => ['method' => 'GET', 'header' => "User-Agent: tarball/" . $versionLocalString . "\r\n"]];
16871689
$context = stream_context_create($options);
16881690
$response = @file_get_contents($versionUrl, false, $context);
1689-
if ($response === false) {
1691+
if ($response === false) {
16901692
return false;
16911693
}
16921694
$versions = json_decode($response, true);
1693-
/* Steer towards to the latest patch first
1695+
/* Steer towards to the latest patch first
16941696
* the user can see on the website if there is a new Major/minor themselves
16951697
* otherwise the latest minor, or Major release. So the user might make the upgrade path:
16961698
* DJ6.0.0 -> DJ6.0.6 -> DJ6.6.0 -> DJ9.0.0 instead of
16971699
* -> DJ6.0.[1..6] -> DJ6.[1..6] -> DJ[7..9].0.0
16981700
*/
1699-
$latestPatchString = $versionLocal;
1700-
if (isset($versions[$versionLocal->getMajor()][$versionLocal->getMinor()])) {
1701-
$latestPatchString = Version::rsortString($versions[$versionLocal->getMajor()][$versionLocal->getMinor()])[0];
1702-
$latestPatch = Version::parse($latestPatchString);
1703-
if (Version::compare($versionLocal, $latestPatch) < 0) {
1704-
return $latestPatchString;
1705-
}
1706-
}
1707-
$latestMinorString = $versionLocal;
1708-
if (isset($versions[$versionLocal->getMajor()])) {
1709-
$highestMinorInMajor = array_keys($versions[$versionLocal->getMajor()]);
1710-
rsort($highestMinorInMajor);
1711-
$latestMinorString = Version::rsortString($versions[$versionLocal->getMajor()][$highestMinorInMajor[0]])[0];
1712-
$latestMinor = Version::parse($latestMinorString);
1713-
if (Version::compare($versionLocal, $latestMinor) < 0) {
1714-
return $latestMinorString;
1701+
$newer_releases = [];
1702+
foreach ([$patch, $minor, $major] as $regex) {
1703+
foreach ($versions as $release) {
1704+
if (preg_match($regex, $release)) {
1705+
$newer_releases[] = $release;
1706+
}
17151707
}
1716-
}
1717-
$latestMajorString = $versionLocal;
1718-
try {
1719-
$highestMajor = array_keys($versions);
1720-
rsort($highestMajor);
1721-
$highestMinorInMajor = array_keys($versions[$highestMajor[0]]);
1722-
rsort($highestMinorInMajor);
1723-
$latestMajorString = Version::rsortString($versions[$highestMajor[0]][$highestMinorInMajor[0]])[0];
1724-
$latestMajor = Version::parse($latestMajorString);
1725-
if (Version::compare($versionLocal, $latestMajor) < 0) {
1726-
return $latestMajorString;
1708+
if (count($newer_releases) > 0) {
1709+
natsort($newer_releases);
1710+
return end($newer_releases);
17271711
}
1728-
} catch (Exception $e) {
1729-
return false;
17301712
}
17311713
return false;
17321714
}

0 commit comments

Comments
 (0)