function acquia_spi_update_definition in Acquia Connector 7.3
Same name and namespace in other branches
- 6.2 acquia_spi/acquia_spi.module \acquia_spi_update_definition()
- 7.2 acquia_spi/acquia_spi.module \acquia_spi_update_definition()
Checks if NSPI server has an updated SPI data definition.
If it does, then this function updates local copy of SPI definition data.
Return value
bool True if SPI definition data has been updated
2 calls to acquia_spi_update_definition()
- AcquiaSPITestCase::testAcquiaSpiUpdateResponse in acquia_spi/
tests/ acquia_spi.test - Needs comment.
- acquia_spi_handle_server_response in acquia_spi/
acquia_spi.module - Act on specific elements of SPI update server response.
File
- acquia_spi/
acquia_spi.module, line 345 - Send site profile information (NSPI) and system data to Acquia Insight.
Code
function acquia_spi_update_definition() {
$core_version = substr(VERSION, 0, 1);
$spi_def_end_point = variable_get('acquia_spi_server', 'https://nspi.acquia.com');
$spi_def_end_point .= '/spi_def/get/' . $core_version;
$options = array(
'method' => 'GET',
'headers' => array(
'Content-type' => 'application/json',
),
'data' => drupal_http_build_query(array(
'spi_data_version' => ACQUIA_SPI_DATA_VERSION,
)),
);
$response = drupal_http_request($spi_def_end_point, $options);
if ($response->code != 200 || !isset($response->data)) {
watchdog('acquia spi', 'Failed to obtain latest SPI data definition. HTTP response: @response', array(
'@response' => var_export($response, TRUE),
), WATCHDOG_ERROR);
return FALSE;
}
else {
$response_data = drupal_json_decode($response->data);
$expected_data_types = array(
'drupal_version' => 'string',
'timestamp' => 'string',
'acquia_spi_variables' => 'array',
);
// Make sure that $response_data contains everything expected.
foreach ($expected_data_types as $key => $values) {
if (!array_key_exists($key, $response_data) || gettype($response_data[$key]) != $expected_data_types[$key]) {
watchdog('acquia spi', 'Received SPI data definition does not match expected pattern while checking "@key". Received and expected data: @data', array(
'@key' => $key,
'@data' => var_export(array_merge(array(
'expected_data' => $expected_data_types,
), array(
'response_data' => $response_data,
)), 1),
), WATCHDOG_ERROR);
return FALSE;
}
}
if ($response_data['drupal_version'] != $core_version) {
watchdog('acquia spi', 'Received SPI data definition does not match with current version of your Drupal installation. Data received for Drupal @version', array(
'@version' => $response_data['drupal_version'],
), WATCHDOG_ERROR);
return FALSE;
}
}
// NSPI response is in expected format.
if ((int) $response_data['timestamp'] > (int) variable_get('acquia_spi_def_timestamp', 0)) {
// Compare stored variable names to incoming and report on update.
$old_vars = variable_get('acquia_spi_def_vars', array());
$new_vars = $response_data['acquia_spi_variables'];
$new_optional_vars = 0;
foreach ($new_vars as $new_var_name => $new_var) {
// Count if received from NSPI optional variable is not present in old
// local SPI definition or if it already was in old SPI definition, but
// was not optional.
if ($new_var['optional'] && !array_key_exists($new_var_name, $old_vars) || $new_var['optional'] && isset($old_vars[$new_var_name]) && !$old_vars[$new_var_name]['optional']) {
$new_optional_vars++;
}
}
// Clean up waived vars that are not exposed by NSPI anymore.
$waived_spi_def_vars = variable_get('acquia_spi_def_waived_vars', array());
$changed_bool = FALSE;
foreach ($waived_spi_def_vars as $key => $waived_var) {
if (!in_array($waived_var, $new_vars)) {
unset($waived_spi_def_vars[$key]);
$changed_bool = TRUE;
}
}
if ($changed_bool) {
variable_set('acquia_spi_def_waived_vars', $waived_spi_def_vars);
}
// Finally, save SPI definition data.
if ($new_optional_vars > 0) {
variable_set('acquia_spi_new_optional_data', 1);
}
variable_set('acquia_spi_def_timestamp', $response_data['timestamp']);
variable_set('acquia_spi_def_vars', $response_data['acquia_spi_variables']);
return TRUE;
}
return FALSE;
}