Verified Commit 8219cb1f authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3223205 by kim.pepper, smustgrave, hmendes, anweshasinha, Berdir,...

Issue #3223205 by kim.pepper, smustgrave, hmendes, anweshasinha, Berdir, andypost, daffie: deprecate system_retrieve_file() without replacement
parent e0d038f5
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -5,10 +5,13 @@
 * Batch process to check the availability of remote or local po files.
 */

use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
@@ -296,11 +299,16 @@ function locale_translation_http_check($uri) {
 *   Directory where the downloaded file will be saved. Defaults to the
 *   temporary file path.
 *
 * @return object
 * @return object|false
 *   File object if download was successful. FALSE on failure.
 */
function locale_translation_download_source($source_file, $directory = 'temporary://') {
  if ($uri = system_retrieve_file($source_file->uri, $directory, FALSE, FileSystemInterface::EXISTS_REPLACE)) {
  try {
    $data = (string) \Drupal::httpClient()->get($source_file->uri)->getBody();
    /** @var \Drupal\Core\File\FileSystemInterface $fileSystem */
    $fileSystem = \Drupal::service('file_system');
    $filename = $fileSystem->basename($source_file->uri);
    if ($uri = $fileSystem->saveData($data, $directory . $filename, FileSystemInterface::EXISTS_REPLACE)) {
      $file = clone($source_file);
      $file->type = LOCALE_TRANSLATION_LOCAL;
      $file->uri = $uri;
@@ -308,6 +316,13 @@ function locale_translation_download_source($source_file, $directory = 'temporar
      $file->timestamp = filemtime($uri);
      return $file;
    }
  }
  catch (TransferException $exception) {
    \Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', ['%error' => $exception->getMessage()]));
  }
  catch (FileException | InvalidStreamWrapperException $e) {
    \Drupal::messenger()->addError(t('Failed to save file due to error "%error"', ['%error' => $e->getMessage()]));
  }
  \Drupal::logger('locale')->error('Unable to download translation file @uri.', ['@uri' => $source_file->uri]);
  return FALSE;
}
+6 −0
Original line number Diff line number Diff line
@@ -1154,8 +1154,14 @@ function system_time_zones($blank = NULL, $grouped = FALSE) {
 *   - If it succeeds and $managed is TRUE, a \Drupal\file\FileInterface
 *     object which describes the file.
 *   - If it fails, FALSE.
 *
 * @deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is no
 *   replacement.
 *
 * @see https://www.drupal.org/node/3223362
 */
function system_retrieve_file($url, $destination = NULL, $managed = FALSE, $replace = FileSystemInterface::EXISTS_RENAME) {
  @trigger_error('system_retrieve_file is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3223362', E_USER_DEPRECATED);
  $parsed_url = parse_url($url);
  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
 * Tests HTTP file fetching and error handling.
 *
 * @group system
 * @group legacy
 */
class RetrieveFileTest extends BrowserTestBase {

+9 −0
Original line number Diff line number Diff line
@@ -29,6 +29,15 @@ public function testSystemTimeZones() {
    system_time_zones();
  }

  /**
   * @covers ::system_retrieve_file
   */
  public function testSystemRetrieveFile() {
    $this->expectDeprecation('system_retrieve_file is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3223362');
    $retrieved_file = system_retrieve_file('http://example.com/foo.txt');
    $this->assertFalse($retrieved_file);
  }

  /**
   * @covers ::system_get_module_admin_tasks
   */
+16 −4
Original line number Diff line number Diff line
@@ -36,9 +36,11 @@
 * root.
 */

use Drupal\Core\Url;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
use GuzzleHttp\Exception\TransferException;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
@@ -211,8 +213,8 @@ function update_manager_archive_verify($project, $archive_file, $directory) {
 * @param $url
 *   The URL of the file on the server.
 *
 * @return string
 *   Path to local file.
 * @return string|false
 *   Path to local file, or FALSE if it could not be retrieved.
 */
function update_manager_file_get($url) {
  $parsed_url = parse_url($url);
@@ -227,7 +229,17 @@ function update_manager_file_get($url) {
  $local = $cache_directory . '/' . \Drupal::service('file_system')->basename($parsed_url['path']);

  if (!file_exists($local) || update_delete_file_if_stale($local)) {
    return system_retrieve_file($url, $local, FALSE, FileSystemInterface::EXISTS_REPLACE);
    try {
      $data = (string) \Drupal::httpClient()->get($url)->getBody();
      return \Drupal::service('file_system')->saveData($data, $local, FileSystemInterface::EXISTS_REPLACE);
    }
    catch (TransferException $exception) {
      \Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', ['%error' => $exception->getMessage()]));
    }
    catch (FileException | InvalidStreamWrapperException $e) {
      \Drupal::messenger()->addError(t('Failed to save file due to error "%error"', ['%error' => $e->getMessage()]));
    }
    return FALSE;
  }
  else {
    return $local;