ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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  private \ILIAS\Refinery\Factory $refinery;
39  protected \ilBiblEntryFactoryInterface $entry_factory;
40  protected \ilBiblFieldFactoryInterface $field_factory;
41  protected \ilBiblAttributeFactoryInterface $attribute_factory;
42 
43  protected \ILIAS\ResourceStorage\Services $storage;
44 
45 
49  public function __construct(
50  ilBiblEntryFactoryInterface $entry_factory,
51  ilBiblFieldFactoryInterface $field_factory,
52  ilBiblAttributeFactoryInterface $attribute_factory
53  ) {
54  global $DIC;
55 
56  $this->entry_factory = $entry_factory;
57  $this->refinery = $DIC->refinery();
58  $this->field_factory = $field_factory;
59  $this->attribute_factory = $attribute_factory;
60  $this->storage = $DIC["resource_storage"];
61  }
62 
63 
64  public function readContent(ResourceIdentification $identification): bool
65  {
66  $stream = $this->storage->consume()->stream($identification)->getStream();
67  $this->setFileContent($stream->getContents());
68 
69  return true;
70  }
71 
72  protected function convertStringToUTF8(string $string): string
73  {
74  return $this->refinery->string()
75  ->encoding()
76  ->latin1ToUtf8()
77  ->transform($string);
78  }
79 
80  public function getFileContent(): string
81  {
82  return $this->file_content;
83  }
84 
85  public function setFileContent(string $file_content): void
86  {
87  $this->file_content = $file_content;
88  }
89 
90  abstract public function parseContent(): array;
91 
95  public function parseContentToEntries(ilObjBibliographic $bib): array
96  {
97  $this->entry_factory->deleteEntriesById($bib->getId());
98 
99  $entries_from_file = $this->parseContent();
100  $entry_instances = [];
101  //fill each entry into a ilBibliographicEntry object and then write it to DB by executing doCreate()
102 
103  foreach ($entries_from_file as $file_entry) {
104  $type = null;
105  $x = 0;
106  $parsed_entry = array();
107  foreach ($file_entry as $key => $attribute) {
108  $key = $this->secure($key);
109  if (is_string($attribute)) {
110  $attribute = $this->secure($attribute);
111  }
112  // if the attribute is an array, make a comma separated string out of it
113  if (is_array($attribute)) {
114  $attribute = array_map(function (string $a): string {
115  return $this->secure($a);
116  }, $attribute);
117  $attribute = implode(", ", $attribute);
118  }
119  // reduce the attribute strings to a maximum of 4000 (ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) characters, in order to fit in the database
120  //if (mb_strlen($attribute, 'UTF-8') > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
121  if (ilStr::strLen($attribute) > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
122  // $attribute = mb_substr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3, 'UTF-8') . '...';
123  $attribute = ilStr::subStr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3) . '...';
124  }
125  // ty (RIS) or entryType (BIB) is the type and is treated seperately
126  if (strtolower($key) === 'ty' || strtolower($key) === 'entrytype') {
127  $type = $attribute;
128  continue;
129  }
130  //TODO - Refactoring for ILIAS 4.5 - get rid off array restructuring
131  //change array structure (name not as the key, but under the key "name")
132  $parsed_entry[$x]['name'] = $key;
133  $parsed_entry[$x]['value'] = $attribute;
134  $x++;
135  }
136 
137  if ($type === null) {
138  continue;
139  }
140  //create the entry and fill data into database by executing doCreate()
141  $entry_factory = $this->getEntryFactory();
142  $entry_model = $entry_factory->getEmptyInstance();
143  $entry_model->setType($type);
144  $entry_model->setDataId($bib->getId());
145  $entry_model->store();
146  foreach ($parsed_entry as $entry) {
147  $this->getAttributeFactory()->createAttribute($entry['name'], $entry['value'], $entry_model->getId());
148  }
149  $entry_instances[] = $entry_model;
150  }
151 
152  return $entry_instances;
153  }
154 
159  {
160  return $this->entry_factory;
161  }
162 
167  {
168  return $this->field_factory;
169  }
170 
175  {
177  }
178 }
Interface ilBiblFileReaderInterface.
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
static strLen(string $a_string)
Definition: class.ilStr.php:63
ILIAS Refinery Factory $refinery
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:25
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.