You are here

function transliteration_install_retroactive in Transliteration 6

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

Helper function; retroactive transliteration of existing filenames.

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 51

Code

function transliteration_install_retroactive() {
  require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';

  // Regexp operators differ between database manufacturers.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $op = 'RLIKE';
      break;
    case 'pgsql':
      $op = '~*';
      break;
  }

  // Get all of the files that need to be converted, that is those that
  // contain characters other than alphanumerics, underscores, dots, or
  // hyphens.
  $result = db_query("SELECT fid, filepath FROM {files} WHERE SUBSTRING_INDEX(filepath, '/', -1) {$op} '[^0-9A-Za-z_.-]'");
  $errors = array();
  while ($file = db_fetch_object($result)) {

    // Transliterate the file's name.
    $filepath = $file->filepath;
    $filepath_new = dirname($filepath) . '/' . transliteration_clean_filename(basename($filepath));

    // Move the file to a new location but do a shortcut check first to avoid
    // unneccessary error messages. It's probably better to not mess with
    // records for missing files.
    $realpath = realpath($filepath);
    if ($realpath && file_exists($realpath) && file_move($filepath, $filepath_new, FILE_EXISTS_RENAME)) {

      // Get the file's new path; it may have changed.
      $filepath_new = $filepath;

      // Update the files table with the new path.
      db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $filepath_new, $file->fid);
    }
    else {
      $errors[] = $file->filepath;
    }
  }
  return $errors;
}