You are here

public function ServiceAuditFilesReferencedNotUsed::auditfilesReferencedNotUsedGetFileList in Audit Files 4.x

Same name and namespace in other branches
  1. 8.3 src/ServiceAuditFilesReferencedNotUsed.php \Drupal\auditfiles\ServiceAuditFilesReferencedNotUsed::auditfilesReferencedNotUsedGetFileList()
  2. 8.2 src/ServiceAuditFilesReferencedNotUsed.php \Drupal\auditfiles\ServiceAuditFilesReferencedNotUsed::auditfilesReferencedNotUsedGetFileList()

Retrieves the file IDs to operate on.

Return value

array The file IDs.

File

src/ServiceAuditFilesReferencedNotUsed.php, line 84

Class

ServiceAuditFilesReferencedNotUsed
List all methods used in referenced not used functionality.

Namespace

Drupal\auditfiles

Code

public function auditfilesReferencedNotUsedGetFileList() {
  $config = $this->configFactory
    ->get('auditfiles.settings');
  $connection = $this->connection;
  $file_references = $files_referenced = [];

  // Get a list of all files that are referenced in content.
  $fields = $field_data = [];
  $fields[] = $this->entityFieldManager
    ->getFieldMapByFieldType('image');
  $fields[] = $this->entityFieldManager
    ->getFieldMapByFieldType('file');
  if ($fields) {
    $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';
          $field_data[$count]['entity_type'] = $table_prefix;
          $count++;
        }
      }
    }
    foreach ($field_data as $value) {
      $table = $value['table'];
      $column = $value['column'];
      $entity_type = $value['entity_type'];
      if ($this->connection
        ->schema()
        ->tableExists($table)) {
        $query = 'SELECT entity_id, ' . $column . ' FROM {' . $table . '}';
        $query .= ' WHERE ' . $column . ' NOT IN (SELECT DISTINCT fid FROM {file_usage})';
        $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;
        }
        $file_references = $connection
          ->query($query)
          ->fetchAll();
        foreach ($file_references as $file_reference) {
          $reference_id = $table . '.' . $column . '.' . $file_reference->entity_id . '.' . $entity_type . '.' . $file_reference->{$column};
          $files_referenced[$reference_id] = [
            'table' => $table,
            'column' => $column,
            'entity_id' => $file_reference->entity_id,
            'file_id' => $file_reference->{$column},
            'entity_type' => $entity_type,
          ];
        }
      }
    }
  }
  return $files_referenced;
}