ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilBiblFileReaderBase.php
Go to the documentation of this file.
1<?php
2
25{
26 use ilBibliographicSecureString;
27
34 const ENCODING_UTF_8 = 'UTF-8';
35 const ENCODING_ASCII = 'ASCII';
36 const ENCODING_ISO_8859_1 = 'ISO-8859-1';
40 protected $file_content = '';
44 protected $path_to_file = '';
48 protected $entry_factory;
52 protected $field_factory;
57
63 public function __construct(
67 ) {
68 $this->entry_factory = $entry_factory;
69 $this->field_factory = $field_factory;
70 $this->attribute_factory = $attribute_factory;
71 }
72
78 public function readContent($path_to_file)
79 {
80 global $DIC;
84 $filesystem = $DIC["filesystem"];
86 $this->setFileContent($this->convertStringToUTF8($filesystem->storage()->read($path_to_file)));
87
88 return true;
89 }
90
96 protected function convertStringToUTF8($string)
97 {
98 if (!function_exists('mb_detect_encoding') || !function_exists('mb_detect_order')
99 || !function_exists("mb_convert_encoding")
100 ) {
101 return $string;
102 }
103 ob_end_clean();
104 $mb_detect_encoding = mb_detect_encoding($string);
105 mb_detect_order(array(self::ENCODING_UTF_8, self::ENCODING_ISO_8859_1));
106 switch ($mb_detect_encoding) {
108 break;
110 $string = utf8_encode(iconv(self::ENCODING_ASCII, 'UTF-8//IGNORE', $string));
111 break;
112 default:
113 $string = mb_convert_encoding($string, self::ENCODING_UTF_8, $mb_detect_encoding);
114 break;
115 }
116
117 return $string;
118 }
119
123 public function getFileContent()
124 {
125 return $this->file_content;
126 }
127
132 {
133 $this->file_content = $file_content;
134 }
135
139 public function getPathToFile()
140 {
141 return $this->path_to_file;
142 }
143
148 {
149 $this->path_to_file = $path_to_file;
150 }
151
155 public function parseContentToEntries(ilObjBibliographic $bib)
156 {
157 $entries_from_file = $this->parseContent();
158 $entry_instances = [];
159 //fill each entry into a ilBibliographicEntry object and then write it to DB by executing doCreate()
160 foreach ($entries_from_file as $file_entry) {
161 $type = null;
162 $x = 0;
163 $parsed_entry = array();
164 foreach ($file_entry as $key => $attribute) {
165 $key = $this->secure($key);
166 if (is_string($attribute)) {
167 $attribute = $this->secure($attribute);
168 }
169 // if the attribute is an array, make a comma separated string out of it
170 if (is_array($attribute)) {
171 $attribute = array_map(function (string $a) : string {
172 return $this->secure($a);
173 }, $attribute);
174 $attribute = implode(", ", $attribute);
175 }
176 // reduce the attribute strings to a maximum of 4000 (ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) characters, in order to fit in the database
177 //if (mb_strlen($attribute, 'UTF-8') > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
178 if (ilStr::strLen($attribute) > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
179 // $attribute = mb_substr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3, 'UTF-8') . '...';
180 $attribute = ilStr::subStr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3) . '...';
181 }
182 // ty (RIS) or entryType (BIB) is the type and is treated seperately
183 if (strtolower($key) == 'ty' || strtolower($key) == 'entrytype') {
184 $type = $attribute;
185 continue;
186 }
187 //TODO - Refactoring for ILIAS 4.5 - get rid off array restructuring
188 //change array structure (name not as the key, but under the key "name")
189 $parsed_entry[$x]['name'] = $key;
190 $parsed_entry[$x]['value'] = $attribute;
191 $x++;
192 }
196 //create the entry and fill data into database by executing doCreate()
198 $entry_model = $entry_factory->getEmptyInstance();
199 $entry_model->setType($type);
200 $entry_model->setDataId($bib->getId());
201 $entry_model->store();
202 foreach ($parsed_entry as $entry) {
203 $this->getAttributeFactory()->createAttribute($entry['name'], $entry['value'], $entry_model->getId());
204 }
205 //$entry_model->doCreate();
206 $entry_instances[] = $entry_model;
207 }
208
209 return $entry_instances;
210 }
211
215 public function getEntryFactory()
216 {
218 }
219
223 public function getFieldFactory()
224 {
226 }
227
231 public function getAttributeFactory()
232 {
234 }
235}
An exception for terminatinating execution or to throw for unit testing.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilBiblEntryFactoryInterface $entry_factory, ilBiblFieldFactoryInterface $field_factory, ilBiblAttributeFactoryInterface $attribute_factory)
ilBiblFileReaderBase constructor.
getEntryFactory()
ilBiblEntryFactoryInterface
getFieldFactory()
ilBiblFieldFactoryInterface
Class ilObjBibliographic.
getId()
get object id @access public
static subStr($a_str, $a_start, $a_length=null)
Definition: class.ilStr.php:15
static strLen($a_string)
Definition: class.ilStr.php:78
global $DIC
Definition: goto.php:24
Interface ilBiblAttributeFactoryInterface.
Interface ilBiblEntryFactoryInterface.
Interface ilBiblFieldFactoryInterface.
Interface ilBiblFileReaderInterface.
readContent($path_to_file)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$type