Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensions - Degrade gracefully if new requirements are not installed #22641

Merged
merged 3 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Extension/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public function getAllTags() {
}

/**
* @return array
* @return CRM_Extension_Info[]
* Ex: $result['org.civicrm.foobar'] = new CRM_Extension_Info(...).
* @throws \CRM_Extension_Exception
* @throws \Exception
Expand Down
28 changes: 13 additions & 15 deletions CRM/Extension/Upgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ protected static function getActiveKeys() {
}

/**
* Sorts active extensions according to their dependencies
*
* @param string[] $keys
* Names of all active modules
*
* @return string[]
* @throws \CRM_Extension_Exception
Expand All @@ -137,30 +140,25 @@ protected static function getActiveKeys() {
protected static function sortKeys($keys) {
$infos = CRM_Extension_System::singleton()->getMapper()->getAllInfos();

// Start with our inputs in a normalized form.
// Ensure a stable starting order.
$todoKeys = array_unique($keys);
sort($todoKeys);

// Goal: Add all active items to $sorter and flag $doneKeys['org.example.foobar']=1.
$doneKeys = [];
$sorter = new \MJS\TopSort\Implementations\FixedArraySort();

while (!empty($todoKeys)) {
$key = array_shift($todoKeys);
if (isset($doneKeys[$key])) {
continue;
}
$doneKeys[$key] = 1;

foreach ($todoKeys as $key) {
/** @var CRM_Extension_Info $info */
$info = @$infos[$key];
$info = $infos[$key] ?? NULL;

if ($info && $info->requires) {
$sorter->add($key, $info->requires);
$todoKeys = array_merge($todoKeys, $info->requires);
// Add dependencies
if ($info) {
// Filter out missing dependencies; missing modules cannot be upgraded
$requires = array_intersect($info->requires ?? [], $keys);
$sorter->add($key, $requires);
}
// This shouldn't ever happen if this function is being passed a list of active extensions.
else {
$sorter->add($key, []);
throw new CRM_Extension_Exception('Invalid extension key: "' . $key . '"');
}
}
return $sorter->sort();
Expand Down
45 changes: 40 additions & 5 deletions CRM/Utils/Check/Component/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,29 @@ public function checkExtensions() {
return $messages;
}

$keys = array_keys($manager->getStatuses());
$stauses = $manager->getStatuses();
$keys = array_keys($stauses);
$enabled = array_keys(array_filter($stauses, function($status) {
return $status === CRM_Extension_Manager::STATUS_INSTALLED;
}));
sort($keys);
$updates = $errors = $okextensions = [];

$extPrettyLabel = function($key) use ($mapper) {
// We definitely know a $key, but we may not have a $label.
// Which is too bad - because it would be nicer if $label could be the reliable start of the string.
$keyFmt = '<code>' . htmlentities($key) . '</code>';
try {
$info = $mapper->keyToInfo($key);
if ($info->label) {
return sprintf('"<em>%s</em>" (%s)', htmlentities($info->label), $keyFmt);
}
}
catch (CRM_Extension_Exception $ex) {
return "($keyFmt)";
}
};

foreach ($keys as $key) {
try {
$obj = $mapper->keyToInfo($key);
Expand All @@ -615,11 +634,20 @@ public function checkExtensions() {
$row = CRM_Admin_Page_Extensions::createExtendedInfo($obj);
switch ($row['status']) {
case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
$errors[] = ts('%1 extension (%2) is installed but missing files.', [1 => $row['label'] ?? NULL, 2 => $key]);
$errors[] = ts('%1 is installed but missing files.', [1 => $extPrettyLabel($key)]);
break;

case CRM_Extension_Manager::STATUS_INSTALLED:
if (!empty($remotes[$key]) && version_compare($row['version'], $remotes[$key]->version, '<')) {
$missingRequirements = array_diff($row['requires'], $enabled);
if (!empty($row['requires']) && $missingRequirements) {
$errors[] = ts('%1 has a missing dependency on %2', [
1 => $extPrettyLabel($key),
2 => implode(', ', array_map($extPrettyLabel, $missingRequirements)),
'plural' => '%1 has missing dependencies: %2',
'count' => count($missingRequirements),
]);
}
elseif (!empty($remotes[$key]) && version_compare($row['version'], $remotes[$key]->version, '<')) {
$updates[] = $row['label'] . ': ' . $mapper->getUpgradeLink($remotes[$key], $row);
}
else {
Expand Down Expand Up @@ -652,8 +680,15 @@ public function checkExtensions() {
if ($errors) {
$messages[] = new CRM_Utils_Check_Message(
__FUNCTION__ . 'Error',
'<ul><li>' . implode('</li><li>', $errors) . '</li></ul>',
ts('Extension Error'),
ts('There is one extension error:', [
'count' => count($errors),
'plural' => 'There are %count extension errors:',
])
. '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>'
. ts('To resolve any errors, go to <a %1>Manage Extensions</a>.', [
1 => 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1') . '"',
]),
ts('Extension Error', ['count' => count($errors), 'plural' => 'Extension Errors']),
\Psr\Log\LogLevel::ERROR,
'fa-plug'
);
Expand Down