Skip to content

Commit

Permalink
Error collector
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Aug 16, 2023
1 parent c537a50 commit baff877
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
51 changes: 25 additions & 26 deletions administrator/components/com_admin/script.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ public function setErrorCollector(callable $callback)
*/
protected function collectError(string $context, \Throwable $error)
{
call_user_func($this->errorCollector, $context, $error);
// The errorCollector are required
// However when someone already running the script manually the code may fail.
if ($this->errorCollector) {
call_user_func($this->errorCollector, $context, $error);
} else {
Log::add($error->getMessage(), Log::ERROR, 'Update');
}
}

/**
Expand Down Expand Up @@ -120,38 +126,28 @@ public function preflight($action, $installer)
*/
public function update($installer)
{
$options['format'] = '{DATE}\t{TIME}\t{LEVEL}\t{CODE}\t{MESSAGE}';
$options['text_file'] = 'joomla_update.php';

Log::addLogger($options, Log::ALL, ['Update', 'databasequery', 'jerror']);

// Uninstall plugins before removing their files and folders
try {
$this->uninstallRepeatableFieldsPlugin();
} catch (\Throwable $e) {
$msg = sprintf('Uninstalling Repeatable fields plugin failed: %s', $e->getMessage());
echo $msg . '<br>';
Log::add($msg, Log::ERROR, 'Update');
$this->collectError('uninstallRepeatableFieldsPlugin', $e);
}

try {
$this->uninstallEosPlugin();
} catch (\Throwable $e) {
$msg = sprintf('Uninstalling EOS plugin failed: %s', $e->getMessage());
echo $msg . '<br>';
Log::add($msg, Log::ERROR, 'Update');
$this->collectError('uninstallEosPlugin', $e);
}

// This needs to stay for 2.5 update compatibility
// Remove old files
try {
Log::add(Text::_('COM_JOOMLAUPDATE_UPDATE_LOG_DELETE_FILES'), Log::INFO, 'Update');
$this->deleteUnexistingFiles();
} catch (\Throwable $e) {
$msg = sprintf('Deleting legacy files failed: %s', $e->getMessage());
echo $msg . '<br>';
Log::add($msg, Log::ERROR, 'Update');
$this->collectError('deleteUnexistingFiles', $e);
}

// Further update
try {
$this->updateManifestCaches();
$this->updateDatabase();
Expand All @@ -160,17 +156,14 @@ public function update($installer)
$this->convertTablesToUtf8mb4(true);
$this->addUserAuthProviderColumn();
} catch (\Throwable $e) {
$msg = sprintf('Processing further update failed: %s', $e->getMessage());
echo $msg . '<br>';
Log::add($msg, Log::ERROR, 'Update');
$this->collectError('Further update', $e);
}

// Clean cache
try {
$this->cleanJoomlaCache();
} catch (\Throwable $e) {
$msg = sprintf('Cleaning cache failed: %s', $e->getMessage());
echo $msg . '<br>';
Log::add($msg, Log::ERROR, 'Update');
$this->collectError('cleanJoomlaCache', $e);
}
}

Expand Down Expand Up @@ -252,7 +245,7 @@ protected function updateDatabaseMysql()
try {
$results = $db->loadObjectList();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';
$this->collectError(__METHOD__, $e);

return;
}
Expand All @@ -267,7 +260,7 @@ protected function updateDatabaseMysql()
try {
$db->execute();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';
$this->collectError(__METHOD__, $e);

return;
}
Expand Down Expand Up @@ -629,7 +622,7 @@ protected function updateManifestCaches()
try {
$extensions = $db->loadObjectList();
} catch (Exception $e) {
echo Text::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage()) . '<br>';
$this->collectError(__METHOD__, $e);

return;
}
Expand All @@ -639,7 +632,10 @@ protected function updateManifestCaches()

foreach ($extensions as $extension) {
if (!$installer->refreshManifestCache($extension->extension_id)) {
echo Text::sprintf('FILES_JOOMLA_ERROR_MANIFEST', $extension->type, $extension->element, $extension->name, $extension->client_id) . '<br>';
$this->collectError(
__METHOD__,
new \Exception(Text::sprintf('FILES_JOOMLA_ERROR_MANIFEST', $extension->type, $extension->element, $extension->name, $extension->client_id))
);
}
}
}
Expand Down Expand Up @@ -8353,6 +8349,9 @@ public function postflight($action, $installer)
}
}

// Refresh versionable assets cache.
Factory::getApplication()->flushAssets();

