ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBiblFileReaderBase.php
Go to the documentation of this file.
1<?php
2
9{
10
17 const ENCODING_UTF_8 = 'UTF-8';
18 const ENCODING_ASCII = 'ASCII';
19 const ENCODING_ISO_8859_1 = 'ISO-8859-1';
23 protected $file_content = '';
27 protected $path_to_file = '';
31 protected $entry_factory;
35 protected $field_factory;
40
41
48 {
49 $this->entry_factory = $entry_factory;
50 $this->field_factory = $field_factory;
51 $this->attribute_factory = $attribute_factory;
52 }
53
54
60 public function readContent($path_to_file)
61 {
62 global $DIC;
66 $filesystem = $DIC["filesystem"];
68 $this->setFileContent($this->convertStringToUTF8($filesystem->storage()->read($path_to_file)));
69
70 return true;
71 }
72
73
79 protected function convertStringToUTF8($string)
80 {
81 if (!function_exists('mb_detect_encoding') || !function_exists('mb_detect_order')
82 || !function_exists("mb_convert_encoding")) {
83 return $string;
84 }
85 ob_end_clean();
86 $mb_detect_encoding = mb_detect_encoding($string);
87 mb_detect_order(array( self::ENCODING_UTF_8, self::ENCODING_ISO_8859_1 ));
88 switch ($mb_detect_encoding) {
90 break;
92 $string = utf8_encode(iconv(self::ENCODING_ASCII, 'UTF-8//IGNORE', $string));
93 break;
94 default:
95 $string = mb_convert_encoding($string, self::ENCODING_UTF_8, $mb_detect_encoding);
96 break;
97 }
98
99 return $string;
100 }
101
102
106 public function getFileContent()
107 {
108 return $this->file_content;
109 }
110
111
116 {
117 $this->file_content = $file_content;
118 }
119
120
124 public function getPathToFile()
125 {
126 return $this->path_to_file;
127 }
128
129
134 {
135 $this->path_to_file = $path_to_file;
136 }
137
138
142 public function parseContentToEntries(ilObjBibliographic $bib)
143 {
144 $entries_from_file = $this->parseContent();
145 $entry_instances = [];
146 //fill each entry into a ilBibliographicEntry object and then write it to DB by executing doCreate()
147 foreach ($entries_from_file as $file_entry) {
148 $type = null;
149 $x = 0;
150 $parsed_entry = array();
151 foreach ($file_entry as $key => $attribute) {
152 // if the attribute is an array, make a comma separated string out of it
153 if (is_array($attribute)) {
154 $attribute = implode(", ", $attribute);
155 }
156 // reduce the attribute strings to a maximum of 4000 (ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) characters, in order to fit in the database
157 //if (mb_strlen($attribute, 'UTF-8') > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
158 if (ilStr::strLen($attribute) > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
159 // $attribute = mb_substr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3, 'UTF-8') . '...';
160 $attribute = ilStr::subStr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3) . '...';
161 }
162 // ty (RIS) or entryType (BIB) is the type and is treated seperately
163 if (strtolower($key) == 'ty' || strtolower($key) == 'entrytype') {
164 $type = $attribute;
165 continue;
166 }
167 //TODO - Refactoring for ILIAS 4.5 - get rid off array restructuring
168 //change array structure (name not as the key, but under the key "name")
169 $parsed_entry[$x]['name'] = $key;
170 $parsed_entry[$x]['value'] = $attribute;
171 $x++;
172 }
176 //create the entry and fill data into database by executing doCreate()
178 $entry_model = $entry_factory->getEmptyInstance();
179 $entry_model->setType($type);
180 $entry_model->setDataId($bib->getId());
181 $entry_model->store();
182 foreach ($parsed_entry as $entry) {
183 $this->getAttributeFactory()->createAttribute($entry['name'], $entry['value'], $entry_model->getId());
184 }
185 //$entry_model->doCreate();
186 $entry_instances[] = $entry_model;
187 }
188
189 return $entry_instances;
190 }
191
192
196 public function getEntryFactory()
197 {
199 }
200
204 public function getFieldFactory()
205 {
207 }
208
209
213 public function getAttributeFactory()
214 {
216 }
217}
An exception for terminatinating execution or to throw for unit testing.
Class ilBiblFileReaderBase.
__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
$x
Definition: complexTest.php:9
$key
Definition: croninfo.php:18
Interface ilBiblAttributeFactoryInterface.
Interface ilBiblEntryFactoryInterface.
Interface ilBiblFieldFactoryInterface.
Interface ilBiblFileReaderInterface.
readContent($path_to_file)
$type
global $DIC
Definition: saml.php:7