ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilAdvancedMDParser.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("Services/Xml/classes/class.ilSaxParser.php");
5include_once("Services/Utilities/classes/class.ilSaxController.php");
6include_once("Services/Utilities/interfaces/interface.ilSaxSubsetParser.php");
7include_once("Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php");
8include_once("Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php");
9
19{
20 protected $obj_id; // [int]
21 protected $rec_id; // [int]
22 protected $mapping; // [object]
23 protected $cdata; // [string]
24 protected $value_records = array(); // [array]
25 protected $current_record; // [ilAdvancedMDValues]
26 protected $current_value; // [ilAdvancedMDFieldDefinition]
27 protected $has_values; // [bool]
28 protected $record_ids = array(); // [array]
29
30 // local adv md record support
31 protected $local_record; // [array]
32 protected $local_rec_map = array(); // [array]
33 protected $local_rec_fields_map = array(); // [array]
34
38 protected $log;
39
40 public function __construct($a_obj_id, $a_mapping)
41 {
43
44 $this->log = ilLoggerFactory::getLogger('amet');
45
46 $parts = explode(":", $a_obj_id);
47 $this->obj_id = $parts[0];
48 $this->mapping = $a_mapping;
49 }
50
51 public function setHandlers($a_xml_parser)
52 {
53 $this->sax_controller = new ilSaxController();
54 $this->sax_controller->setHandlers($a_xml_parser);
55 $this->sax_controller->setDefaultElementHandler($this);
56 }
57
58 public function createLocalRecord($a_old_id, $a_xml, $a_obj_id, $a_sub_type = null)
59 {
60 $tmp_file = ilUtil::ilTempnam();
61 file_put_contents($tmp_file, $a_xml);
62
63 // see ilAdvancedMDSettingsGUI::importRecord()
64 try {
65 // the (old) record parser does only support files
66 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecordParser.php');
67 $parser = new ilAdvancedMDRecordParser($tmp_file);
68 $parser->setContext($a_obj_id, ilObject::_lookupType($a_obj_id), $a_sub_type);
70 $parser->startParsing();
72 $parser->startParsing();
73 } catch (ilSAXParserException $exc) {
74 }
75
76 unlink($tmp_file);
77
78 $map = $parser->getRecordMap();
79 foreach ($map as $record_id => $fields) {
80 $this->local_rec_fields_map[$record_id] = $fields;
81
82 // needed for glossary field order
83 foreach ($fields as $import_id => $new_id) {
84 $old_id = array_pop(explode("_", $import_id));
85 $this->mapping->addMapping("Services/AdvancedMetaData", "lfld", $old_id, $new_id);
86 }
87 }
88
89 $new_id = array_shift(array_keys($map));
90 $this->local_rec_map[$a_old_id] = $new_id;
91 }
92
93 public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
94 {
95 switch ($a_name) {
96 case 'AdvancedMetaData':
97 break;
98
99 case 'Record':
100 $this->local_record = array('id' => $a_attribs['local_id']);
101 break;
102
103 case 'Value':
104 $this->initValue($a_attribs['id'], $a_attribs['sub_type'], $a_attribs['sub_id'], $a_attribs['local_rec_id']);
105 break;
106 }
107 }
108
109 public function handlerEndTag($a_xml_parser, $a_name)
110 {
111 switch ($a_name) {
112 case 'AdvancedMetaData':
113 // we need to write all records that have been created (1 for each sub-item)
114 foreach ($this->value_records as $record) {
115 $record->write();
116 }
117 break;
118
119 case 'Record':
120 $this->local_record['xml'] = base64_decode(trim($this->cdata));
121 $this->log->debug("Local Record XML: " . $this->local_record['xml']);
122 break;
123
124 case 'Value':
125 $value = trim($this->cdata);
126 $this->log->debug("End Tag Value: -" . is_object($this->current_value) . "-" . $value);
127 if (is_object($this->current_value) && $value != "") {
128 $this->current_value->importValueFromXML($value);
129 }
130 break;
131 }
132 $this->cdata = '';
133 }
134
135 public function handlerCharacterData($a_xml_parser, $a_data)
136 {
137 if ($a_data != "\n") {
138 // Replace multiple tabs with one space
139 $a_data = preg_replace("/\t+/", " ", $a_data);
140
141 $this->cdata .= $a_data;
142 }
143 }
144
145 protected function initValue($a_import_id, $a_sub_type = "", $a_sub_id = 0, $a_local_rec_id = null)
146 {
147 $this->current_value = null;
148
149 // get parent objects
150 $new_parent_id = $this->mapping->getMapping("Services/AdvancedMetaData", "parent", $this->obj_id);
151 $this->log->notice('Found new parent id:' . $new_parent_id);
152 if (!$new_parent_id) {
153 return;
154 }
155 if (
156 $a_sub_type &&
157 strcmp($a_sub_type, '-') !== 0
158 ) {
159 $new_sub_id = $this->mapping->getMapping("Services/AdvancedMetaData", "advmd_sub_item", "advmd:" . $a_sub_type . ":" . $a_sub_id);
160 if (!$new_sub_id) {
161 return;
162 }
163 }
164
165 // init local record?
166 // done here because we need object context
167 if (is_array($this->local_record)) {
168 $this->createLocalRecord($this->local_record['id'], $this->local_record['xml'], $new_parent_id, $a_sub_type);
169 $this->local_record = null;
170 }
171
172 $rec_id = null;
173
174 // find record via import id
175 if (!$a_local_rec_id) {
176 if ($field = ilAdvancedMDFieldDefinition::getInstanceByImportId($a_import_id)) {
177 $rec_id = $field->getRecordId();
178 }
179 }
180 // (new) local record
181 else {
182 $rec_id = $this->local_rec_map[$a_local_rec_id];
183 }
184
185 if (!$rec_id) {
186 return;
187 }
188
189 // init record definitions
190 if ($a_sub_type) {
191 $rec_idx = $rec_id . ";" . $a_sub_type . ";" . $new_sub_id;
192 if (!array_key_exists($rec_idx, $this->value_records)) {
193 $this->value_records[$rec_idx] = new ilAdvancedMDValues($rec_id, $new_parent_id, $a_sub_type, $new_sub_id);
194 }
195 } else {
196 $rec_idx = $rec_id . ";;";
197 if (!array_key_exists($rec_idx, $this->value_records)) {
198 $this->value_records[$rec_idx] = new ilAdvancedMDValues($rec_id, $new_parent_id);
199 }
200 }
201
202 // init ADTGroup before definitions to bind definitions to group
203 $this->value_records[$rec_idx]->getADTGroup();
204
205 // find element with import id
206 $this->log->debug("Find element: " . $a_import_id . ", local rec_id: " . $a_local_rec_id);
207 if (!$a_local_rec_id) {
208 foreach ($this->value_records[$rec_idx]->getDefinitions() as $def) {
209 if ($a_import_id == $def->getImportId()) {
210 $this->current_value = $def;
211 break;
212 }
213 }
214 } else {
215 // find element in new local record
216 $field_id = $this->local_rec_fields_map[$rec_id][$a_import_id];
217 if ($field_id) {
218 $this->log->debug("- Field id: " . $field_id);
219 foreach ($this->value_records[$rec_idx]->getDefinitions() as $def) {
220 $this->log->debug("- Def field id: " . $def->getFieldId());
221 if ($field_id == $def->getFieldId()) {
222 $this->current_value = $def;
223 break;
224 }
225 }
226 } else {
227 $this->log->debug("- No Field id. local rec: " . $a_local_rec_id .
228 ", rec id:" . $rec_id . ", import id: " . $a_import_id . ", map: " . print_r($this->local_rec_fields_map, true));
229 }
230 }
231
232 // record will be selected for parent
233 // see ilAdvancedMetaDataImporter
234 if ($this->current_value &&
235 !$a_local_rec_id) {
236 $this->record_ids[$new_parent_id][$a_sub_type][] = $rec_id;
237 }
238 }
239
240 public function getRecordIds()
241 {
242 return $this->record_ids;
243 }
244}
$parser
Definition: BPMN2Parser.php:23
An exception for terminatinating execution or to throw for unit testing.
static getInstanceByImportId($a_import_id)
Get definition instance by import id.
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
setHandlers($a_xml_parser)
set event handlers
__construct($a_obj_id, $a_mapping)
Constructor setup ILIAS global object @access public.
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
createLocalRecord($a_old_id, $a_xml, $a_obj_id, $a_sub_type=null)
initValue($a_import_id, $a_sub_type="", $a_sub_id=0, $a_local_rec_id=null)
SAX based XML parser for record import files.
static getLogger($a_component_id)
Get component logger.
static _lookupType($a_id, $a_reference=false)
lookup object type
Controller class for sax element handlers.
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
Interface definition for sax subset parsers.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc