ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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
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 }
212
216 public function getFileDirectory(): string
217 {
218 return "{$this->getType()}/{$this->getId()}";
219 }
220
224 private function copyFile(string $file_to_copy): void
225 {
226 $target = $this->getFileDirectory() . '/' . basename($file_to_copy);
227 $this->filesystem->copy($file_to_copy, $target);
228 }
229
230 protected function deleteFile(): bool
231 {
232 $path = $this->getFileDirectory();
233 try {
234 $this->filesystem->deleteDir($path);
235 } catch (IOException) {
236 return false;
237 }
238
239 return true;
240 }
241
245 public function getFilePath(bool $without_filename = false): ?string
246 {
247 $file_name = $this->getFilename();
248
249 if ($without_filename) {
250 return substr((string) $file_name, 0, strrpos((string) $file_name, DIRECTORY_SEPARATOR));
251 }
252 return $file_name;
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() !== null) {
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 Online(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
416 {
417 return $this->bib_type_factory->getInstanceForFileName($filename)->getId();
418 }
419}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static where($where, $operator=null)
Indicates general problems with the input or output operations.
Definition: IOException.php:28
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...
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...
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getInstance(int $a_copy_id)
Class ilObjBibliographicStakeholder.
Class ilObjBibliographic.
doCreate(bool $clone_mode=false)
Create object.
__construct(int $existant_bibl_id=0)
If bibliographic object exists, read it's data from database, otherwise create it.
doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id=null)
ilBiblOverviewModelFactory $bib_overview_factory
ilBiblFileReaderFactory $bib_filereader_factory
determineFileTypeByFileName(string $filename)
setResourceId(ResourceIdentification $identification)
cloneStructure(int $original_id)
@description Attention only use this for objects who have not yet been created (use like: $x = new il...
ilBiblFieldFactory $bib_field_factory
ilObjBibliographicStakeholder $stakeholder
ilBiblAttributeFactory $bib_attribute_factory
ilBiblTypeFactory $bib_type_factory
doDelete(bool $leave_out_il_bibl_data=false, bool $leave_out_delete_file=false)
handleUpload()
handles a FileUpload and returns an IRSS identification string.
parseFileToDatabase()
Reads out the source file and writes all entries to the database.
ilBiblEntryFactory $bib_entry_factory
copyFile(string $file_to_copy)
getFilePath(bool $without_filename=false)
ResourceIdentification $resource_id
ilBiblDataFactoryInterface $bib_data_factory
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setId(int $id)
setType(string $type)
setTitle(string $title)
string $type
setDescription(string $description)
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26