Skip to content

Commit

Permalink
feat: rewrite of the update script using Twig (#2492)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Oct 23, 2023
1 parent 09e3f52 commit 75062f0
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 207 deletions.
4 changes: 3 additions & 1 deletion nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ server {
rewrite api/bookmark/([0-9]+) /api/index.php last;

# Setup APIs
rewrite api/setup/update /api/index.php last;
rewrite api/setup/check /api/index.php last;
rewrite api/setup/backup /api/index.php last;
rewrite api/setup/update-database /api/index.php last;

# Administration API
rewrite admin/api/dashboard/versions /admin/api/index.php last;
Expand Down
4 changes: 3 additions & 1 deletion phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ RewriteRule api/autocomplete api/index.php
RewriteRule api/bookmark/([0-9]+) api/index.php

# Setup APIs
RewriteRule api/setup/update api/index.php
RewriteRule api/setup/check api/index.php
RewriteRule api/setup/backup api/index.php
RewriteRule api/setup/update-database api/index.php

# Administration API
RewriteRule admin/api/dashboard/versions admin/api/index.php
Expand Down
114 changes: 111 additions & 3 deletions phpmyfaq/assets/src/configuration/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,120 @@
*/

export const handleUpdateNextStepButton = () => {
const nextStepButton = document.getElementById('phpmyfaq-update-next-step');
const nextStepButton = document.getElementById('phpmyfaq-update-next-step-button');
const nextStep = document.getElementById('phpmyfaq-update-next-step');

if (nextStepButton) {
if (nextStepButton && nextStep) {
nextStepButton.addEventListener('click', (event) => {
event.preventDefault();
window.location.replace('./update.php?step=2');
window.location.replace(`./update.php?step=${nextStep.value}`);
});
}
};

export const handleUpdateInformation = () => {
if (window.location.href.endsWith('update.php')) {
const installedVersion = document.getElementById('phpmyfaq-update-installed-version');

fetch('../../api/setup/check', {
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
body: installedVersion.value,
})
.then(async (response) => {
if (response.ok) {
return response.json();
}
throw new Error('Network response was not ok: ', { cause: { response } });
})
.then((data) => {
const button = document.getElementById('phpmyfaq-update-next-step-button');
const alert = document.getElementById('phpmyfaq-update-check-success');

alert.classList.remove('d-none');

button.classList.remove('disabled');
button.disabled = false;
})
.catch(async (error) => {
const errorMessage = await error.cause.response.json();
const alert = document.getElementById('phpmyfaq-update-check-alert');
const alertResult = document.getElementById('phpmyfaq-update-check-result');

alert.classList.remove('d-none');
alertResult.innerText = errorMessage.message;
});
}
};

export const handleConfigBackup = () => {
if (window.location.href.endsWith('update.php?step=2')) {
const installedVersion = document.getElementById('phpmyfaq-update-installed-version');

fetch('../../api/setup/backup', {
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
body: installedVersion.value,
})
.then(async (response) => {
if (response.ok) {
return response.json();
}
throw new Error('Network response was not ok: ', { cause: { response } });
})
.then((data) => {
const downloadLink = document.getElementById('phpmyfaq-update-backup-download-link');
downloadLink.href = data.backupFile;
})
.catch(async (error) => {
const errorMessage = await error.cause.response.json();
return errorMessage.error;
});
}
};

export const handleDatabaseUpdate = () => {
if (window.location.href.endsWith('update.php?step=3')) {
const installedVersion = document.getElementById('phpmyfaq-update-installed-version');

fetch('../../api/setup/update-database', {
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
body: installedVersion.value,
})
.then(async (response) => {
if (response.ok) {
return response.json();
}
throw new Error('Network response was not ok: ', { cause: { response } });
})
.then((data) => {
if (data.success) {
const alert = document.getElementById('phpmyfaq-update-database-success');
alert.classList.remove('d-none');
} else {
const alert = document.getElementById('phpmyfaq-update-database-error');
const errorList = document.getElementById('error-messages');
alert.classList.remove('d-none');
errorList.innerHTML = '<li>' + data.error + '</li>';
}
})
.catch(async (error) => {
const errorMessage = await error.cause.response.json();
const alert = document.getElementById('phpmyfaq-update-database-error');

alert.classList.remove('d-none');
alert.innerHTML = errorMessage.error;
å;
});
}
};
10 changes: 9 additions & 1 deletion phpmyfaq/assets/src/update.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { handleUpdateNextStepButton } from './configuration';
import {
handleConfigBackup,
handleDatabaseUpdate,
handleUpdateInformation,
handleUpdateNextStepButton,
} from './configuration';

handleUpdateNextStepButton();
handleUpdateInformation();
handleConfigBackup();
handleDatabaseUpdate();
4 changes: 2 additions & 2 deletions phpmyfaq/assets/templates/setup/update.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@

<div class="form-header d-flex my-4">
<span class="stepIndicator">Update information</span>
<span class="stepIndicator">File backups</span>
<span class="stepIndicator">Configuration backup</span>
<span class="stepIndicator">Database updates</span>
</div>

{{ include('setup/update/step1.twig') }}
{{ include('setup/update/step' ~ currentStep ~ '.twig') }}

</div>
</section>
Expand Down
40 changes: 11 additions & 29 deletions phpmyfaq/assets/templates/setup/update/step1.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<form action="#" method="post" name="phpmyfaq-update-form" id="phpmyfaq-update-form">
<input name="installed-version" type="hidden" value="{{ installedVersion }}">
<input name="installed-version" id="phpmyfaq-update-installed-version" type="hidden" value="{{ installedVersion }}">
<input name="next-step" id="phpmyfaq-update-next-step" type="hidden" value="2">

<div class="row">
Expand Down Expand Up @@ -36,43 +36,25 @@
<div class="row">
<div class="col">

{% if errorCheckPreUpgrade %}
<div class="alert alert-danger" role="alert">
<h4 class="alert-heading">Fatal error!</h4>
{{ errorMessagePreUpgrade }}
<div class="alert alert-success d-none" role="alert" id="phpmyfaq-update-check-success">
<h4 class="alert-heading">Well done!</h4>
<p>
Your phpMyFAQ installation check was successful. Please click on the button below to continue.
</p>
</div>
{% endif %}

{% if configTableNotAvailable %}
<div class="alert alert-danger" role="alert">
<h4 class="alert-heading">Fatal error!</h4>
Looks like there's a problem with the <code>faqconfig</code> table.
</div>
{% endif %}

{% if installedVersionTooOld %}
<div class="alert alert-danger" role="alert">
<h4 class="alert-heading">Attention</h4>
Your current installed version is phpMyFAQ {{ installedVersion }}.
Please update to the latest phpMyFAQ 3.0 version first.
</div>
{% endif %}

{% if isMaintenanceModeEnabled %}
<div class="alert alert-danger" role="alert">
<div class="alert alert-danger d-none" role="alert" id="phpmyfaq-update-check-alert">
<h4 class="alert-heading">Heads up!</h4>
Please enable the maintenance mode in the <a href="../admin/?action=config">admin section</a>
before running the update script.
</div>
{% endif %}
<div id="phpmyfaq-update-check-result"></div>
</div>

</div>
</div>

<div class="row">
<div class="col d-flex justify-content-end">
<button class="btn btn-primary btn-next btn-lg {{ nextStepButtonEnabled }}" type="button"
id="phpmyfaq-update-next-step" {{ nextStepButtonEnabled }}>
<button class="btn btn-primary btn-next btn-lg disabled" type="button"
id="phpmyfaq-update-next-step-button" disabled>
Next update step
</button>
</div>
Expand Down
31 changes: 31 additions & 0 deletions phpmyfaq/assets/templates/setup/update/step2.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<form action="#" method="post" name="phpmyfaq-update-form" id="phpmyfaq-update-form">
<input name="installed-version" id="phpmyfaq-update-installed-version" type="hidden" value="{{ installedVersion }}">
<input name="next-step" id="phpmyfaq-update-next-step" type="hidden" value="3">

<div class="row">
<div class="col mt-5">

<div class="alert alert-success">
<h4 class="alert-heading">Backup</h4>
<p>
Before we start the database update process, we created a backup of your current phpMyFAQ configuration files.
This is a safety measure in case something goes wrong during the update process.
</p>
<p>
<strong>Important:</strong> Please make sure that you have a copy of your
<a href="" id="phpmyfaq-update-backup-download-link">configuration.</a>
</p>
</div>

</div>
</div>

<div class="row">
<div class="col d-flex justify-content-end">
<button class="btn btn-primary btn-next btn-lg {{ nextStepButtonEnabled }}" type="button"
id="phpmyfaq-update-next-step-button" {{ nextStepButtonEnabled }}>
Next update step
</button>
</div>
</div>
</form>
30 changes: 30 additions & 0 deletions phpmyfaq/assets/templates/setup/update/step3.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<form action="#" method="post" name="phpmyfaq-update-form" id="phpmyfaq-update-form">
<input name="installed-version" id="phpmyfaq-update-installed-version" type="hidden" value="{{ installedVersion }}">

<div class="row">
<div class="col mt-5">

<div class="alert alert-success d-none" id="phpmyfaq-update-database-success">
<h4 class="alert-heading">Hooray!</h4>
The database was updated successfully. Thank you very much for updating.
</div>

<div class="alert alert-danger d-none" id="phpmyfaq-update-database-error">
<h4 class="alert-heading">Oh no!</h4>
<p>
The database could not be updated. Please check the following error message(s):
</p>
<ul class="list-unstyled" id="error-messages"></ul>
</div>

</div>
</div>

<div class="row">
<div class="col d-flex justify-content-end">
<a href="../index.php" class="btn btn-primary btn-next btn-lg" type="button">
Return to your updated phpMyFAQ installation
</a>
</div>
</div>
</form>
Loading

0 comments on commit 75062f0

Please sign in to comment.