### Eclipse Workspace Patch 1.0 #P CCK 6.x Index: content.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/cck/Attic/content.install,v retrieving revision 1.85.2.30 diff -u -r1.85.2.30 content.install --- content.install 7 Nov 2008 16:24:58 -0000 1.85.2.30 +++ content.install 20 Mar 2009 00:23:00 -0000 @@ -518,4 +518,79 @@ } variable_set('content_schema_version', 6009); return $ret; -} \ No newline at end of file +} + +/** + * Fix multiple serialization caused by per-field to per-type migration. + * See http://drupal.org/node/407446. + */ +function content_update_6010(&$sandbox) { + $ret = array(); + + // Gather list of tables and columns that need to be updated. + if (!isset($sandbox['tables'])) { + $sandbox['tables'] = array(); + $fields = content_fields(); + foreach ($fields as $name => $field) { + $db_info = content_database_info($field); + foreach ($db_info['columns'] as $column => $attributes) { + if (isset($attributes['serialize']) && $attributes['serialize']) { + $sandbox['tables'][$db_info['table']]['table'] = $db_info['table']; + $sandbox['tables'][$db_info['table']]['columns'][] = $attributes['column']; + } + } + } + + $sandbox['count'] = count($sandbox['tables']); + $sandbox['current_node'] = 0; + } + + // Number of rows to fix at once: + $limit = 500; + + // Start correcting data. + if ($table_info = array_shift($sandbox['tables'])) { + $table = $table_info['table']; + $columns = $table_info['columns']; + //$data_column = $field['db_info']['columns']['data']['column']; + + $result = db_query_range("SELECT * FROM {" . $table . "} WHERE vid > %d ORDER BY vid ASC", $sandbox['current_node'], 0, $limit); + + // Add the row back into the list of fields to be processed if rows remain. + if (db_affected_rows() == $limit) { + array_unshift($table_info, $sandbox['tables']); + } + + // Loop through the rows and start fixing. + while ($row = db_fetch_array($result)) { + $update_query = $update_args = array(); + foreach ($columns as $column) { + $data = $row[$column]; + + // No need to do anything if the data is NULL. + if (!empty($data)) { + // Unserialize until we get something that is not a string + while (is_string($data)) { + $data = unserialize($data); + } + // Re-serialize once. + $data = serialize($data); + + $update_query[] = "$column = '%s'"; + $update_args[] = $data; + } + } + + if ($update_query) { + $update_args[] = $row['vid']; + db_query("UPDATE {" . $table . "} SET ". implode(', ', $update_query) ." WHERE vid = %d", $update_args); + } + } + } + + if ($sandbox['count']) { + $ret['#finished'] = 1 - count($sandbox['tables']) / $sandbox['count']; + } + + return $ret; +}