diff --git a/modules/system/system.install b/modules/system/system.install index 8b92fee..863cfb7 100644 --- a/modules/system/system.install +++ b/modules/system/system.install @@ -2810,8 +2810,9 @@ function system_update_7061(&$sandbox) { + db_query('SELECT COUNT(*) FROM {files}')->fetchField(); // There is a chance that we have multiple file entries pointing to the same - // physical file, see https://www.drupal.org/node/1260938 - // Therefore create batch requests to update those files. + // physical file, see https://www.drupal.org/node/1260938. + // Create batch requests to update those files, by deduplicating them + // and point to the first of the possible file IDs. $sandbox['last_fid_processed'] = -1; // Therefore create a mapping table between old and new FIDs. @@ -2832,36 +2833,34 @@ function system_update_7061(&$sandbox) { $fid_filepath = db_query_range('SELECT fid, filepath FROM {files} WHERE fid > :lastfid ORDER BY FID ASC', 0, $limit, array(':lastfid' => $sandbox['last_fid_processed'])) ->fetchAll(); if (!empty($fid_filepath)) { - foreach ($fid_filepath as $object) { - $fid = $object->fid; - $filepath = $object->filepath; + foreach ($fid_filepath as $row) { // Check whether for the filepath of this particular file we have // duplicate entries. - $fids = db_query('SELECT fid FROM {files} WHERE filepath = :filepath ORDER BY fid ASC', array(':filepath' => $filepath)) + $duplicate_fids = db_query('SELECT fid FROM {files} WHERE filepath = :filepath ORDER BY fid ASC', array(':filepath' => $row->filepath)) ->fetchCol(); // Detect whether we have duplicates. - if (count($fids) > 1) { + if (count($duplicate_fids) > 1) { // In case we have duplicate FIDs for a given filepath, keep just the first // one. - $kept_fid = array_shift($fids); + $kept_fid = array_shift($duplicate_fids); db_delete('files') - ->condition('fid', $fids, 'IN') + ->condition('fid', $duplicate_fids, 'IN') ->execute(); db_update('upload') ->fields(array('fid' => $kept_fid)) - ->condition('fid', $fids, 'IN') + ->condition('fid', $duplicate_fids, 'IN') ->execute(); $insert = db_insert('system_update_7061_fid_mapping') ->fields(array('old_fid', 'new_fid')); - foreach ($fids as $fid) { + foreach ($duplicate_fids as $fid) { $insert->values(array('old_fid' => $fid, 'new_fid' => $kept_fid)); } $insert->execute(); } - $sandbox['last_fid_processed'] = $fid; + $sandbox['last_fid_processed'] = $row->fid; $sandbox['progress']++; }