|
31 | 31 | use App\Entity\Testcase; |
32 | 32 | use App\Entity\User; |
33 | 33 | use App\Utils\FreezeData; |
| 34 | +use App\Utils\UpdateStrategy; |
34 | 35 | use App\Utils\Utils; |
35 | 36 | use DateTime; |
36 | 37 | use Doctrine\ORM\EntityManagerInterface; |
37 | 38 | use Doctrine\ORM\NonUniqueResultException; |
38 | 39 | use Doctrine\ORM\NoResultException; |
39 | 40 | use Doctrine\ORM\Query\Expr\Join; |
40 | 41 | use Doctrine\ORM\QueryBuilder; |
| 42 | +use Exception; |
41 | 43 | use InvalidArgumentException; |
42 | 44 | use Psr\Log\LoggerInterface; |
43 | 45 | use ReflectionClass; |
| 46 | +use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
44 | 47 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
45 | 48 | use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
46 | 49 | use Symfony\Component\HttpFoundation\Cookie; |
|
62 | 65 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
63 | 66 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
64 | 67 | use Symfony\Component\Security\Core\User\UserInterface; |
| 68 | +use Symfony\Contracts\Cache\ItemInterface; |
65 | 69 | use Twig\Environment; |
66 | 70 | use ZipArchive; |
67 | 71 |
|
@@ -108,6 +112,10 @@ public function __construct( |
108 | 112 | protected string $projectDir, |
109 | 113 | #[Autowire('%domjudge.vendordir%')] |
110 | 114 | protected string $vendorDir, |
| 115 | + #[Autowire('%domjudge.version%')] |
| 116 | + protected readonly string $domjudgeVersion, |
| 117 | + #[Autowire('%domjudge.installmethod%')] |
| 118 | + protected readonly string $domjudgeInstallMethod, |
111 | 119 | ) {} |
112 | 120 |
|
113 | 121 | /** |
@@ -1699,4 +1707,89 @@ public function getAllowedLanguagesForContest(?Contest $contest) : array { |
1699 | 1707 | ->getQuery() |
1700 | 1708 | ->getResult(); |
1701 | 1709 | } |
| 1710 | + |
| 1711 | + /** |
| 1712 | + * Returns either the next strictly higher version or false when nothing is found/requested. |
| 1713 | + */ |
| 1714 | + public function checkNewVersion(): string|false { |
| 1715 | + if ($this->config->get('check_new_version', false) === UpdateStrategy::Strategy_none) { |
| 1716 | + return false; |
| 1717 | + } |
| 1718 | + // The local version is something like "x.y.z / commit hash", e.g. "8.4.0DEV/4e25adb13" for development |
| 1719 | + // or 8.3.2 for a released version |
| 1720 | + // In case of development we remove the commit hash for some anonymity but keep the DEV to not count those as the (possibly) released version |
| 1721 | + $localVersionString = strtok($this->domjudgeVersion, "/"); |
| 1722 | + $localVersion = explode(".", $localVersionString); |
| 1723 | + if (count($localVersion) !== 3) { |
| 1724 | + // Unknown version, someone might have locally modified and used their own versioning |
| 1725 | + return false; |
| 1726 | + } |
| 1727 | + |
| 1728 | + $cache = new FilesystemAdapter(); |
| 1729 | + $versions = $cache->get('domjudge_versions', function (ItemInterface $item, string $localVersionString): string|false { |
| 1730 | + $item->expiresAfter(86400); |
| 1731 | + |
| 1732 | + $versionUrl = 'https://versions.domjudge.org'; |
| 1733 | + $options = ['http' => ['timeout' => 1, 'method' => 'GET', 'header' => "User-Agent: DOMjudge#" . $this->domjudgeInstallMethod . "/" . $localVersionString . "\r\n"]]; |
| 1734 | + $context = stream_context_create($options); |
| 1735 | + $response = @file_get_contents($versionUrl, false, $context); |
| 1736 | + if ($response === false) { |
| 1737 | + return false; |
| 1738 | + } |
| 1739 | + // Assume we get a one-level unordered JSON list with the released versions e.g. ["10.0.0", "9.11.0", "12.0.12", "10.0.1"] |
| 1740 | + $tmp_versions = json_decode($response, true); |
| 1741 | + natsort($tmp_versions); |
| 1742 | + return array_reverse($tmp_versions); |
| 1743 | + }); |
| 1744 | + |
| 1745 | + if (!$versions) { |
| 1746 | + return false; |
| 1747 | + } |
| 1748 | + |
| 1749 | + preg_match("/\d.\d.\d/", $this->domjudgeVersion, $matches); |
| 1750 | + $extractedLocalVersionString = $matches[0]; |
| 1751 | + if ($this->config->get('check_new_version', false) === UpdateStrategy::Strategy_incremental) { |
| 1752 | + /* Steer towards the nearest highest patch release first |
| 1753 | + * So the expected path would be: |
| 1754 | + * DJ6.0.0 -> DJ6.0.6 -> DJ6.6.0 -> DJ9.1.2 instead of |
| 1755 | + * -> DJ6.0.[1..6] -> DJ6.[1..6].* -> DJ[7..9].*.* |
| 1756 | + * skipping all patch releases in between, when no patch release |
| 1757 | + * is available, try the highest minor and otherwise the highest Major |
| 1758 | + * instead of going to the latest release: |
| 1759 | + * DJ6.0.0 -> DJ9.1.2 |
| 1760 | + */ |
| 1761 | + $patch = "/" . $localVersion[0] . "." . $localVersion[1] . ".\d/"; |
| 1762 | + $minor = "/" . $localVersion[0] . ".\d.\d/"; |
| 1763 | + $major = "/\d.\d.\d/"; |
| 1764 | + foreach ([$patch, $minor, $major] as $regex) { |
| 1765 | + foreach ($versions as $release) { |
| 1766 | + if (preg_match($regex, $release)) { |
| 1767 | + if (strnatcmp($release, $extractedLocalVersionString) === 1) { |
| 1768 | + return $release; |
| 1769 | + } |
| 1770 | + if (strnatcmp($release, $extractedLocalVersionString) === 0 && str_contains($localVersionString, "DEV")) { |
| 1771 | + // Special case, the development version is now released |
| 1772 | + return $release; |
| 1773 | + } |
| 1774 | + } |
| 1775 | + } |
| 1776 | + } |
| 1777 | + } |
| 1778 | + elseif ($this->config->get('check_new_version', false) === UpdateStrategy::Strategy_major_release) { |
| 1779 | + /* Steer towards the latest version directly |
| 1780 | + * So the expected path would be: |
| 1781 | + * DJ6.0.0 -> DJ9.1.2 |
| 1782 | + * This should be safe as doctrine migrations check for upgrades regardless of current DOMjudge release |
| 1783 | + */ |
| 1784 | + $latest = $versions[0]; |
| 1785 | + if (strnatcmp($latest, $extractedLocalVersionString) === 1) { |
| 1786 | + return $latest; |
| 1787 | + } |
| 1788 | + if (strnatcmp($latest, $extractedLocalVersionString) === 0 && str_contains($localVersionString, "DEV")) { |
| 1789 | + // Special case, the development version is now released |
| 1790 | + return $latest; |
| 1791 | + } |
| 1792 | + } |
| 1793 | + return false; |
| 1794 | + } |
1702 | 1795 | } |
0 commit comments