ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilAdvancedMDRecordParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
34 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
35 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
36 
38 {
39  const MODE_UPDATE = 1;
40  const MODE_INSERT = 2;
43 
44  private $mode;
45 
46  private $fields = array();
47 
48  private $is_error = false;
49  private $error_msg = array();
50 
51  protected $context; // [array]
52 
53  protected $scopes = [];
54 
58  protected $log;
59 
67  public function __construct($a_file)
68  {
69  parent::__construct($a_file, true);
70  $this->log = ilLoggerFactory::getLogger('amet');
71  }
72 
80  public function setMode($a_mode)
81  {
82  $this->mode = $a_mode;
83  }
84 
91  public function getMode()
92  {
93  return $this->mode;
94  }
95 
96 
104  public function startParsing()
105  {
106  parent::startParsing();
107  if ($this->is_error) {
108  include_once('./Services/Xml/exceptions/class.ilSaxParserException.php');
109  throw new ilSaxParserException(implode('<br/>', $this->error_msg));
110  }
111  }
112 
119  public function setHandlers($a_xml_parser)
120  {
121  xml_set_object($a_xml_parser, $this);
122  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
123  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
124  }
125 
131  protected function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
132  {
133  switch ($a_name) {
134  case 'AdvancedMetaDataRecords':
135  $this->is_error = false;
136  $this->error_msg = array();
137  // Nothing to do
138  break;
139 
140  case 'Scope':
141  $this->scopes = [];
142  break;
143 
144  case 'ScopeEntry':
145  $parsed_id = ilUtil::parseImportId($a_attribs['id']);
146  if (
147  $parsed_id['inst_id'] == IL_INST_ID &&
148  ilObject::_exists($parsed_id['id'], true, $parsed_id['type'])
149  ) {
150  $scope = new ilAdvancedMDRecordScope();
151  $scope->setRefId($parsed_id['id']);
152  $this->scopes[] = $scope;
153  }
154  break;
155 
156 
157  case 'Record':
158  $this->fields = array();
159  $this->current_field = null;
160  $this->current_record = null;
161  if (!strlen($a_attribs['id']) or !isset($a_attribs['active'])) {
162  $this->appendErrorMessage('Missing XML attribute for element "Record".');
163  }
164  if (!$this->initRecordObject($a_attribs['id'])) {
165  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
166  }
167  $this->getCurrentRecord()->setActive($a_attribs['active']);
168  $this->getCurrentRecord()->setImportId($a_attribs['id']);
169  $this->getCurrentRecord()->setAssignedObjectTypes(array());
170  break;
171 
172  case 'Title':
173  break;
174 
175  case 'Field':
176  if (!strlen($a_attribs['id']) or !isset($a_attribs['searchable']) or !isset($a_attribs['fieldType'])) {
177  $this->appendErrorMessage('Missing XML attribute for element "Field".');
178  }
179  if (!$this->initFieldObject($a_attribs['id'], $a_attribs['fieldType'])) {
180  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
181  }
182  $this->getCurrentField()->setImportId($a_attribs['id']);
183  $this->getCurrentField()->setSearchable($a_attribs['searchable'] == 'Yes' ? true : false);
184  break;
185 
186  case 'FieldTitle':
187  case 'FieldDescription':
188  case 'FieldPosition':
189  case 'FieldValue':
190  $this->field_value_id = $a_attribs['id'];
191  break;
192  }
193  }
194 
200  protected function handlerEndTag($a_xml_parser, $a_name)
201  {
202  switch ($a_name) {
203  case 'AdvancedMetaDataRecords':
204  break;
205 
206  case 'Record':
207  $this->storeRecords();
208  break;
209 
210  case 'Scope':
211  $this->getCurrentRecord()->setScopes($this->scopes);
212  break;
213 
214  case 'Title':
215  $this->getCurrentRecord()->setTitle(trim($this->cdata));
216  break;
217 
218  case 'Description':
219  $this->getCurrentRecord()->setDescription(trim($this->cdata));
220  break;
221 
222  case 'ObjectType':
223  // #12980
224  $parts = explode(":", trim($this->cdata));
225  $this->getCurrentRecord()->appendAssignedObjectType($parts[0], $parts[1]);
226  break;
227 
228  case 'Field':
229  break;
230 
231 
232  case 'FieldTitle':
233  $this->getCurrentField()->setTitle(trim($this->cdata));
234  break;
235 
236  case 'FieldDescription':
237  $this->getCurrentField()->setDescription(trim($this->cdata));
238  break;
239 
240  case 'FieldPosition':
241  $this->getCurrentField()->setPosition((int) trim($this->cdata));
242  break;
243 
244  case 'FieldValue':
245  $this->getCurrentField()->importXMLProperty($this->field_value_id, trim($this->cdata));
246  break;
247  }
248  $this->cdata = '';
249  }
250 
257  protected function handlerCharacterData($a_xml_parser, $a_data)
258  {
259  if ($a_data != "\n") {
260  // Replace multiple tabs with one space
261  $a_data = preg_replace("/\t+/", " ", $a_data);
262 
263  $this->cdata .= $a_data;
264  }
265  }
266 
274  private function initRecordObject($a_id)
275  {
276  switch ($this->getMode()) {
277  case self::MODE_INSERT:
278  case self::MODE_INSERT_VALIDATION:
279  $this->current_record = new ilAdvancedMDRecord(0);
280  return true;
281 
282  default:
283  $this->current_record = ilAdvancedMDRecord::_getInstanceByRecordId($this->extractRecordId($a_id));
284  return true;
285  break;
286  }
287  }
288 
296  private function initFieldObject($a_id, $a_type)
297  {
298  switch ($this->getMode()) {
299  case self::MODE_INSERT:
300  case self::MODE_INSERT_VALIDATION:
302  $this->fields[] = $this->current_field;
303  return true;
304 
305  default:
306  // ??? nonsense
307  $this->current_field = ilAdvancedMDRecord::_getInstanceByFieldId($this->extractFieldId($a_id));
308  return true;
309  break;
310  }
311  }
312 
313 
314 
321  private function getCurrentRecord()
322  {
323  return $this->current_record;
324  }
325 
331  private function getCurrentField()
332  {
333  return $this->current_field;
334  }
335 
343  private function extractRecordId($a_id_string)
344  {
345  // first lookup import id
346  if ($record_id = ilAdvancedMDRecord::_lookupRecordIdByImportId($a_id_string)) {
347  $this->record_exists = true;
348  return $record_id;
349  }
350  return 0;
351  }
352 
353 
354 
362  private function appendErrorMessage($a_msg)
363  {
364  $this->is_error = true;
365  $this->error_msg[] = $a_msg;
366  }
367 
375  private function storeRecords()
376  {
377  switch ($this->getMode()) {
378  case self::MODE_INSERT_VALIDATION:
379  case self::MODE_UPDATE_VALIDATION:
380  return true;
381 
382  case self::MODE_INSERT:
383  // set local context
384  if (is_array($this->context)) {
385  $this->getCurrentRecord()->setParentObject($this->context["obj_id"]);
386  $this->getCurrentRecord()->setAssignedObjectTypes(array(
387  array(
388  "obj_type" => $this->context["obj_type"],
389  "sub_type" => $this->context["sub_type"],
390  "optional" => false
391  )));
392  }
393 
394  $this->getCurrentRecord()->save();
395  break;
396  }
397  foreach ($this->fields as $field) {
398  $field->setRecordId($this->getCurrentRecord()->getRecordId());
399  switch ($this->getMode()) {
400  case self::MODE_INSERT:
401  $field->save();
402 
403  // see getRecordMap()
404  $this->log->debug("add to record map, rec id: " . $this->getCurrentRecord()->getRecordId() .
405  ", import id: " . $field->getImportId() . ", field id:" . $field->getFieldId());
406  $this->rec_map[$this->getCurrentRecord()->getRecordId()][$field->getImportId()] = $field->getFieldId();
407  break;
408  }
409  }
410  }
411 
412  public function setContext($a_obj_id, $a_obj_type, $a_sub_type = null)
413  {
414  if (!$a_sub_type) {
415  $a_sub_type = "-";
416  }
417 
418  $this->context = array(
419  "obj_id" => $a_obj_id,
420  "obj_type" => $a_obj_type,
421  "sub_type" => $a_sub_type
422  );
423  }
424 
425  public function getRecordMap()
426  {
427  return $this->rec_map;
428  }
429 }
Scope restrictions for advanced md records.
setHandlers($a_xml_parser)
set event handlers
static _exists($a_id, $a_reference=false, $a_type=null)
checks if an object exists in object_data
initFieldObject($a_id, $a_type)
Init field definition object.
static parseImportId($a_import_id)
Parse an ilias import id Typically of type il_[IL_INST_ID]_[OBJ_TYPE]_[OBJ_ID] returns array( &#39;orig&#39; ...
SaxParserException thrown by ilSaxParser if property throwException is set.
handlerEndTag($a_xml_parser, $a_name)
Handler for end tags.
startParsing()
stores xml data in array
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
$errors fields
Definition: imgupload.php:51
$a_type
Definition: workflow.php:92
static _getInstanceByRecordId($a_record_id)
Get instance by record id.
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
Handler for start tags.
extractRecordId($a_id_string)
Extract id.
Create styles array
The data for the language used.
initRecordObject($a_id)
Init record object.
setContext($a_obj_id, $a_obj_type, $a_sub_type=null)
getCurrentField()
get current field definition private
static getLogger($a_component_id)
Get component logger.
SAX based XML parser for record import files.
static getInstanceByTypeString($a_type)
Get instance by type string (used by import)
static _lookupRecordIdByImportId($a_ilias_id)
Lookup record Id by import id.