ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAdvancedMDParser.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 protected int $obj_id;
28 protected int $rec_id = 0;
30 protected string $cdata = "";
32
36 protected array $value_records = [];
37
39 protected array $record_ids = [];
40
41 // local adv md record support
42 protected ?array $local_record = null;
43 protected array $local_rec_map = [];
44 protected array $local_rec_fields_map = [];
45
49 protected ilLogger $log;
50
51 public function __construct(string $a_obj_id, ilImportMapping $a_mapping)
52 {
54
55 $this->log = ilLoggerFactory::getLogger('amet');
56
57 $parts = explode(":", $a_obj_id);
58 $this->obj_id = (int) $parts[0];
59 $this->mapping = $a_mapping;
60 }
61
62 public function setHandlers($a_xml_parser): void
63 {
64 $this->sax_controller = new ilSaxController();
65 $this->sax_controller->setHandlers($a_xml_parser);
66 $this->sax_controller->setDefaultElementHandler($this);
67 }
68
69 public function createLocalRecord(int $a_old_id, string $a_xml, int $a_obj_id, ?string $a_sub_type = null): void
70 {
71 $tmp_file = ilFileUtils::ilTempnam();
72 file_put_contents($tmp_file, $a_xml);
73
74 // see ilAdvancedMDSettingsGUI::importRecord()
75 $parser = null;
76 try {
77 // the (old) record parser does only support files
78 $parser = new ilAdvancedMDRecordParser($tmp_file);
79 $parser->setContext($a_obj_id, ilObject::_lookupType($a_obj_id), $a_sub_type);
81 $parser->startParsing();
83 $parser->startParsing();
84 } catch (ilSaxParserException $exc) {
85 $this->log->error('Parsing failed with message: ' . $exc->getMessage());
86 return;
87 } finally {
88 unlink($tmp_file);
89 }
90 $map = $parser->getRecordMap();
91 foreach ($map as $record_id => $fields) {
92 $this->local_rec_fields_map[$record_id] = $fields;
93
94 // needed for glossary field order
95 foreach ($fields as $import_id => $new_id) {
96 $import_ids = explode('_', $import_id);
97 $old_id = array_pop($import_ids);
98 $this->mapping->addMapping("components/ILIAS/AdvancedMetaData", "lfld", $old_id, (string) $new_id);
99 }
100 }
101 $map_keys = array_keys($map);
102 $new_id = array_shift($map_keys);
103 $this->local_rec_map[$a_old_id] = $new_id;
104 }
105
106 public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
107 {
108 switch ($a_name) {
109 case 'AdvancedMetaData':
110 break;
111
112 case 'Record':
113 $this->local_record = array('id' => $a_attribs['local_id']);
114 break;
115
116 case 'Value':
117 $this->initValue(
118 (string) $a_attribs['id'],
119 (string) $a_attribs['sub_type'],
120 (int) $a_attribs['sub_id'],
121 isset($a_attribs['local_rec_id']) ? (int) $a_attribs['local_rec_id'] : null
122 );
123 break;
124 }
125 }
126
127 public function handlerEndTag($a_xml_parser, string $a_name): void
128 {
129 switch ($a_name) {
130 case 'AdvancedMetaData':
131 // we need to write all records that have been created (1 for each sub-item)
132 foreach ($this->value_records as $record) {
133 $record->write();
134 }
135 break;
136
137 case 'Record':
138 $this->local_record['xml'] = base64_decode(trim($this->cdata));
139 $this->log->debug("Local Record XML: " . $this->local_record['xml']);
140 break;
141
142 case 'Value':
143 $value = trim($this->cdata);
144 $this->log->debug("End Tag Value: -" . is_object($this->current_value) . "-" . $value);
145 if (is_object($this->current_value) && $value != "") {
146 $this->current_value->importValueFromXML($value);
147 }
148 break;
149 }
150 $this->cdata = '';
151 }
152
153 public function handlerCharacterData($a_xml_parser, string $a_data): void
154 {
155 if ($a_data != "\n") {
156 // Replace multiple tabs with one space
157 $a_data = preg_replace("/\t+/", " ", $a_data);
158
159 $this->cdata .= $a_data;
160 }
161 }
162
163 protected function initValue(
164 string $a_import_id,
165 string $a_sub_type = "",
166 int $a_sub_id = 0,
167 ?int $a_local_rec_id = null
168 ): void {
169 $this->current_value = null;
170
171 // get parent objects
172 $new_parent_id = (int) $this->mapping->getMapping("components/ILIAS/AdvancedMetaData", "parent", (string) $this->obj_id);
173 $this->log->notice('Found new parent id:' . $new_parent_id);
174 if (!$new_parent_id) {
175 return;
176 }
177 $new_sub_id = '';
178 if ($a_sub_type && strcmp($a_sub_type, '-') !== 0) {
179 $new_sub_id = $this->mapping->getMapping(
180 "components/ILIAS/AdvancedMetaData",
181 "advmd_sub_item",
182 "advmd:" . $a_sub_type . ":" . $a_sub_id
183 );
184 if (!$new_sub_id) {
185 return;
186 }
187 }
188
189 // init local record?
190 // done here because we need object context
191 if (is_array($this->local_record)) {
192 $this->createLocalRecord(
193 (int) $this->local_record['id'],
194 $this->local_record['xml'],
195 $new_parent_id,
196 $a_sub_type
197 );
198 $this->local_record = null;
199 }
200
201 $rec_id = null;
202
203 // find record via import id
204 if (!$a_local_rec_id) {
205 if ($field = ilAdvancedMDFieldDefinition::getInstanceByImportId($a_import_id)) {
206 $rec_id = $field->getRecordId();
207 }
208 } // (new) local record
209 else {
210 $rec_id = $this->local_rec_map[$a_local_rec_id];
211 }
212
213 if (!$rec_id) {
214 return;
215 }
216
217 // init record definitions
218 if ($a_sub_type) {
219 $rec_idx = $rec_id . ";" . $a_sub_type . ";" . $new_sub_id;
220 if (!array_key_exists($rec_idx, $this->value_records)) {
221 $this->value_records[$rec_idx] = new ilAdvancedMDValues(
222 $rec_id,
223 $new_parent_id,
224 $a_sub_type,
225 $new_sub_id
226 );
227 }
228 } else {
229 $rec_idx = $rec_id . ";;";
230 if (!array_key_exists($rec_idx, $this->value_records)) {
231 $this->value_records[$rec_idx] = new ilAdvancedMDValues($rec_id, $new_parent_id);
232 }
233 }
234
235 // init ADTGroup before definitions to bind definitions to group
236 $this->value_records[$rec_idx]->getADTGroup();
237
238 // find element with import id
239 $this->log->debug("Find element: " . $a_import_id . ", local rec_id: " . $a_local_rec_id);
240 if (!$a_local_rec_id) {
241 foreach ($this->value_records[$rec_idx]->getDefinitions() as $def) {
242 if ($a_import_id == $def->getImportId()) {
243 $this->current_value = $def;
244 break;
245 }
246 }
247 } else {
248 // find element in new local record
249 $field_id = $this->local_rec_fields_map[$rec_id][$a_import_id];
250 if ($field_id) {
251 $this->log->debug("- Field id: " . $field_id);
252 foreach ($this->value_records[$rec_idx]->getDefinitions() as $def) {
253 $this->log->debug("- Def field id: " . $def->getFieldId());
254 if ($field_id == $def->getFieldId()) {
255 $this->current_value = $def;
256 break;
257 }
258 }
259 } else {
260 $this->log->debug("- No Field id. local rec: " . $a_local_rec_id .
261 ", rec id:" . $rec_id . ", import id: " . $a_import_id . ", map: " . print_r(
262 $this->local_rec_fields_map,
263 true
264 ));
265 }
266 }
267
268 // record will be selected for parent
269 // see ilAdvancedMetaDataImporter
270 if ($this->current_value &&
271 !$a_local_rec_id) {
272 $this->record_ids[$new_parent_id][$a_sub_type][] = $rec_id;
273 }
274 }
275
279 public function getRecordIds(): array
280 {
281 return $this->record_ids;
282 }
283}
static getInstanceByImportId(string $a_import_id)
handlerCharacterData($a_xml_parser, string $a_data)
Character data handler.
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
Start element handler.
createLocalRecord(int $a_old_id, string $a_xml, int $a_obj_id, ?string $a_sub_type=null)
handlerEndTag($a_xml_parser, string $a_name)
End element handler.
ilAdvancedMDFieldDefinition $current_value
initValue(string $a_import_id, string $a_sub_type="", int $a_sub_id=0, ?int $a_local_rec_id=null)
__construct(string $a_obj_id, ilImportMapping $a_mapping)
SAX based XML parser for record import files.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
static _lookupType(int $id, bool $reference=false)
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...
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...
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc