From 77587a1ab8a8581f9590f9b434e2a24712c4dd15 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Thu, 28 Nov 2013 00:52:13 -0800
Subject: [PATCH] Issue #2104229 by claudiu.cristea: Deprecate file_usage().

---
 core/includes/file.inc                        |  4 ++--
 core/modules/editor/editor.module             |  4 ++--
 .../editor/Tests/EditorFileUsageTest.php      | 15 +++++++------
 core/modules/file/file.module                 | 15 ++++++++-----
 .../file/lib/Drupal/file/Entity/File.php      |  4 ++--
 .../Drupal/file/FileUsage/FileUsageBase.php   |  4 ++--
 .../file/lib/Drupal/file/Tests/DeleteTest.php | 13 ++++++-----
 .../lib/Drupal/file/Tests/FileListingTest.php |  9 ++++----
 .../file/lib/Drupal/file/Tests/UsageTest.php  | 22 ++++++++++---------
 core/modules/image/image.module               | 12 +++++-----
 10 files changed, 55 insertions(+), 47 deletions(-)

diff --git a/core/includes/file.inc b/core/includes/file.inc
index 037d195ad770..545fca3de8d1 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.inc
@@ -953,7 +953,7 @@ function file_create_filename($basename, $directory) {
  *   The file id.
  *
  * @see file_unmanaged_delete()
- * @see file_usage()->listUsage()
+ * @see \Drupal\file\FileUsage\FileUsageBase::delete()
  */
 function file_delete($fid) {
   return file_delete_multiple(array($fid));
@@ -970,7 +970,7 @@ function file_delete($fid) {
  *   The file id.
  *
  * @see file_unmanaged_delete()
- * @see file_usage()->listUsage()
+ * @see \Drupal\file\FileUsage\FileUsageBase::delete()
  */
 function file_delete_multiple(array $fids) {
   entity_delete_multiple('file', $fids);
diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module
index 07c174f6d5e7..5684ad0cddfc 100644
--- a/core/modules/editor/editor.module
+++ b/core/modules/editor/editor.module
@@ -495,7 +495,7 @@ function _editor_record_file_usage(array $uuids, EntityInterface $entity) {
       $file->status = FILE_STATUS_PERMANENT;
       $file->save();
     }
-    file_usage()->add($file, 'editor', $entity->entityType(), $entity->id());
+    \Drupal::service('file.usage')->add($file, 'editor', $entity->entityType(), $entity->id());
   }
 }
 
@@ -515,7 +515,7 @@ function _editor_record_file_usage(array $uuids, EntityInterface $entity) {
 function _editor_delete_file_usage(array $uuids, EntityInterface $entity, $count) {
   foreach ($uuids as $uuid) {
     $file = entity_load_by_uuid('file', $uuid);
-    file_usage()->delete($file, 'editor', $entity->entityType(), $entity->id(), $count);
+    \Drupal::service('file.usage')->delete($file, 'editor', $entity->entityType(), $entity->id(), $count);
   }
 }
 
diff --git a/core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php b/core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php
index 33313167e66b..357d1280cd5c 100644
--- a/core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php
+++ b/core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php
@@ -63,7 +63,8 @@ public function testEditorEntityHooks() {
     $image->setFileUri('core/misc/druplicon.png');
     $image->setFilename(drupal_basename($image->getFileUri()));
     $image->save();
-    $this->assertIdentical(array(), file_usage()->listUsage($image), 'The image has zero usages.');
+    $file_usage = $this->container->get('file.usage');
+    $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages.');
 
     // Test editor_entity_insert(): increment.
     $this->createUser();
@@ -77,7 +78,7 @@ public function testEditorEntityHooks() {
       'uid' => 1,
     ));
     $node->save();
-    $this->assertIdentical(array('editor' => array('node' => array(1 => '1'))), file_usage()->listUsage($image), 'The image has 1 usage.');
+    $this->assertIdentical(array('editor' => array('node' => array(1 => '1'))), $file_usage->listUsage($image), 'The image has 1 usage.');
 
     // Test editor_entity_update(): increment, twice, by creating new revisions.
     $node->setNewRevision(TRUE);
@@ -85,7 +86,7 @@ public function testEditorEntityHooks() {
     $second_revision_id = $node->getRevisionId();
     $node->setNewRevision(TRUE);
     $node->save();
-    $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), file_usage()->listUsage($image), 'The image has 3 usages.');
+    $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image), 'The image has 3 usages.');
 
     // Test hook_entity_update(): decrement, by modifying the last revision:
     // remove the data- attribute from the body field.
