ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilBiblFileReaderBase.php
Go to the documentation of this file.
1 <?php
2 
20 
26 {
33  public const ENCODING_UTF_8 = 'UTF-8';
34  public const ENCODING_ASCII = 'ASCII';
35  public const ENCODING_ISO_8859_1 = 'ISO-8859-1';
36  protected string $file_content = '';
37  protected string $path_to_file = '';
38  protected \ilBiblEntryFactoryInterface $entry_factory;
39  protected \ilBiblFieldFactoryInterface $field_factory;
40  protected \ilBiblAttributeFactoryInterface $attribute_factory;
41 
42  protected \ILIAS\ResourceStorage\Services $storage;
43 
44 
48  public function __construct(
49  ilBiblEntryFactoryInterface $entry_factory,
50  ilBiblFieldFactoryInterface $field_factory,
51  ilBiblAttributeFactoryInterface $attribute_factory
52  ) {
53  global $DIC;
54 
55  $this->entry_factory = $entry_factory;
56  $this->field_factory = $field_factory;
57  $this->attribute_factory = $attribute_factory;
58  $this->storage = $DIC["resource_storage"];
59  }
60 
61 
62  public function readContent(ResourceIdentification $identification): bool
63  {
64  $stream = $this->storage->consume()->stream($identification)->getStream();
65  $this->setFileContent($stream->getContents());
66 
67  return true;
68  }
69 
70  protected function convertStringToUTF8(string $string): string
71  {
72  if (!function_exists('mb_detect_encoding') || !function_exists('mb_detect_order')
73  || !function_exists("mb_convert_encoding")
74  ) {
75  return $string;
76  }
77 
78  ob_end_clean();
79 
80  $mb_detect_encoding = mb_detect_encoding($string);
81  mb_detect_order(array(self::ENCODING_UTF_8, self::ENCODING_ISO_8859_1));
82  switch ($mb_detect_encoding) {
83  case self::ENCODING_UTF_8:
84  break;
85  case self::ENCODING_ASCII:
86  $string = utf8_encode(iconv(self::ENCODING_ASCII, 'UTF-8//IGNORE', $string));
87  break;
88  default:
89  $string = mb_convert_encoding($string, self::ENCODING_UTF_8, $mb_detect_encoding);
90  break;
91  }
92 
93  return $string;
94  }
95 
96  public function getFileContent(): string
97  {
98  return $this->file_content;
99  }
100 
101  public function setFileContent(string $file_content): void
102  {
103  $this->file_content = $file_content;
104  }
105 
106  abstract public function parseContent(): array;
107 
111  public function parseContentToEntries(ilObjBibliographic $bib): array
112  {
113  $this->entry_factory->deleteEntriesById($bib->getId());
114 
115  $entries_from_file = $this->parseContent();
116  $entry_instances = [];
117  //fill each entry into a ilBibliographicEntry object and then write it to DB by executing doCreate()
118 
119  foreach ($entries_from_file as $file_entry) {
120  $type = null;
121  $x = 0;
122  $parsed_entry = array();
123  foreach ($file_entry as $key => $attribute) {
124  $key = $this->secure($key);
125  if (is_string($attribute)) {
126  $attribute = $this->secure($attribute);
127  }
128  // if the attribute is an array, make a comma separated string out of it
129  if (is_array($attribute)) {
130  $attribute = array_map(function (string $a): string {
131  return $this->secure($a);
132  }, $attribute);
133  $attribute = implode(", ", $attribute);
134  }
135  // reduce the attribute strings to a maximum of 4000 (ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) characters, in order to fit in the database
136  //if (mb_strlen($attribute, 'UTF-8') > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
137  if (ilStr::strLen($attribute) > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
138  // $attribute = mb_substr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3, 'UTF-8') . '...';
139  $attribute = ilStr::subStr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3) . '...';
140  }
141  // ty (RIS) or entryType (BIB) is the type and is treated seperately
142  if (strtolower($key) === 'ty' || strtolower($key) === 'entrytype') {
143  $type = $attribute;
144  continue;
145  }
146  //TODO - Refactoring for ILIAS 4.5 - get rid off array restructuring
147  //change array structure (name not as the key, but under the key "name")
148  $parsed_entry[$x]['name'] = $key;
149  $parsed_entry[$x]['value'] = $attribute;
150  $x++;
151  }
152 
153  if ($type === null) {
154  continue;
155  }
156  //create the entry and fill data into database by executing doCreate()
157  $entry_factory = $this->getEntryFactory();
158  $entry_model = $entry_factory->getEmptyInstance();
159  $entry_model->setType($type);
160  $entry_model->setDataId($bib->getId());
161  $entry_model->store();
162  foreach ($parsed_entry as $entry) {
163  $this->getAttributeFactory()->createAttribute($entry['name'], $entry['value'], $entry_model->getId());
164  }
165  $entry_instances[] = $entry_model;
166  }
167 
168  return $entry_instances;
169  }
170 
175  {
176  return $this->entry_factory;
177  }
178 
183  {
184  return $this->field_factory;
185  }
186 
191  {
193  }
194 }
Interface ilBiblFileReaderInterface.
$type
static subStr(string $a_str, int $a_start, ?int $a_length=null)
Definition: class.ilStr.php:24
__construct(ilBiblEntryFactoryInterface $entry_factory, ilBiblFieldFactoryInterface $field_factory, ilBiblAttributeFactoryInterface $attribute_factory)
ilBiblFileReaderBase constructor.
Class ilBiblFileReaderBase.
ilBiblAttributeFactoryInterface $attribute_factory
global $DIC
Definition: feed.php:28
static strLen(string $a_string)
Definition: class.ilStr.php:63
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193
setFileContent(string $file_content)
parseContentToEntries(ilObjBibliographic $bib)
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...
readContent(ResourceIdentification $identification)
ILIAS ResourceStorage Services $storage
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
ilBiblEntryFactoryInterface $entry_factory
ilBiblFieldFactoryInterface $field_factory
Class ilObjBibliographic.