/[drupal]/contributions/modules/transliteration/transliteration.install
ViewVC logotype

Contents of /contributions/modules/transliteration/transliteration.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Fri Oct 9 12:19:44 2009 UTC (6 weeks, 6 days ago) by smk
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +4 -4 lines
File MIME type: text/x-php
Updated doxygen documentation.
1 <?php
2 // $Id: transliteration.install,v 1.4 2009/08/25 14:44:25 smk Exp $
3
4 /**
5 * @file
6 * Install, update, and uninstall functions for the transliteration module.
7 */
8
9 /**
10 * Whether to perform retroactive transliteration of existing files.
11 * Since this might break hard-coded links to files in content, it is disabled
12 * by default. Set to TRUE to enable.
13 */
14 define('TRANSLITERATION_RETROACTIVE', FALSE);
15
16 /**
17 * Implementation of hook_install().
18 */
19 function transliteration_install() {
20 if (TRANSLITERATION_RETROACTIVE) {
21 transliteration_install_retroactive();
22 }
23 else {
24 $t = get_t();
25 drupal_set_message($t('Existing filenames have not been transliterated.'));
26 }
27 }
28
29 /**
30 * Implementation of hook_uninstall().
31 */
32 function transliteration_uninstall() {
33 }
34
35 /**
36 * Helper function; retroactivly transliterate existing file names.
37 */
38 function transliteration_install_retroactive() {
39 module_load_include('inc', 'transliteration');
40
41 $t = get_t();
42 $errors = array();
43
44 // Get all of the files that need to be converted, that is those that
45 // contain characters other than alphanumerics, underscores, dots, or
46 // hyphens.
47 $query = "SELECT fid, filepath FROM {files} WHERE filepath ";
48
49 // Regexp operators differ between database manufacturers.
50 switch ($GLOBALS['db_type']) {
51 case 'mysql':
52 case 'mysqli':
53 $query .= "REGEXP '[^0-9a-z_./-]'";
54 break;
55
56 case 'pgsql':
57 $query .= "~* '[^0-9a-z_./-]'";
58 break;
59
60 case 'mssql':
61 // SQL Server's LIKE is case sensitive.
62 $query .= "LIKE '%[^0-9A-Za-z_./-]%'";
63 break;
64
65 default:
66 drupal_set_message($t('Filenames could not be transliterated: database type not supported.'), 'error');
67 return;
68 }
69
70 $result = db_query($query);
71
72 while ($file = db_fetch_object($result)) {
73 // Transliterate the filename, skip if result is identical.
74 $transliterated = dirname($file->filepath) . '/' . transliteration_clean_filename(basename($file->filepath));
75 if ($transliterated == $file->filepath) {
76 continue;
77 }
78
79 // Rename the file but do a shortcut check first to avoid warnings.
80 if (realpath($file->filepath) && file_move($file->filepath, $transliterated, FILE_EXISTS_RENAME)) {
81 $transliterated = $file->filepath;
82 db_query("UPDATE {files} SET filepath = '%s' WHERE fid = %d", $transliterated, $file->fid);
83 }
84 else {
85 $errors[] = $file->filepath;
86 }
87 }
88
89 if ($errors) {
90 $message = $t('Not all existing filenames could be transliterated. The following files could not be accessed:');
91 $message .= theme_item_list($errors);
92 drupal_set_message($message, 'error');
93 }
94 else {
95 drupal_set_message($t('Existing filenames have been successfully transliterated.'));
96 }
97 }
98
99 /**
100 * Retroactively transliterate existing filenames.
101 */
102 function transliteration_update_1() {
103 $ret = array();
104 if (TRANSLITERATION_RETROACTIVE) {
105 transliteration_install_retroactive();
106 }
107 else {
108 $t = get_t();
109 drupal_set_message($t('Existing filenames have not been transliterated.'));
110 }
111 return $ret;
112 }
113

  ViewVC Help
Powered by ViewVC 1.1.2