1 system.admin.inc system_transliteration_retroactive_submit($form, &$form_state)

Form submit function; retroactively transliterates existing file names.

See also

system_transliteration_retroactive()

File

core/modules/system/system.admin.inc, line 2633
Admin page callbacks for the system module.

Code

function system_transliteration_retroactive_submit($form, &$form_state) {
  include_once BACKDROP_ROOT . '/core/includes/transliteration.inc';
  $count = 0;
  $errors = array();
  $result = system_transliteration_file_query()->execute();

  while ($row = $result->fetchAssoc()) {
    $file = new File($row);
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
    $scheme = file_uri_scheme($file->uri);

    // Missing implementation.
    if (!$wrapper) {
      $errors[] = file_uri_target($file->uri);
      continue;
    }

    // Skip non-writable stream wrappers.
    $writeable_stream_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE);
    if (!isset($writeable_stream_wrappers[$scheme])) {
      continue;
    }

    // Missing file.
    if (!file_exists($wrapper->realpath())) {
      $errors[] = file_uri_target($file->uri);
      continue;
    }

    // Sanitize file name.
    $filename = transliteration_clean_filename(backdrop_basename($file->uri));

    // Build destination URI.
    $destination = file_stream_wrapper_uri_normalize(backdrop_dirname($file->uri) . '/' . $filename);
    $destination = file_destination($destination, FILE_EXISTS_RENAME);

    // Rename and update the file record accordingly.
    if ($wrapper->rename($file->uri, $destination)) {
      $file->uri = $destination;
      // Don't use file_save() as it modifies the timestamp.
      backdrop_write_record('file_managed', $file, 'fid');
      // Inform modules that the file has been updated.
      module_invoke_all('file_update', $file);
      $count++;
    }
    else {
      $errors[] = file_uri_target($file->uri);
    }
  }

  if ($errors) {
    $message = t('Not all file names could be converted. The following files could not be accessed and have been ignored:');
    $message .= theme('item_list', array('items' => $errors));
    backdrop_set_message($message, 'error');
  }
  else {
    backdrop_set_message(format_plural($count, 'One file name has been successfully transliterated.', '@count file names have been successfully transliterated.'));
  }

  backdrop_flush_all_caches();
}