@@ -94,21 +95,21 @@ public function testEditorEntityHooks() {
     $new_value = str_replace('data-editor-file-uuid', 'data-editor-file-uuid-modified', $original_value);
     $body->setValue($new_value);
     $node->save();
-    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), file_usage()->listUsage($image), 'The image has 2 usages.');
+    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.');
 
     // Test hook_entity_update(): increment, by modifying the last revision:
     // readd the data- attribute to the body field.
     $node->get('body')->offsetGet(0)->get('value')->setValue($original_value);
     $node->save();
-    $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), file_usage()->listUsage($image), 'The image has 3 usages.');
+    $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image), 'The image has 3 usages.');
 
     // Test editor_entity_revision_delete(): decrement, by deleting a revision.
     entity_revision_delete('node', $second_revision_id);
-    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), file_usage()->listUsage($image), 'The image has 2 usages.');
+    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.');
 
     // Test editor_entity_delete().
     $node->delete();
-    $this->assertIdentical(array(), file_usage()->listUsage($image), 'The image has zero usages again.');
+    $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages again.');
   }
 
 }
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 8ac15147cfc0..85d9e94960b0 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -121,6 +121,9 @@ function file_load($fid, $reset = FALSE) {
 /**
  * Returns the file usage service.
  *
+ * @deprecated as of Drupal 8.0. Use \Drupal::service('file.usage') instead, or
+ *   even better have the file usage service injected into your object.
+ *
  * @return \Drupal\file\FileUsage\FileUsageInterface.
  */
 function file_usage() {
@@ -275,7 +278,7 @@ function file_move(File $source, $destination = NULL, $replace = FILE_EXISTS_REN
     \Drupal::moduleHandler()->invokeAll('file_move', array($file, $source));
 
     // Delete the original if it's not in use elsewhere.
-    if ($delete_source && !file_usage()->listUsage($source)) {
+    if ($delete_source && !\Drupal::service('file.usage')->listUsage($source)) {
       $source->delete();
     }
 
@@ -703,7 +706,7 @@ function file_cron() {
   $result = \Drupal::entityManager()->getStorageController('file')->retrieveTemporaryFiles();
   foreach ($result as $row) {
     if ($file = file_load($row->fid)) {
-      $references = file_usage()->listUsage($file);
+      $references = \Drupal::service('file.usage')->listUsage($file);
       if (empty($references)) {
         if (file_exists($file->getFileUri())) {
           $file->delete();
@@ -723,8 +726,8 @@ function file_cron() {
  * Saves file uploads to a new location.
  *
  * The files will be added to the {file_managed} table as temporary files.
- * Temporary files are periodically cleaned. Use file_usage()->add() to register
- * the usage of the file which will automatically mark it as permanent.
+ * Temporary files are periodically cleaned. Use the 'file.usage' service to
+ * register the usage of the file which will automatically mark it as permanent.
  *
  * @param $form_field_name
  *   A string that is the associative array key of the upload form element in
@@ -1362,7 +1365,7 @@ function file_managed_file_validate(&$element, &$form_state) {
     foreach ($fids as $fid) {
       if ($file = file_load($fid)) {
         if ($file->isPermanent()) {
-          $references = file_usage()->listUsage($file);
+          $references = \Drupal::service('file.usage')->listUsage($file);
           if (empty($references)) {
             form_error($element, $form_state, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title'])));
           }
@@ -1856,7 +1859,7 @@ function file_get_file_references(File $file, $field = NULL, $age = EntityStorag
   // Fill the static cache, disregard $field and $field_type for now.
   if (!isset($references[$file->id()][$age])) {
     $references[$file->id()][$age] = array();
-    $usage_list = file_usage()->listUsage($file);
+    $usage_list = \Drupal::service('file.usage')->listUsage($file);
     $file_usage_list = isset($usage_list['file']) ? $usage_list['file'] : array();
     foreach ($file_usage_list as $entity_type => $entity_ids) {
       $entity_info = entity_get_info($entity_type);
diff --git a/core/modules/file/lib/Drupal/file/Entity/File.php b/core/modules/file/lib/Drupal/file/Entity/File.php
index 3239cdffb5c1..57b8105397f5 100644
--- a/core/modules/file/lib/Drupal/file/Entity/File.php
+++ b/core/modules/file/lib/Drupal/file/Entity/File.php
@@ -198,10 +198,10 @@ public static function preDelete(EntityStorageControllerInterface $storage_contr
 
     foreach ($entities as $entity) {
       // Delete all remaining references to this file.
-      $file_usage = file_usage()->listUsage($entity);
+      $file_usage = \Drupal::service('file.usage')->listUsage($entity);
       if (!empty($file_usage)) {
         foreach ($file_usage as $module => $usage) {
-          file_usage()->delete($entity, $module);
+          \Drupal::service('file.usage')->delete($entity, $module);
         }
       }
       // Delete the actual file. Failures due to invalid files and files that
diff --git a/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php b/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php
index 2111492cbaf6..aef295cd6049 100644
--- a/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php
+++ b/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php
@@ -18,7 +18,7 @@ abstract class FileUsageBase implements FileUsageInterface {
    * Implements Drupal\file\FileUsage\FileUsageInterface::add().
    */
   public function add(File $file, $module, $type, $id, $count = 1) {
-    // Make sure that a used file is permament.
+    // Make sure that a used file is permanent.
     if (!$file->isPermanent()) {
       $file->setPermanent();
       $file->save();
@@ -31,7 +31,7 @@ public function add(File $file, $module, $type, $id, $count = 1) {
   public function delete(File $file, $module, $type = NULL, $id = NULL, $count = 1) {
     // If there are no more remaining usages of this file, mark it as temporary,
     // which result in a delete through system_cron().
-    $usage = file_usage()->listUsage($file);
+    $usage = \Drupal::service('file.usage')->listUsage($file);
     if (empty($usage)) {
       $file->setTemporary();
       $file->save();
diff --git a/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php b/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php
index 9bd52bce2d7b..c6486fee910b 100644
--- a/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php
+++ b/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php
@@ -38,11 +38,12 @@ function testUnused() {
    */
   function testInUse() {
     $file = $this->createFile();
-    file_usage()->add($file, 'testing', 'test', 1);
-    file_usage()->add($file, 'testing', 'test', 1);
+    $file_usage = $this->container->get('file.usage');
+    $file_usage->add($file, 'testing', 'test', 1);
+    $file_usage->add($file, 'testing', 'test', 1);
 
-    file_usage()->delete($file, 'testing', 'test', 1);
-    $usage = file_usage()->listUsage($file);
+    $file_usage->delete($file, 'testing', 'test', 1);
+    $usage = $file_usage->listUsage($file);
     $this->assertEqual($usage['testing']['test'], array(1 => 1), 'Test file is still in use.');
     $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
     $this->assertTrue(file_load($file->id()), 'File still exists in the database.');
@@ -50,8 +51,8 @@ function testInUse() {
     // Clear out the call to hook_file_load().
     file_test_reset();
 
-    file_usage()->delete($file, 'testing', 'test', 1);
-    $usage = file_usage()->listUsage($file);
+    $file_usage->delete($file, 'testing', 'test', 1);
+    $usage = $file_usage->listUsage($file);
     $this->assertFileHooksCalled(array('load', 'update'));
     $this->assertTrue(empty($usage), 'File usage data was removed.');
     $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
diff --git a/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php b/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php
index 598a52fa7e75..5582ce358ad9 100644
--- a/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php
+++ b/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php
@@ -60,7 +60,8 @@ protected function sumUsages($usage) {
    * Tests file overview with different user permissions.
    */
   function testFileListingPages() {
-    // Users without sufficent permissions should not see file listing.
+    $file_usage = $this->container->get('file.usage');
+    // Users without sufficient permissions should not see file listing.
     $this->drupalLogin($this->base_user);
     $this->drupalGet('admin/content/files');
     $this->assertResponse(403);
@@ -114,11 +115,11 @@ function testFileListingPages() {
 
     $this->drupalGet('admin/content/files');
     $file = entity_load('file', $orphaned_file);
-    $usage = $this->sumUsages(file_usage()->listUsage($file));
+    $usage = $this->sumUsages($file_usage->listUsage($file));
     $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);
 
     $file = entity_load('file', $used_file);
-    $usage = $this->sumUsages(file_usage()->listUsage($file));
+    $usage = $this->sumUsages($file_usage->listUsage($file));
     $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);
 
     $result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", array(':value' => t('Temporary')));
@@ -127,7 +128,7 @@ function testFileListingPages() {
     // Test file usage page.
     foreach ($nodes as $node) {
       $file = entity_load('file', $node->file->target_id);
-      $usage = file_usage()->listUsage($file);
+      $usage = $file_usage->listUsage($file);
       $this->drupalGet('admin/content/files/usage/' . $file->id());
       $this->assertResponse(200);
       $this->assertText($node->getTitle(), 'Node title found on usage page.');
diff --git a/core/modules/file/lib/Drupal/file/Tests/UsageTest.php b/core/modules/file/lib/Drupal/file/Tests/UsageTest.php
index 17e51d867a53..c4b053c65dad 100644
--- a/core/modules/file/lib/Drupal/file/Tests/UsageTest.php
+++ b/core/modules/file/lib/Drupal/file/Tests/UsageTest.php
@@ -20,7 +20,7 @@ public static function getInfo() {
   }
 
   /**
-   * Tests file_usage()->listUsage().
+   * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::listUsage().
    */
   function testGetUsage() {
     $file = $this->createFile();
@@ -43,7 +43,7 @@ function testGetUsage() {
       ))
       ->execute();
 
-    $usage = file_usage()->listUsage($file);
+    $usage = $this->container->get('file.usage')->listUsage($file);
 
     $this->assertEqual(count($usage['testing']), 2, 'Returned the correct number of items.');
     $this->assertTrue(isset($usage['testing']['foo'][1]), 'Returned the correct id.');
@@ -53,15 +53,16 @@ function testGetUsage() {
   }
 
   /**
-   * Tests file_usage()->add().
+   * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::add().
    */
   function testAddUsage() {
     $file = $this->createFile();
-    file_usage()->add($file, 'testing', 'foo', 1);
+    $file_usage = $this->container->get('file.usage');
+    $file_usage->add($file, 'testing', 'foo', 1);
     // Add the file twice to ensure that the count is incremented rather than
     // creating additional records.
-    file_usage()->add($file, 'testing', 'bar', 2);
-    file_usage()->add($file, 'testing', 'bar', 2);
+    $file_usage->add($file, 'testing', 'bar', 2);
+    $file_usage->add($file, 'testing', 'bar', 2);
 
     $usage = db_select('file_usage', 'f')
       ->fields('f')
@@ -78,10 +79,11 @@ function testAddUsage() {
   }
 
   /**
-   * Tests file_usage()->delete().
+   * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::delete().
    */
   function testRemoveUsage() {
     $file = $this->createFile();
+    $file_usage = $this->container->get('file.usage');
     db_insert('file_usage')
       ->fields(array(
         'fid' => $file->id(),
@@ -93,7 +95,7 @@ function testRemoveUsage() {
       ->execute();
 
     // Normal decrement.
-    file_usage()->delete($file, 'testing', 'bar', 2);
+    $file_usage->delete($file, 'testing', 'bar', 2);
     $count = db_select('file_usage', 'f')
       ->fields('f', array('count'))
       ->condition('f.fid', $file->id())
@@ -102,7 +104,7 @@ function testRemoveUsage() {
     $this->assertEqual(2, $count, 'The count was decremented correctly.');
 
     // Multiple decrement and removal.
-    file_usage()->delete($file, 'testing', 'bar', 2, 2);
+    $file_usage->delete($file, 'testing', 'bar', 2, 2);
     $count = db_select('file_usage', 'f')
       ->fields('f', array('count'))
       ->condition('f.fid', $file->id())
@@ -111,7 +113,7 @@ function testRemoveUsage() {
     $this->assertIdentical(FALSE, $count, 'The count was removed entirely when empty.');
 
     // Non-existent decrement.
-    file_usage()->delete($file, 'testing', 'bar', 2);
+    $file_usage->delete($file, 'testing', 'bar', 2);
     $count = db_select('file_usage', 'f')
       ->fields('f', array('count'))
       ->condition('f.fid', $file->id())
diff --git a/core/modules/image/image.module b/core/modules/image/image.module
index ec12e15cae76..23443ff4d403 100644
--- a/core/modules/image/image.module
+++ b/core/modules/image/image.module
@@ -475,12 +475,12 @@ function image_field_entity_update(FieldInterface $field) {
     if ($file_new) {
       $file_new->status = FILE_STATUS_PERMANENT;
       $file_new->save();
-      file_usage()->add($file_new, 'image', 'default_image', $field->uuid);
+      \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field->uuid);
     }
 
     // Is there an old file?
     if ($fid_old && ($file_old = file_load(reset($fid_old)))) {
-      file_usage()->delete($file_old, 'image', 'default_image', $field->uuid);
+      \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid);
     }
   }
 
@@ -522,11 +522,11 @@ function image_field_instance_update(FieldInstanceInterface $field_instance) {
     if ($file_new) {
       $file_new->status = FILE_STATUS_PERMANENT;
       $file_new->save();
-      file_usage()->add($file_new, 'image', 'default_image', $field_instance->uuid);
+      \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field_instance->uuid);
     }
     // Delete the old file, if present.
     if ($fid_old && ($file_old = file_load($fid_old[0]))) {
-      file_usage()->delete($file_old, 'image', 'default_image', $field_instance->uuid);
+      \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_instance->uuid);
     }
   }
 
@@ -550,7 +550,7 @@ function image_field_entity_delete(FieldInterface $field) {
   // The value of a managed_file element can be an array if #extended == TRUE.
   $fid = (isset($field->settings['default_image']['fid']['fids']) ? $field->settings['default_image']['fid']['fids'] : $field->settings['default_image']['fid']);
   if ($fid && ($file = file_load($fid[0]))) {
-    file_usage()->delete($file, 'image', 'default_image', $field->uuid);
+    \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid);
   }
 }
 
@@ -573,6 +573,6 @@ function image_field_instance_delete(FieldInstanceInterface $field_instance) {
 
   // Remove the default image when the instance is deleted.
   if ($fid && ($file = file_load($fid))) {
-    file_usage()->delete($file, 'image', 'default_image', $field_instance->uuid);
+    \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field_instance->uuid);
   }
 }
-- 
GitLab