Commit 77587a1a authored by webchick's avatar webchick

Issue #2104229 by claudiu.cristea: Deprecate file_usage().

parent 22cb5c3f
......@@ -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);
......
......@@ -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);
}
}
......
......@@ -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.');
}
}
......@@ -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);
......
......@@ -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
......
......@@ -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();
......
......@@ -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.');
......
......@@ -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.');
......
......@@ -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())
......
......@@ -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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment