ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 function __construct($a_obj_id, $a_mapping)
41 {
42 parent::__construct();
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 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 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 {
66 // the (old) record parser does only support files
67 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecordParser.php');
68 $parser = new ilAdvancedMDRecordParser($tmp_file);
69 $parser->setContext($a_obj_id, ilObject::_lookupType($a_obj_id), $a_sub_type);
71 $parser->startParsing();
73 $parser->startParsing();
74 }
75 catch(ilSAXParserException $exc)
76 {
77
78 }
79
80 unlink($tmp_file);
81
82 $map = $parser->getRecordMap();
83 foreach($map as $record_id => $fields)
84 {
85 $this->local_rec_fields_map[$record_id] = $fields;
86
87 // needed for glossary field order
88 foreach($fields as $import_id => $new_id)
89 {
90 $old_id = array_pop(explode("_", $import_id));
91 $this->mapping->addMapping("Services/AdvancedMetaData", "lfld", $old_id, $new_id);
92 }
93 }
94
95 $new_id = array_shift(array_keys($map));
96 $this->local_rec_map[$a_old_id] = $new_id;
97 }
98
99 function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
100 {
101 switch($a_name)
102 {
103 case 'AdvancedMetaData':
104 break;
105
106 case 'Record':
107 $this->local_record = array('id'=>$a_attribs['local_id']);
108 break;
109
110 case 'Value':
111 $this->initValue($a_attribs['id'], $a_attribs['sub_type'], $a_attribs['sub_id'], $a_attribs['local_rec_id']);
112 break;
113 }
114 }
115
116 function handlerEndTag($a_xml_parser,$a_name)
117 {
118 switch($a_name)
119 {
120 case 'AdvancedMetaData':
121 // we need to write all records that have been created (1 for each sub-item)
122 foreach($this->value_records as $record)
123 {
124 $record->write();
125 }
126 break;
127
128 case 'Record':
129 $this->local_record['xml'] = base64_decode(trim($this->cdata));
130 $this->log->debug("Local Record XML: ".$this->local_record['xml']);
131 break;
132
133 case 'Value':
134 $value = trim($this->cdata);
135 $this->log->debug("End Tag Value: -".is_object($this->current_value)."-".$value);
136 if(is_object($this->current_value) && $value != "")
137 {
138 $this->current_value->importValueFromXML($value);
139 }
140 break;
141 }
142 $this->cdata = '';
143 }
144
145 function handlerCharacterData($a_xml_parser,$a_data)
146 {
147 if($a_data != "\n")
148 {
149 // Replace multiple tabs with one space
150 $a_data = preg_replace("/\t+/"," ",$a_data);
151
152 $this->cdata .= $a_data;
153 }
154 }
155
156 protected function initValue($a_import_id, $a_sub_type = "", $a_sub_id = 0, $a_local_rec_id = null)
157 {
158 $this->current_value = null;
159
160 // get parent objects
161 $new_parent_id = $this->mapping->getMapping("Services/AdvancedMetaData", "parent", $this->obj_id);
162 if(!$new_parent_id)
163 {
164 return;
165 }
166 if($a_sub_type)
167 {
168 $new_sub_id = $this->mapping->getMapping("Services/AdvancedMetaData", "advmd_sub_item", "advmd:".$a_sub_type.":".$a_sub_id);
169 if(!$new_sub_id)
170 {
171 return;
172 }
173 }
174
175 // init local record?
176 // done here because we need object context
177 if(is_array($this->local_record))
178 {
179 $this->createLocalRecord($this->local_record['id'], $this->local_record['xml'], $new_parent_id, $a_sub_type);
180 $this->local_record = null;
181 }
182
183 $rec_id = null;
184
185 // find record via import id
186 if(!$a_local_rec_id)
187 {
189 {
190 $rec_id = $field->getRecordId();
191 }
192 }
193 // (new) local record
194 else
195 {
196 $rec_id = $this->local_rec_map[$a_local_rec_id];
197 }
198
199 if(!$rec_id)
200 {
201 return;
202 }
203
204 // init record definitions
205 if($a_sub_type)
206 {
207 $rec_idx = $rec_id.";".$a_sub_type.";".$new_sub_id;
208 if(!array_key_exists($rec_idx, $this->value_records))
209 {
210 $this->value_records[$rec_idx] = new ilAdvancedMDValues($rec_id, $new_parent_id, $a_sub_type, $new_sub_id);
211 }
212 }
213 else
214 {
215 $rec_idx = $rec_id.";;";
216 if(!array_key_exists($rec_idx, $this->value_records))
217 {
218 $this->value_records[$rec_idx] = new ilAdvancedMDValues($rec_id, $new_parent_id);
219 }
220 }
221
222 // init ADTGroup before definitions to bind definitions to group
223 $this->value_records[$rec_idx]->getADTGroup();
224
225 // find element with import id
226 $this->log->debug("Find element: ".$a_import_id.", local rec_id: ".$a_local_rec_id);
227 if(!$a_local_rec_id)
228 {
229 foreach($this->value_records[$rec_idx]->getDefinitions() as $def)
230 {
231 if($a_import_id == $def->getImportId())
232 {
233 $this->current_value = $def;
234 break;
235 }
236 }
237 }
238 else
239 {
240 // find element in new local record
241 $field_id = $this->local_rec_fields_map[$rec_id][$a_import_id];
242 if($field_id)
243 {
244 $this->log->debug("- Field id: ".$field_id);
245 foreach($this->value_records[$rec_idx]->getDefinitions() as $def)
246 {
247 $this->log->debug("- Def field id: ".$def->getFieldId());
248 if($field_id == $def->getFieldId())
249 {
250 $this->current_value = $def;
251 break;
252 }
253 }
254 }
255 else
256 {
257 $this->log->debug("- No Field id. local rec: ".$a_local_rec_id.
258 ", rec id:".$rec_id.", import id: ".$a_import_id.", map: ".print_r($this->local_rec_fields_map, true));
259 }
260 }
261
262 // record will be selected for parent
263 // see ilAdvancedMetaDataImporter
264 if($this->current_value &&
265 !$a_local_rec_id)
266 {
267 $this->record_ids[$new_parent_id][$a_sub_type][] = $rec_id;
268 }
269 }
270
271 public function getRecordIds()
272 {
273 return $this->record_ids;
274 }
275}
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)
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)
Create a temporary file in an ILIAS writable directory.
Interface definition for sax subset parsers.