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