[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/transliteration/ -> transliteration.admin.inc (source)

   1  <?php
   2  // $Id: transliteration.admin.inc,v 1.1.2.7 2010/07/16 13:36:16 smk Exp $
   3  
   4  /**
   5   * @file
   6   * Retroactive transliteration and admin settings UI.
   7   */
   8  
   9  /**
  10   * Form builder function; generate retroactive transliteration confirm form.
  11   *
  12   * @see transliteration_retroactive_submit()
  13   * @ingroup forms
  14   */
  15  function transliteration_retroactive() {
  16    if (!$query = transliteration_file_query(TRUE)) {
  17      drupal_set_message(t('Database not supported.'), 'error');
  18      $form['description']['#value'] = t('Retroactive transliteration is not supported for the database system of this Drupal installation. If you think this should be fixed please <a href="@issues-url">file an issue</a> in the project issue queue.', array('@issues-url' => 'http://drupal.org/project/issues/transliteration'));
  19      return $form;
  20    }
  21  
  22    $count = db_result(db_query($query));
  23    if (!$count) {
  24      drupal_set_message(t('Transliteration is not required.'), 'status');
  25      $form['description']['#value'] = t('There are currently no files names containing non-ASCII characters.');
  26      return $form;
  27    }
  28  
  29    $form['#redirect'] = 'admin/settings/file-system/settings';
  30    $question = t('Are you sure you want to transliterate existing file names?');
  31  
  32    // Generate a sample list.
  33    $rows = array();
  34    $header = array(
  35      t('Original file name'),
  36      t('Transliterated file name')
  37    );
  38    $result = db_query_range(transliteration_file_query(), 0, 10);
  39    while($file = db_fetch_object($result)) {
  40      $filename = basename($file->filepath);
  41      $rows[] = array(l($filename, file_create_url($file->filepath)), transliteration_clean_filename($filename));
  42    }
  43    $description = '<p><strong>' . t('The database currently lists @x_filenames containing non-ASCII characters.', array('@x_filenames' => format_plural($count, '1 file name', '@count file names'))) . '</strong><br />';
  44    $description .= t('This count might be inaccurate, though, since some files may not need to be renamed.') . '</p>';
  45    $description .= theme('table', $header, $rows);
  46    if ($count > 10) {
  47      $description .= '<p>' . t('Note: table shows only the first 10 entries.') . '</p>';
  48    }
  49    $description .= '<p>' . t('<strong>WARNING:</strong> if you have manually entered image or file paths in text fields (for example, text areas or WYSIWYG editors), renaming the files will break these references. Since there is currently no automated way to also fix referenced files in textual contents, it is a very good idea to backup the database and %files directory beforehand. Modules accessing files using their internal system ids are not affected.', array('%files' => file_directory_path())) . '</p>';
  50    $description .= '<p style="color: red; font-weight: bold; font-size: 18px;">' . t('This action cannot be undone.') . '</p>';
  51  
  52    return confirm_form($form, $question, 'admin/settings/file-system/settings', $description, t('Transliterate'));
  53  }
  54  
  55  /**
  56   * Form submit function; retroactively transliterate existing file names.
  57   *
  58   * @see transliteration_retroactive()
  59   */
  60  function transliteration_retroactive_submit($form, &$form_state) {
  61    $count = 0;
  62    $errors = array();
  63    $result = db_query(transliteration_file_query());
  64  
  65    while ($file = db_fetch_object($result)) {
  66      if (!file_exists('./'. $file->filepath)) {
  67        // Missing file.
  68        $errors[] = $file->filepath;
  69        continue;
  70      }
  71  
  72      // Sanitize file name.
  73      $filename = transliteration_clean_filename(basename($file->filepath));
  74      // Build destination path.
  75      $destination = dirname($file->filepath) . '/' . $filename;
  76  
  77      // Rename and update the file record accordingly.
  78      if (file_move($file->filepath, $destination, FILE_EXISTS_RENAME)) {
  79        db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $file->filepath, $file->fid);
  80        $count++;
  81      }
  82      else {
  83        $errors[] = $file->filepath;
  84      }
  85    }
  86  
  87    if ($errors) {
  88      $message = t('Not all file names could be converted. The following files could not be accessed and have been ignored:');
  89      $message .= theme('item_list', $errors);
  90      drupal_set_message($message, 'error');
  91    }
  92    else {
  93      drupal_set_message(t('@filenames have been successfully transliterated.', array('@filenames' => format_plural($count, '1 file name', '@count file names'))));
  94    }
  95  
  96    // Flush page cache.
  97    cache_clear_all();
  98  }
  99  
 100  /**
 101   * Build a query that returns all file names from the database containing non-ASCII characters.
 102   *
 103   * @param $count
 104   *   Set to TRUE to return a count query.
 105   */
 106  function transliteration_file_query($count = FALSE) {
 107    // Regular expressions are not supported by Drupal's database layer and
 108    // operators differ between manufacturers.
 109    switch ($GLOBALS['db_type']) {
 110      case 'mysql':
 111      case 'mysqli':
 112        $operator = 'NOT REGEXP';
 113        $regex = '/[a-z0-9_.-]+$';
 114        break;
 115  
 116      case 'pgsql':
 117        $operator = '!~*';
 118        $regex = '/[a-z0-9_.-]+$';
 119        break;
 120  
 121      default:
 122        return FALSE;
 123    }
 124  
 125    $fields = ($count ? 'COUNT(*)' : '*');
 126    return "SELECT $fields FROM {files} WHERE filepath $operator '$regex'";
 127  }
 128  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7