locale_translation_http_check

locale_translation_http_check($uri)

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.

File

core/modules/locale/locale.batch.inc, line 237
Batch process to check the availability of remote or local po files.

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 = array();

    // 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.', array('@uri' => $uri));
        return TRUE;
      }
      $logger->notice('HTTP request to @url failed with error: @error.', array('@url' => $uri, '@error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase()));
    }
  }

  return FALSE;
}
doc_Drupal
2016-10-29 09:24:25
Comments
Leave a Comment

Please login to continue.