Index: imagefield.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagefield/imagefield.install,v
retrieving revision 1.16
diff -u -r1.16 imagefield.install
--- imagefield.install	7 Nov 2008 00:55:40 -0000	1.16
+++ imagefield.install	13 Dec 2008 17:43:45 -0000
@@ -52,10 +52,6 @@
   return $ret;
 }
 
-
-
-
-
 /**
  * Schema change to enable alt and title tags.
  */
@@ -91,13 +87,6 @@
 
 /**
  * Upgrade to CCK 2 and Drupal 6.
- * @todo: update widget type from 'image' to 'imagefield_widget_default'
- * @todo: update formatter name changes.
- *    default => image_plain
- *    imagefield_nodelink => image_nodelink
- *    imagefield_imagelink => image_imagelink
- *    imagefield_path => path_plain
- *    imagefield_url => url_plain
  */
 function imagefield_update_6001() {
   // Make sure CCK's been updated first.
@@ -108,138 +97,159 @@
   module_load_include('inc', 'content', 'includes/content.admin');
   module_load_include('inc', 'content', 'includes/content.crud');
 
-  foreach (content_fields() as $field) {
-    // skip non imagefields.
-    if ($field['type'] != 'image') continue;
-
-    if (isset($field['widget']['max_number_images'])) {
-      if ($field['widget']['max_number_images'] == 0) {
-        $field['multiple'] = 1; // 1 means "Unlimited" in CCK.
-      }
-      elseif ($field['widget']['max_number_images'] == 1) {
-        $field['multiple'] = 0; // 0 means "Not Multiple" in CCK.
-      }
-      else {
-        $field['multiple'] == $field['widget']['max_number_images'];
-      }
-      $field = array_merge($field, $field['widget']);
-    }
+  $ret[] = update_sql("UPDATE {". content_instance_tablename() ."} SET widget_type = 'imagefield_widget' WHERE widget_type = 'image'");
+  content_associate_fields('imagefield');
 
-    // update widget names.
-    if ($field['widget']['type'] == 'image') $field['widget']['type'] = 'imagefield_widget_default';
+  foreach (content_types_install() as $type_name => $fields) {
+    foreach ($fields as $field) {
+      // skip non imagefields.
+      if ($field['type'] != 'image') continue;
+
+      // Map 'max_number_images' parameter to CCK 'multiple'.
+      if (!empty($field['widget']['multiple']) && isset($field['widget']['max_number_images'])) {
+        if ($field['widget']['max_number_images'] == 0) {
+          $field['multiple'] = 1; // 1 means "Unlimited" in CCK.
+        }
+        elseif ($field['widget']['max_number_images'] == 1) {
+          $field['multiple'] = 0; // 0 means "Not Multiple" in CCK.
+        }
+        else {
+          $field['multiple'] == $field['widget']['max_number_images'];
+        }
+      }
+      unset($field['widget']['max_number_images']);
 
-    // rename image_path to file_path to help convergence with filefield.module.
-    $field['widget']['file_path'] = $field['widget']['image_path'];
+      // rename image_path to file_path to help convergence with filefield.module.
+      $field['widget']['file_path'] = $field['widget']['image_path'];
 
-    // update formatter names.
-    $display_settings = array('teaser','full','4');
-    foreach ($display_settings as $context) {
-      switch($field['display_settings'][$context]['formatter']) {
-        case 'default':
-          $field['display_settings'][$context]['formatter'] = 'image_plain';
-          break;
-        case 'imagefield_nodelink':
-          $field['display_settings'][$context]['formatter'] = 'image_nodelink';
-          break;
-        case 'imagefield_imagelink':
-          $field['display_settings'][$context]['formatter'] = 'image_imagelink';
-          break;
-        case 'imagefield_path':
-          $field['display_settings'][$context]['formatter'] = 'path_plain';
-          break;
-        case 'imagefield_url':
-          $field['display_settings'][$context]['formatter'] = 'url_plain';
-          break;
+      // update formatter names.
+      $display_settings = array('teaser', 'full', '4');
+      foreach ($display_settings as $context) {
+        switch($field['display_settings'][$context]['formatter']) {
+          case 'imagefield_nodelink':
+            $field['display_settings'][$context]['formatter'] = 'image_nodelink';
+            break;
+          case 'imagefield_imagelink':
+            $field['display_settings'][$context]['formatter'] = 'image_imagelink';
+            break;
+          case 'imagefield_path':
+            $field['display_settings'][$context]['formatter'] = 'path_plain';
+            break;
+          case 'imagefield_url':
+            $field['display_settings'][$context]['formatter'] = 'url_plain';
+            break;
+          case 'default':
+            $field['display_settings'][$context]['formatter'] = 'image_plain';
+            break;
+        }
       }
+
+      // set list options inherited from file field to behave consistently with old imagefield.
+      $field['list_default'] = 1;
+      $field['force_list_default'] = 1;
+
+      content_field_instance_update($field);
     }
+  }
 
-    // set list options inherited from file field to behave consistently with old imagefield.
-    $field['list_default'] = 1;
-    $field['force_list_default'] = 1;
-
-    content_field_instance_update($field);
-
-
-    // Add data column to imagefield.
-    $new_field = $field;
-    $new_field['columns']['data'] = array('type' => 'text', 'serialize' => true);
-    content_alter_db($field, $new_field);
-
-    // move alt and title values to serializes 'data' column.
-    $batch = array(
-      'title' => t('Migrating filefield values'),
-      'operations' => array(),
-      'file' => drupal_get_path('module', 'imagefield') .'/imagefield.install',
-    );
-    $batch['operations'][] = array('_imagefield_update_6102_move_operation', array($field));
-    $batch['operations'][] = array('_imagefield_update_6102_drop_operation', array($field));
-    batch_set($batch);
-  }
-  // clear cache..
-  cache_clear_all('content_type_info', content_cache_tablename());
-  return array();
-}
+  content_clear_type_cache(TRUE);
 
+  return $ret;
+}
 
 /**
- * Move the list and descriptions column into the serialized data column.
+ * Migrate fields to the new structure.
  */
-function _imagefield_update_6001_move_operation($field, &$context) {
-  // Setup the first through
+function imagefield_update_6002(&$context) {
+  include_once './'. drupal_get_path('module', 'content') .'/content.install';
+  include_once './'. drupal_get_path('module', 'imagefield') .'/imagefield_file.inc';
+
+  $ret = array();
+
   if (!isset($context['sandbox']['progress'])) {
-    $db_info = content_database_info($field);
+    if ($abort = content_check_update('imagefield')) {
+      return $abort;
+    }
+
+    // Get the latest cache values and schema.
+    content_clear_type_cache(TRUE, TRUE);
+
+    // Grab the list of fields to update.
+    $context['sandbox']['fields'] = array();
+    foreach (content_types_install() as $type_name => $fields) {
+      foreach ($fields as $field) {
+        if ($field['type'] == 'image') {
+          // We only process a given field once.
+          $context['sandbox']['fields'][$field['field_name']] = $field;
+        }
+      }
+    }
+
+    if (empty($context['sandbox']['fields'])) {
+      return $ret;
+    }
 
-    $context['sandbox']['db_info'] = $db_info;
-    $context['sandbox']['table'] = $db_info['table'];
-    $context['sandbox']['col_data'] = $db_info['columns']['data']['column'];
-    $context['sandbox']['col_alt'] = $db_info['columns']['alt']['column'];
-    $context['sandbox']['col_title'] = $db_info['columns']['title']['column'];
-    $context['sandbox']['max'] = db_result(db_query("SELECT COUNT(*) FROM {". $db_info['table'] ."}"));
     $context['sandbox']['progress'] = 0;
+    $context['sandbox']['total'] = count($context['sandbox']['fields']);
     $context['sandbox']['current_node'] = 0;
   }
 
-
-  // Work our way through the field values 50 rows at a time.
-  $limit = 50;
-  $result = db_query_range("SELECT * FROM {{$context['sandbox']['table']}} WHERE vid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
+  $field = array_shift($context['sandbox']['fields']);
+  $db_info = content_database_info($field);
+  $table = $db_info['table'];
+  $col_fid = $field['field_name'] .'_fid';
+  $col_alt = $field['field_name'] .'_alt';
+  $col_title = $field['field_name'] .'_title';
+  $col_data = $field['field_name'] .'_data';
+
+  $limit = 10;
+  $result = db_query_range("SELECT * FROM {". $table ."} WHERE vid > %d ORDER BY vid ASC", $context['sandbox']['current_node'], 0, $limit);
+  $has_processed = FALSE;
   while ($row = db_fetch_array($result)) {
+
     // Try to unserialize the data column.
-    if (!empty($row[$context['sandbox']['col_data']])) {
-      $data = unserialize($row[$context['sandbox']['col_data']]);
+    if (!empty($row[$col_data])) {
+      $data = unserialize($row[$col_data]);
     }
     if (empty($data)) {
       $data = array();
     }
 
     // Copy move the values from the columns into the array...
-    $data['alt'] = $row[$context['sandbox']['col_alt']];
-    $data['title'] = $row[$context['sandbox']['col_title']];
+    $data['alt'] = $row[$col_alt];
+    $data['title'] = $row[$col_title];
 
     // ...serialize it and store it back to the db.
-    db_query("UPDATE {{$context['sandbox']['table']}} SET {$context['sandbox']['col_data']} = '%s' WHERE vid = %d", serialize($data), $row['vid']);
+    db_query("UPDATE {". $table ."} SET $col_data = '%s' WHERE vid = %d", serialize($data), $row['vid']);
+
+    // Create the thumbnail for that image.
+    $file = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $row[$col_fid]));
+    if ($file && file_exists($file->filepath) && !file_exists(imagefield_file_admin_thumb_path($file))) {
+      imagefield_file_insert($file);
+    }
 
     // Update our progress information.
-    $context['sandbox']['progress']++;
     $context['sandbox']['current_node'] = $row['vid'];
+    $has_processed = TRUE;
   }
 
-  // Inform the batch engine that we are not finished,
-  // and provide an estimation of the completion level we reached.
-  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
-    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
+  if ($has_processed) {
+    // Not finished, put back the field in the array.
+    array_unshift($context['sandbox']['fields'], $field);
   }
-}
+  else {
+    // Cleanup the old columns.
+    db_query('ALTER TABLE {'. $table .'} DROP COLUMN '. $col_alt);
+    db_query('ALTER TABLE {'. $table .'} DROP COLUMN '. $col_title);
 
-/**
- * Drop the list and description columns.
- */
-function _imagefield_update_6001_drop_operation($field, &$context) {
-  $db_info = content_database_info($field);
-  // TODO: Now that the data has been migrated we can drop the columns.
-  db_query('ALTER TABLE '. $db_info['table'] .' DROP COLUMN '.  $db_info['columns']['alt']['column']);
-  db_query('ALTER TABLE '. $db_info['table'] .' DROP COLUMN '.  $db_info['columns']['title']['column']);
-  $context['finished'] = 1;
-}
+    // Process to next field.
+    $context['sandbox']['progress']++;
+    $context['sandbox']['current_node'] = 0;
+  }
 
+  if (!empty($context['sandbox']['fields'])) {
+    $ret['#finished'] = $context['sandbox']['progress'] / $context['sandbox']['total'];
+  }
 
+  return $ret;
+}
