ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBiblFileReaderBase.php
Go to the documentation of this file.
1<?php
2
22
28{
29 use ilBibliographicSecureString;
35 public const ENCODING_UTF_8 = 'UTF-8';
36 public const ENCODING_ASCII = 'ASCII';
37 public const ENCODING_ISO_8859_1 = 'ISO-8859-1';
38 protected string $file_content = '';
39 protected string $path_to_file = '';
41
42 protected Services $storage;
43
44
48 public function __construct(
49 protected \ilBiblEntryFactoryInterface $entry_factory,
50 protected \ilBiblFieldFactoryInterface $field_factory,
51 protected \ilBiblAttributeFactoryInterface $attribute_factory
52 ) {
53 global $DIC;
54 $this->refinery = $DIC->refinery();
55 $this->storage = $DIC["resource_storage"];
56 }
57
58
59 public function readContent(ResourceIdentification $identification): bool
60 {
61 $stream = $this->storage->consume()->stream($identification)->getStream();
62 $this->setFileContent($stream->getContents());
63
64 return true;
65 }
66
67 protected function convertStringToUTF8(string $string): string
68 {
69 return $this->refinery->string()
70 ->encoding()
71 ->latin1ToUtf8()
72 ->transform($string);
73 }
74
75 public function getFileContent(): string
76 {
78 }
79
80 public function setFileContent(string $file_content): void
81 {
82 $this->file_content = $file_content;
83 }
84
85 abstract public function parseContent(): array;
86
90 public function parseContentToEntries(ilObjBibliographic $bib): array
91 {
92 $this->entry_factory->deleteEntriesById($bib->getId());
93
94 $entries_from_file = $this->parseContent();
95 $entry_instances = [];
96 //fill each entry into a ilBibliographicEntry object and then write it to DB by executing doCreate()
97
98 foreach ($entries_from_file as $file_entry) {
99 $type = null;
100 $x = 0;
101 $parsed_entry = [];
102 foreach ($file_entry as $key => $attribute) {
103 $key = $this->secure($key);
104 if (is_string($attribute)) {
105 $attribute = $this->secure($attribute);
106 }
107 // if the attribute is an array, make a comma separated string out of it
108 if (is_array($attribute)) {
109 $attribute = array_map(fn(string $a): string => $this->secure($a), $attribute);
110 $attribute = implode(", ", $attribute);
111 }
112 // reduce the attribute strings to a maximum of 4000 (ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) characters, in order to fit in the database
113 //if (mb_strlen($attribute, 'UTF-8') > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
114 if (ilStr::strLen($attribute) > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
115 // $attribute = mb_substr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3, 'UTF-8') . '...';
116 $attribute = ilStr::subStr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3) . '...';
117 }
118 // ty (RIS) or entryType (BIB) is the type and is treated seperately
119 if (strtolower($key) === 'ty' || strtolower($key) === 'entrytype') {
120 $type = $attribute;
121 continue;
122 }
123 //TODO - Refactoring for ILIAS 4.5 - get rid off array restructuring
124 //change array structure (name not as the key, but under the key "name")
125 $parsed_entry[$x]['name'] = $key;
126 $parsed_entry[$x]['value'] = $attribute;
127 $x++;
128 }
129
130 if ($type === null) {
131 continue;
132 }
133 //create the entry and fill data into database by executing doCreate()
134 $entry_factory = $this->getEntryFactory();
135 $entry_model = $entry_factory->getEmptyInstance();
136 $entry_model->setType($type);
137 $entry_model->setDataId($bib->getId());
138 $entry_model->store();
139 foreach ($parsed_entry as $entry) {
140 $this->getAttributeFactory()->createAttribute($entry['name'], $entry['value'], $entry_model->getId());
141 }
142 $entry_instances[] = $entry_model;
143 }
144
145 return $entry_instances;
146 }
147
152 {
153 return $this->entry_factory;
154 }
155
160 {
161 return $this->field_factory;
162 }
163
168 {
169 return $this->attribute_factory;
170 }
171}
Builds data types.
Definition: Factory.php:36
Class ilBiblFileReaderBase.
setFileContent(string $file_content)
__construct(protected \ilBiblEntryFactoryInterface $entry_factory, protected \ilBiblFieldFactoryInterface $field_factory, protected \ilBiblAttributeFactoryInterface $attribute_factory)
ilBiblFileReaderBase constructor.
readContent(ResourceIdentification $identification)
parseContentToEntries(ilObjBibliographic $bib)
@inheritDoc
Class ilObjBibliographic.
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:21
static strLen(string $a_string)
Definition: class.ilStr.php:60
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilBiblEntryFactoryInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilBiblFileReaderInterface.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26