image模块转换到CCK ImageField

关于image模块转换到CCK ImageField的讨论可以看http://drupal.org/node/201983这篇文章,从去年我就一直关注这个问题,下面我来谈谈我的经验。

我的网站使用的image模块版本是5.x-1.9,并启用了image_gallery、image_attach、image_import子模块,image_attach只是安装了但没有用到。

http://drupal.org/node/201983这篇文章有很多热心人发布了转换代码,但经过我测试都有问题,最后经我修改测试终于弄好了。

这里讲的image模块版本是5.x-1.9,6.x的image模块转换到CCK ImageField,请看:http://drupal.org/node/432860

由于我没有安装video.module以及image_attach没有任何数据,所以这部分代码我不能保证可行。

下面代码中我保留原作者的注释。

  • 转换之前请备份你的数据和文件
  • 建立新的内容类型,如:photos,建立imagefield field如:photo(如果你建立其他名字的内容类型和imagefield field,请修改代码中的$field_name和$type_name变量值),将imagefield field的图片保存路径设置和你的image模块的一样,通常是:images,另外作者要求Multiple values是选中的。
  • 根据你的数据表表前缀修改$table_pfx变量
  • 将下面代码保存为imagefield_migrate.php在你的drupal安装目录,作者推荐新建一个页面用超链接到imagefield_migrate.php,升级完毕记得删除或移动imagefield_migrate.php
  • 转换完毕请禁用并卸载image模块

<?php
/**
* @file
* Migrate all image.module nodes to imagefields. Any image nodes that participate in image_attach will be
* properly attached the imagefield. The script does migrate video_image.module images.
*
* PREREQUISITES
* --------------
* - You must create a single imagefield field to which all your images will be migrated. You
* should create a new content type for that.
* - The imagefield's Image path should be identical to the image.module's configured path (i.e. 'images' by default)
* - The imagefield should be configured for 'multiple values'.
* - If you use image_attach, you should also add this imagefield field to each content type that
* is image_attach enabled.
*
* USAGE
* ----------
* - Backup your Drupal database. Really.
* - Edit the 'Configuration' section below.
* - Place this script in the root of your Drupal site.
* - Run this script by requesting <a href="http://<yoursite>/imagefield_migrate.php" title="http://<yoursite>/imagefield_migrate.php" rel="nofollow">http://<yoursite>/imagefield_migrate.php</a> in your browser.
* - Remove this script from your site to prevent accidental re-run.
* - Disable and uninstall image and image_attach modules.
*
* KNOWN ISSUES
* -------------
* - Rename files that have a '+' in them in the files table and also rename in filesystem
*
* AUTHORS
* -----------
* spydor (see <a href="http://drupal.org/node/201983#comment-828698" title="http://drupal.org/node/201983#comment-828698" rel="nofollow">http://drupal.org/node/201983#comment-828698</a>)
* Moshe Weitzman (<a href="http://drupal.org/moshe" title="http://drupal.org/moshe" rel="nofollow">http://drupal.org/moshe</a>)
* hobbes_vt
*/


// ***** CONFIGURATION *******

// Table prefix ... in case you use it - otherwise leave blank
$table_pfx = 'drupal_';

// The imagefield field that you have already created and configured as per Prerequisites.
$field_name = 'field_photo';

// The content type that you have already created as per Prerequisites.
$type_name = 'photos';

// ***** END CONFIGURATION *******

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Populate the imagefield table for every image node.
$table = $table_pfx. 'content_type_'. $type_name; // this changed from the original version
$table_content_field = $table_pfx. 'content_'. $field_name;
$fid = $field_name. '_fid';
$title = $field_name. '_title';
$alt = $field_name. '_alt';
$table_node = $table_pfx. 'node'; // add prefix
$table_files = $table_pfx. 'files'; // add prefix

$sql = "INSERT INTO {$table_content_field} (vid, nid, $fid, $title, $alt) SELECT n.vid, n.nid, f.fid, n.title, n.title FROM {$table_node} n INNER JOIN {$table_files} f ON n.nid = f.nid WHERE n.type = 'image' AND f.filename = '_original'";
if (db_query($sql)) {
echo "- $table_content_field populated.<br />\n";
}

