ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilObjBibliographic.php
Go to the documentation of this file.
1 <?php
2 
26 
35 {
38  protected \ilBiblFileReaderFactory $bib_filereader_factory;
39  protected \ilBiblTypeFactory $bib_type_factory;
40  protected \ilBiblEntryFactory $bib_entry_factory;
41  protected \ilBiblFieldFactory $bib_field_factory;
42  protected \ilBiblDataFactoryInterface $bib_data_factory;
43  protected \ilBiblOverviewModelFactory $bib_overview_factory;
44  protected \ilBiblAttributeFactory $bib_attribute_factory;
45  protected Services $storage;
46  protected \ilObjBibliographicStakeholder $stakeholder;
47  protected ?string $filename = null;
48  protected array $entries = [];
49  protected int $file_type = 0;
51  protected bool $is_migrated = false;
52 
53  protected function initType(): void
54  {
55  $this->type = "bibl";
56  }
57 
62  public function __construct(int $existant_bibl_id = 0)
63  {
64  global $DIC;
65 
66  $this->storage = $DIC->resourceStorage();
67  $this->upload_service = $DIC->upload();
68  $this->stakeholder = new ilObjBibliographicStakeholder();
69  $this->filesystem = $DIC->filesystem()->storage();
70 
71  if ($existant_bibl_id !== 0) {
72  $this->setId($existant_bibl_id);
73  $this->doRead();
74  }
75  parent::__construct($existant_bibl_id, false);
76 
77  $this->bib_type_factory = new ilBiblTypeFactory();
78  $this->bib_field_factory = new ilBiblFieldFactory($this->bib_type_factory->getInstanceForType($this->getFileType()));
79  $this->bib_overview_factory = new ilBiblOverviewModelFactory();
80  $this->bib_entry_factory = new ilBiblEntryFactory(
81  $this->bib_field_factory,
82  $this->bib_type_factory->getInstanceForType($this->getFileType()),
83  $this->bib_overview_factory
84  );
85  $this->bib_filereader_factory = new ilBiblFileReaderFactory();
86  $this->bib_attribute_factory = new ilBiblAttributeFactory($this->bib_field_factory);
87  }
88 
93  private function handleUpload(): ?ResourceIdentification
94  {
95  if (!$this->upload_service->hasBeenProcessed()) {
96  $this->upload_service->process();
97  }
98  $array_result = $this->upload_service->getResults();
99  $result = reset($array_result); // FileUpload is the first element
100  if (!$result->isOK()) {
101  return null;
102  }
103 
104  if ($this->getResourceId() !== null) {
105  $this->storage->manage()->appendNewRevision(
106  $this->getResourceId(),
107  $result,
108  $this->stakeholder
109  );
110  return $this->getResourceId();
111  }
112 
113  return $this->storage->manage()->upload($result, $this->stakeholder);
114  }
115 
120  protected function doCreate(bool $clone_mode = false): void
121  {
122  if ($this->upload_service->hasUploads()) {
123  if (!$this->upload_service->hasBeenProcessed()) {
124  $this->upload_service->process();
125  }
126  $this->setResourceId($this->handleUpload());
127  }
128 
129  $this->db->insert(
130  "il_bibl_data",
131  [
132  "id" => ["integer", $this->getId()],
133  "filename" => ["text", $this->getFilename()],
134  "file_type" => ["integer",
135  $this->getFilename() ? $this->determineFileTypeByFileName($this->getFilename()) : ""
136  ],
137  "rid" => ["string", (($rid = $this->getResourceId()) !== null) ? $rid->serialize() : ''],
138  ]
139  );
140  $this->parseFileToDatabase();
141  $this->getObjectProperties()->storePropertyIsOnline(new Online(false));
142  }
143 
144  protected function doRead(): void
145  {
147  $bibl_data = ilBiblData::where(['id' => $this->getId()])->first();
148  if (!$this->getFilename() && $bibl_data->getFilename() !== null) {
149  $this->setFilename($bibl_data->getFilename());
150  }
151  $this->setFileType($bibl_data->getFileType());
152  if (!empty($rid = $bibl_data->getResourceId()) && $id = $this->storage->manage()->find($rid)) {
153  $this->setResourceId($id);
154  $this->setMigrated(true);
155  }
156  }
157 
158  protected function doUpdate(): void
159  {
160  $has_valid_upload = $this->upload_service->hasUploads() && !$this->upload_service->hasBeenProcessed();
161 
162  if ($has_valid_upload) {
163  $identification = $this->handleUpload();
164  if ($identification instanceof ResourceIdentification) {
165  $this->setResourceId($identification);
166  if (!$this->isMigrated()) {
167  $this->deleteFile();
168  $this->setMigrated(true);
169  }
170  }
171  }
172  if ($has_valid_upload) {
173  // Delete the object, but leave the db table 'il_bibl_data' for being able to update it using WHERE, and also leave the file
174  $this->doDelete(true, true);
175  $this->parseFileToDatabase();
176  }
177 
178  $this->db->update(
179  "il_bibl_data",
180  [
181  "filename" => ["text", $this->getFilename()],
182  "file_type" => ["integer", $this->getFileType()],
183  "rid" => ["string", (($rid = $this->getResourceId()) !== null) ? $rid->serialize() : ''],
184  ],
185  ["id" => ["integer", $this->getId()]]
186  );
187  }
188 
189  protected function doDelete(bool $leave_out_il_bibl_data = false, bool $leave_out_delete_file = false): void
190  {
191  if (!$leave_out_delete_file) {
192  $this->deleteFile();
193  }
194  //il_bibl_attribute
195  $this->db->manipulate(
196  "DELETE FROM il_bibl_attribute WHERE il_bibl_attribute.entry_id IN "
197  . "(SELECT il_bibl_entry.id FROM il_bibl_entry WHERE il_bibl_entry.data_id = " . $this->db->quote(
198  $this->getId(),
199  "integer"
200  ) . ")"
201  );
202  //il_bibl_entry
203  $this->bib_entry_factory->deleteEntryById($this->getId());
204 
205  if (!$leave_out_il_bibl_data) {
206  //il_bibl_data
207  $this->db->manipulate(
208  "DELETE FROM il_bibl_data WHERE id = " . $this->db->quote($this->getId(), "integer")
209  );
210  }
211  // delete history entries
213  }
214 
218  public function getFileDirectory(): string
219  {
220  return "{$this->getType()}/{$this->getId()}";
221  }
222 
226  private function copyFile(string $file_to_copy): void
227  {
228  $target = $this->getFileDirectory() . '/' . basename($file_to_copy);
229  $this->filesystem->copy($file_to_copy, $target);
230  }
231 
232  protected function deleteFile(): bool
233  {
234  $path = $this->getFileDirectory();
235  try {
236  $this->filesystem->deleteDir($path);
237  } catch (IOException) {
238  return false;
239  }
240 
241  return true;
242  }
243 
247  public function getFilePath(bool $without_filename = false): ?string
248  {
249  $file_name = $this->getFilename();
250 
251  if ($without_filename) {
252  return substr((string) $file_name, 0, strrpos((string) $file_name, DIRECTORY_SEPARATOR));
253  }
254  return $file_name;
255  }
256 
257  public function setFilename(string $filename): void
258  {
259  $this->filename = $filename;
260  }
261 
262  public function getFilename(): ?string
263  {
264  if ($this->getResourceId() !== null) {
265  return $this->filename = $this->storage->manage()
266  ->getCurrentRevision($this->getResourceId())
267  ->getInformation()
268  ->getTitle();
269  }
270  return $this->filename;
271  }
272 
277  public function getFileAbsolutePath(): string
278  {
279  return $this->getFileDirectory() . DIRECTORY_SEPARATOR . $this->getFilename();
280  }
281 
282  public function getLegacyAbsolutePath()
283  {
284  $stream = ($this->isMigrated()) ?
285  $this->storage->consume()->stream($this->getResourceId())->getStream() :
286  $this->filesystem->readStream($this->getFileAbsolutePath());
287 
288  return $stream->getMetadata('uri');
289  }
290 
294  public function getFileTypeAsString(): string
295  {
296  $type = $this->getFileType();
297 
298  return $this->bib_type_factory->getInstanceForType($type)->getStringRepresentation();
299  }
300 
301  public function getFileType(): int
302  {
303  $filename = $this->getFilename();
304  if ($filename === null) {
306  }
307  $instance = $this->bib_type_factory->getInstanceForFileName($filename);
308 
309  return $instance->getId();
310  }
311 
312  protected function doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id = null): void
313  {
314  assert($new_obj instanceof ilObjBibliographic);
315  //copy online status if object is not the root copy object
316  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
317 
318  if (!$cp_options->isRootNode($this->getRefId())) {
319  $new_obj->getObjectProperties()->storePropertyIsOnline(new Online(false));
320  }
321 
322  $new_obj->cloneStructure($this->getId());
323  $new_obj->parseFileToDatabase();
324  }
325 
332  public function cloneStructure(int $original_id): void
333  {
334  $original = new ilObjBibliographic($original_id);
335  $this->setFilename($original->getFilename());
336  $this->setDescription($original->getDescription());
337  $this->setTitle($original->getTitle());
338  $this->setType($original->getType());
339  $identification = $original->getResourceId();
340  if ($identification instanceof ResourceIdentification) {
341  $new_identification = $this->storage->manage()->clone($identification);
342  $this->setResourceId($new_identification);
343  } else {
344  $this->copyFile($original->getFileAbsolutePath());
345  }
346  $this->parseFileToDatabase();
347  $this->setMigrated($original->isMigrated());
348  $this->doUpdate();
349 
350  // copy filters
351 
352  $filters = new ilBiblFieldFilterFactory();
353  foreach ($filters->getAllForObjectId($original_id) as $filter) {
354  $cloned_filter = clone $filter;
355  $cloned_filter->setId(0);
356  $cloned_filter->setObjectId($this->getId());
357  $cloned_filter->create();
358  }
359  }
360 
364  public function parseFileToDatabase(): void
365  {
366  //Read File
367  if ($this->getResourceId() === null) {
368  return;
369  }
370  $type = $this->getFileType();
371  $reader = $this->bib_filereader_factory->getByType(
372  $type,
373  $this->bib_entry_factory,
374  $this->bib_field_factory,
375  $this->bib_attribute_factory
376  );
377  $reader->readContent($this->getResourceId());
378  $this->entries = $reader->parseContentToEntries($this);
379  }
380 
381  public function setFileType(int $file_type): void
382  {
383  $this->file_type = $file_type;
384  }
385 
386  public function setResourceId(ResourceIdentification $identification): void
387  {
388  $this->resource_id = $identification;
389  }
390 
395  {
396  return $this->resource_id;
397  }
398 
399  public function getStorageId(): string
400  {
401  if (!$this->getResourceId() instanceof ResourceIdentification) {
402  return '-';
403  }
404  return $this->storage->manage()->getResource($this->getResourceId())->getStorageID();
405  }
406 
407  public function isMigrated(): bool
408  {
409  return $this->is_migrated;
410  }
411 
412  public function setMigrated(bool $migrated): void
413  {
414  $this->is_migrated = $migrated;
415  }
416 
417  public function determineFileTypeByFileName(string $filename): int
418  {
419  return $this->bib_type_factory->getInstanceForFileName($filename)->getId();
420  }
421 }
string $type
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)
Indicates general problems with the input or output operations.
Definition: IOException.php:27
ilBiblEntryFactory $bib_entry_factory
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilBiblAttributeFactory $bib_attribute_factory
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:29
setId(int $id)
ilObjBibliographicStakeholder $stakeholder
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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)
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:26
Class FileUpload.
Definition: FileUpload.php:37
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...
ResourceIdentification $resource_id
copyFile(string $file_to_copy)
ilBiblOverviewModelFactory $bib_overview_factory
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__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.