ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjFileInfoRepository.php
Go to the documentation of this file.
1 <?php
2 
22 
30 {
31  private static array $cache = [];
35  private Services $irss;
39  private ilDBInterface $db;
40  private array $inline_suffixes = [];
41 
42  public function __construct(bool $with_cleared_cache = false)
43  {
44  global $DIC;
45  if ($with_cleared_cache) {
46  self::$cache = [];
47  }
48  $this->irss = $DIC->resourceStorage();
49  $this->db = $DIC->database();
50  $this->inline_suffixes = $this->initInlineSuffixes();
51  }
52 
53  public function getByRefId(int $ref_id): ilObjFileInfo
54  {
55  $object_id = ilObject2::_lookupObjectId($ref_id);
56 
57  return $this->getByObjectId($object_id);
58  }
59 
60  private function initInlineSuffixes(): array
61  {
62  $settings = new ilSetting('file_access');
63  return array_map('strtolower', explode(' ', (string) $settings->get('inline_file_extensions', '')));
64  }
65 
66  public function preloadData(array $ids, bool $are_ref_ids = false): void
67  {
68  if ($are_ref_ids) {
69  $query = "SELECT title, rid, file_id, page_count, on_click_mode
70  FROM file_data
71  JOIN object_data ON object_data.obj_id = file_data.file_id
72  JOIN object_reference ON object_reference.obj_id = object_data.obj_id
73  WHERE rid IS NOT NULL AND " . $this->db->in(
74  'ref_id',
75  $ids,
76  false,
77  'integer'
78  );
79  } else {
80  $query = "SELECT title, rid, file_id, page_count, on_click_mode FROM file_data JOIN object_data ON object_data.obj_id = file_data.file_id WHERE rid IS NOT NULL AND " . $this->db->in(
81  'file_id',
82  $ids,
83  false,
84  'integer'
85  );
86  }
87 
88  $res = $this->db->query(
89  $query
90  );
91  $rids = [];
92  $page_counts = [];
93  $object_titles = [];
94  $click_modes = [];
95 
96  while ($row = $this->db->fetchObject($res)) {
97  $rids[(int) $row->file_id] = $row->rid;
98  $page_counts[(int) $row->file_id] = $row->page_count;
99  $object_titles[(int) $row->file_id] = $row->title;
100  $click_modes[(int) $row->file_id] = (int) ($row->on_click_mode ?? ilObjFile::CLICK_MODE_DOWNLOAD);
101  }
102  $this->irss->preload($rids);
103 
104  foreach ($rids as $file_id => $rid) {
105  if (($id = $this->irss->manage()->find($rid)) !== null) {
106  $max = $this->irss->manage()->getResource($id)->getCurrentRevision();
107 
108  $info = new ilObjFileInfo(
109  $object_titles[$file_id] ?? $max->getTitle(),
110  $id,
111  $max->getInformation()->getTitle(),
112  $max->getInformation()->getSuffix(),
113  in_array(strtolower($max->getInformation()->getSuffix()), $this->inline_suffixes, true),
114  $click_modes[$file_id] === ilObjFile::CLICK_MODE_DOWNLOAD,
115  $max->getVersionNumber(),
116  $max->getInformation()->getCreationDate(),
117  in_array(strtolower($max->getInformation()->getMimeType()), [
118  MimeType::APPLICATION__ZIP,
119  MimeType::APPLICATION__X_ZIP_COMPRESSED
120  ], true),
121  $max->getInformation()->getMimeType(),
122  new DataSize($max->getInformation()->getSize() ?? 0, DataSize::Byte),
123  $page_counts[$file_id] === null ? null : (int) $page_counts[$file_id]
124  );
125 
126  self::$cache[$file_id] = $info;
127  }
128  }
129  }
130 
131  public function getByObjectId(int $object_id): ilObjFileInfo
132  {
133  if (isset(self::$cache[$object_id])) {
134  return self::$cache[$object_id];
135  }
136 
137  $this->preloadData([$object_id]);
138 
139  return self::$cache[$object_id] ?? new ilObjFileInfo(
140  'Unknown',
141  null,
142  'Unknown',
143  '',
144  false,
145  false,
146  0,
147  new DateTimeImmutable(),
148  false,
149  '',
150  new DataSize(0, DataSize::Byte),
151  null
152  );
153  }
154 
155 }
$res
Definition: ltiservices.php:69
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLICK_MODE_DOWNLOAD
static _lookupObjectId(int $ref_id)
preloadData(array $ids, bool $are_ref_ids=false)
__construct(bool $with_cleared_cache=false)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23