|
38 | 38 | use Doctrine\ORM\NoResultException;
|
39 | 39 | use Doctrine\ORM\Query\Expr\Join;
|
40 | 40 | use Doctrine\ORM\QueryBuilder;
|
| 41 | +use Exception; |
41 | 42 | use InvalidArgumentException;
|
42 | 43 | use Psr\Log\LoggerInterface;
|
43 | 44 | use ReflectionClass;
|
|
63 | 64 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
64 | 65 | use Symfony\Component\Security\Core\User\UserInterface;
|
65 | 66 | use Twig\Environment;
|
| 67 | +use z4kn4fein\SemVer\Version; |
66 | 68 | use ZipArchive;
|
67 | 69 |
|
68 | 70 | class DOMJudgeService
|
@@ -107,6 +109,8 @@ public function __construct(
|
107 | 109 | protected string $projectDir,
|
108 | 110 | #[Autowire('%domjudge.vendordir%')]
|
109 | 111 | protected string $vendorDir,
|
| 112 | + #[Autowire('%domjudge.version%')] |
| 113 | + protected readonly string $domjudgeVersion, |
110 | 114 | ) {}
|
111 | 115 |
|
112 | 116 | /**
|
@@ -1671,4 +1675,59 @@ public function getAllowedLanguagesForContest(?Contest $contest) : array {
|
1671 | 1675 | ->getQuery()
|
1672 | 1676 | ->getResult();
|
1673 | 1677 | }
|
| 1678 | + |
| 1679 | + public function checkNewVersion(): string|false { |
| 1680 | + if (!$this->config->get('check_new_version')) { |
| 1681 | + return false; |
| 1682 | + } |
| 1683 | + $versionLocalString = explode("/", str_replace("DEV", "-prerelease", $this->domjudgeVersion))[0]; |
| 1684 | + $versionLocal = Version::parse($versionLocalString, false); |
| 1685 | + $versionUrl = 'https://versions.domjudge.org'; |
| 1686 | + $options = ['http' => ['method' => 'GET', 'header' => "User-Agent: tarball/" . $versionLocalString . "\r\n"]]; |
| 1687 | + $context = stream_context_create($options); |
| 1688 | + $response = @file_get_contents($versionUrl, false, $context); |
| 1689 | + if ($response === false) { |
| 1690 | + return false; |
| 1691 | + } |
| 1692 | + $versions = json_decode($response, true); |
| 1693 | + /* Steer towards to the latest patch first |
| 1694 | + * the user can see on the website if there is a new Major/minor themselves |
| 1695 | + * otherwise the latest minor, or Major release. So the user might make the upgrade path: |
| 1696 | + * DJ6.0.0 -> DJ6.0.6 -> DJ6.6.0 -> DJ9.0.0 instead of |
| 1697 | + * -> DJ6.0.[1..6] -> DJ6.[1..6] -> DJ[7..9].0.0 |
| 1698 | + */ |
| 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; |
| 1715 | + } |
| 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; |
| 1727 | + } |
| 1728 | + } catch (Exception $e) { |
| 1729 | + return false; |
| 1730 | + } |
| 1731 | + return false; |
| 1732 | + } |
1674 | 1733 | }
|
0 commit comments