Skip to content

Commit 8346167

Browse files
committed
feat: added nightly and release paths for downloads (#2492)
1 parent df46ad0 commit 8346167

File tree

6 files changed

+112
-21
lines changed

6 files changed

+112
-21
lines changed

phpmyfaq/admin/assets/src/configuration/upgrade.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const handleCheckForUpdates = () => {
6363
if (downloadButton) {
6464
downloadButton.addEventListener('click', (event) => {
6565
event.preventDefault();
66-
fetch(window.location.pathname + 'api/download-package/3.2.1', {
66+
fetch(window.location.pathname + 'api/download-package/nightly', {
6767
method: 'POST',
6868
headers: {
6969
Accept: 'application/json, text/plain, */*',

phpmyfaq/admin/upgrade.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
use phpMyFAQ\Configuration;
1919
use phpMyFAQ\Enums\ReleaseType;
20-
use phpMyFAQ\Setup\Upgrade;
21-
use phpMyFAQ\System;
2220
use phpMyFAQ\Template\TwigWrapper;
2321
use phpMyFAQ\Translation;
2422
use phpMyFAQ\User\CurrentUser;
@@ -44,8 +42,6 @@
4442
'dateLastChecked' => $faqConfig->get('upgrade.dateLastChecked')
4543
];
4644

47-
$upgrade = new Upgrade(new System(), $faqConfig);
48-
4945
echo $template->render($templateVars);
5046
} else {
5147
require 'no-permission.php';

phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,15 @@ public function downloadPackage(Request $request): JsonResponse
104104
$versionNumber = Filter::filterVar($request->get('versionNumber'), FILTER_SANITIZE_SPECIAL_CHARS);
105105

106106
$upgrade = new Upgrade(new System(), Configuration::getConfigurationInstance());
107+
$upgrade->setIsNightly(true);
107108
$result = $upgrade->downloadPackage($versionNumber);
108109

109-
$response->setData(['success' => $result]);
110+
if ($result !== false) {
111+
$response->setStatusCode(Response::HTTP_OK);
112+
$response->setData(['success' => $result]);
113+
} else {
114+
$response->setStatusCode(Response::HTTP_BAD_GATEWAY);
115+
}
110116

111117
return $response;
112118
}

phpmyfaq/src/phpMyFAQ/Enums/DownloadHostType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
enum DownloadHostType: string
2121
{
22-
case GITHUB = 'https://github.com/thorsten/phpMyFAQ/releases/download/';
22+
case GITHUB = 'https://github.com/';
2323
case PHPMYFAQ = 'https://download.phpmyfaq.de/';
2424
}

phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace phpMyFAQ\Setup;
1818

19+
use JsonException;
1920
use Monolog\Level;
2021
use phpMyFAQ\Configuration;
2122
use phpMyFAQ\Enums\DownloadHostType;
@@ -31,6 +32,12 @@
3132

3233
class Upgrade extends Setup
3334
{
35+
public const GITHUB_PATH = 'thorsten/phpMyFAQ/releases/download/development-nightly-%s/';
36+
private const GITHUB_FILENAME = 'phpMyFAQ-nightly-%s.zip';
37+
private const PHPMYFAQ_FILENAME = 'phpMyFAQ-%s.zip';
38+
39+
private $isNightly = false;
40+
3441
public function __construct(protected System $system, private readonly Configuration $configuration)
3542
{
3643
parent::__construct($this->system);
@@ -49,14 +56,14 @@ public function checkFilesystem(): bool
4956
}
5057
}
5158
if (
52-
is_dir(PMF_CONTENT_DIR . '\user\attachments') && is_dir(PMF_CONTENT_DIR . '\user\images') && is_dir(
53-
PMF_CONTENT_DIR . '\core\data'
54-
) && is_dir(PMF_ROOT_DIR . '\assets\themes')
59+
is_dir(PMF_CONTENT_DIR . '\user\attachments') &&
60+
is_dir(PMF_CONTENT_DIR . '\user\images') &&
61+
is_dir(PMF_CONTENT_DIR . '\core\data') &&
62+
is_dir(PMF_ROOT_DIR . '\assets\themes')
5563
) {
5664
if (
57-
is_file(PMF_CONTENT_DIR . '\core\config\constants.php') && is_file(
58-
PMF_CONTENT_DIR . '\core\config\database.php'
59-
)
65+
is_file(PMF_CONTENT_DIR . '\core\config\constants.php') &&
66+
is_file(PMF_CONTENT_DIR . '\core\config\database.php')
6067
) {
6168
if ($this->configuration->isElasticsearchActive()) {
6269
if (!is_file(PMF_CONTENT_DIR . '\core\config\elasticsearch.php')) {
@@ -92,8 +99,7 @@ public function checkFilesystem(): bool
9299
*/
93100
public function downloadPackage(string $version): string|bool
94101
{
95-
$zipFile = 'phpMyFAQ-' . $version . '.zip';
96-
$url = DownloadHostType::PHPMYFAQ->value . $zipFile;
102+
$url = $this->getDownloadHost() . $this->getPath() . $this->getFilename($version);
97103

98104
$client = HttpClient::create();
99105

@@ -106,9 +112,9 @@ public function downloadPackage(string $version): string|bool
106112

107113
$package = $response->getContent();
108114

109-
file_put_contents(PMF_CONTENT_DIR . '/upgrades/' . $zipFile, $package);
115+
file_put_contents(PMF_CONTENT_DIR . '/upgrades/' . $this->getFilename($version), $package);
110116

111-
return PMF_CONTENT_DIR . '/upgrades/' . $zipFile;
117+
return PMF_CONTENT_DIR . '/upgrades/' . $this->getFilename($version);
112118
} catch (
113119
TransportExceptionInterface |
114120
ClientExceptionInterface |
@@ -124,10 +130,10 @@ public function downloadPackage(string $version): string|bool
124130
/**
125131
* Method to verify the downloaded phpMyFAQ package
126132
*
127-
* @return bool
128-
* @throws TransportExceptionInterface|ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|JsonException
129133
* @param string $path | Path to zip file
130134
* @param string $version | Version to verify
135+
* @return bool
136+
* @throws TransportExceptionInterface|ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|JsonException
131137
*/
132138
public function verifyPackage(string $path, string $version): bool
133139
{
@@ -161,7 +167,6 @@ public function verifyPackage(string $path, string $version): bool
161167
*
162168
* @return bool
163169
* @param string $path | Path of the package
164-
* @throws ZipException
165170
*/
166171
public function unpackPackage(string $path): bool
167172
{
@@ -187,7 +192,6 @@ public function unpackPackage(string $path): bool
187192
*
188193
* @param string $backupName | Name of the created backup
189194
* @return bool
190-
* @throws ZipException
191195
*/
192196
public function createTemporaryBackup(string $backupName): bool
193197
{
@@ -240,4 +244,60 @@ public function restoreTemporaryBackup()
240244
public function installPackage()
241245
{
242246
}
247+
248+
/**
249+
* Returns the host for download packages, so either github.com or download.phpmyfaq.de
250+
* @return string
251+
*/
252+
public function getDownloadHost(): string
253+
{
254+
if ($this->isNightly()) {
255+
return DownloadHostType::GITHUB->value;
256+
}
257+
258+
return DownloadHostType::PHPMYFAQ->value;
259+
}
260+
261+
/**
262+
* Returns the path to the download package, it's an empty string for development and production releases
263+
* @return string
264+
*/
265+
public function getPath(): string
266+
{
267+
if ($this->isNightly()) {
268+
return sprintf(self::GITHUB_PATH, date('Y-m-d', strtotime('-1 days')));
269+
}
270+
271+
return '';
272+
}
273+
274+
/**
275+
* Returns the filename of the download package
276+
* @param string $version
277+
* @return string
278+
*/
279+
public function getFilename(string $version): string
280+
{
281+
if ($this->isNightly()) {
282+
return sprintf(self::GITHUB_FILENAME, date('Y-m-d', strtotime('-1 days')));
283+
}
284+
285+
return sprintf(self::PHPMYFAQ_FILENAME, $version);
286+
}
287+
288+
/**
289+
* @return bool
290+
*/
291+
public function isNightly(): bool
292+
{
293+
return $this->isNightly;
294+
}
295+
296+
/**
297+
* @param bool $isNightly
298+
*/
299+
public function setIsNightly(bool $isNightly): void
300+
{
301+
$this->isNightly = $isNightly;
302+
}
243303
}

tests/phpMyFAQ/Setup/UpgradeTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use phpMyFAQ\Configuration;
66
use phpMyFAQ\Database\Sqlite3;
7+
use phpMyFAQ\Enums\DownloadHostType;
78
use phpMyFAQ\System;
89
use PHPUnit\Framework\TestCase;
910

@@ -58,4 +59,32 @@ public function testInstallPackage(): void
5859
{
5960
$this->markTestSkipped();
6061
}
62+
public function testGetDownloadHostForNightly(): void
63+
{
64+
$this->upgrade->setIsNightly(true);
65+
66+
$this->assertEquals(DownloadHostType::GITHUB->value, $this->upgrade->getDownloadHost());
67+
}
68+
69+
public function testGetDownloadHostForNonNightly(): void
70+
{
71+
$this->upgrade->setIsNightly(false);
72+
73+
$this->assertEquals(DownloadHostType::PHPMYFAQ->value, $this->upgrade->getDownloadHost());
74+
}
75+
76+
public function testGetPathForNightly(): void
77+
{
78+
$this->upgrade->setIsNightly(true);
79+
80+
$expectedPath = sprintf(Upgrade::GITHUB_PATH, date('Y-m-d', strtotime('-1 days')));
81+
$this->assertEquals($expectedPath, $this->upgrade->getPath());
82+
}
83+
84+
public function testGetPathForNonNightly(): void
85+
{
86+
$this->upgrade->setIsNightly(false);
87+
88+
$this->assertEquals('', $this->upgrade->getPath());
89+
}
6190
}

0 commit comments

Comments
 (0)