Merge pull request 'Apply standard licensing + install-completion installer script pattern' (#202) from chore/installer-script-template into main

This commit was merged in pull request #202.
This commit is contained in:
2026-07-04 20:27:26 +00:00
+56 -61
View File
@@ -12,6 +12,7 @@ defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Router\Route;
class Pkg_MokoSuiteBackupInstallerScript
@@ -73,22 +74,25 @@ class Pkg_MokoSuiteBackupInstallerScript
/* Save download key before Joomla re-registers the update site */
if ($type === 'update') {
$this->preflight_saveKey();
$this->backupDownloadKey();
}
return true;
}
/**
* Called before install/update to preserve the download key.
*
* Joomla re-registers update sites from the manifest on every update,
* which can reset the extra_query (download key). We save it here
* and restore it in postflight.
* The download key cached during preflight so it survives an update.
*/
private ?string $savedDownloadKey = null;
public function preflight_saveKey(): void
/**
* Cache the existing download key from the update sites table before update runs.
*
* Joomla re-registers update sites from the manifest on every update, which
* can reset the extra_query (download key). We save it here and restore it
* in postflight.
*/
private function backupDownloadKey(): void
{
try {
$db = Factory::getDbo();
@@ -108,19 +112,16 @@ class Pkg_MokoSuiteBackupInstallerScript
->where($db->quoteName('e.element') . ' = ' . $db->quote('pkg_mokosuitebackup'))
->where($db->quoteName('e.type') . ' = ' . $db->quote('package'))
->setLimit(1);
$db->setQuery($query);
$key = $db->loadResult();
if (!empty($key)) {
$this->savedDownloadKey = $key;
$db->setQuery($query);
$extraQuery = (string) $db->loadResult();
if (!empty($extraQuery)) {
parse_str($extraQuery, $output);
$this->savedDownloadKey = $output['dlid'] ?? $extraQuery;
}
} catch (\Exception $e) {
error_log('MokoSuiteBackup: Could not save download key: ' . $e->getMessage());
Factory::getApplication()->enqueueMessage(
'MokoSuiteBackup could not preserve your download/license key before the update. '
. 'Please verify your license key is still configured in System → Update Sites after this update completes.',
'warning'
);
Log::add('MokoSuiteBackup: Could not backup download key: ' . $e->getMessage(), Log::WARNING, 'jerror');
}
}
@@ -138,8 +139,8 @@ class Pkg_MokoSuiteBackupInstallerScript
return;
}
/* Restore download key if it was saved before update */
if ($this->savedDownloadKey !== null) {
/* Restore the download key preserved before the update re-registered the site */
if ($type === 'update') {
$this->restoreDownloadKey();
}
@@ -168,14 +169,17 @@ class Pkg_MokoSuiteBackupInstallerScript
/* Sync submenu icons in #__menu (Joomla doesn't update icons on upgrades) */
$this->syncMenuIcons();
/* Warn if no license key configured */
$this->warnMissingLicenseKey();
/* Migrate profiles with old default backup_dir values to [DEFAULT_DIR] placeholder */
$this->migrateDefaultBackupDir();
/* Remind user to review backup profile settings */
/* Install completion notice (install and update) */
$this->installSuccessful();
if ($type === 'install') {
/* Fresh install never carries a download key — prompt for one */
$this->warnMissingLicenseKey();
/* Remind user to review backup profile settings */
$profileUrl = Route::_('index.php?option=com_mokosuitebackup&view=profiles');
Factory::getApplication()->enqueueMessage(
@@ -640,66 +644,57 @@ class Pkg_MokoSuiteBackupInstallerScript
->where($db->quoteName('e.element') . ' = ' . $db->quote('pkg_mokosuitebackup'))
->where($db->quoteName('e.type') . ' = ' . $db->quote('package'))
->setLimit(1);
$db->setQuery($query);
$updateSiteId = (int) $db->loadResult();
if ($updateSiteId > 0) {
if ($updateSiteId > 0 && !empty($this->savedDownloadKey)) {
$query = $db->getQuery(true)
->update($db->quoteName('#__update_sites'))
->set($db->quoteName('extra_query') . ' = ' . $db->quote($this->savedDownloadKey))
->set($db->quoteName('extra_query') . ' = ' . $db->quote('dlid=' . $this->savedDownloadKey))
->where($db->quoteName('update_site_id') . ' = ' . $updateSiteId);
$db->setQuery($query);
$db->execute();
}
} catch (\Exception $e) {
error_log('MokoSuiteBackup: Could not restore download key: ' . $e->getMessage());
Log::add('MokoSuiteBackup: Could not restore download key: ' . $e->getMessage(), Log::WARNING, 'jerror');
Factory::getApplication()->enqueueMessage(
'MokoSuiteBackup: Your download/license key could not be preserved during the update. '
. 'Please re-enter it in the Update Sites configuration to continue receiving updates.',
'<h4>MokoSuiteBackup</h4>'
. '<p>Your download/license key could not be preserved during the update.</p>'
. '<p>Please re-enter it in the <a class="btn btn-sm btn-warning ms-2" href="index.php?option=com_installer&view=updatesites&filter[search]=pkg_mokosuitebackup">Update Sites</a> manager to continue receiving updates.</p>',
'warning'
);
}
}
/**
* Show post-install license key prompt.
*/
private function warnMissingLicenseKey(): void
{
try
{
$db = Factory::getDbo();
$db->setQuery(
$db->getQuery(true)
->select([$db->quoteName('update_site_id'), $db->quoteName('extra_query')])
->from($db->quoteName('#__update_sites'))
->where('(' . $db->quoteName('name') . ' LIKE ' . $db->quote('%MokoSuiteBackup%') . ' OR ' . $db->quoteName('location') . ' LIKE ' . $db->quote('%MokoSuiteBackup%') . ')')
->setLimit(1)
);
$site = $db->loadObject();
if ($site)
{
$eq = (string) ($site->extra_query ?? '');
if (!empty($eq) && strpos($eq, 'dlid=') !== false) { parse_str($eq, $p); if (!empty($p['dlid'])) { return; } }
$editUrl = 'index.php?option=com_installer&task=updatesite.edit&update_site_id=' . (int) $site->update_site_id;
}
else
{
$editUrl = 'index.php?option=com_installer&view=updatesites';
}
try {
Factory::getApplication()->enqueueMessage(
'<strong>Moko Consulting License Key Required</strong> — '
. 'No download key is configured. Updates will not be available until a valid license key is entered. '
. '<a href="' . $editUrl . '" class="btn btn-sm btn-warning ms-2">Enter License Key</a>',
'<h4>MokoSuiteBackup License Key Required</h4>'
. '<p>A download/license key (DLID) is required to receive updates.</p>'
. '<p>Enter your key in the <a class="btn btn-sm btn-warning ms-2" href="index.php?option=com_installer&view=updatesites&filter[search]=pkg_mokosuitebackup">Update Sites</a> manager '
. 'or contact <a class="btn btn-sm btn-warning ms-2" href="https://mokoconsulting.tech/support" target="_blank" rel="noopener">Moko Consulting Support</a> to obtain one.</p>',
'warning'
);
}
catch (\Exception $e) {
error_log('MokoSuiteBackup: License key check failed: ' . $e->getMessage());
} catch (\Exception $e) {}
}
/**
* Show install successful prompt.
*/
private function installSuccessful(): void
{
try {
Factory::getApplication()->enqueueMessage(
'MokoSuiteBackup could not verify your license key status. '
. 'Please check System &rarr; Update Sites to ensure a valid license key is configured.',
'warning'
'<h4>MokoSuiteBackup installed successfully!</h4>',
'info'
);
}
} catch (\Exception $e) {}
}
}