function 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|boolean 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_fetch_remote in drupal/core/modules/locale/locale.batch.inc
Batch operation callback: Check the availability of a remote po file.

File

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

Code

function locale_translation_http_check($uri) {
  try {
    $response = Drupal::httpClient()
      ->head($uri)
      ->send();
    $result = array();

    // In case of a permanent redirected response, return the final location.
    if ($previous = $response
      ->getPreviousResponse()) {
      if ($previous
        ->getStatusCode() == 301) {
        $result['location'] = $previous
          ->getLocation();
      }
    }
    $result['last_modified'] = $response
      ->getLastModified() ? strtotime($response
      ->getLastModified()) : 0;
    return $result;
  } catch (BadResponseException $e) {

    // Handle 4xx and 5xx http responses.
    $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.
      watchdog('locale', 'Translation file not found: @uri.', array(
        '@uri' => $uri,
      ));
      return TRUE;
    }
    watchdog('locale', 'HTTP request to @url failed with error: @error.', array(
      '@url' => $uri,
      '@error' => $response
        ->getStatusCode() . ' ' . $response
        ->getReasonPhrase(),
    ));
  } catch (RequestException $e) {

    // Handle connection problems and cURL specific errors (CurlException) and
    // other http related errors.
    watchdog('locale', 'HTTP request to @url failed with error: @error.', array(
      '@url' => $uri,
      '@error' => $e
        ->getMessage(),
    ));
  }
  return FALSE;
}