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

Don't put API calls in the upgrader #24364

Closed
wants to merge 4 commits into from
Closed
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
11 changes: 1 addition & 10 deletions CRM/Upgrade/DispatchPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,7 @@ public static function pick(): ?array {
return NULL;
}

// Have we run CRM_Upgrade_Form::doCoreFinish() for this version?
$codeVer = CRM_Utils_System::version();
$isCoreCurrent = CRM_Core_DAO::singleValueQuery('
SELECT count(*) as count
FROM civicrm_log
WHERE entity_table = "civicrm_domain"
AND data LIKE %1
', [1 => ['upgrade:%->' . $codeVer, 'String']]);

return CRM_Upgrade_DispatchPolicy::get($isCoreCurrent < 1 ? 'upgrade.main' : 'upgrade.finish');
return CRM_Upgrade_DispatchPolicy::get(CRM_Upgrade_Form::isDatabaseStillBroken() ? 'upgrade.main' : 'upgrade.finish');
}

/**
Expand Down
78 changes: 78 additions & 0 deletions CRM/Upgrade/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,4 +891,82 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $latestVe
}
}

/**
* Has the condition of the database reached parity with the codebase?
*
* @return bool
* @throws \CRM_Core_Exception
*/
public static function isDatabaseStillBroken(): bool {
// Have we run CRM_Upgrade_Form::doCoreFinish() for this version?
$codeVer = CRM_Utils_System::version();
$isCoreCurrent = CRM_Core_DAO::singleValueQuery('
SELECT count(*) as count
FROM civicrm_log
WHERE entity_table = "civicrm_domain"
AND data LIKE %1
', [1 => ['upgrade:%->' . $codeVer, 'String']]);
return $isCoreCurrent < 1;
}

/**
* We want to stop new API calls from getting into the upgrader. There are a couple already in
* there, and it's annoying to understand+replace after-the-fact, so this function is just a
* list of grandfathered methods.
*
* @return bool
*/
public static function isPreExistingEvil(): bool {
$functions = [];
$stackFrames = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
foreach ($stackFrames as $stackFrame) {
$functions[] = ($stackFrame['class'] ?? '') . '::' . ($stackFrame['function'] ?? '');
if (($stackFrame['class'] ?? NULL) === CRM_Queue_Task::class) {
// Some upgrade functions are inherited, yielding two identities (e.g.
// 'CRM_Upgrade_Incremental_php_Base::updateSmartGroups' and
// 'CRM_Upgrade_Incremental_php_FiveFifteen::updateSmartGroups').
// The latter is better for grandfathering exceptions.
$task = $stackFrame['object'];
$functions[] = implode('::', (array) $task->callback);
}
}
$evilShallNotBeAllowedToGrow = [
'CRM_Upgrade_Incremental_php_FourSeven::disableFlexibleJobsExtension',
'CRM_Upgrade_Incremental_php_FourSeven::removePaymentProcessorType',
'CRM_Upgrade_Incremental_php_FourSeven::updateWysiwyg',
'CRM_Upgrade_Incremental_php_FourSeven::addDeletedByMergeActivityType',
'CRM_Upgrade_Incremental_php_FourSeven::addRefundAndChargeBackAccountsIfNotExist',
'CRM_Upgrade_Incremental_php_FourSeven::addWysiwygPresets',
'CRM_Upgrade_Incremental_php_FourSeven::addChangeCaseSubjectActivityType',
'CRM_Upgrade_Incremental_php_FourSeven::pickActivityRevisionPolicy',
'CRM_Upgrade_Incremental_php_FourSeven::removeContributionLoggingReports',
'CRM_Upgrade_Incremental_php_FiveFour::addActivityDefaultAssigneeOptions',
'CRM_Upgrade_Incremental_php_FiveEleven::updateSmartGroups',
'CRM_Upgrade_Incremental_php_FiveTwelve::updateSmartGroups',
'CRM_Upgrade_Incremental_php_FiveFifteen::updateSmartGroups',
'CRM_Upgrade_Incremental_php_FiveFifteen::updateContributeSettings',
'CRM_Upgrade_Incremental_php_FiveSixteen::updateSmartGroups',
'CRM_Upgrade_Incremental_php_FiveSeventeen::updateSmartGroups',
'CRM_Upgrade_Incremental_php_FiveSeventeen::updateFileTypes',
'CRM_Upgrade_Incremental_php_FiveEighteen::updateSmartGroups',
'CRM_Upgrade_Incremental_php_FiveEighteen::joinDateReportUpdate',
'CRM_Upgrade_Incremental_php_FiveNineteen::api4Menu',
'CRM_Upgrade_Incremental_php_FiveTwenty::templateStatus',
'CRM_Upgrade_Incremental_php_FiveTwentyThree::addXoauth2ProtocolOption',
'CRM_Upgrade_Incremental_php_FiveTwentyFive::convertReportsJcalendarToDatePicker',
'CRM_Upgrade_Incremental_php_FiveTwentySix::addNLBEOptionValue',
'CRM_Upgrade_Incremental_php_FiveThirtyNine::updateSmartGroups',
'CRM_Upgrade_Incremental_php_FiveForty::addGroupOptionList',
'CRM_Upgrade_Incremental_php_FiveForty::updateCkeditorOptionLabel',
'CRM_Upgrade_Incremental_php_FiveFortyTwo::addOptionGroup',
'CRM_Upgrade_Incremental_php_FiveFortySix::addImportCustomMenu',
'CRM_Upgrade_Incremental_php_FiveFifty::convertMappingFieldLabelsToNames',
'CRM_Upgrade_Incremental_php_FiveFiftyOne::convertMappingFieldLabelsToNames',
'CRM_Upgrade_Incremental_php_FiveFiftyThree::addInvoicePDFFormat',
'CRM_Upgrade_Incremental_php_FiveFiftyThree::addRecentItemsProviders',
'CRM_Upgrade_Incremental_php_FiveSixty::addScheduledReminderSmartySetting',
];
return !empty(array_intersect($functions, $evilShallNotBeAllowedToGrow));
}

}
2 changes: 1 addition & 1 deletion CRM/Upgrade/Incremental/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static function setPreUpgradeMessage(&$preUpgradeMessage, $currentVer, $l
}

