ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjBibliographic.php
Go to the documentation of this file.
1 <?php
2 
23 
32 {
33  protected \ILIAS\Filesystem\Filesystem $filesystem;
35  protected \ilBiblFileReaderFactory $bib_filereader_factory;
36  protected \ilBiblTypeFactory $bib_type_factory;
37  protected \ilBiblEntryFactory $bib_entry_factory;
38  protected \ilBiblFieldFactory $bib_field_factory;
39  protected \ilBiblDataFactoryInterface $bib_data_factory;
40  protected \ilBiblOverviewModelFactory $bib_overview_factory;
41  protected \ilBiblAttributeFactory $bib_attribute_factory;
42  protected Services $storage;
43  protected \ilObjBibliographicStakeholder $stakeholder;
44  protected ?string $filename = null;
45  protected array $entries = [];
46  protected int $file_type = 0;
48  protected bool $is_migrated = false;
49 
50  protected function initType(): void
51  {
52  $this->type = "bibl";
53  }
54 
59  public function __construct(int $existant_bibl_id = 0)
60  {
61  global $DIC;
62 
63  $this->storage = $DIC->resourceStorage();
64  $this->upload_service = $DIC->upload();
65  $this->stakeholder = new ilObjBibliographicStakeholder();
66  $this->filesystem = $DIC->filesystem()->storage();
67 
68  if ($existant_bibl_id) {
69  $this->setId($existant_bibl_id);
70  $this->doRead();
71  }
72  parent::__construct($existant_bibl_id, false);
73 
74  $this->bib_type_factory = new ilBiblTypeFactory();
75  $this->bib_field_factory = new ilBiblFieldFactory($this->bib_type_factory->getInstanceForType($this->getFileType()));
76  $this->bib_overview_factory = new ilBiblOverviewModelFactory();
77  $this->bib_entry_factory = new ilBiblEntryFactory(
78  $this->bib_field_factory,
79  $this->bib_type_factory->getInstanceForType($this->getFileType()),
80  $this->bib_overview_factory
81  );
82  $this->bib_filereader_factory = new ilBiblFileReaderFactory();
83  $this->bib_attribute_factory = new ilBiblAttributeFactory($this->bib_field_factory);
84  }
85 
90  private function handleUpload(): ?\ILIAS\ResourceStorage\Identification\ResourceIdentification
91  {
92  if (!$this->upload_service->hasBeenProcessed()) {
93  $this->upload_service->process();
94  }
95  $array_result = $this->upload_service->getResults();
96  $result = reset($array_result); // FileUpload is the first element
97  if (!$result->isOK()) {
98  return null;
99  }
100 
101  if ($this->getResourceId()) {
102  $this->storage->manage()->appendNewRevision(
103  $this->getResourceId(),
104  $result,
105  $this->stakeholder
106  );
107  return $this->getResourceId();
108  }
109 
110  return $this->storage->manage()->upload($result, $this->stakeholder);
111  }
112 
117  protected function doCreate(bool $clone_mode = false): void
118  {
119  if ($this->upload_service->hasUploads()) {
120  $this->setResourceId($this->handleUpload());
121  }
122 
123  $this->db->insert(
124  "il_bibl_data",
125  [
126  "id" => ["integer", $this->getId()],
127  "filename" => ["text", $this->getFilename()],
128  "file_type" => ["integer",
129  $this->getFilename() ? $this->determineFileTypeByFileName($this->getFilename()) : ""
130  ],
131  "rid" => ["string", ($rid = $this->getResourceId()) ? $rid->serialize() : ''],
132  ]
133  );
134  $this->parseFileToDatabase();
135  $this->getObjectProperties()->storePropertyIsOnline(new ilObjectPropertyIsOnline(false));
136  }
137 
138  protected function doRead(): void
139  {
141  $bibl_data = ilBiblData::where(array('id' => $this->getId()))->first();
142  if (!$this->getFilename() && $bibl_data->getFilename() !== null) {
143  $this->setFilename($bibl_data->getFilename());
144  }
145  $this->setFileType($bibl_data->getFileType());
146  if (!empty($rid = $bibl_data->getResourceId()) && $id = $this->storage->manage()->find($rid)) {
147  $this->setResourceId($id);
148  $this->setMigrated(true);
149  }
150  }
151 
152  protected function doUpdate(): void
153  {
154  $has_valid_upload = $this->upload_service->hasUploads() && !$this->upload_service->hasBeenProcessed();
155 
156  if ($has_valid_upload) {
157  $identification = $this->handleUpload();
158  if ($identification instanceof ResourceIdentification) {
159  $this->setResourceId($identification);
160  if (!$this->isMigrated()) {
161  $this->deleteFile();
162  $this->setMigrated(true);
163  }
164  }
165  }
166  if ($has_valid_upload) {
167  // Delete the object, but leave the db table 'il_bibl_data' for being able to update it using WHERE, and also leave the file
168  $this->doDelete(true, true);
169  $this->parseFileToDatabase();
170  }
171 
172  $this->db->update(
173  "il_bibl_data",
174  [
175  "filename" => ["text", $this->getFilename()],
176  "file_type" => ["integer", $this->getFileType()],
177  "rid" => ["string", ($rid = $this->getResourceId()) ? $rid->serialize() : ''],
178  ],
179  ["id" => ["integer", $this->getId()]]
180  );
181  }
182 
183  protected function doDelete(bool $leave_out_il_bibl_data = false, bool $leave_out_delete_file = false): void
184  {
185  if (!$leave_out_delete_file) {
186  $this->deleteFile();
187  }
188  //il_bibl_attribute
189  $this->db->manipulate(
190  "DELETE FROM il_bibl_attribute WHERE il_bibl_attribute.entry_id IN "
191  . "(SELECT il_bibl_entry.id FROM il_bibl_entry WHERE il_bibl_entry.data_id = " . $this->db->quote(
192  $this->getId(),
193  "integer"
194  ) . ")"
195  );
196  //il_bibl_entry
197  $this->bib_entry_factory->deleteEntryById($this->getId());
198 
199  if (!$leave_out_il_bibl_data) {
200  //il_bibl_data
201  $this->db->manipulate(
202  "DELETE FROM il_bibl_data WHERE id = " . $this->db->quote($this->getId(), "integer")
203  );
204  }
205  // delete history entries
207  }
208 
212  public function getFileDirectory(): string
213  {
214  return "{$this->getType()}/{$this->getId()}";
215  }
216 
220  private function copyFile(string $file_to_copy): void
221  {
222  $target = $this->getFileDirectory() . '/' . basename($file_to_copy);
223  $this->filesystem->copy($file_to_copy, $target);
224  }
225 
226  protected function deleteFile(): bool
227  {
228  $path = $this->getFileDirectory();
229  try {
230  $this->filesystem->deleteDir($path);
231  } catch (\ILIAS\Filesystem\Exception\IOException $e) {
232  return false;
233  }
234 
235  return true;
236  }
237 
241  public function getFilePath(bool $without_filename = false)
242  {
243  $file_name = $this->getFilename();
244 
245  if ($without_filename) {
246  return substr($file_name, 0, strrpos($file_name, DIRECTORY_SEPARATOR));
247  } else {
248  return $file_name;
249  }
250  }
251 
252  public function setFilename(string $filename): void
253  {
254  $this->filename = $filename;
255  }
256 
257  public function getFilename(): ?string
258  {
259  if ($this->getResourceId()) {
260  return $this->filename = $this->storage->manage()
261  ->getCurrentRevision($this->getResourceId())
262  ->getInformation()
263  ->getTitle();
264  }
265  return $this->filename;
266  }
267 
272  public function getFileAbsolutePath(): string
273  {
274  return $this->getFileDirectory() . DIRECTORY_SEPARATOR . $this->getFilename();
275  }
276 
277  public function getLegacyAbsolutePath()
278  {
279  $stream = ($this->isMigrated()) ?
280  $this->storage->consume()->stream($this->getResourceId())->getStream() :
281  $this->filesystem->readStream($this->getFileAbsolutePath());
282 
283  return $stream->getMetadata('uri');
284  }
285 
289  public function getFileTypeAsString(): string
290  {
291  $type = $this->getFileType();
292 
293  return $this->bib_type_factory->getInstanceForType($type)->getStringRepresentation();
294  }
295 
296  public function getFileType(): int
297  {
298  $filename = $this->getFilename();
299  if ($filename === null) {
301  }
302  $instance = $this->bib_type_factory->getInstanceForFileName($filename);
303 
304  return $instance->getId();
305  }
306 
307  protected function doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id = null): void
308  {
309  assert($new_obj instanceof ilObjBibliographic);
310  //copy online status if object is not the root copy object
311  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
312 
313  if (!$cp_options->isRootNode($this->getRefId())) {
314  $new_obj->getObjectProperties()->storePropertyIsOnline(new ilObjectPropertyIsOnline(false));
315  }
316 
317  $new_obj->cloneStructure($this->getId());
318  $new_obj->parseFileToDatabase();
319  }
320 
327  public function cloneStructure(int $original_id): void
328  {
329  $original = new ilObjBibliographic($original_id);
330  $this->setFilename($original->getFilename());
331  $this->setDescription($original->getDescription());
332  $this->setTitle($original->getTitle());
333  $this->setType($original->getType());
334  $identification = $original->getResourceId();
335  if ($identification instanceof ResourceIdentification) {
336  $new_identification = $this->storage->manage()->clone($identification);
337  $this->setResourceId($new_identification);
338  } else {
339  $this->copyFile($original->getFileAbsolutePath());
340  }
341  $this->parseFileToDatabase();
342  $this->setMigrated($original->isMigrated());
343  $this->doUpdate();
344  }
345 
349  public function parseFileToDatabase(): void
350  {
351  //Read File
352  if ($this->getResourceId() === null) {
353  return;
354  }
355  $type = $this->getFileType();
356  $reader = $this->bib_filereader_factory->getByType(
357  $type,
358  $this->bib_entry_factory,
359  $this->bib_field_factory,
360  $this->bib_attribute_factory
361  );
362  $reader->readContent($this->getResourceId());
363  $this->entries = $reader->parseContentToEntries($this);
364  }
365 
366  public function setFileType(int $file_type): void
367  {
368  $this->file_type = $file_type;
369  }
370 
371  public function setResourceId(ResourceIdentification $identification): void
372  {
373  $this->resource_id = $identification;
374  }
375 
380  {
381  return $this->resource_id;
382  }
383 
384  public function getStorageId(): string
385  {
386  if (!$this->getResourceId() instanceof ResourceIdentification) {
387  return '-';
388  }
389  return $this->storage->manage()->getResource($this->getResourceId())->getStorageID();
390  }
391 
392  public function isMigrated(): bool
393  {
394  return $this->is_migrated;
395  }
396 
397  public function setMigrated(bool $migrated): void
398  {
399  $this->is_migrated = $migrated;
400  }
401 
402  public function determineFileTypeByFileName(string $filename): int
403  {
404  return $this->bib_type_factory->getInstanceForFileName($filename)->getId();
405  }
406 }
string $type
ILIAS Filesystem Filesystem $filesystem
cloneStructure(int $original_id)
Attention only use this for objects who have not yet been created (use like: $x = new ilObjDataCollec...
ilBiblFieldFactory $bib_field_factory
__construct(int $existant_bibl_id=0)
If bibliographic object exists, read it&#39;s data from database, otherwise create it.
handleUpload()
handles a FileUpload and returns an IRSS identification string.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getFilePath(bool $without_filename=false)
ilBiblEntryFactory $bib_entry_factory
ilBiblAttributeFactory $bib_attribute_factory
Class ChatMainBarProvider .
ilBiblFileReaderFactory $bib_filereader_factory
setTitle(string $title)
static where($where, $operator=null)
determineFileTypeByFileName(string $filename)
$path
Definition: ltiservices.php:32
setId(int $id)
ilObjBibliographicStakeholder $stakeholder
global $DIC
Definition: feed.php:28
setType(string $type)
setResourceId(ResourceIdentification $identification)
static _removeEntriesForObject(int $a_obj_id)
remove all history entries for an object
__construct(VocabulariesInterface $vocabularies)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id=null)
ILIAS ResourceStorage Identification ResourceIdentification $resource_id
Class ilObjBibliographicStakeholder.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class FileUpload.
Definition: FileUpload.php:34
doDelete(bool $leave_out_il_bibl_data=false, bool $leave_out_delete_file=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
copyFile(string $file_to_copy)
ilBiblOverviewModelFactory $bib_overview_factory
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
ilBiblTypeFactory $bib_type_factory
static _getInstance(int $a_copy_id)
Class ilObjBibliographic.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setDescription(string $description)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
parseFileToDatabase()
Reads out the source file and writes all entries to the database.
ilBiblDataFactoryInterface $bib_data_factory
doCreate(bool $clone_mode=false)
Create object.