diff --git a/cck.install b/cck.install index 49a2f9b..75526ed 100644 --- a/cck.install +++ b/cck.install @@ -93,3 +93,61 @@ function cck_update_7000() { db_add_field('cck_field_settings', 'language', $field); } } + +function cck_update_7001(&$sandbox = array()) { + $ret = array(); + + // Query the existing D6 files table for possible duplicate URIs in D7. + if (db_table_exists('files')) { + try { + db_add_index('files', 'path', array('filepath')); + } + catch(DatabaseSchemaObjectExistsException $e) {} + if (!isset($sandbox['progress'])) { + $sandbox['progress'] = 0; + $sandbox['current_file'] = 0; + $sandbox['max'] = db_query("SELECT COUNT(f1.fid) FROM {files} f1 WHERE (SELECT COUNT(f2.filepath) FROM {files} f2 WHERE f2.filepath = f1.filepath && f1.`fid` > f2.`fid` LIMIT 1) > 0")->fetchField(); + } + // Log an error if the destination does not contain a valid stream. + $destination = variable_get('file_default_scheme', 'public') . '://'; + $destination_scheme = file_uri_scheme($destination); + if (!$destination_scheme || !file_stream_wrapper_valid_scheme($destination_scheme)) { + watchdog('cck', 'The file could not be copied, because the destination %destination is invalid.', array('%destination' => $destination), WATCHDOG_ERROR); + throw new Exception('error'); + } + + // Update 50 files at a time. + $result = db_query("SELECT f1.fid, f1.filename, f1.filepath FROM {files} f1 WHERE (SELECT COUNT(f2.filepath) FROM {files} f2 WHERE f2.filepath = f1.filepath && f1.`fid` > f2.`fid` LIMIT 1) > 0 AND f1.fid > :fid ORDER BY f1.fid LIMIT 50", + array(':fid' => $sandbox['current_file'])); + + foreach ($result as $record) { + // Duplicate file via copy. + if (file_exists($record->filepath)) { + $new_path = str_replace("public://", "", file_destination($destination . $record->filepath, FILE_EXISTS_RENAME)); + $saved_filepath = file_unmanaged_copy($record->filepath, $new_path, FILE_EXISTS_RENAME); + } + else { + $saved_filepath = $record->filepath . "_DUMMY_COPY_" . $record->fid; + } + + // Update the database to use the new filename. + if ($saved_filepath) { + $saved_filepath_base = basename($saved_filepath); + $num_updated = db_update('files') + ->fields(array( + 'filename' => $saved_filepath_base, + 'filepath' => $saved_filepath, + )) + ->condition('fid', $record->fid, '=') + ->execute(); + watchdog('cck', "Updated file: fid: $record->fid, $record->filename to $saved_filepath_base"); + } + $sandbox['current_file'] = $record->fid; + $sandbox['progress']++; + } + + $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); + } + + return $ret; +}