Move into nested docroot
This commit is contained in:
parent
83a0d3a149
commit
c8b70abde9
13405 changed files with 0 additions and 0 deletions
111
web/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php
Normal file
111
web/core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php
Normal file
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\file\FileUsage;
|
||||
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Drupal\file\FileInterface;
|
||||
|
||||
/**
|
||||
* Defines the database file usage backend. This is the default Drupal backend.
|
||||
*/
|
||||
class DatabaseFileUsageBackend extends FileUsageBase {
|
||||
|
||||
/**
|
||||
* The database connection used to store file usage information.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The name of the SQL table used to store file usage information.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tableName;
|
||||
|
||||
/**
|
||||
* Construct the DatabaseFileUsageBackend.
|
||||
*
|
||||
* @param \Drupal\Core\Database\Connection $connection
|
||||
* The database connection which will be used to store the file usage
|
||||
* information.
|
||||
* @param string $table
|
||||
* (optional) The table to store file usage info. Defaults to 'file_usage'.
|
||||
*/
|
||||
public function __construct(Connection $connection, $table = 'file_usage') {
|
||||
$this->connection = $connection;
|
||||
|
||||
$this->tableName = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add(FileInterface $file, $module, $type, $id, $count = 1) {
|
||||
$this->connection->merge($this->tableName)
|
||||
->keys(array(
|
||||
'fid' => $file->id(),
|
||||
'module' => $module,
|
||||
'type' => $type,
|
||||
'id' => $id,
|
||||
))
|
||||
->fields(array('count' => $count))
|
||||
->expression('count', 'count + :count', array(':count' => $count))
|
||||
->execute();
|
||||
|
||||
parent::add($file, $module, $type, $id, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $count = 1) {
|
||||
// Delete rows that have a exact or less value to prevent empty rows.
|
||||
$query = $this->connection->delete($this->tableName)
|
||||
->condition('module', $module)
|
||||
->condition('fid', $file->id());
|
||||
if ($type && $id) {
|
||||
$query
|
||||
->condition('type', $type)
|
||||
->condition('id', $id);
|
||||
}
|
||||
if ($count) {
|
||||
$query->condition('count', $count, '<=');
|
||||
}
|
||||
$result = $query->execute();
|
||||
|
||||
// If the row has more than the specified count decrement it by that number.
|
||||
if (!$result && $count > 0) {
|
||||
$query = $this->connection->update($this->tableName)
|
||||
->condition('module', $module)
|
||||
->condition('fid', $file->id());
|
||||
if ($type && $id) {
|
||||
$query
|
||||
->condition('type', $type)
|
||||
->condition('id', $id);
|
||||
}
|
||||
$query->expression('count', 'count - :count', array(':count' => $count));
|
||||
$query->execute();
|
||||
}
|
||||
|
||||
parent::delete($file, $module, $type, $id, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function listUsage(FileInterface $file) {
|
||||
$result = $this->connection->select($this->tableName, 'f')
|
||||
->fields('f', array('module', 'type', 'id', 'count'))
|
||||
->condition('fid', $file->id())
|
||||
->condition('count', 0, '>')
|
||||
->execute();
|
||||
$references = array();
|
||||
foreach ($result as $usage) {
|
||||
$references[$usage->module][$usage->type][$usage->id] = $usage->count;
|
||||
}
|
||||
return $references;
|
||||
}
|
||||
|
||||
}
|
36
web/core/modules/file/src/FileUsage/FileUsageBase.php
Normal file
36
web/core/modules/file/src/FileUsage/FileUsageBase.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\file\FileUsage;
|
||||
|
||||
use Drupal\file\FileInterface;
|
||||
|
||||
/**
|
||||
* Defines the base class for database file usage backend.
|
||||
*/
|
||||
abstract class FileUsageBase implements FileUsageInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add(FileInterface $file, $module, $type, $id, $count = 1) {
|
||||
// Make sure that a used file is permanent.
|
||||
if (!$file->isPermanent()) {
|
||||
$file->setPermanent();
|
||||
$file->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete(FileInterface $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 = \Drupal::service('file.usage')->listUsage($file);
|
||||
if (empty($usage)) {
|
||||
$file->setTemporary();
|
||||
$file->save();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
70
web/core/modules/file/src/FileUsage/FileUsageInterface.php
Normal file
70
web/core/modules/file/src/FileUsage/FileUsageInterface.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\file\FileUsage;
|
||||
|
||||
use Drupal\file\FileInterface;
|
||||
|
||||
/**
|
||||
* File usage backend interface.
|
||||
*/
|
||||
interface FileUsageInterface {
|
||||
|
||||
/**
|
||||
* Records that a module is using a file.
|
||||
*
|
||||
* Examples:
|
||||
* - A module that associates files with nodes, so $type would be
|
||||
* 'node' and $id would be the node's nid. Files for all revisions are
|
||||
* stored within a single nid.
|
||||
* - The User module associates an image with a user, so $type would be 'user'
|
||||
* and the $id would be the user's uid.
|
||||
*
|
||||
* @param \Drupal\file\FileInterface $file
|
||||
* A file entity.
|
||||
* @param string $module
|
||||
* The name of the module using the file.
|
||||
* @param string $type
|
||||
* The type of the object that contains the referenced file.
|
||||
* @param int $id
|
||||
* The unique, numeric ID of the object containing the referenced file.
|
||||
* @param int $count
|
||||
* (optional) The number of references to add to the object. Defaults to 1.
|
||||
*/
|
||||
public function add(FileInterface $file, $module, $type, $id, $count = 1);
|
||||
|
||||
/**
|
||||
* Removes a record to indicate that a module is no longer using a file.
|
||||
*
|
||||
* @param \Drupal\file\FileInterface $file
|
||||
* A file entity.
|
||||
* @param string $module
|
||||
* The name of the module using the file.
|
||||
* @param string $type
|
||||
* (optional) The type of the object that contains the referenced file. May
|
||||
* be omitted if all module references to a file are being deleted. Defaults
|
||||
* to NULL.
|
||||
* @param int $id
|
||||
* (optional) The unique, numeric ID of the object containing the referenced
|
||||
* file. May be omitted if all module references to a file are being
|
||||
* deleted. Defaults to NULL.
|
||||
* @param int $count
|
||||
* (optional) The number of references to delete from the object. Defaults
|
||||
* to 1. Zero may be specified to delete all references to the file within a
|
||||
* specific object.
|
||||
*/
|
||||
public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $count = 1);
|
||||
|
||||
/**
|
||||
* Determines where a file is used.
|
||||
*
|
||||
* @param \Drupal\file\FileInterface $file
|
||||
* A file entity.
|
||||
*
|
||||
* @return array
|
||||
* A nested array with usage data. The first level is keyed by module name,
|
||||
* the second by object type and the third by the object id. The value of
|
||||
* the third level contains the usage count.
|
||||
*/
|
||||
public function listUsage(FileInterface $file);
|
||||
|
||||
}
|
Reference in a new issue