$ftAclSetting = Civi::settings()->get('acl_financial_type');
$financialAclExtension = civicrm_api3('extension', 'get', ['key' => 'biz.jmaconsulting.financialaclreport', 'sequential' => 1]);
$financialAclExtension = CRM_Extension_System::singleton()->getMapper()->isActiveModule('financialaclreport');
if ($ftAclSetting && (($financialAclExtension['count'] == 1 && $financialAclExtension['values'][0]['status'] != 'Installed') || $financialAclExtension['count'] !== 1)) {
$preUpgradeMessage .= '<br />' . ts('CiviCRM will in the future require the extension %1 for CiviCRM Reports to work correctly with the Financial Type ACLs. The extension can be downloaded <a href="%2">here</a>', [
1 => 'biz.jmaconsulting.financialaclreport',
Expand Down
6 changes: 6 additions & 0 deletions Civi/API/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public function run($entity, $action, $params) {
* @throws \CRM_Core_Exception
*/
public function runSafe($entity, $action, $params) {
if (\CRM_Core_Config::isUpgradeMode() && \CRM_Upgrade_Form::isDatabaseStillBroken() && !\CRM_Upgrade_Form::isPreExistingEvil()) {
throw new \API_Exception("Don't put API calls in the upgrader.");
// This ^^ sometimes gets swallowed by API callers. For a more certain audit, use `die()`.
// debug_print_backtrace(); die("Don't put API calls in the upgrader.");
}

$apiRequest = [];
try {
$apiRequest = Request::create($entity, $action, $params);
Expand Down