| 1 |
<?php
|
| 2 |
// $Id: image.install,v 1.32 2009/09/05 15:04:48 joachim Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_schema().
|
| 6 |
*/
|
| 7 |
function image_schema() {
|
| 8 |
$schema['image'] = array(
|
| 9 |
'description' => 'Stores image files information.',
|
| 10 |
'fields' => array(
|
| 11 |
'nid' => array(
|
| 12 |
'description' => 'Primary Key: The {node}.nid of the image node.',
|
| 13 |
'type' => 'int',
|
| 14 |
'unsigned' => TRUE,
|
| 15 |
'not null' => TRUE,
|
| 16 |
'default' => 0,
|
| 17 |
),
|
| 18 |
'fid' => array(
|
| 19 |
'description' => 'Index: The {files}.fid of the image file.',
|
| 20 |
'type' => 'int',
|
| 21 |
'unsigned' => TRUE,
|
| 22 |
'not null' => TRUE,
|
| 23 |
'default' => 0,
|
| 24 |
),
|
| 25 |
'image_size' => array(
|
| 26 |
'description' => 'Primary Key: The {files}.filename of the image file. For image module this indicates the file size.',
|
| 27 |
'type' => 'varchar',
|
| 28 |
'length' => 32,
|
| 29 |
'not null' => TRUE,
|
| 30 |
'default' => '',
|
| 31 |
),
|
| 32 |
),
|
| 33 |
'primary key' => array('nid', 'image_size'),
|
| 34 |
'indexes' => array(
|
| 35 |
'fid' => array('fid'),
|
| 36 |
),
|
| 37 |
);
|
| 38 |
return $schema;
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Implementation of hook_install().
|
| 43 |
*/
|
| 44 |
function image_install() {
|
| 45 |
drupal_install_schema('image');
|
| 46 |
// Set reasonable default node options (not promoted to front page).
|
| 47 |
variable_set('node_options_image', array('status'));
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Implementation of hook_uninstall().
|
| 52 |
*/
|
| 53 |
function image_uninstall() {
|
| 54 |
drupal_uninstall_schema('image');
|
| 55 |
|
| 56 |
variable_del('image_max_upload_size');
|
| 57 |
variable_del('image_updated');
|
| 58 |
variable_del('image_default_path');
|
| 59 |
variable_del('image_sizes');
|
| 60 |
variable_del('image_block_0_number_images');
|
| 61 |
variable_del('image_block_1_number_images');
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Implementation of hook_requirements().
|
| 66 |
*/
|
| 67 |
function image_requirements($phase) {
|
| 68 |
$requirements = array();
|
| 69 |
|
| 70 |
if ($phase == 'runtime') {
|
| 71 |
// Make sure we've got a working toolkit
|
| 72 |
if ($toolkit = image_get_toolkit()) {
|
| 73 |
$requirements['image_toolkit'] = array(
|
| 74 |
'value' => t('The %toolkit_name toolkit is installed.', array('%toolkit_name' => $toolkit)),
|
| 75 |
'severity' => REQUIREMENT_OK,
|
| 76 |
);
|
| 77 |
}
|
| 78 |
else {
|
| 79 |
$requirements['image_toolkit'] = array(
|
| 80 |
'value' => t('Not installed.'),
|
| 81 |
'severity' => REQUIREMENT_ERROR,
|
| 82 |
'description' => t('No image toolkit is currently enabled. Without one the image module will not be able to resize your images. You can select one from the <a href="@link">image toolkit settings page</a>.', array('@link' => url('admin/settings/image-toolkit'))),
|
| 83 |
);
|
| 84 |
}
|
| 85 |
$requirements['image_toolkit']['title'] = t('Image toolkit');
|
| 86 |
|
| 87 |
|
| 88 |
// File paths
|
| 89 |
$image_path = file_create_path(file_directory_path() .'/'. variable_get('image_default_path', 'images'));
|
| 90 |
$temp_path = $image_path . '/temp';
|
| 91 |
if (!file_check_directory($image_path, FILE_CREATE_DIRECTORY)) {
|
| 92 |
$requirements['image_dirs'] = array(
|
| 93 |
'value' => t('Missing directory.'),
|
| 94 |
'severity' => REQUIREMENT_ERROR,
|
| 95 |
'description' => t("The image module's image directory %image_dir is missing.", array('%image_dir' => $image_path)),
|
| 96 |
);
|
| 97 |
}
|
| 98 |
else if (!file_check_directory($temp_path, FILE_CREATE_DIRECTORY)) {
|
| 99 |
$requirements['image_dirs'] = array(
|
| 100 |
'value' => t('Missing temp directory.'),
|
| 101 |
'severity' => REQUIREMENT_ERROR,
|
| 102 |
'description' => t("The image module's temp directory %temp_dir is missing.", array('%temp_dir' => $temp_path)),
|
| 103 |
);
|
| 104 |
}
|
| 105 |
else {
|
| 106 |
$requirements['image_dirs'] = array(
|
| 107 |
'value' => t('Exists (%path).', array('%path' => $image_path)),
|
| 108 |
'severity' => REQUIREMENT_OK,
|
| 109 |
);
|
| 110 |
}
|
| 111 |
$requirements['image_dirs']['title'] = t('Image module directories');
|
| 112 |
}
|
| 113 |
|
| 114 |
return $requirements;
|
| 115 |
}
|
| 116 |
|
| 117 |
|
| 118 |
/**
|
| 119 |
* Upgrade to the new image_sizes variable format.
|
| 120 |
*/
|
| 121 |
function image_update_2() {
|
| 122 |
$sizes = variable_get('image_sizes', FALSE);
|
| 123 |
if ($sizes) {
|
| 124 |
$new_sizes = array(IMAGE_ORIGINAL => array('width' => '', 'height' => '', 'label' => t('Original')));
|
| 125 |
foreach ($sizes as $size) {
|
| 126 |
$key = drupal_strtolower($size['label']);
|
| 127 |
$size['label'] = drupal_ucfirst($size['label']);
|
| 128 |
$new_sizes[$key] = $size;
|
| 129 |
}
|
| 130 |
variable_set('image_sizes', $new_sizes);
|
| 131 |
}
|
| 132 |
return array();
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Add the link field to each size.
|
| 137 |
*/
|
| 138 |
function image_update_3() {
|
| 139 |
$sizes = variable_get('image_sizes', FALSE);
|
| 140 |
if ($sizes) {
|
| 141 |
$new_sizes = array();
|
| 142 |
foreach ($sizes as $key => $size) {
|
| 143 |
$size['link'] = 1;
|
| 144 |
$new_sizes[$key] = $size;
|
| 145 |
}
|
| 146 |
variable_set('image_sizes', $new_sizes);
|
| 147 |
}
|
| 148 |
return array();
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Clean up all the records that aren't in the files directory.
|
| 153 |
*/
|
| 154 |
function image_update_4() {
|
| 155 |
$ret = array();
|
| 156 |
|
| 157 |
// Locate image files that aren't stored in the files directory.
|
| 158 |
$files_path = rtrim(file_directory_path(), '\\/');
|
| 159 |
$result = db_query("SELECT f.nid, f.fid, f.filename, f.filepath FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND f.filename = '_original' AND NOT f.filepath LIKE '%s/%%'", $files_path);
|
| 160 |
while ($file = db_fetch_object($result)) {
|
| 161 |
$file->filepath = file_create_path($file->filepath);
|
| 162 |
if (file_exists($file->filepath)) {
|
| 163 |
// File exists, make sure there's not a duplicate record.
|
| 164 |
if (db_result(db_query("SELECT COUNT(*) FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND filepath = '%s' AND fid <> %d", $file->filepath, $file->fid))) {
|
| 165 |
$ret[] = update_sql("DELETE FROM {files} WHERE fid = ". (int)$file->fid);
|
| 166 |
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE fid = ". (int)$file->fid);
|
| 167 |
}
|
| 168 |
else {
|
| 169 |
$ret[] = update_sql("UPDATE {files} SET filepath = '". db_escape_string($file->filepath) ."' WHERE fid = ". (int)$file->fid);
|
| 170 |
}
|
| 171 |
}
|
| 172 |
else {
|
| 173 |
$ret[] = update_sql("DELETE FROM {files} WHERE fid = ". (int)$file->fid);
|
| 174 |
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE fid = ". (int)$file->fid);
|
| 175 |
}
|
| 176 |
}
|
| 177 |
|
| 178 |
// Check for and remove {files} with duplicate filenames.
|
| 179 |
$result = db_query("SELECT f1.fid, f1.nid, f1.filepath FROM {files} f1 INNER JOIN {node} n ON f1.nid = n.nid WHERE n.type = 'image' AND EXISTS ( SELECT * FROM {files} f2 WHERE f2.filepath = f1.filepath AND f1.fid <> f2.fid AND f1.fid < f2.fid )");
|
| 180 |
while ($file = db_fetch_object($result)) {
|
| 181 |
$ret[] = update_sql("DELETE FROM {files} WHERE fid = ". (int)$file->fid);
|
| 182 |
}
|
| 183 |
|
| 184 |
// Delete rows from {file_revisions} that don't have matching {files}.
|
| 185 |
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE NOT EXISTS (SELECT * FROM {files} WHERE {files}.fid = {file_revisions}.fid)");
|
| 186 |
|
| 187 |
return $ret;
|
| 188 |
}
|
| 189 |
|
| 190 |
/**
|
| 191 |
* Make sure that everyone's size settings are in the right format.
|
| 192 |
*/
|
| 193 |
function image_update_5() {
|
| 194 |
$ret = array();
|
| 195 |
|
| 196 |
if ($old_sizes = variable_get('image_sizes', FALSE)) {
|
| 197 |
// Make sure all the required sizes are represented.
|
| 198 |
if (!isset($old_sizes[IMAGE_ORIGINAL])) {
|
| 199 |
drupal_set_message(t("The original image size was missing so no changes were made. See this <a href='@link'>image module issue</a> for more information. Include the following:<br /><pre>@old_sizes\n</pre>", array('@link' => 'http://drupal.org/node/158334', '@old_sizes' => print_r($old_sizes, 1))), 'error');
|
| 200 |
return array();
|
| 201 |
}
|
| 202 |
// These sizes may already exist under incorrect keys. We'll put a default
|
| 203 |
// copy in that will either be overwritten by the existing version, or used
|
| 204 |
// if there isn't an existing version.
|
| 205 |
if (!isset($old_sizes[IMAGE_PREVIEW])) {
|
| 206 |
$old_sizes[IMAGE_PREVIEW] = array('width' => 640, 'height' => 640, 'label' => t('Preview'), 'link' => IMAGE_LINK_SHOWN);
|
| 207 |
}
|
| 208 |
if (!isset($old_sizes[IMAGE_THUMBNAIL])) {
|
| 209 |
$old_sizes[IMAGE_THUMBNAIL] = array('width' => 100, 'height' => 100, 'label' => t('Thumbnail'), 'link' => IMAGE_LINK_SHOWN);
|
| 210 |
}
|
| 211 |
|
| 212 |
$new_sizes = array();
|
| 213 |
foreach ($old_sizes as $old_key => $size) {
|
| 214 |
// Keys should be lowercase and less than 32 chars long.
|
| 215 |
$new_key = drupal_strtolower(drupal_substr($old_key, 0, 32));
|
| 216 |
// Update the files.
|
| 217 |
if ($new_key != $old_key) {
|
| 218 |
$ret[] = update_sql("UPDATE {files} f INNER JOIN {node} n ON f.nid = n.nid SET f.filename = '". db_escape_string($new_key) ."' WHERE n.type = 'image' AND filename = '". db_escape_string($old_key) ."'");
|
| 219 |
}
|
| 220 |
$new_sizes[$new_key] = $size;
|
| 221 |
}
|
| 222 |
// Save the sizes.
|
| 223 |
variable_set('image_sizes', $new_sizes);
|
| 224 |
}
|
| 225 |
|
| 226 |
return $ret;
|
| 227 |
}
|
| 228 |
|
| 229 |
/**
|
| 230 |
* Move image files into their own table.
|
| 231 |
*
|
| 232 |
* First update for the 5.2 branch, using the update naming convention layed
|
| 233 |
* out in: http://drupal.org/node/114774#update-n
|
| 234 |
*/
|
| 235 |
function image_update_5200() {
|
| 236 |
$ret = array();
|
| 237 |
|
| 238 |
// Versions prior to 5 had an {image} table which is no longer used on 5.1
|
| 239 |
// but not removed.
|
| 240 |
if (db_table_exists('image')) {
|
| 241 |
db_rename_table($ret, 'image', 'image_old');
|
| 242 |
// Notify the user that their table has been moved.
|
| 243 |
drupal_set_message('Your existing {image} table has been renamed {image_old}.');
|
| 244 |
}
|
| 245 |
|
| 246 |
switch ($GLOBALS['db_type']) {
|
| 247 |
case 'mysql':
|
| 248 |
case 'mysqli':
|
| 249 |
$ret[] = update_sql("CREATE TABLE {image} (
|
| 250 |
`nid` INTEGER UNSIGNED NOT NULL,
|
| 251 |
`fid` INTEGER UNSIGNED NOT NULL,
|
| 252 |
`image_size` VARCHAR(32) NOT NULL,
|
| 253 |
PRIMARY KEY (`nid`, `image_size`),
|
| 254 |
INDEX image_fid(`fid`)
|
| 255 |
) /*!40100 DEFAULT CHARACTER SET utf8 */;");
|
| 256 |
break;
|
| 257 |
case 'pgsql':
|
| 258 |
$ret[] = update_sql("CREATE TABLE {image} (
|
| 259 |
nid int_unsigned NOT NULL,
|
| 260 |
fid int_unsigned NOT NULL,
|
| 261 |
image_size VARCHAR(32) NOT NULL,
|
| 262 |
PRIMARY KEY (nid, image_size)
|
| 263 |
);");
|
| 264 |
$ret[] = update_sql("CREATE INDEX {image_fid} on {image}(fid);");
|
| 265 |
break;
|
| 266 |
}
|
| 267 |
// Copy image files records into the new table.
|
| 268 |
drupal_load('module', 'image');
|
| 269 |
$args = array_map('db_escape_string', array_keys(image_get_sizes()));
|
| 270 |
$cond = " IN ('". implode("', '", $args) ."')";
|
| 271 |
|
| 272 |
// Upgrade from 5.x-1.x to 5.x-2.x.
|
| 273 |
// Group by clause prevents duplicate insertions from bad data:
|
| 274 |
// @see http://drupal.org/node/207557
|
| 275 |
if (db_table_exists('file_revisions')) {
|
| 276 |
$ret[] = update_sql("INSERT INTO {image} SELECT DISTINCT f.nid, f.fid, f.filename FROM {files} f INNER JOIN {node} n ON f.nid = n.nid WHERE n.type = 'image' AND f.filename" . $cond . " GROUP BY nid,filename");
|
| 277 |
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE EXISTS (SELECT * FROM {image} WHERE {image}.fid = {file_revisions}.fid)");
|
| 278 |
}
|
| 279 |
// Upgrade from 5.x to 6.x-1.x.
|
| 280 |
else {
|
| 281 |
$ret[] = update_sql("INSERT INTO {image} SELECT DISTINCT u.nid, f.fid, f.filename FROM {upload} u INNER JOIN {files} f ON u.fid = f.fid INNER JOIN {node} n ON u.nid = n.nid WHERE n.type = 'image' AND f.filename" . $cond . " GROUP BY nid,filename");
|
| 282 |
$ret[] = update_sql("DELETE FROM {upload} WHERE EXISTS (SELECT * FROM {image} WHERE {image}.fid = {upload}.fid)");
|
| 283 |
}
|
| 284 |
|
| 285 |
return $ret;
|
| 286 |
}
|
| 287 |
|
| 288 |
/**
|
| 289 |
* Updating image size settings to use scaling as the default operation.
|
| 290 |
*/
|
| 291 |
function image_update_5201() {
|
| 292 |
drupal_load('module', 'image');
|
| 293 |
$sizes = image_get_sizes();
|
| 294 |
foreach ($sizes as $key => $size) {
|
| 295 |
$sizes[$key]['operation'] = 'scale';
|
| 296 |
}
|
| 297 |
variable_set('image_sizes', $sizes);
|
| 298 |
|
| 299 |
return array();
|
| 300 |
}
|
| 301 |
|
| 302 |
/**
|
| 303 |
* Add primary key and index on 'fid' to {image} table.
|
| 304 |
*/
|
| 305 |
function image_update_6100() {
|
| 306 |
$ret = array();
|
| 307 |
db_change_field($ret, 'image', 'nid', 'nid', array(
|
| 308 |
'type' => 'int',
|
| 309 |
'unsigned' => TRUE,
|
| 310 |
'not null' => TRUE,
|
| 311 |
'default' => 0,
|
| 312 |
));
|
| 313 |
db_change_field($ret, 'image', 'fid', 'fid', array(
|
| 314 |
'type' => 'int',
|
| 315 |
'unsigned' => TRUE,
|
| 316 |
'not null' => TRUE,
|
| 317 |
'default' => 0,
|
| 318 |
));
|
| 319 |
|
| 320 |
/**
|
| 321 |
* We remove and re-add primary keys and indexes because the column types
|
| 322 |
* are changed.
|
| 323 |
* Sometimes however these don't exist in the first place (@see
|
| 324 |
* <http://drupal.org/node/562810>; the @ takes care of suppressing the
|
| 325 |
* error message this causes.
|
| 326 |
*/
|
| 327 |
@db_drop_primary_key($ret, 'image');
|
| 328 |
db_add_primary_key($ret, 'image', array('nid', 'image_size'));
|
| 329 |
@db_drop_index($ret, 'image', 'image_fid');
|
| 330 |
db_add_index($ret, 'image', 'fid', array('fid'));
|
| 331 |
|
| 332 |
// Notify the user that they may get harmless fail messages here.
|
| 333 |
$ret[] = array('success' => TRUE, 'query' => t('If you see a message of the form "Failed: ALTER TABLE {image} DROP PRIMARY KEY" or "DROP INDEX image_fid" here it is harmless.'));
|
| 334 |
return $ret;
|
| 335 |
}
|
| 336 |
|
| 337 |
/**
|
| 338 |
* Ensure that 'image_default_path' variable contains no trailing slash.
|
| 339 |
*/
|
| 340 |
function image_update_6101() {
|
| 341 |
$ret = array();
|
| 342 |
$path = variable_get('image_default_path', NULL);
|
| 343 |
if (!empty($path)) {
|
| 344 |
variable_set('image_default_path', rtrim($path, '/'));
|
| 345 |
}
|
| 346 |
return $ret;
|
| 347 |
}
|
| 348 |
|
| 349 |
/**
|
| 350 |
* Set reasonable default node options (not promoted to front page).
|
| 351 |
*/
|
| 352 |
function image_update_6102() {
|
| 353 |
$ret = array();
|
| 354 |
variable_set('node_options_image', variable_get('node_options_image', array('status')));
|
| 355 |
return $ret;
|
| 356 |
}
|
| 357 |
|
| 358 |
/**
|
| 359 |
* Rename permission "edit images" to "edit any images".
|
| 360 |
*/
|
| 361 |
function image_update_6103() {
|
| 362 |
$ret = array();
|
| 363 |
$result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
|
| 364 |
while ($role = db_fetch_object($result)) {
|
| 365 |
$renamed_permission = strtr($role->perm, array(
|
| 366 |
'edit images' => 'edit any images, delete any images',
|
| 367 |
'edit own images' => 'edit own images, delete own images',
|
| 368 |
));
|
| 369 |
if ($renamed_permission != $role->perm) {
|
| 370 |
$args = array($renamed_permission, $role->rid);
|
| 371 |
$query = "UPDATE {permission} SET perm = '%s' WHERE rid = %d";
|
| 372 |
_db_query_callback($args, TRUE);
|
| 373 |
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
|
| 374 |
$ret[] = update_sql($query);
|
| 375 |
}
|
| 376 |
}
|
| 377 |
return $ret;
|
| 378 |
}
|