View source
<?php
function image_install() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("CREATE TABLE {image} (\n `nid` INTEGER UNSIGNED NOT NULL,\n `fid` INTEGER UNSIGNED NOT NULL,\n `image_size` VARCHAR(32) NOT NULL,\n PRIMARY KEY (`nid`, `image_size`),\n INDEX image_fid(`fid`)\n ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
break;
case 'pgsql':
db_query("CREATE TABLE {image} (\n nid int_unsigned NOT NULL,\n fid int_unsigned NOT NULL,\n image_size VARCHAR(32) NOT NULL,\n PRIMARY KEY (nid, image_size)\n );\n CREATE INDEX {image_fid} on {image}(fid);");
break;
case 'oracle10':
db_query("CREATE TABLE {image} (\n nid INTEGER NOT NULL,\n fid INTEGER NOT NULL,\n image_size VARCHAR2(32) NOT NULL,\n CONSTRAINT {image}_pkey PRIMARY KEY (nid, image_size)\n )");
db_query("CREATE INDEX {image_fid} on {image}(fid)");
break;
}
}
function image_uninstall() {
db_query('DROP TABLE {image}');
variable_del('image_max_upload_size');
variable_del('image_updated');
variable_del('image_default_path');
variable_del('image_sizes');
variable_del('image_block_0_number_images');
variable_del('image_block_1_number_images');
}
function image_update_1() {
if (db_table_exists('image')) {
if ($result = db_query("SELECT * FROM {image}")) {
$fields = array(
'thumb_path' => 'thumbnail',
'preview_path' => 'preview',
'image_path' => '_original',
);
while ($old_image = db_fetch_object($result)) {
foreach ($fields as $old => $new) {
$old_file = '';
if (file_exists($old_image->{$old})) {
$old_file = $old_image->{$old};
}
else {
$old_file = file_create_path($old_image->{$old});
}
if ($old_file && $old_image->{$old} != '' && db_num_rows(db_query("SELECT fid FROM {files} WHERE nid=%d and filename='%s'", $old_image->nid, $new)) == 0) {
_image_insert($old_image, $new, $old_file);
}
}
}
}
}
return array();
}
function image_update_2() {
$sizes = variable_get('image_sizes', FALSE);
if ($sizes) {
$new_sizes = array(
IMAGE_ORIGINAL => array(
'width' => '',
'height' => '',
'label' => t('Original'),
),
);
foreach ($sizes as $size) {
$key = drupal_strtolower($size['label']);
$size['label'] = drupal_ucfirst($size['label']);
$new_sizes[$key] = $size;
}
variable_set('image_sizes', $new_sizes);
}
return array();
}
function image_update_3() {
$sizes = variable_get('image_sizes', FALSE);
if ($sizes) {
$new_sizes = array();
foreach ($sizes as $key => $size) {
$size['link'] = 1;
$new_sizes[$key] = $size;
}
variable_set('image_sizes', $new_sizes);
}
return array();
}
function image_update_4() {
$ret = array();
$files_path = rtrim(file_directory_path(), '\\/');
$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);
while ($file = db_fetch_object($result)) {
$file->filepath = file_create_path($file->filepath);
if (file_exists($file->filepath)) {
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))) {
$ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE fid = " . (int) $file->fid);
}
else {
$ret[] = update_sql("UPDATE {files} SET filepath = '" . db_escape_string($file->filepath) . "' WHERE fid = " . (int) $file->fid);
}
}
else {
$ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE fid = " . (int) $file->fid);
}
}
$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 )");
while ($file = db_fetch_object($result)) {
$ret[] = update_sql("DELETE FROM {files} WHERE fid = " . (int) $file->fid);
}
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE NOT EXISTS (SELECT * FROM {files} WHERE {files}.fid = {file_revisions}.fid)");
return $ret;
}
function image_update_5() {
$ret = array();
if ($old_sizes = variable_get('image_sizes', FALSE)) {
if (!isset($old_sizes[IMAGE_ORIGINAL])) {
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' => url('http://drupal.org/node/158334'),
'@old_sizes' => print_r($old_sizes, 1),
)), 'error');
return array();
}
if (!isset($old_sizes[IMAGE_PREVIEW])) {
$old_sizes[IMAGE_PREVIEW] = array(
'width' => 640,
'height' => 640,
'label' => t('Preview'),
'link' => IMAGE_LINK_SHOWN,
);
}
if (!isset($old_sizes[IMAGE_THUMBNAIL])) {
$old_sizes[IMAGE_THUMBNAIL] = array(
'width' => 100,
'height' => 100,
'label' => t('Thumbnail'),
'link' => IMAGE_LINK_SHOWN,
);
}
$new_sizes = array();
foreach ($old_sizes as $old_key => $size) {
$new_key = drupal_strtolower(drupal_substr($old_key, 0, 32));
if ($new_key != $old_key) {
$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) . "'");
}
$new_sizes[$new_key] = $size;
}
variable_set('image_sizes', $new_sizes);
}
return $ret;
}
function image_update_5200() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("CREATE TABLE {image} (\n `nid` INTEGER UNSIGNED NOT NULL,\n `fid` INTEGER UNSIGNED NOT NULL,\n `image_size` VARCHAR(32) NOT NULL,\n PRIMARY KEY (`nid`, `image_size`),\n INDEX image_fid(`fid`)\n ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
break;
case 'pgsql':
$ret[] = update_sql("CREATE TABLE {image} (\n nid int_unsigned NOT NULL,\n fid int_unsigned NOT NULL,\n image_size VARCHAR(32) NOT NULL,\n PRIMARY KEY (nid, image_size)\n );");
$ret[] = update_sql("CREATE INDEX {image_fid} on {image}(fid);");
break;
}
drupal_load('module', 'image');
$args = array_map('db_escape_string', array_keys(image_get_sizes()));
$cond = " IN ('" . implode("', '", $args) . "')";
if (db_table_exists('file_revisions')) {
$ret[] = update_sql("INSERT IGNORE 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);
$ret[] = update_sql("DELETE FROM {file_revisions} WHERE EXISTS (SELECT * FROM {image} WHERE {image}.fid = {file_revisions}.fid)");
}
else {
$ret[] = update_sql("INSERT IGNORE 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);
$ret[] = update_sql("DELETE FROM {upload} WHERE EXISTS (SELECT * FROM {image} WHERE {image}.fid = {upload}.fid)");
}
return $ret;
}
function image_update_5201() {
drupal_load('module', 'image');
$sizes = image_get_sizes();
foreach ($sizes as $key => $size) {
$sizes[$key]['operation'] = 'scale';
}
variable_set('image_sizes', $sizes);
return array();
}