return true;
}

Expand Down
45 changes: 26 additions & 19 deletions administrator/components/com_joomlaupdate/src/Model/UpdateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public function finaliseUpgrade()
ob_start();

if ($manifestClass->preflight('update', $installer) === false) {
$this->collectError('JoomlaInstallerScript::preflight', new \Exception('Preflight finished with "false".'));
$this->collectError('JoomlaInstallerScript::preflight', new \Exception('Script::preflight finished with "false" result.'));
$installer->abort(
Text::sprintf(
'JLIB_INSTALLER_ABORT_INSTALL_CUSTOM_INSTALL_FAILURE',
Expand All @@ -698,6 +698,7 @@ public function finaliseUpgrade()
return false;
}

// Append messages.
$msg .= ob_get_contents();
ob_end_clean();
} catch (\Throwable $e) {
Expand All @@ -724,6 +725,7 @@ public function finaliseUpgrade()
try {
$db->execute();
} catch (\RuntimeException $e) {
$this->collectError('Extension check', $e);
// Install failed, roll back changes.
$installer->abort(
Text::sprintf('JLIB_INSTALLER_ABORT_FILE_ROLLBACK', Text::_('JLIB_INSTALLER_UPDATE'), $e->getMessage())
Expand All @@ -746,6 +748,7 @@ public function finaliseUpgrade()
$row->manifest_cache = $installer->generateManifestCache();

if (!$row->store()) {
$this->collectError('Update the manifest_cache', new \Exception('Update the manifest_cache finished with "false" result.'));
// Install failed, roll back changes.
$installer->abort(
Text::sprintf('JLIB_INSTALLER_ABORT_FILE_ROLLBACK', Text::_('JLIB_INSTALLER_UPDATE'), $row->getError())
Expand All @@ -769,6 +772,7 @@ public function finaliseUpgrade()
$row->set('manifest_cache', $installer->generateManifestCache());

if (!$row->store()) {
$this->collectError('Write the manifest_cache', new \Exception('Writing the manifest_cache finished with "false" result.'));
// Install failed, roll back changes.
$installer->abort(Text::sprintf('JLIB_INSTALLER_ABORT_FILE_INSTALL_ROLLBACK', $row->getError()));

Expand All @@ -786,6 +790,7 @@ public function finaliseUpgrade()
$result = $installer->parseSchemaUpdates($manifest->update->schemas, $row->extension_id);

if ($result === false) {
$this->collectError('installer::parseSchemaUpdates', new \Exception('installer::parseSchemaUpdates finished with "false" result.'));
// Install failed, rollback changes (message already logged by the installer).
$installer->abort();

Expand All @@ -795,11 +800,12 @@ public function finaliseUpgrade()
// Reinitialise the installer's extensions table's properties.
$installer->extension->getFields(true);

// Start Joomla! 1.6.
ob_start();
try{
ob_start();

if ($manifestClass && method_exists($manifestClass, 'update')) {
if ($manifestClass->update($installer) === false) {
$this->collectError('JoomlaInstallerScript::update', new \Exception('Script::update finished with "false" result.'));

// Install failed, rollback changes.
$installer->abort(
Text::sprintf(
Expand All @@ -810,11 +816,14 @@ public function finaliseUpgrade()

return false;
}
}

// Append messages.
$msg .= ob_get_contents();
ob_end_clean();
// Append messages.
$msg .= ob_get_contents();
ob_end_clean();
} catch (\Throwable $e) {
$this->collectError('JoomlaInstallerScript::update', $e);
return false;
}

// Clobber any possible pending updates.
$update = new \Joomla\CMS\Table\Update($db);
Expand All @@ -827,24 +836,22 @@ public function finaliseUpgrade()
}

// And now we run the postflight.
ob_start();

if ($manifestClass && method_exists($manifestClass, 'postflight')) {
try{
ob_start();
$manifestClass->postflight('update', $installer);
}

// Append messages.
$msg .= ob_get_contents();
ob_end_clean();
// Append messages.
$msg .= ob_get_contents();
ob_end_clean();
} catch (\Throwable $e) {
$this->collectError('JoomlaInstallerScript::postflight', $e);
return false;
}

if ($msg) {
$installer->set('extension_message', $msg);
Log::add(str_replace('<br>', PHP_EOL . PHP_EOL, $msg), Log::INFO, 'Update');
}

// Refresh versionable assets cache.
Factory::getApplication()->flushAssets();

return true;
}

Expand Down

0 comments on commit baff877

Please sign in to comment.