Files
MokoWaaS/source/script.php
T

210 lines
5.8 KiB
PHP
Raw Normal View History

2026-06-07 16:36:27 +00:00
<?php
/**
* MokoWaaS → MokoSuite Migration Wrapper
*
* This script runs when existing MokoWaaS sites update to the migration
* package. It downloads and installs the MokoSuite package, which handles
* all table/param/extension migration in its own postflight().
*
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
* @license GNU General Public License version 3 or later
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Log\Log;
class Pkg_MokowaasInstallerScript
{
/** @var string URL to the MokoSuite stable release package */
private const MOKOSUITE_URL = 'https://git.mokoconsulting.tech/MokoConsulting/MokoSuite/releases/download/stable/pkg_mokosuite.zip';
/** @var string|null Saved download key from the old update site */
private ?string $savedDownloadKey = null;
public function preflight(string $type, InstallerAdapter $parent): bool
{
$this->saveDownloadKey();
return true;
}
public function postflight(string $type, InstallerAdapter $parent): void
{
$app = Factory::getApplication();
$app->enqueueMessage(
'<strong>MokoWaaS → MokoSuite Migration</strong><br>'
. 'Beginning migration from MokoWaaS to MokoSuite. '
. 'Downloading MokoSuite package...',
'notice'
);
2026-06-07 16:36:27 +00:00
// Download the MokoSuite package
$tmpPath = $app->get('tmp_path', sys_get_temp_dir());
2026-06-07 16:36:27 +00:00
$zipFile = $tmpPath . '/pkg_mokosuite_migration.zip';
// Append dlid if we have one
$url = self::MOKOSUITE_URL;
if ($this->savedDownloadKey)
{
$url .= (strpos($url, '?') === false ? '?' : '&') . $this->savedDownloadKey;
}
try
{
$http = \Joomla\CMS\Http\HttpFactory::getHttp();
$response = $http->get($url, [], 60);
if ($response->code !== 200)
{
$app->enqueueMessage(
'<strong>Migration Failed</strong> — Could not download MokoSuite package (HTTP ' . $response->code . '). '
2026-06-07 16:36:27 +00:00
. 'Please install MokoSuite manually from the Joomla extension manager.',
'error'
);
return;
}
file_put_contents($zipFile, $response->body);
}
catch (\Throwable $e)
{
$app->enqueueMessage(
'<strong>Migration Failed</strong> — Download error: ' . $e->getMessage() . '. '
2026-06-07 16:36:27 +00:00
. 'Please install MokoSuite manually.',
'error'
);
return;
}
// Install the MokoSuite package (its postflight handles all migration)
try
{
$installer = Installer::getInstance();
$result = $installer->install($zipFile);
if ($result)
{
// Transfer the download key to the new MokoSuite update site
$this->restoreDownloadKey();
2026-06-07 16:36:27 +00:00
$app->enqueueMessage(
'<div style="padding:15px;background:#d4edda;border:1px solid #c3e6cb;border-radius:6px;margin:10px 0;">'
. '<h3 style="margin:0 0 10px 0;color:#155724;">✓ Migration Complete!</h3>'
. '<p style="margin:0;color:#155724;">'
. 'Your MokoWaaS installation has been successfully migrated to <strong>MokoSuite</strong>.<br>'
. '• All plugin settings have been preserved<br>'
. '• Database tables have been migrated<br>'
. '• Helpdesk tickets and data are intact<br>'
. '• Your license key has been transferred<br><br>'
. 'MokoWaaS extensions have been removed. Future updates will come from MokoSuite.'
. '</p></div>',
2026-06-07 16:36:27 +00:00
'message'
);
Log::add('MokoWaaS → MokoSuite migration completed successfully', Log::INFO, 'mokosuite');
2026-06-07 16:36:27 +00:00
}
else
{
$app->enqueueMessage(
'<strong>Migration Warning</strong> — MokoSuite package install returned false. '
2026-06-07 16:36:27 +00:00
. 'Please check the Joomla installer log and install MokoSuite manually if needed.',
'warning'
);
}
}
catch (\Throwable $e)
{
$app->enqueueMessage(
'<strong>Migration Failed</strong> — Install error: ' . $e->getMessage(),
2026-06-07 16:36:27 +00:00
'error'
);
}
finally
{
if (is_file($zipFile))
{
@unlink($zipFile);
}
}
}
/**
* Save the download key from whichever update site exists.
*/
private function saveDownloadKey(): void
{
try
{
$db = Factory::getDbo();
foreach (['pkg_mokosuite', 'pkg_mokowaas'] as $element)
{
$db->setQuery(
$db->getQuery(true)
->select($db->quoteName('us.extra_query'))
->from($db->quoteName('#__update_sites', 'us'))
->join('INNER', $db->quoteName('#__update_sites_extensions', 'use') . ' ON use.update_site_id = us.update_site_id')
->join('INNER', $db->quoteName('#__extensions', 'e') . ' ON e.extension_id = use.extension_id')
->where($db->quoteName('e.element') . ' = ' . $db->quote($element))
->setLimit(1)
);
$key = $db->loadResult();
if (!empty($key) && strpos($key, 'dlid=') !== false)
{
$this->savedDownloadKey = $key;
break;
}
}
}
catch (\Throwable $e) {}
}
/**
* Copy the download key to the MokoSuite update site.
*/
private function restoreDownloadKey(): void
{
if ($this->savedDownloadKey === null)
{
return;
}
try
{
$db = Factory::getDbo();
$db->setQuery(
$db->getQuery(true)
->select($db->quoteName('us.update_site_id'))
->from($db->quoteName('#__update_sites', 'us'))
->join('INNER', $db->quoteName('#__update_sites_extensions', 'use') . ' ON use.update_site_id = us.update_site_id')
->join('INNER', $db->quoteName('#__extensions', 'e') . ' ON e.extension_id = use.extension_id')
->where($db->quoteName('e.element') . ' = ' . $db->quote('pkg_mokosuite'))
->setLimit(1)
);
$siteId = (int) $db->loadResult();
if ($siteId > 0)
{
$db->setQuery(
$db->getQuery(true)
->update($db->quoteName('#__update_sites'))
->set($db->quoteName('extra_query') . ' = ' . $db->quote($this->savedDownloadKey))
->where($db->quoteName('update_site_id') . ' = ' . $siteId)
)->execute();
Log::add('Migrated download key to MokoSuite update site', Log::INFO, 'mokosuite');
}
}
catch (\Throwable $e) {}
}
}