ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
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 bool $is_online = false;
47  protected int $file_type = 0;
49  protected bool $is_migrated = false;
50 
51  protected function initType(): void
52  {
53  $this->type = "bibl";
54  }
55 
60  public function __construct(int $existant_bibl_id = 0)
61  {
62  global $DIC;
63 
64  $this->storage = $DIC->resourceStorage();
65  $this->upload_service = $DIC->upload();
66  $this->stakeholder = new ilObjBibliographicStakeholder();
67  $this->filesystem = $DIC->filesystem()->storage();
68 
69  if ($existant_bibl_id) {
70  $this->setId($existant_bibl_id);
71  $this->doRead();
72  }
73  parent::__construct($existant_bibl_id, false);
74 
75  $this->bib_type_factory = new ilBiblTypeFactory();
76  $this->bib_field_factory = new ilBiblFieldFactory($this->bib_type_factory->getInstanceForType($this->getFileType()));
77  $this->bib_overview_factory = new ilBiblOverviewModelFactory();
78  $this->bib_entry_factory = new ilBiblEntryFactory(
79  $this->bib_field_factory,
80  $this->bib_type_factory->getInstanceForType($this->getFileType()),
81  $this->bib_overview_factory
82  );
83  $this->bib_filereader_factory = new ilBiblFileReaderFactory();
84  $this->bib_attribute_factory = new ilBiblAttributeFactory($this->bib_field_factory);
85  }
86 
91  private function handleUpload(): ?\ILIAS\ResourceStorage\Identification\ResourceIdentification
92  {
93  if (!$this->upload_service->hasBeenProcessed()) {
94  $this->upload_service->process();
95  }
96  $array_result = $this->upload_service->getResults();
97  $result = reset($array_result); // FileUpload is the first element
98  if (!$result->isOK()) {
99  return null;
100  }
101 
102  if ($this->getResourceId()) {
103  $this->storage->manage()->appendNewRevision(
104  $this->getResourceId(),
105  $result,
106  $this->stakeholder
107  );
108  return $this->getResourceId();
109  }
110 
111  return $this->storage->manage()->upload($result, $this->stakeholder);
112  }
113 
118  protected function doCreate(bool $clone_mode = false): void
119  {
120  if ($this->upload_service->hasUploads()) {
121  $this->setResourceId($this->handleUpload());
122  }
123 
124  $this->db->insert(
125  "il_bibl_data",
126  [
127  "id" => ["integer", $this->getId()],
128  "filename" => ["text", $this->getFilename()],
129  "is_online" => ["integer", $this->getOnline()],
130  "file_type" => ["integer",
131  $this->getFilename() ? $this->determineFileTypeByFileName($this->getFilename()) : ""
132  ],
133  "rid" => ["string", ($rid = $this->getResourceId()) ? $rid->serialize() : ''],
134  ]
135  );
136  $this->parseFileToDatabase();
137  }
138 
139  protected function doRead(): void
140  {
142  $bibl_data = ilBiblData::where(array('id' => $this->getId()))->first();
143  if (!$this->getFilename() && $bibl_data->getFilename() !== null) {
144  $this->setFilename($bibl_data->getFilename());
145  }
146  $this->setFileType($bibl_data->getFileType());
147  $this->setOnline($bibl_data->isOnline());
148  if (!empty($rid = $bibl_data->getResourceId()) && $id = $this->storage->manage()->find($rid)) {
149  $this->setResourceId($id);
150  $this->setMigrated(true);
151  }
152  }
153 
154  protected function doUpdate(): void
155  {
156  $has_valid_upload = $this->upload_service->hasUploads() && !$this->upload_service->hasBeenProcessed();
157 
158  if ($has_valid_upload) {
159  $identification = $this->handleUpload();
160  if ($identification instanceof ResourceIdentification) {
161  $this->setResourceId($identification);
162  if (!$this->isMigrated()) {
163  $this->deleteFile();
164  $this->setMigrated(true);
165  }
166  }
167  }
168  if ($has_valid_upload) {
169  // Delete the object, but leave the db table 'il_bibl_data' for being able to update it using WHERE, and also leave the file
170  $this->doDelete(true, true);
171  $this->parseFileToDatabase();
172  }
173 
174  $this->db->update(
175  "il_bibl_data",
176  [
177  "filename" => ["text", $this->getFilename()],
178  "is_online" => ["integer", $this->getOnline()],
179  "file_type" => ["integer", $this->getFileType()],
180  "rid" => ["string", ($rid = $this->getResourceId()) ? $rid->serialize() : ''],
181  ],
182  ["id" => ["integer", $this->getId()]]
183  );
184  }
185 
186  protected function doDelete(bool $leave_out_il_bibl_data = false, bool $leave_out_delete_file = false): void
187  {
188  if (!$leave_out_delete_file) {
189  $this->deleteFile();
190  }
191  //il_bibl_attribute
192  $this->db->manipulate(
193  "DELETE FROM il_bibl_attribute WHERE il_bibl_attribute.entry_id IN "
194  . "(SELECT il_bibl_entry.id FROM il_bibl_entry WHERE il_bibl_entry.data_id = " . $this->db->quote(
195  $this->getId(),
196  "integer"
197  ) . ")"
198  );
199  //il_bibl_entry
200  $this->bib_entry_factory->deleteEntryById($this->getId());
201 
202  if (!$leave_out_il_bibl_data) {
203  //il_bibl_data
204  $this->db->manipulate(
205  "DELETE FROM il_bibl_data WHERE id = " . $this->db->quote($this->getId(), "integer")
206  );
207  }
208  // delete history entries
210  }
211 
215  public function getFileDirectory(): string
216  {
217  return "{$this->getType()}/{$this->getId()}";
218  }
219 
223  private function copyFile(string $file_to_copy): void
224  {
225  $target = $this->getFileDirectory() . '/' . basename($file_to_copy);
226  $this->filesystem->copy($file_to_copy, $target);
227  }
228 
229  protected function deleteFile(): bool
230  {
231  $path = $this->getFileDirectory();
232  try {
233  $this->filesystem->deleteDir($path);
234  } catch (\ILIAS\Filesystem\Exception\IOException $e) {
235  return false;
236  }
237 
238  return true;
239  }
240 
244  public function getFilePath(bool $without_filename = false)
245  {
246  $file_name = $this->getFilename();
247 
248  if ($without_filename) {
249  return substr($file_name, 0, strrpos($file_name, DIRECTORY_SEPARATOR));
250  } else {
251  return $file_name;
252  }
253  }
254 
255  public function setFilename(string $filename): void
256  {
257  $this->filename = $filename;
258  }
259 
260  public function getFilename(): ?string
261  {
262  if ($this->getResourceId()) {
263  return $this->filename = $this->storage->manage()
264  ->getCurrentRevision($this->getResourceId())
265  ->getInformation()
266  ->getTitle();
267  }
268  return $this->filename;
269  }
270 
275  public function getFileAbsolutePath(): string
276  {
277  return $this->getFileDirectory() . DIRECTORY_SEPARATOR . $this->getFilename();
278  }
279 
280  public function getLegacyAbsolutePath()
281  {
282  $stream = ($this->isMigrated()) ?
283  $this->storage->consume()->stream($this->getResourceId())->getStream() :
284  $this->filesystem->readStream($this->getFileAbsolutePath());
285 
286  return $stream->getMetadata('uri');
287  }
288 
292  public function getFileTypeAsString(): string
293  {
294  $type = $this->getFileType();
295 
296  return $this->bib_type_factory->getInstanceForType($type)->getStringRepresentation();
297  }
298 
299  public function getFileType(): int
300  {
301  $filename = $this->getFilename();
302  if ($filename === null) {
304  }
305  $instance = $this->bib_type_factory->getInstanceForFileName($filename);
306 
307  return $instance->getId();
308  }
309 
310  protected function doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id = null): void
311  {
312  assert($new_obj instanceof ilObjBibliographic);
313  //copy online status if object is not the root copy object
314  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
315 
316  if (!$cp_options->isRootNode($this->getRefId())) {
317  $new_obj->setOnline($this->getOnline());
318  }
319 
320  $new_obj->cloneStructure($this->getId());
321  $new_obj->parseFileToDatabase();
322  }
323 
330  public function cloneStructure(int $original_id): void
331  {
332  $original = new ilObjBibliographic($original_id);
333  $this->setFilename($original->getFilename());
334  $this->setDescription($original->getDescription());
335  $this->setTitle($original->getTitle());
336  $this->setType($original->getType());
337  $identification = $original->getResourceId();
338  if ($identification instanceof ResourceIdentification) {
339  $new_identification = $this->storage->manage()->clone($identification);
340  $this->setResourceId($new_identification);
341  } else {
342  $this->copyFile($original->getFileAbsolutePath());
343  }
344  $this->parseFileToDatabase();
345  $this->setMigrated($original->isMigrated());
346  $this->doUpdate();
347  }
348 
352  public function parseFileToDatabase(): void
353  {
354  //Read File
355  if ($this->getResourceId() === null) {
356  return;
357  }
358  $type = $this->getFileType();
359  $reader = $this->bib_filereader_factory->getByType(
360  $type,
361  $this->bib_entry_factory,
362  $this->bib_field_factory,
363  $this->bib_attribute_factory
364  );
365  $reader->readContent($this->getResourceId());
366  $this->entries = $reader->parseContentToEntries($this);
367  }
368 
369  public function setFileType(int $file_type): void
370  {
371  $this->file_type = $file_type;
372  }
373 
374  public function setOnline(bool $a_online): void
375  {
376  $this->is_online = $a_online;
377  }
378 
379  public function getOnline(): bool
380  {
381  return $this->is_online;
382  }
383 
384  public function setResourceId(ResourceIdentification $identification): void
385  {
386  $this->resource_id = $identification;
387  }
388 
393  {
394  return $this->resource_id;
395  }
396 
397  public function getStorageId(): string
398  {
399  if (!$this->getResourceId() instanceof ResourceIdentification) {
400  return '-';
401  }
402  return $this->storage->manage()->getResource($this->getResourceId())->getStorageID();
403  }
404 
405  public function isMigrated(): bool
406  {
407  return $this->is_migrated;
408  }
409 
410  public function setMigrated(bool $migrated): void
411  {
412  $this->is_migrated = $migrated;
413  }
414 
415  public function determineFileTypeByFileName(string $filename): int
416  {
417  return $this->bib_type_factory->getInstanceForFileName($filename)->getId();
418  }
419 }
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilObjBibliographicStakeholder $stakeholder
global $DIC
Definition: feed.php:28
setType(string $type)
setDescription(string $desc)
setResourceId(ResourceIdentification $identification)
static _removeEntriesForObject(int $a_obj_id)
remove all history entries for an object
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
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class FlySystemFileAccessTest disabled disabled disabled.
parseFileToDatabase()
Reads out the source file and writes all entries to the database.
ilBiblDataFactoryInterface $bib_data_factory
doCreate(bool $clone_mode=false)
Create object.