diff --git a/core/lib/Drupal/Component/Utility/DiffArray.php b/core/lib/Drupal/Component/Utility/DiffArray.php index 983c3f3..b03be19 100644 --- a/core/lib/Drupal/Component/Utility/DiffArray.php +++ b/core/lib/Drupal/Component/Utility/DiffArray.php @@ -54,9 +54,9 @@ public static function diffAssocRecursive(array $array1, array $array2) { * remove duplicates. For example: * @code * array_diff([1, 1, 1], [1]); // [] - * \Drupal\Component\Utility\DiffArray::diffOnce([1, 1, 1], [1]) // [1, 1] + * \Drupal\Component\Utility\DiffArray::diffOnce([1, 1, 1], [1]); // [1, 1] * @endcode - * + * * Keys are maintained from the $array1. * * @param array $array1 diff --git a/core/modules/editor/editor.install b/core/modules/editor/editor.install index 8596d6c..314bea4 100644 --- a/core/modules/editor/editor.install +++ b/core/modules/editor/editor.install @@ -24,22 +24,64 @@ function editor_update_8001() { /** * Recalculates file usages. */ -function editor_update_8002() { - /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */ - $file_usage = \Drupal::service('file.usage'); - $file_usage->clear('editor'); - - foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) { - if ($entity_type->isSubclassOf('\Drupal\Core\Entity\FieldableEntityInterface')) { - $entity_type_id = $entity_type->id(); - // Load 10 entities at once to avoid memory usage issues. - $result = \Drupal::entityQuery($entity_type_id)->execute(); - while ($ids = array_splice($result, 0, 10)) { - $entities = \Drupal::entityTypeManager()->getStorage($entity_type_id)->loadMultiple($ids); - foreach ($entities as $entity) { - editor_entity_insert($entity); +function editor_update_8002(&$sandbox) { + $entity_load_limit = 50; + + if (!\Drupal::moduleHandler()->moduleExists('file')) { + return; + } + + if (!isset($sandbox['current'])) { + $file_ids = \Drupal::entityQuery('file')->execute(); + if (empty($file_ids)) { + return; + } + $sandbox['data']['file_ids'] = $file_ids; + + foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) { + if ($entity_type->isSubclassOf('\Drupal\Core\Entity\FieldableEntityInterface')) { + $entity_type_id = $entity_type->id(); + $entity_ids = \Drupal::entityQuery($entity_type_id)->execute(); + if (!empty($entity_ids)) { + $sandbox['data']['entity_ids'][$entity_type_id] = $entity_ids; } } } + + $sandbox['current'] = 0; + $sandbox['max'] = count($sandbox['data']['file_ids']) + array_sum(array_map('count', $sandbox['data']['entity_ids'])); + } + + if (!empty($sandbox['data']['file_ids'])) { + + // Step 1: delete existing file usages. + /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */ + $file_usage = \Drupal::service('file.usage'); + $files = \Drupal::entityTypeManager() + ->getStorage('file') + ->loadMultiple(array_splice($sandbox['data']['file_ids'], 0, $entity_load_limit)); + foreach ($files as $file) { + $usages = $file_usage->listUsage($file); + if (!empty($usages['editor'])) { + $file_usage->delete($file, 'editor', NULL, NULL, 0); + } + } } + else { + + // Step 2: recalculate file usages. + reset($sandbox['data']['entity_ids']); + $entity_type_id = key($sandbox['data']['entity_ids']); + $entities = \Drupal::entityTypeManager() + ->getStorage($entity_type_id) + ->loadMultiple(array_splice($sandbox['data']['entity_ids'][$entity_type_id], 0, $entity_load_limit)); + foreach ($entities as $entity) { + editor_entity_insert($entity); + } + $sandbox['data']['entity_ids'] = array_filter($sandbox['data']['entity_ids']); + } + + $current_amount = count($sandbox['data']['file_ids']) + array_sum(array_map('count', $sandbox['data']['entity_ids'])); + $sandbox['current'] = $sandbox['max'] - $current_amount; + $sandbox['#finished'] = empty($current_amount) ? 1 : ($sandbox['current'] / $sandbox['max']); } diff --git a/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php b/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php index adfe6a5..12647b1 100644 --- a/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php +++ b/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php @@ -108,13 +108,4 @@ public function listUsage(FileInterface $file) { return $references; } - /** - * {@inheritdoc} - */ - public function clear($module) { - $this->connection->delete($this->tableName) - ->condition('module', $module) - ->execute(); - } - } diff --git a/core/modules/file/src/FileUsage/FileUsageBase.php b/core/modules/file/src/FileUsage/FileUsageBase.php index 8a8931f..c90359b 100644 --- a/core/modules/file/src/FileUsage/FileUsageBase.php +++ b/core/modules/file/src/FileUsage/FileUsageBase.php @@ -33,11 +33,4 @@ public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $ } } - /** - * {@inheritdoc} - */ - public function clear($module) { - // Base implementation exists only for backward compatibility purposes. - } - } diff --git a/core/modules/file/src/FileUsage/FileUsageInterface.php b/core/modules/file/src/FileUsage/FileUsageInterface.php index 6a2a76e..6acda42 100644 --- a/core/modules/file/src/FileUsage/FileUsageInterface.php +++ b/core/modules/file/src/FileUsage/FileUsageInterface.php @@ -67,15 +67,4 @@ public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $ */ public function listUsage(FileInterface $file); - /** - * Clear usages by module. - * - * @param string $module - * The name of the module. - * - * @internal This method should be used only in special cases. For example, - * if a module needs to completely recalculate its file usages. - */ - public function clear($module); - }