You are here

public function ServiceAuditFilesUsedNotReferenced::auditfilesUsedNotReferencedGetFileList in Audit Files 8.2

Same name and namespace in other branches
  1. 8.3 src/ServiceAuditFilesUsedNotReferenced.php \Drupal\auditfiles\ServiceAuditFilesUsedNotReferenced::auditfilesUsedNotReferencedGetFileList()
  2. 4.x src/ServiceAuditFilesUsedNotReferenced.php \Drupal\auditfiles\ServiceAuditFilesUsedNotReferenced::auditfilesUsedNotReferencedGetFileList()

Retrieves the file IDs to operate on.

Return value

array The file IDs.

File

src/ServiceAuditFilesUsedNotReferenced.php, line 73

Class

ServiceAuditFilesUsedNotReferenced
List all methods used in files used not managed functionality.

Namespace

Drupal\auditfiles

Code

public function auditfilesUsedNotReferencedGetFileList() {
  $connection = $this->connection;
  $config = $this->configFactory
    ->get('auditfiles.settings');
  $maximum_records = $config
    ->get('auditfiles_report_options_maximum_records');
  $query = $connection
    ->select('file_usage', 'fu')
    ->fields('fu', [
    'fid',
  ])
    ->distinct();
  if ($maximum_records > 0) {
    $query
      ->range(0, $maximum_records);
  }
  $files_in_file_usage = $query
    ->execute()
    ->fetchCol();
  $files_in_fields = $field_data = [];
  $fields[] = $this->entityFieldManager
    ->getFieldMapByFieldType('image');
  $fields[] = $this->entityFieldManager
    ->getFieldMapByFieldType('file');
  $count = 0;
  foreach ($fields as $value) {
    foreach ($value as $table_prefix => $entity_type) {
      foreach ($entity_type as $key1 => $value1) {
        $field_data[$count]['table'] = $table_prefix . '__' . $key1;
        if ($this->entityTypeManager
          ->getStorage($table_prefix)
          ->getEntityType()
          ->isRevisionable()) {
          $field_data[$count]['table'] = $table_prefix . '_revision__' . $key1;
        }
        else {
          $field_data[$count]['table'] = $table_prefix . '__' . $key1;
        }
        $field_data[$count]['column'] = $key1 . '_target_id';
        $count++;
      }
    }
  }
  foreach ($field_data as $value) {
    $table = $value['table'];
    $column = $value['column'];
    if ($this->connection
      ->schema()
      ->tableExists($table)) {
      $query = "SELECT t.{$column} FROM {{$table}} AS t INNER JOIN {file_usage} AS f ON f.fid = t.{$column}";
      $result = $connection
        ->query($query)
        ->fetchCol();
      foreach ($result as $fid) {
        if (in_array($fid, $files_in_file_usage)) {
          $files_in_fields[] = $fid;
        }
      }
    }
  }
  return array_diff($files_in_file_usage, $files_in_fields);
}