You are here

function transliteration_install_retroactive in Transliteration 5.2

Same name and namespace in other branches
  1. 6 transliteration.install \transliteration_install_retroactive()
  2. 6.2 transliteration.install \transliteration_install_retroactive()

Helper function; retroactivly transliterate existing file names.

2 calls to transliteration_install_retroactive()
transliteration_install in ./transliteration.install
Implementation of hook_install().
transliteration_update_1 in ./transliteration.install
Retroactively transliterate existing filenames.

File

./transliteration.install, line 37
Install, update, and uninstall functions for the transliteration module.

Code

function transliteration_install_retroactive() {
  require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
  $t = get_t();
  $errors = array();

  // Get all of the files that need to be converted, that is those that
  // contain characters other than alphanumerics, underscores, dots, or
  // hyphens.
  $query = "SELECT fid, filepath FROM {files} WHERE filepath ";

  // Regexp operators differ between database manufacturers.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $query .= "REGEXP '[^0-9a-z_./-]'";
      break;
    case 'pgsql':
      $query .= "~* '[^0-9a-z_./-]'";
      break;
    case 'mssql':

      // SQL Server's LIKE is case sensitive.
      $query .= "LIKE '%[^0-9A-Za-z_./-]%'";
      break;
    default:
      drupal_set_message($t('Filenames could not be transliterated: database type not supported.'), 'error');
      return;
  }
  $result = db_query($query);
  while ($file = db_fetch_object($result)) {

    // Transliterate the filename, skip if result is identical.
    $transliterated = dirname($file->filepath) . '/' . transliteration_clean_filename(basename($file->filepath));
    if ($transliterated == $file->filepath) {
      continue;
    }

    // Rename the file but do a shortcut check first to avoid warnings.
    if (realpath($file->filepath) && file_move($file->filepath, $transliterated, FILE_EXISTS_RENAME)) {
      $transliterated = $file->filepath;
      db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $transliterated, $file->fid);
    }
    else {
      $errors[] = $file->filepath;
    }
  }
  if ($errors) {
    $message = $t('Not all existing filenames could be transliterated. The following files could not be accessed:');
    $message .= theme_item_list($errors);
    drupal_set_message($message, 'error');
  }
  else {
    drupal_set_message($t('Existing filenames have been successfully transliterated.'));
  }
}