transliteration.admin.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @file
  4. * Retroactive transliteration and admin settings UI.
  5. */
  6. /**
  7. * Form builder function; generates retroactive transliteration confirm form.
  8. *
  9. * @see transliteration_retroactive_submit()
  10. * @ingroup forms
  11. */
  12. function transliteration_retroactive() {
  13. if (!$query = transliteration_file_query()) {
  14. drupal_set_message(t('Database not supported.'), 'error');
  15. $form['description']['#markup'] = 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'));
  16. return $form;
  17. }
  18. $count = $query->countQuery()->execute()->fetchColumn();
  19. if (!$count) {
  20. drupal_set_message(t('Transliteration is not required.'), 'status');
  21. $form['description']['#markup'] = t('There are currently no files names containing non-ASCII characters.');
  22. return $form;
  23. }
  24. $form['#redirect'] = 'admin/config/media/file-system/settings';
  25. $question = t('Are you sure you want to transliterate existing file names?');
  26. // Generate a sample list.
  27. $rows = array();
  28. $header = array(
  29. t('Original file name'),
  30. t('Transliterated file name')
  31. );
  32. foreach ($query->range(0, 10)->execute() as $file) {
  33. $filename = basename($file->uri);
  34. $rows[] = array(l($filename, file_create_url($file->uri)), transliteration_clean_filename($filename));
  35. }
  36. $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 />';
  37. $description .= t('This count might be inaccurate, though, since some files may not need to be renamed. For example, off-site files will never be changed.') . '</p>';
  38. $description .= theme('table', array('header' => $header, 'rows' => $rows));
  39. if ($count > 10) {
  40. $description .= '<p>' . t('Note: table shows only the first 10 entries.') . '</p>';
  41. }
  42. $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' => drupal_realpath(file_default_scheme() . '://'))) . '</p>';
  43. $description .= '<p style="color: red; font-weight: bold; font-size: 18px;">' . t('This action cannot be undone.') . '</p>';
  44. return confirm_form($form, $question, 'admin/config/media/file-system/settings', $description, t('Transliterate'));
  45. }
  46. /**
  47. * Form submit function; retroactively transliterates existing file names.
  48. *
  49. * @see transliteration_retroactive()
  50. */
  51. function transliteration_retroactive_submit($form, &$form_state) {
  52. $count = 0;
  53. $errors = array();
  54. $result = transliteration_file_query()->execute();
  55. while ($file = $result->fetchObject()) {
  56. $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
  57. $scheme = file_uri_scheme($file->uri);
  58. // Missing implementation.
  59. if (!$wrapper) {
  60. $errors[] = file_uri_target($file->uri);
  61. continue;
  62. }
  63. // Skip non-writable stream wrappers.
  64. $writeable_stream_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE);
  65. if (!isset($writeable_stream_wrappers[$scheme])) {
  66. continue;
  67. }
  68. // Missing file.
  69. if (!file_exists($wrapper->realpath())) {
  70. $errors[] = file_uri_target($file->uri);
  71. continue;
  72. }
  73. // Sanitize file name.
  74. $filename = transliteration_clean_filename(basename($file->uri));
  75. // Build destination URI.
  76. $destination = file_stream_wrapper_uri_normalize(drupal_dirname($file->uri) . '/' . $filename);
  77. $destination = file_destination($destination, FILE_EXISTS_RENAME);
  78. // Rename and update the file record accordingly.
  79. if ($wrapper->rename($file->uri, $destination)) {
  80. // If possible, add a url redirect to handle old URL references.
  81. if (module_exists('redirect')) {
  82. $redirect = new stdClass();
  83. redirect_object_prepare($redirect, array(
  84. 'source' => $wrapper->getDirectoryPath() . '/' . file_uri_target($file->uri),
  85. 'redirect' => $wrapper->getDirectoryPath() . '/' . file_uri_target($destination),
  86. 'status_code' => 301)
  87. );
  88. redirect_save($redirect);
  89. }
  90. $file->uri = $destination;
  91. // Don't use file_save() as it modifies the timestamp.
  92. drupal_write_record('file_managed', $file, 'fid');
  93. // Inform modules that the file has been updated.
  94. module_invoke_all('file_update', $file);
  95. $count++;
  96. }
  97. else {
  98. $errors[] = file_uri_target($file->uri);
  99. }
  100. }
  101. if ($errors) {
  102. $message = t('Not all file names could be converted. The following files could not be accessed and have been ignored:');
  103. $message .= theme('item_list', array('items' => $errors));
  104. drupal_set_message($message, 'error');
  105. }
  106. else {
  107. drupal_set_message(t('@filenames have been successfully transliterated.', array('@filenames' => format_plural($count, '1 file name', '@count file names'))));
  108. }
  109. // Flush page cache.
  110. cache_clear_all();
  111. }
  112. /**
  113. * Builds a query that returns all file names from the database containing non-ASCII characters.
  114. *
  115. * @return
  116. * SelectQuery
  117. */
  118. function transliteration_file_query() {
  119. // Regular expressions are not supported by Drupal's database layer and
  120. // operators differ between manufacturers.
  121. switch (db_driver()) {
  122. case 'mysql':
  123. case 'mysqli':
  124. $operator = 'NOT REGEXP';
  125. if (variable_get('transliteration_file_lowercase', TRUE)) {
  126. $operator .= ' BINARY';
  127. }
  128. $regex = '/[a-z0-9_.-]+$';
  129. break;
  130. case 'pgsql':
  131. $operator = '!~*';
  132. $regex = '/[a-z0-9_.-]+$';
  133. break;
  134. case 'mssql':
  135. $operator = 'LIKE';
  136. $regex = '%[^a-z0-9_.-]%';
  137. break;
  138. default:
  139. return FALSE;
  140. }
  141. return db_select('file_managed')
  142. ->fields('file_managed')
  143. ->condition('uri', $regex, $operator);
  144. }