Skip to content

Commit c1f774c

Browse files
committed
feat: added check if maintenance mode is enabled, refactored API usage
1 parent 7c68fa6 commit c1f774c

File tree

8 files changed

+86
-32
lines changed

8 files changed

+86
-32
lines changed

phpmyfaq/admin/assets/src/api/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ export * from './category';
22
export * from './faqs';
33
export * from './glossary';
44
export * from './group';
5+
export * from './news';
56
export * from './statistics';
67
export * from './tags';
8+
export * from './upgrade';
79
export * from './user';
8-
export * from './news';

phpmyfaq/admin/assets/src/api/tags.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @package phpMyFAQ
99
* @author Thorsten Rinne <[email protected]>
10-
* @copyright 2023 phpMyFAQ Team
10+
* @copyright 2023-2024 phpMyFAQ Team
1111
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
1212
* @link https://www.phpmyfaq.de
1313
* @since 2023-04-12
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Upgrade API functionality
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public License,
5+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
* obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* @package phpMyFAQ
9+
* @author Thorsten Rinne <[email protected]>
10+
* @copyright 2024 phpMyFAQ Team
11+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
12+
* @link https://www.phpmyfaq.de
13+
* @since 2024-05-30
14+
*/
15+
16+
export const fetchHealthCheck = async () => {
17+
try {
18+
const response = await fetch(`./api/health-check`, {
19+
method: 'GET',
20+
cache: 'no-cache',
21+
headers: {
22+
'Content-Type': 'application/json',
23+
},
24+
redirect: 'follow',
25+
referrerPolicy: 'no-referrer',
26+
});
27+
28+
return await response.json();
29+
} catch (error) {
30+
console.error(error);
31+
}
32+
};

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
* @link https://www.phpmyfaq.de
1414
* @since 2023-07-11
1515
*/
16+
1617
import { addElement } from '../../../../assets/src/utils';
18+
import { fetchHealthCheck } from '../api';
1719

1820
export const handleCheckForUpdates = () => {
1921
const checkHealthButton = document.getElementById('pmf-button-check-health');
@@ -24,37 +26,29 @@ export const handleCheckForUpdates = () => {
2426

2527
// Health Check
2628
if (checkHealthButton) {
27-
checkHealthButton.addEventListener('click', (event) => {
29+
checkHealthButton.addEventListener('click', async (event) => {
2830
event.preventDefault();
29-
fetch(window.location.pathname + 'api/health-check', {
30-
method: 'POST',
31-
headers: {
32-
Accept: 'application/json, text/plain, */*',
33-
'Content-Type': 'application/json',
34-
},
35-
})
36-
.then(async (response) => {
37-
if (response.ok) {
38-
return response.json();
39-
}
40-
throw new Error('Network response was not ok: ', { cause: { response } });
41-
})
42-
.then((response) => {
43-
const result = document.getElementById('result-check-health');
44-
const card = document.getElementById('pmf-update-step-health-check');
45-
if (result) {
46-
card.classList.add('text-bg-success');
47-
if (response.success === 'ok') {
48-
result.replaceWith(addElement('p', { innerText: response.message }));
49-
} else {
50-
result.replaceWith(addElement('p', { innerText: response.message }));
51-
}
52-
}
53-
})
54-
.catch(async (error) => {
31+
try {
32+
const responseData = await fetchHealthCheck();
33+
const result = document.getElementById('result-check-health');
34+
const card = document.getElementById('pmf-update-step-health-check');
35+
36+
if (responseData.success) {
37+
card.classList.add('text-bg-success');
38+
result.replaceWith(addElement('p', { innerText: responseData.success }));
39+
}
40+
if (responseData.error) {
41+
card.classList.add('text-bg-danger');
42+
result.replaceWith(addElement('p', { innerText: responseData.error }));
43+
}
44+
} catch (error) {
45+
if (error.cause && error.cause.response) {
5546
const errorMessage = await error.cause.response.json();
5647
console.error(errorMessage);
57-
});
48+
} else {
49+
console.error(error.message);
50+
}
51+
}
5852
});
5953
}
6054

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,29 @@ public function healthCheck(): JsonResponse
5656
$dateLastChecked = $dateTime->format(DateTimeInterface::ATOM);
5757
$upgrade = new Upgrade(new System(), $configuration);
5858

59+
if (!$upgrade->isMaintenanceEnabled()) {
60+
return $this->json(
61+
[
62+
'error' => Translation::get('msgNotInMaintenanceMode'),
63+
'dateLastChecked' => $dateLastChecked,
64+
],
65+
Response::HTTP_CONFLICT
66+
);
67+
}
68+
5969
try {
6070
$upgrade->checkFilesystem();
6171
return $this->json(
6272
[
63-
'message' => Translation::get('healthCheckOkay'),
73+
'success' => Translation::get('healthCheckOkay'),
6474
'dateLastChecked' => $dateLastChecked,
6575
],
6676
Response::HTTP_OK
6777
);
6878
} catch (Exception $exception) {
6979
return $this->json(
7080
[
71-
'message' => $exception->getMessage(),
81+
'error' => $exception->getMessage(),
7282
'dateLastChecked' => $dateLastChecked,
7383
],
7484
Response::HTTP_BAD_REQUEST

phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Upgrade extends Setup
4646

4747
private bool $isNightly;
4848

49+
private bool $isMaintenanceEnabled = false;
50+
4951
public function __construct(protected System $system, private readonly Configuration $configuration)
5052
{
5153
parent::__construct($this->system);
@@ -404,4 +406,17 @@ public function setIsNightly(bool $isNightly): void
404406
{
405407
$this->isNightly = $isNightly;
406408
}
409+
410+
public function isMaintenanceEnabled(): bool
411+
{
412+
return $this->isMaintenanceEnabled = $this->configuration->get('main.maintenanceMode');
413+
}
414+
415+
public function setIsMaintenanceEnabled(bool $isMaintenanceEnabled): Upgrade
416+
{
417+
$this->isMaintenanceEnabled = $isMaintenanceEnabled;
418+
$this->configuration->set('main.maintenanceMode', $isMaintenanceEnabled);
419+
420+
return $this;
421+
}
407422
}

phpmyfaq/translations/language_de.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,5 +1425,6 @@
14251425

14261426
// added v4.0.0-alpha.2 - 2024-04-30 by Thorsten
14271427
$PMF_LANG['msgNoQuestionAndAnswer'] = 'Keine Frage und Antwort gefunden.';
1428+
$PMF_LANG['msgNotInMaintenanceMode'] = 'Die FAQ ist nicht im Wartungs-Modus.';
14281429

14291430
return $PMF_LANG;

phpmyfaq/translations/language_en.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,5 +1445,6 @@
14451445

14461446
// added v4.0.0-alpha.2 - 2024-04-30 by Thorsten
14471447
$PMF_LANG['msgNoQuestionAndAnswer'] = 'No question and answer found.';
1448+
$PMF_LANG['msgNotInMaintenanceMode'] = 'The FAQ is not in maintenance mode.';
14481449

14491450
return $PMF_LANG;

0 commit comments

Comments
 (0)