transliteration.module

Tracking 7.x-3.x branch
  1. drupal
    1. 5 contributions/transliteration/transliteration.module
    2. 6 contributions/transliteration/transliteration.module
    3. 7 contributions/transliteration/transliteration.module

Converts non-latin text to US-ASCII and sanitizes file names.

@author Stefan M. Kudwien (http://drupal.org/user/48898)

Functions & methods

NameDescription
transliteration_clean_filenameTransliterates and sanitizes a file name.
transliteration_filter_infoImplements hook_filter_info().
transliteration_form_search_admin_settings_alterImplements hook_form_FORM_ID_alter().
transliteration_form_system_file_system_settings_alterImplements hook_form_FORM_ID_alter().
transliteration_getTransliterates text.
transliteration_initImplements hook_init().
transliteration_menuImplements hook_menu().
transliteration_search_preprocessImplements hook_search_preprocess().
_transliteration_filter_processProcess callback for the transliteration filter.
_transliteration_filter_settingsSettings callback for the transliteration filter.
_transliteration_filter_tipsFilter tips callback for the transliteration filter.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Converts non-latin text to US-ASCII and sanitizes file names.
  5. *
  6. * @author Stefan M. Kudwien (http://drupal.org/user/48898)
  7. */
  8. /**
  9. * Implements hook_menu().
  10. */
  11. function transliteration_menu() {
  12. $items['admin/config/media/file-system/settings'] = array(
  13. 'title' => 'Settings',
  14. 'file path' => drupal_get_path('module', 'system'),
  15. 'weight' => -10,
  16. 'type' => MENU_DEFAULT_LOCAL_TASK,
  17. );
  18. $items['admin/config/media/file-system/transliteration'] = array(
  19. 'title' => 'Transliteration',
  20. 'description' => 'Convert existing file names to US-ASCII.',
  21. 'page callback' => 'drupal_get_form',
  22. 'page arguments' => array('transliteration_retroactive'),
  23. 'access arguments' => array('administer site configuration'),
  24. 'file' => 'transliteration.admin.inc',
  25. 'weight' => 10,
  26. 'type' => MENU_LOCAL_TASK,
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Implements hook_form_FORM_ID_alter().
  32. *
  33. * Adds transliteration settings to the file system configuration form.
  34. */
  35. function transliteration_form_system_file_system_settings_alter(&$form, &$form_state) {
  36. $form['transliteration'] = array(
  37. '#type' => 'item',
  38. '#title' => t('Transliteration'),
  39. '#value' => '',
  40. );
  41. $form['transliteration']['transliteration_file_uploads'] = array(
  42. '#type' => 'checkbox',
  43. '#title' => t('Transliterate file names during upload.'),
  44. '#description' => t('Enable to convert file names to US-ASCII character set for cross-platform compatibility.'),
  45. '#default_value' => variable_get('transliteration_file_uploads', TRUE),
  46. );
  47. $form['transliteration']['transliteration_file_lowercase'] = array(
  48. '#type' => 'checkbox',
  49. '#title' => t('Lowercase transliterated file names.'),
  50. '#default_value' => variable_get('transliteration_file_lowercase', TRUE),
  51. '#description' => t('This is a recommended setting to prevent issues with case-insensitive file systems. It has no effect if transliteration has been disabled.'),
  52. );
  53. $form['buttons']['#weight'] = 1;
  54. }
  55. /**
  56. * Implements hook_form_FORM_ID_alter().
  57. *
  58. * Adds transliteration settings to the search settings form.
  59. */
  60. function transliteration_form_search_admin_settings_alter(&$form, &$form_state) {
  61. $form['transliteration'] = array(
  62. '#type' => 'fieldset',
  63. '#title' => t('Transliteration'),
  64. );
  65. $form['transliteration']['transliteration_search'] = array(
  66. '#type' => 'checkbox',
  67. '#title' => t('Transliterate search index and searched strings.'),
  68. '#description' => t('Enable to allow searching and indexing using US-ASCII character set, i.e. to treat accented and unaccented letters the same.'),
  69. '#default_value' => variable_get('transliteration_search', TRUE),
  70. );
  71. }
  72. /**
  73. * Transliterates and sanitizes a file name.
  74. *
  75. * The resulting file name has white space replaced with underscores, consists
  76. * of only US-ASCII characters, and is converted to lowercase (if configured).
  77. * If multiple files have been submitted as an array, the names will be
  78. * processed recursively.
  79. *
  80. * @param $filename
  81. * A file name, or an array of file names.
  82. * @param $source_langcode
  83. * Optional ISO 639 language code that denotes the language of the input and
  84. * is used to apply language-specific variations. If the source language is
  85. * not known at the time of transliteration, it is recommended to set this
  86. * argument to the site default language to produce consistent results.
  87. * Otherwise the current display language will be used.
  88. * @return
  89. * Sanitized file name, or array of sanitized file names.
  90. *
  91. * @see language_default()
  92. */
  93. function transliteration_clean_filename($filename, $source_langcode = NULL) {
  94. if (is_array($filename)) {
  95. foreach ($filename as $key => $value) {
  96. $filename[$key] = transliteration_clean_filename($value, $source_langcode);
  97. }
  98. return $filename;
  99. }
  100. $filename = transliteration_get($filename, '', $source_langcode);
  101. // Replace whitespace.
  102. $filename = str_replace(' ', '_', $filename);
  103. // Remove remaining unsafe characters.
  104. $filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);
  105. // Remove multiple consecutive non-alphabetical characters.
  106. $filename = preg_replace('/(_)_+|(\.)\.+|(-)-+/', '\\1\\2\\3', $filename);
  107. // Force lowercase to prevent issues on case-insensitive file systems.
  108. if (variable_get('transliteration_file_lowercase', TRUE)) {
  109. $filename = strtolower($filename);
  110. }
  111. return $filename;
  112. }
  113. /**
  114. * Transliterates text.
  115. *
  116. * Takes an input string in any language and character set, and tries to
  117. * represent it in US-ASCII characters by conveying, in Roman letters, the
  118. * pronunciation expressed by the text in some other writing system.
  119. *
  120. * @param $text
  121. * UTF-8 encoded text input.
  122. * @param $unknown
  123. * Replacement string for characters that do not have a suitable ASCII
  124. * equivalent.
  125. * @param $source_langcode
  126. * Optional ISO 639 language code that denotes the language of the input and
  127. * is used to apply language-specific variations. If the source language is
  128. * not known at the time of transliteration, it is recommended to set this
  129. * argument to the site default language to produce consistent results.
  130. * Otherwise the current display language will be used.
  131. * @return
  132. * Transliterated text.
  133. *
  134. * @see language_default()
  135. */
  136. function transliteration_get($text, $unknown = '?', $source_langcode = NULL) {
  137. if (!function_exists('_transliteration_process')) {
  138. module_load_include('inc', 'transliteration');
  139. }
  140. return _transliteration_process($text, $unknown, $source_langcode);
  141. }
  142. /**
  143. * Implements hook_init().
  144. *
  145. * Sanitizes file names during upload.
  146. */
  147. function transliteration_init() {
  148. if (!empty($_FILES['files']) && variable_get('transliteration_file_uploads', TRUE)) {
  149. // Figure out language, which is available in $_POST['language'] for node
  150. // forms.
  151. $langcode = NULL;
  152. if (!empty($_POST['language'])) {
  153. $languages = language_list();
  154. if (isset($languages[$_POST['language']])) {
  155. $langcode = $_POST['language'];
  156. }
  157. }
  158. foreach ($_FILES['files']['name'] as $field => $filename) {
  159. // Keep a copy of the unaltered file name.
  160. $_FILES['files']['orig_name'][$field] = $filename;
  161. $_FILES['files']['name'][$field] = transliteration_clean_filename($filename, $langcode);
  162. }
  163. }
  164. }
  165. /**
  166. * Implements hook_search_preprocess().
  167. *
  168. * Transliterates text added to the search index and user submitted search
  169. * keywords.
  170. */
  171. function transliteration_search_preprocess($text) {
  172. if (variable_get('transliteration_search', TRUE)) {
  173. return transliteration_get($text, '', language_default('language'));
  174. }
  175. return $text;
  176. }
  177. /**
  178. * Implements hook_filter_info().
  179. */
  180. function transliteration_filter_info() {
  181. return array(
  182. 'transliteration' => array(
  183. 'title' => t('Convert all characters to US-ASCII'),
  184. 'process callback' => '_transliteration_filter_process',
  185. 'settings callback' => '_transliteration_filter_settings',
  186. 'default settings' => array(
  187. 'no_known_transliteration' => '?'
  188. ),
  189. 'tips callback' => '_transliteration_filter_tips',
  190. ),
  191. );
  192. }
  193. /**
  194. * Process callback for the transliteration filter.
  195. */
  196. function _transliteration_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  197. return transliteration_get($text, $filter->settings['no_known_transliteration'], $langcode);
  198. }
  199. /**
  200. * Settings callback for the transliteration filter.
  201. */
  202. function _transliteration_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
  203. $filter->settings += $defaults;
  204. return array(
  205. 'no_known_transliteration' => array(
  206. '#type' => 'textfield',
  207. '#title' => t('Placeholder for characters with no known US-ASCII equivalent'),
  208. '#size' => 2,
  209. // The maximum length is 5 in order to accommodate unicode multibyte input.
  210. '#maxlength' => 5,
  211. '#default_value' => $filter->settings['no_known_transliteration'],
  212. )
  213. );
  214. }
  215. /**
  216. * Filter tips callback for the transliteration filter.
  217. */
  218. function _transliteration_filter_tips($filter, $format, $long) {
  219. return t('Non-latin text (e.g., å, ö, 漢) will be converted to US-ASCII equivalents (a, o, ?).');
  220. }