transliteration.install in Transliteration 6
File
transliteration.install
View source
<?php
function transliteration_install() {
transliteration_install_compat();
$errors = transliteration_install_retroactive();
if (!$errors) {
drupal_set_message('Existing filenames have been successfully transliterated.');
}
else {
$message = 'Not all existing filenames could be transliterated. The following files could not be accessed:';
$message .= theme_item_list($errors);
drupal_set_message($message, 'error');
}
}
function transliteration_install_compat() {
switch ($GLOBALS['db_type']) {
case 'pgsql':
if (!db_result(db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'substring_index'"))) {
db_query("CREATE OR REPLACE FUNCTION substring_index(text, text, integer)\n RETURNS text AS \$\$\n DECLARE\n tokens text[];\n BEGIN\n tokens := pg_catalog.string_to_array(\$1, \$2); \n\n IF \$3 >= 0 THEN\n RETURN pg_catalog.array_to_string(tokens[1:\$3], \$2);\n ELSE\n RETURN pg_catalog.array_to_string(tokens[(\$3 * -1):pg_catalog.array_upper(tokens, 1)], \$2);\n END IF;\n END;\n \$\$ IMMUTABLE STRICT LANGUAGE PLPGSQL;");
break;
}
}
}
function transliteration_install_retroactive() {
require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$op = 'RLIKE';
break;
case 'pgsql':
$op = '~*';
break;
}
$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)) {
$filepath = $file->filepath;
$filepath_new = dirname($filepath) . '/' . transliteration_clean_filename(basename($filepath));
$realpath = realpath($filepath);
if ($realpath && file_exists($realpath) && file_move($filepath, $filepath_new, FILE_EXISTS_RENAME)) {
$filepath_new = $filepath;
db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $filepath_new, $file->fid);
}
else {
$errors[] = $file->filepath;
}
}
return $errors;
}
function transliteration_update_1() {
$ret = array();
transliteration_install_compat();
$errors = transliteration_install_retroactive();
if (!$errors) {
drupal_set_message('Existing filenames have been successfully transliterated.');
}
else {
$message = 'Not all existing filenames could be transliterated. The following files could not be accessed:';
$message .= theme_item_list($errors);
drupal_set_message($message, 'error');
}
return $ret;
}