#336580 by drewish: "imagecache" directory left behind after uninstall.
[project/imagecache.git] / imagecache.install
1 <?php
2 // $Id$
3
4 function imagecache_requirements($phase) {
5 $requirements = array();
6 // Ensure translations don't break at install time.
7 $t = get_t();
8
9 if ($phase == 'runtime') {
10
11 $imagecache_directory = file_create_path() .'/imagecache';
12 if (!file_check_directory($imagecache_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
13 if (!is_dir($imagecache_directory)) {
14 $requirements['imagecache_directory'] = array(
15 'title' => $t('ImageCache Directory'),
16 'value' => $t('%p does not a directory or is not readable by the webserver.', array('%p' => $imagecache_directory)),
17 'severity' => REQUIREMENT_ERROR,
18 );
19 }
20 elseif (!is_writable($imagecache_directory)) {
21 $requirements['imagecache_directory'] = array(
22 'title' => $t('ImageCache Directory'),
23 'value' => $t('%p is not writeable by the webserver.', array('%p' => $imagecache_directory)),
24 'severity' => REQUIREMENT_ERROR,
25 );
26 }
27 else {
28 $requirements['imagecache_directory'] = array(
29 'title' => $t('ImageCache Directory'),
30 'value' => $t('An unknown error occured.'),
31 'description' => $t('An unknown error occured trying to verify %p is a directory and is writable.', array('%p' => $imagecache_directory)),
32 'severity' => REQUIREMENT_ERROR,
33 );
34 }
35 }
36
37 if (!is_writable(file_directory_temp())) {
38 $requirements['imagecache_directory'] = array(
39 'title' => $t('ImageCache Temp Directory'),
40 'value' => $t('%p is not writeable by the webserver.', array('%p' => file_directory_temp())),
41 'severity' => REQUIREMENT_ERROR,
42 );
43 }
44 }
45 return $requirements;
46 }
47
48 function imagecache_schema() {
49 $schema['imagecache_preset'] = array(
50 'fields' => array(
51 'presetid' => array(
52 'description' => t('The primary identifier for an imagecache_preset.'),
53 'type' => 'serial',
54 'unsigned' => TRUE,
55 'not null' => TRUE),
56 'presetname' => array(
57 'description' => t('The primary identifier for a node.'),
58 'type' => 'varchar',
59 'length' => 255,
60 'not null' => TRUE),
61 ),
62 'primary key' => array('presetid'),
63 );
64
65 $schema['imagecache_action'] = array(
66 'fields' => array(
67 'actionid' => array(
68 'description' => t('The primary identifier for an imagecache_action.'),
69 'type' => 'serial',
70 'unsigned' => TRUE,
71 'not null' => TRUE),
72 'presetid' => array(
73 'description' => t('The primary identifier for an imagecache_preset.'),
74 'type' => 'int',
75 'unsigned' => TRUE,
76 'not null' => TRUE,
77 'default' => 0),
78 'weight' => array(
79 'description' => t('The weight of the action in the preset.'),
80 'type' => 'int',
81 'unsigned' => FALSE,
82 'not null' => TRUE,
83 'default' => 0),
84 'module' => array(
85 'description' => t('The module that defined the action.'),
86 'type' => 'varchar',
87 'length' => 255,
88 'not null' => TRUE),
89 'action' => array(
90 'description' => t('The unique ID of the action to be executed.'),
91 'type' => 'varchar',
92 'length' => 255,
93 'not null' => TRUE),
94 'data' => array(
95 'description' => t('The configuration data for the action.'),
96 'type' => 'text',
97 'not null' => TRUE,
98 'size' => 'big',
99 'serialize' => TRUE),
100 ),
101 'primary key' => array('actionid'),
102 'indexes' => array(
103 'presetid' => array('presetid'),
104 ),
105 );
106
107
108
109 return $schema;
110 }
111
112 /**
113 * Implementation of hook_install().
114 */
115 function imagecache_install() {
116 drupal_install_schema('imagecache');
117 }
118
119 /**
120 * Implementation of hook_uninstall().
121 */
122 function imagecache_uninstall() {
123 // Remove any cached images.
124 $path = file_directory_path() .'/imagecache/';
125 if (is_dir($path)) {
126 _imagecache_recursive_delete($path);
127 }
128
129 drupal_uninstall_schema('imagecache');
130 }
131
132 // Add action id to actions table.
133 function imagecache_update_1() {
134 $ret = array();
135 $ret[] = update_sql('ALTER TABLE {imagecache_actions} ADD COLUMN actionid INT UNSIGNED NOT NULL primary key auto_increment');
136 return $ret;
137 }
138
139 // Rename rulesets to presets; Make all table names singular;
140 function imagecache_update_2() {
141 $ret = array();
142 $ret[] = update_sql('ALTER TABLE {imagecache_rulesets} RENAME TO {imagecache_preset}');
143 $ret[] = update_sql('ALTER TABLE {imagecache_actions} RENAME TO {imagecache_action}');
144 switch ($GLOBALS['db_type']) {
145 case 'mysql':
146 case 'mysqli':
147 $ret[] = update_sql('ALTER TABLE {imagecache_preset} CHANGE rulesetid presetid INT UNSIGNED NOT NULL AUTO_INCREMENT');
148 $ret[] = update_sql('ALTER TABLE {imagecache_preset} CHANGE rulesetname presetname VARCHAR(255) NOT NULL DEFAULT \'\'');
149 $ret[] = update_sql('ALTER TABLE {imagecache_action} CHANGE rulesetid presetid INTEGER NOT NULL DEFAULT 0');
150 break;
151
152 case 'pgsql':
153 $ret[] = update_sql('ALTER TABLE {imagecache_preset} RENAME COLUMN rulesetid TO presetid');
154 $ret[] = update_sql('ALTER TABLE {imagecache_preset} RENAME COLUMN rulesetname TO presetname');
155 $ret[] = update_sql('ALTER TABLE {imagecache_action} RENAME COLUMN rulesetid TO presetid');
156 break;
157 }
158 return $ret;
159 }
160
161
162 /**
163 * Remove auto-increment from tables, instead depending on the sequences table and db_next_id()
164 */
165 function imagecache_update_3() {
166 $ret = array();
167
168 $count_action = db_result(db_query('SELECT max(actionid) FROM {imagecache_action}')) + 1;
169 $count_preset = db_result(db_query('SELECT max(presetid) FROM {imagecache_preset}')) + 1;
170
171 switch ($GLOBALS['db_type']) {
172 case 'mysql':
173 case 'mysqli':
174 $ret[] = update_sql("ALTER TABLE {imagecache_action} CHANGE actionid actionid INT UNSIGNED NOT NULL");
175 $ret[] = update_sql("ALTER TABLE {imagecache_preset} CHANGE presetid presetid INT UNSIGNED NOT NULL");
176 // Add the sequences
177 $ret[] = update_sql("INSERT INTO {sequences} (name, id) VALUES ('{imagecache_action}_actionid', $count_action)");
178 $ret[] = update_sql("INSERT INTO {sequences} (name, id) VALUES ('{imagecache_preset}_presetid', $count_preset)");
179 break;
180 case 'pgsql':
181 db_change_column($ret, 'imagecache_action', 'actionid', 'actionid', 'INT', $attributes = array('not null' => TRUE, 'default' => '0'));
182 db_change_column($ret, 'imagecache_preset', 'presetid', 'presetid', 'INT', $attributes = array('not null' => TRUE, 'default' => '0'));
183 // Re-add our indexes
184 $ret[] = update_sql("ALTER TABLE {imagecache_action} ADD PRIMARY KEY (actionid)");
185 $ret[] = update_sql("ALTER TABLE {imagecache_preset} ADD PRIMARY KEY (rulesetid)");
186 // Add the sequences
187 $ret[] = update_sql("CREATE SEQUENCE {imagecache_action}_actionid_seq INCREMENT 1 START $count_action;");
188 $ret[] = update_sql("CREATE SEQUENCE {imagecache_preset}_presetid_seq INCREMENT 1 START $count_preset;");
189 }
190 return $ret;
191 }
192
193 function imagecache_update_4() {
194 $ret = array();
195
196 // add action column to the imagecache_action table just becuase serialization bugs me.
197 switch ($GLOBALS['db_type']) {
198 case 'mysql':
199 case 'mysqli':
200 $ret[] = update_sql("ALTER TABLE {imagecache_action} ADD COLUMN action varchar(255) not null default '' after weight");
201 case 'pgsql':
202 $ret[] = update_sql("ALTER TABLE {imagecache_action} ADD COLUMN action varchar(255) NOT NULL DEFAULT ''");
203 break;
204 }
205
206 // unserialize what we can.
207 $result = db_query("SELECT * FROM {imagecache_action}");
208 while ($row = db_fetch_array($result)) {
209 $data = unserialize($row['data']);
210
211 // remove function from data if present;
212 $function = $data['function'];
213 unset($data['function']);
214 $data = serialize($data);
215
216 // Rename scale and crop for any people who upgraded early...
217 if ($function == 'scale and crop') {
218 $function = 'scale_and_crop';
219 }
220 // Keep scale and crop and the old scale function seperate... I don't really want to break BC with
221 // the 2.x update. We'll deprecate this version.
222 if ($function == 'scale') {
223 $function = 'deprecated_scale';
224 }
225
226 // prefix with module name as per new status quo.
227 // since other modules couldn't implement actions before this update
228 // we assume imagecache...
229 $function = 'imagecache_'. $function;
230
231 db_query("UPDATE {imagecache_action} SET action='%s', data='%s' WHERE actionid = %d", $function, $data, $row['actionid']);
232 }
233 cache_clear_all('*', 'cache', TRUE);
234 return $ret;
235 }
236
237 function imagecache_update_5() {
238 // enable image API.
239 module_rebuild_cache(); // make sure new modules are in the system table.
240 module_enable(array('imageapi', 'imageapi_gd', 'imageapi_imagemagick')); // enable our new module.
241
242 // @todo: update formatter names: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/imagecache/imagecache.module?r1=1.68&r2=1.68.2.8&pathrev=DRUPAL-5--2
243 // ln: 516 diff 511.
244
245 return array();
246 }