// Set the needed filename in the files table.
$image_path = file_create_path(variable_get('image_default_path', 'images'));
$length = strlen($image_path)+2;
$sql = "UPDATE {$table_files} SET filename = SUBSTRING(filepath, $length) WHERE filename = '_original'";
if (db_query($sql)) {
echo "- files table updated<br />\n";
}

// Change the content type from 'image' to the configured type.
$sql = "UPDATE {$table_node} SET type = '%s' WHERE type = 'image'";
db_query($sql, $type_name);

// Loop over the image_attach records
if (module_exists('image_attach')) {
$table_image_attach = $table_pfx. 'image_attach'; // add prefix
$sql = "SELECT n.nid, n.vid, ia.iid FROM {$table_image_attach} ia INNER JOIN {$table_node} n ON ia.nid=n.nid";
$result = db_query($sql);
if ($num = db_num_rows($result)) {
while ($row = db_fetch_object($result)) {
// UPDATE the imagefield to point to the attached node, not the standalone node
$sql = "UPDATE $table_content_field SET nid = $row->nid, vid = $row->vid WHERE nid = $row->iid";
if (db_query($sql)) {
// Successful update. Now unpublish the standalone node that we just made.
$sql = "UPDATE {$table_node} SET status = 0 WHERE nid = $row->iid";
db_query($sql);
}
else {
echo "update $table_content_field failed for $row->iid<br />\n";
}
}
echo "- $num image_attach relationships were migrated.<br />\n";
}
}

if (module_exists('video_image')) {
// Loop over the video.module nodes. Migrate video_image thumbnails to imagefield.
$sql = "SELECT nid, vid FROM {$table_node} WHERE type = 'video'";
$result = db_query($sql);
if ($num = db_num_rows($result)) {
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
if ($iid = $node->iid) {
$sql = "UPDATE {$table_content_field} SET nid = $row->nid, vid = $row->vid WHERE nid = $iid";
if (db_query($sql)) {
// Successful update. Now unpublish the standalone node that we just made.
$sql = "UPDATE {$table_node} SET status = 0 WHERE nid = $iid";
db_query($sql);
}
else {
echo "update $table_content_field failed for $iid<br />\n";
}
};
}
echo "- $num video.module thumbnails were migrated.<br />\n";
}
}

// Clear CCK cache.
$table_cache_content = $table_pfx. 'cache_content'; // add prefix
$sql = "DELETE FROM {$table_cache_content}";
db_query($sql);
?>

转换后的数据库清洁:

注意:请根据你的数据库表前缀修改,我的表前缀是“drupal_”,第四行代码请根据你的 imagefield field名字修改

delete fr from drupal_files f, drupal_file_revisions fr WHERE f.fid = fr.fid AND f.filename = 'thumbnail'

delete fr from drupal_files f, drupal_file_revisions fr WHERE f.fid = fr.fid AND f.filename = 'preview'

delete from drupal_files WHERE filename = 'preview' OR filename = 'thumbnail'

delete fr from drupal_content_field_photo cfi, drupal_file_revisions fr WHERE cfi.field_photo_fid = fr.fid

另外:我启用了image_gallery,这里我们要做一些改动,修改image模块目录下contrib/image_gallery/image_gallery.install
找到:
 if ($vid = variable_get('image_gallery_nav_vocabulary', FALSE)) {
    taxonomy_del_vocabulary($vid);
  }

将这3行代码注释掉,不然当你卸载image_gallery时,他会将你以前的相册分类全部删除掉

然后登录phpmyadmin打开表drupal_vocabulary,修改Image Galleries的module字段为:taxonomy

最后更具自己的需要决定是删除以前image模块生成的图片。

评论

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 允许HTML标签:<a> <img> <em> <del> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <h6>
  • 自动断行和分段。
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

更多关於格式化选项的信息

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.