Skip to content

Commit

Permalink
Merge branch '8.x-1.x' into feature/drafts-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Jan 8, 2024
2 parents c0a2efb + 61d0a10 commit e959ec6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 47 deletions.
44 changes: 38 additions & 6 deletions modules/quant_api/src/Client/QuantClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
*/
class QuantClient implements QuantClientInterface {

/**
* The configuration object for Quant API.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;

/**
* The logger service.
*
Expand Down Expand Up @@ -63,15 +70,40 @@ class QuantClient implements QuantClientInterface {
* {@inheritdoc}
*/
public function __construct(Client $client, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory) {
$config = $config_factory->get('quant_api.settings');
$this->config = $config_factory->get('quant_api.settings');
$this->client = $client;
$this->logger = $logger_factory->get('quant_api');

$this->username = $config->get('api_account');
$this->token = $config->get('api_token');
$this->project = $config->get('api_project');
$this->endpoint = $config->get('api_endpoint') . '/v1';
$this->tlsDisabled = $config->get('api_tls_disabled');
$this->username = $this->config->get('api_account');
$this->token = $this->config->get('api_token');
$this->project = $this->config->get('api_project');
$this->endpoint = $this->config->get('api_endpoint') . '/v1';
$this->tlsDisabled = $this->config->get('api_tls_disabled');
}

/**
* Get API overrides.
*/
public function getOverrides() {
// Note this has to be processed in this class instead of in the
// SettingsForm because the overrides aren't available in the form.
$overrides = [];
$keys = [
'api_endpoint',
'api_account',
'api_project',
'api_token',
'api_tls_disabled',
];
foreach ($keys as $key) {
$original = $this->config->getOriginal($key, FALSE);
$active = $this->config->get($key);
if ($original != $active) {
$overrides[$key] = $active;
}
}

return $overrides;
}

/**
Expand Down
27 changes: 26 additions & 1 deletion modules/quant_api/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,35 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form['api_tls_disabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Disable TLS verification'),
'#description' => $this->t('You can optionally disable SSL verification for all Quant API requests. This is <strong>not recommended</strong>, but may be necessary in some configurations. For example, old web servers may have issues validating modern SSL certificates.'),
'#description' => $this->t('You can optionally disable TLS verification for all Quant API requests. This is <strong>not recommended</strong>, but may be necessary in some configurations. For example, old web servers may have issues validating modern TSL/SSL certificates.'),
'#default_value' => $config->get('api_tls_disabled', FALSE),
];

// API values might be overridden in the settings file.
$overrides = $this->client->getOverrides();
foreach ($overrides as $key => $value) {
if ($key === 'api_token') {
// Don't show the token in the UI.
$message = $this->t('QuantAPI override: <code>api_token</code> has been overridden in the settings file.');
}
else {
$message = $this->t('QuantAPI override: <em>@key</em> has been overridden in the settings file with <em>@value</em>.',
[
'@key' => $key,
'@value' => $value,
]);
}

// Show warning and add to description.
\Drupal::messenger()->addWarning($message);
$form[$key]['#description'] = $form[$key]['#description'] . ' <strong>' . $message . '</strong>';
}

// Show error if not using TSL verification.
if ((isset($overrides['api_tls_disabled']) && !$overrides['api_tls_disabled']) || !$config->get('api_tls_disabled')) {
\Drupal::messenger()->addError($this->t('<strong>DANGER ZONE:</strong> TLS verification is disabled for Quant API connections. It is <strong>highly recommended</strong> that you update your server configuration to handle TLS rather than disabling TLS verification.'));
}

return parent::buildForm($form, $form_state);
}

Expand Down
4 changes: 4 additions & 0 deletions quant.module
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ function quant_modules_uninstalled($modules) {
* The path alias.
*/
function quant_path_alias_presave(EntityInterface $pathAlias) {
if (!$pathAlias || !$pathAlias->original) {
return;
}

// Original path alias.
$original_path = $pathAlias->original->get('path')->value;
$original_alias = $pathAlias->original->get('alias')->value;
Expand Down
88 changes: 48 additions & 40 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,55 +142,63 @@ public static function processDrafts() {
* The markup with the page info.
*/
public static function getPageInfo(array $urls = NULL) : string {
// Default to the current page.
if (!$urls) {
$urls = [self::getUrl()];
}

$client = \Drupal::service('quant_api.client');
$response = $client->getUrlMeta($urls);

if (isset($response['global_meta']['records'])) {
// Show meta information for the pages in Quant.
$found_urls = [];
$output = '<div class="quant-page-info messages messages--warning">';
$output .= '<h2>' . t('Quant Page Info') . '</h2>';
foreach ($response['global_meta']['records'] as $record) {
$found_urls[] = $url = $record['meta']['url'];
$output .= '<div class="quant-page-info">';
$output .= '<strong>Page info for ' . $url . '</strong>';
$output .= '<ul>';
$output .= '<li><strong>Published</strong>: ' . ($record['meta']['published'] ? t('Yes') : t('No')) . '</li>';
$output .= '<li><strong>Revisions</strong>: ' . $record['meta']['revision_count'] . '</li>';
$date = DrupalDateTime::createFromTimestamp($record['meta']['content_timestamp'])->format('Y-m-d H:i:s');
$output .= '<li><strong>Updated</strong>: ' . $date . '</li>';
$date = DrupalDateTime::createFromTimestamp($record['meta']['date_timestamp'])->format('Y-m-d H:i:s');
$output .= '<li><strong>Synced</strong>: ' . $date . '</li>';
$output .= '</ul>';
$output .= '</div>';
try {
// Default to the current page.
if (!$urls) {
$urls = [self::getUrl()];
}

// Note any URLs that were not in Quant.
if (count($urls) != count($found_urls)) {
if (count($urls) === 1) {
$output .= '<strong>' . t('Page info could not be found for this URL:') . '</strong>';
$client = \Drupal::service('quant_api.client');
$response = $client->getUrlMeta($urls);

if (isset($response['global_meta']['records'])) {
// Show meta information for the pages in Quant.
$found_urls = [];
$output = '<div class="quant-page-info messages messages--warning">';
$output .= '<h2>' . t('Quant Page Info') . '</h2>';
foreach ($response['global_meta']['records'] as $record) {
$found_urls[] = $url = ($record['meta']['url'] ?? 'Url unknown');
$output .= '<div class="quant-page-info">';
$output .= '<strong>Page info for ' . $url . '</strong>';
$output .= '<ul>';
// @todo Fix underlying data per issue #3412934.
$output .= '<li><strong>Published</strong>: ' . (($record['meta']['published'] ?? FALSE) ? t('Yes') : t('No')) . '</li>';
$output .= '<li><strong>Revisions</strong>: ' . ($record['meta']['revision_count'] ?? 0) . '</li>';
$date = DrupalDateTime::createFromTimestamp($record['meta']['content_timestamp'] ?? 0)->format('Y-m-d H:i:s');
$output .= '<li><strong>Updated</strong>: ' . $date . '</li>';
$date = DrupalDateTime::createFromTimestamp($record['meta']['date_timestamp'] ?? 0)->format('Y-m-d H:i:s');
$output .= '<li><strong>Synced</strong>: ' . $date . '</li>';
$output .= '</ul>';
$output .= '</div>';
}
else {
$output .= '<strong>' . t('Page info could not be found for the following URLs:') . '</strong>';

// Note any URLs that were not in Quant.
if (count($urls) != count($found_urls)) {
if (count($urls) === 1) {
$output .= '<strong>' . t('Page info could not be found for this URL:') . '</strong>';
}
else {
$output .= '<strong>' . t('Page info could not be found for the following URLs:') . '</strong>';
}
$output .= '<ul>';
}
$output .= '<ul>';
}
foreach ($urls as $url) {
if (!in_array($url, $found_urls)) {
$output .= '<li>' . $url . '</li>';
foreach ($urls as $url) {
if (!in_array($url, $found_urls)) {
$output .= '<li>' . $url . '</li>';
}
$output .= '</ul>';
}
$output .= '</ul>';

$output .= '</div>';
}

$output .= '</div>';
return $output;
}
catch (\Exception $e) {
\Drupal::logger('quant')->error($e->getMessage());
return '';
}

return $output;
}

}

0 comments on commit e959ec6

Please sign in to comment.