ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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  if (!$this->upload_service->hasBeenProcessed()) {
121  $this->upload_service->process();
122  }
123  $this->setResourceId($this->handleUpload());
124  }
125 
126  $this->db->insert(
127  "il_bibl_data",
128  [
129  "id" => ["integer", $this->getId()],
130  "filename" => ["text", $this->getFilename()],
131  "file_type" => ["integer",
132  $this->getFilename() ? $this->determineFileTypeByFileName($this->getFilename()) : ""
133  ],
134  "rid" => ["string", ($rid = $this->getResourceId()) ? $rid->serialize() : ''],
135  ]
136  );
137  $this->parseFileToDatabase();
138  $this->getObjectProperties()->storePropertyIsOnline(new ilObjectPropertyIsOnline(false));
139  }
140 
141  protected function doRead(): void
142  {
144  $bibl_data = ilBiblData::where(array('id' => $this->getId()))->first();
145  if (!$this->getFilename() && $bibl_data->getFilename() !== null) {
146  $this->setFilename($bibl_data->getFilename());
147  }
148  $this->setFileType($bibl_data->getFileType());
149  if (!empty($rid = $bibl_data->getResourceId()) && $id = $this->storage->manage()->find($rid)) {
150  $this->setResourceId($id);
151  $this->setMigrated(true);
152  }
153  }
154 
155  protected function doUpdate(): void
156  {
157  $has_valid_upload = $this->upload_service->hasUploads() && !$this->upload_service->hasBeenProcessed();
158 
159  if ($has_valid_upload) {
160  $identification = $this->handleUpload();
161  if ($identification instanceof ResourceIdentification) {
162  $this->setResourceId($identification);
163  if (!$this->isMigrated()) {
164  $this->deleteFile();
165  $this->setMigrated(true);
166  }
167  }
168  }
169  if ($has_valid_upload) {
170  // Delete the object, but leave the db table 'il_bibl_data' for being able to update it using WHERE, and also leave the file
171  $this->doDelete(true, true);
172  $this->parseFileToDatabase();
173  }
174 
175  $this->db->update(
176  "il_bibl_data",
177  [
178  "filename" => ["text", $this->getFilename()],
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->getObjectProperties()->storePropertyIsOnline(new ilObjectPropertyIsOnline(false));
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  // copy filters
349 
350  $filters = new ilBiblFieldFilterFactory();
351  foreach ($filters->getAllForObjectId($original_id) as $filter) {
352  $cloned_filter = clone $filter;
353  $cloned_filter->setId(0);
354  $cloned_filter->setObjectId($this->getId());
355  $cloned_filter->create();
356  }
357  }
358 
362  public function parseFileToDatabase(): void
363  {
364  //Read File
365  if ($this->getResourceId() === null) {
366  return;
367  }
368  $type = $this->getFileType();
369  $reader = $this->bib_filereader_factory->getByType(
370  $type,
371  $this->bib_entry_factory,
372  $this->bib_field_factory,
373  $this->bib_attribute_factory
374  );
375  $reader->readContent($this->getResourceId());
376  $this->entries = $reader->parseContentToEntries($this);
377  }
378 
379  public function setFileType(int $file_type): void
380  {
381  $this->file_type = $file_type;
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
Interface Observer Contains several chained tasks and infos about them.
ilBiblFileReaderFactory $bib_filereader_factory
setTitle(string $title)
static where($where, $operator=null)
determineFileTypeByFileName(string $filename)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$path
Definition: ltiservices.php:30
setId(int $id)
ilObjBibliographicStakeholder $stakeholder
setType(string $type)
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...
global $DIC
Definition: shib_login.php:25
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
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
__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...
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.