function locale_translation_http_check

Same name and namespace in other branches
  1. 9 core/modules/locale/locale.batch.inc \locale_translation_http_check()
  2. 8.9.x core/modules/locale/locale.batch.inc \locale_translation_http_check()
  3. 10 core/modules/locale/locale.batch.inc \locale_translation_http_check()

Check if remote file exists and when it was last updated.

Parameters

string $uri: URI of remote file.

Return value

array|bool Associative array of file data with the following elements:

  • last_modified: Last modified timestamp of the translation file.
  • (optional) location: The location of the translation file. Is only set when a redirect (301) has occurred.

TRUE if the file is not found. FALSE if a fault occurred.

1 call to locale_translation_http_check()
locale_translation_batch_status_check in core/modules/locale/locale.batch.inc
Implements callback_batch_operation().

File

core/modules/locale/locale.batch.inc, line 280

Code

function locale_translation_http_check($uri) {
    $logger = \Drupal::logger('locale');
    try {
        $actual_uri = NULL;
        $response = \Drupal::service('http_client_factory')->fromOptions([
            'allow_redirects' => [
                'on_redirect' => function (RequestInterface $request, ResponseInterface $response, UriInterface $request_uri) use (&$actual_uri) {
                    $actual_uri = (string) $request_uri;
                },
            ],
        ])
            ->head($uri);
        $result = [];
        // Return the effective URL if it differs from the requested.
        if ($actual_uri && $actual_uri !== $uri) {
            $result['location'] = $actual_uri;
        }
        $result['last_modified'] = $response->hasHeader('Last-Modified') ? strtotime($response->getHeaderLine('Last-Modified')) : 0;
        return $result;
    } catch (RequestException $e) {
        // Handle 4xx and 5xx http responses.
        if ($response = $e->getResponse()) {
            if ($response->getStatusCode() == 404) {
                // File not found occurs when a translation file is not yet available
                // at the translation server. But also if a custom module or custom
                // theme does not define the location of a translation file. By default
                // the file is checked at the translation server, but it will not be
                // found there.
                $logger->notice('Translation file not found: @uri.', [
                    '@uri' => $uri,
                ]);
                return TRUE;
            }
            $logger->notice('HTTP request to @url failed with error: @error.', [
                '@url' => $uri,
                '@error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase(),
            ]);
        }
    } catch (ConnectException $e) {
        $logger->notice('HTTP request to @url failed with error: @error.', [
            '@url' => $uri,
            '@error' => $e->getMessage(),
        ]);
    }
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.