You are here

public function ServiceAuditFilesUsedNotReferenced::auditfilesUsedNotReferencedGetFileList in Audit Files 4.x

Same name and namespace in other branches
  1. 8.3 src/ServiceAuditFilesUsedNotReferenced.php \Drupal\auditfiles\ServiceAuditFilesUsedNotReferenced::auditfilesUsedNotReferencedGetFileList()
  2. 8.2 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 65

Class

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

Namespace

Drupal\auditfiles

Code

public function auditfilesUsedNotReferencedGetFileList() {
  $config = $this->configFactory
    ->get('auditfiles.settings');
  $connection = $this->connection;
  $query = 'SELECT DISTINCT fid FROM {file_usage} fu';
  $maximum_records = $config
    ->get('auditfiles_report_options_maximum_records') ? $config
    ->get('auditfiles_report_options_maximum_records') : 250;
  if ($maximum_records > 0) {
    $query .= ' LIMIT ' . $maximum_records;
  }
  $files_in_file_usage = $connection
    ->query($query)
    ->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;
        $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);
      foreach ($result as $fid) {
        if (in_array($fid->{$column}, $files_in_file_usage)) {
          $files_in_fields[] = $fid->{$column};
        }
      }
    }
  }
  return array_diff($files_in_file_usage, $files_in_fields);
}