feat: database-backed download key preservation for all extensions

Replace JSON file backup with #__mokowaas_download_keys table as
the persistent single source of truth for download keys.

- Core plugin: syncKeysToTable() copies keys from Joomla to our table,
  applyKeysFromTable() re-applies from our table to Joomla. Runs on
  every admin page load — Joomla can wipe keys all it wants.
- Install script: preflight saves to table, postflight re-applies.
- ExtensionsModel: saveDownloadKey(), applyDownloadKey(),
  reapplyAllDownloadKeys() static method for install/update hooks.
- Extension manager: prompt for download key on install, skip
  extensions with no release, show missing key warning badge.
- Catalog: expanded to 11 Joomla extensions.

Authored-by: Moko Consulting
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan Miller
2026-06-06 15:31:04 -05:00
parent 76f9da07a9
commit fc068866d9
5 changed files with 460 additions and 112 deletions
+152 -17
View File
@@ -109,8 +109,9 @@ class Pkg_MokowaasInstallerScript
// Clean up stale/duplicate update sites
$this->cleanupStaleUpdateSites();
// Restore download keys saved in preflight (before Joomla wiped them)
// Restore download keys: first from preflight backup, then from DB table
$this->restoreDownloadKeys($this->savedDownloadKeys);
$this->reapplyKeysFromDatabase();
// Fix orphaned update records (extension_id=0)
$this->fixUpdateRecords();
@@ -689,26 +690,12 @@ class Pkg_MokowaasInstallerScript
foreach ($rows as $row)
{
// Key by location so we can match after IDs change
$keys[$row->location] = $row->extra_query;
$keys['id_' . $row->update_site_id] = $row->extra_query;
}
// Also save to file backup for the preserveDownloadKeys() runtime guard
$backupFile = JPATH_ADMINISTRATOR . '/cache/mokowaas_dlkeys.json';
$existing = [];
if (file_exists($backupFile))
{
$existing = json_decode(file_get_contents($backupFile), true) ?: [];
}
foreach ($rows as $row)
{
$existing[$row->update_site_id] = $row->extra_query;
}
file_put_contents($backupFile, json_encode($existing, JSON_PRETTY_PRINT));
// Also save to our persistent database table
$this->syncKeysToDatabase($db, $rows);
}
catch (\Throwable $e)
{
@@ -718,6 +705,154 @@ class Pkg_MokowaasInstallerScript
return $keys;
}
/**
* Sync current download keys to the persistent #__mokowaas_download_keys table.
*/
private function syncKeysToDatabase($db, array $rows): void
{
try
{
// Check if table exists
$tables = $db->getTableList();
$prefix = $db->getPrefix();
if (!\in_array($prefix . 'mokowaas_download_keys', $tables, true))
{
return;
}
$now = gmdate('Y-m-d H:i:s');
foreach ($rows as $row)
{
parse_str($row->extra_query, $parsed);
$dlid = $parsed['dlid'] ?? '';
if (empty($dlid))
{
continue;
}
// Find the element for this update site
$db->setQuery(
$db->getQuery(true)
->select('e.' . $db->quoteName('element'))
->from($db->quoteName('#__update_sites_extensions', 'use'))
->join('INNER', $db->quoteName('#__extensions', 'e') . ' ON e.extension_id = use.extension_id')
->where($db->quoteName('use.update_site_id') . ' = ' . (int) $row->update_site_id),
0, 1
);
$element = (string) $db->loadResult();
if (empty($element))
{
continue;
}
// Upsert
$db->setQuery(
$db->getQuery(true)
->select('COUNT(*)')
->from($db->quoteName('#__mokowaas_download_keys'))
->where($db->quoteName('element') . ' = ' . $db->quote($element))
);
if ((int) $db->loadResult() > 0)
{
$db->setQuery(
$db->getQuery(true)
->update($db->quoteName('#__mokowaas_download_keys'))
->set($db->quoteName('dlid') . ' = ' . $db->quote($dlid))
->set($db->quoteName('location') . ' = ' . $db->quote($row->location))
->set($db->quoteName('modified') . ' = ' . $db->quote($now))
->where($db->quoteName('element') . ' = ' . $db->quote($element))
)->execute();
}
else
{
$db->setQuery(
$db->getQuery(true)
->insert($db->quoteName('#__mokowaas_download_keys'))
->columns([$db->quoteName('element'), $db->quoteName('location'), $db->quoteName('dlid'), $db->quoteName('created'), $db->quoteName('modified')])
->values(implode(',', [
$db->quote($element), $db->quote($row->location), $db->quote($dlid), $db->quote($now), $db->quote($now),
]))
)->execute();
}
}
}
catch (\Throwable $e)
{
// Non-critical — table may not exist yet
}
}
/**
* Re-apply all download keys from our persistent database table.
*/
private function reapplyKeysFromDatabase(): void
{
try
{
$db = Factory::getDbo();
$tables = $db->getTableList();
$prefix = $db->getPrefix();
if (!\in_array($prefix . 'mokowaas_download_keys', $tables, true))
{
return;
}
$db->setQuery(
$db->getQuery(true)
->select('*')
->from($db->quoteName('#__mokowaas_download_keys'))
->where($db->quoteName('dlid') . ' != ' . $db->quote(''))
);
$keys = $db->loadObjectList() ?: [];
$restored = 0;
foreach ($keys as $key)
{
$db->setQuery(
$db->getQuery(true)
->select('us.' . $db->quoteName('update_site_id'))
->from($db->quoteName('#__update_sites', 'us'))
->join('INNER', $db->quoteName('#__update_sites_extensions', 'use') . ' ON us.update_site_id = use.update_site_id')
->join('INNER', $db->quoteName('#__extensions', 'e') . ' ON e.extension_id = use.extension_id')
->where($db->quoteName('e.element') . ' = ' . $db->quote($key->element))
->where('(' . $db->quoteName('us.extra_query') . ' = ' . $db->quote('')
. ' OR ' . $db->quoteName('us.extra_query') . ' NOT LIKE ' . $db->quote('%dlid=%') . ')')
);
$siteIds = $db->loadColumn() ?: [];
foreach ($siteIds as $siteId)
{
$db->setQuery(
$db->getQuery(true)
->update($db->quoteName('#__update_sites'))
->set($db->quoteName('extra_query') . ' = ' . $db->quote('dlid=' . $key->dlid))
->where($db->quoteName('update_site_id') . ' = ' . (int) $siteId)
)->execute();
$restored++;
}
}
if ($restored > 0)
{
Factory::getApplication()->enqueueMessage(
sprintf('Re-applied %d download key(s) from persistent storage.', $restored),
'message'
);
}
}
catch (\Throwable $e)
{
// Non-critical
}
}
/**
* Restore download keys that were cleared by update site cleanup.
*