ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ilFileXMLParser Class Reference
+ Inheritance diagram for ilFileXMLParser:
+ Collaboration diagram for ilFileXMLParser:

Public Member Functions

 __construct ($file, $a_xml_data, $obj_id=-1, $mode=0)
 Constructor. More...
 
 setImportDirectory ($a_val)
 Set import directory. More...
 
 getImportDirectory ()
 Get import directory. More...
 
 setHandlers ($a_xml_parser)
 set event handlers More...
 
 handlerBeginTag ($a_xml_parser, $a_name, $a_attribs)
 handler for begin of element More...
 
 handlerEndTag ($a_xml_parser, $a_name)
 handler for end of element More...
 
 handlerCharacterData ($a_xml_parser, $a_data)
 handler for character data More...
 
 setFileContents ()
 update file according to filename and version, does not update history has to be called after (!) file save for new objects, since file storage will be initialised with obj id. More...
 
 updateFileContents ()
 update file according to filename and version and create history entry has to be called after (!) file save for new objects, since file storage will be initialised with obj id. More...
 
 start ()
 starts parsing an changes object by side effect. More...
 
- Public Member Functions inherited from ilSaxParser
 __construct ($a_xml_file='', $throwException=false)
 Constructor setup ILIAS global object public. More...
 
 setXMLContent ($a_xml_content)
 
 getXMLContent ()
 
 getInputType ()
 
 startParsing ()
 stores xml data in array More...
 
 createParser ()
 create parser More...
 
 setOptions ($a_xml_parser)
 set parser options More...
 
 setHandlers ($a_xml_parser)
 set event handler should be overwritten by inherited class private More...
 
 openXMLFile ()
 open xml file More...
 
 parse ($a_xml_parser, $a_fp=null)
 parse xml file More...
 
 freeParser ($a_xml_parser)
 free xml parser handle More...
 
 setThrowException ($throwException)
 set error handling More...
 
- Public Member Functions inherited from PEAR
 __construct ($error_class=null)
 Constructor. More...
 
 _PEAR ()
 Destructor (the emulated type of...). More...
 
 registerShutdownFunc ($func, $args=array())
 Use this function to register a shutdown method for static classes. More...
 
 isError ($data, $code=null)
 Tell whether a value is a PEAR error. More...
 
 expectError ($code=' *')
 This method is used to tell which errors you expect to get. More...
 
 popExpect ()
 This method pops one element off the expected error codes stack. More...
 
 _checkDelExpect ($error_code)
 This method checks unsets an error code if available. More...
 
 delExpect ($error_code)
 This method deletes all occurences of the specified element from the expected error codes stack. More...
 
raiseError ($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
 This method is a wrapper that returns an instance of the configured error class with this object's default error handling applied. More...
 
throwError ($message=null, $code=null, $userinfo=null)
 Simpler form of raiseError with fewer options. More...
 
 staticPushErrorHandling ($mode, $options=null)
 
 staticPopErrorHandling ()
 
 pushErrorHandling ($mode, $options=null)
 Push a new error handler on top of the error handler options stack. More...
 
 popErrorHandling ()
 Pop the last error handler used. More...
 
 loadExtension ($ext)
 OS independant PHP extension load. More...
 

Static Public Member Functions

static normalizeRelativePath ($path)
 Normalize relative directories in a path. More...
 
- Static Public Member Functions inherited from PEAR
static & getStaticProperty ($class, $var)
 If you have a class that's mostly/entirely static, and you need static properties, you can use this method to simulate them. More...
 
static setErrorHandling ($mode=null, $options=null)
 Sets how errors generated by this object should be handled. More...
 

Data Fields

 $file
 
 $obj_id
 
 $result
 
 $mode
 
 $tmpFilename
 
- Data Fields inherited from ilSaxParser
 $input_type = null
 
 $xml_content = ''
 
 $ilias
 
 $lng
 
 $xml_file
 
 $throwException = false
 
- Data Fields inherited from PEAR
 $_debug = false
 
 $_default_error_mode = null
 
 $_default_error_options = null
 
 $_default_error_handler = ''
 
 $_error_class = 'PEAR_Error'
 
 $_expected_errors = array()
 

Static Public Attributes

static $CONTENT_NOT_COMPRESSED = 0
 
static $CONTENT_GZ_COMPRESSED = 1
 
static $CONTENT_ZLIB_COMPRESSED = 2
 
static $CONTENT_COPY = 4
 
static $CONTENT_REST = 5
 

Protected Attributes

 $version = null
 
 $action = null
 
 $max_version = null
 
 $date = null
 
 $usr_id = null
 
 $versions = []
 

Additional Inherited Members

- Protected Member Functions inherited from ilSaxParser
 handleError ($message, $code)
 use given error handler to handle error message or internal ilias error message handle More...
 

Detailed Description

Definition at line 22 of file class.ilFileXMLParser.php.

Constructor & Destructor Documentation

◆ __construct()

ilFileXMLParser::__construct (   $file,
  $a_xml_data,
  $obj_id = -1,
  $mode = 0 
)

Constructor.

Parameters
ilObjFile$fileexisting file object
string$a_xml_filexml data
int$obj_idobj id of exercise which is to be updated

public

Definition at line 111 of file class.ilFileXMLParser.php.

References $file, $mode, $obj_id, ILIAS\GlobalScreen\Provider\__construct(), and ilSaxParser\setXMLContent().

112  {
114  $this->file = $file;
115  $this->setXMLContent($a_xml_data);
116  $this->obj_id = $obj_id;
117  $this->result = false;
118  $this->mode = $mode;
119  }
__construct(Container $dic, ilPlugin $plugin)
setXMLContent($a_xml_content)
+ Here is the call graph for this function:

Member Function Documentation

◆ getImportDirectory()

ilFileXMLParser::getImportDirectory ( )

Get import directory.

Returns
string import directory

Definition at line 138 of file class.ilFileXMLParser.php.

Referenced by handlerEndTag().

139  {
140  return $this->importDirectory;
141  }
+ Here is the caller graph for this function:

◆ handlerBeginTag()

ilFileXMLParser::handlerBeginTag (   $a_xml_parser,
  $a_name,
  $a_attribs 
)

handler for begin of element

Parameters
resource$a_xml_parserxml parser
string$a_nameelement name
array$a_attribselement attributes array
Exceptions
ilFileExceptionwhen obj id != - 1 and if it it does not match the id in the xml or deflation mode is not supported

Definition at line 169 of file class.ilFileXMLParser.php.

References $CONTENT_COPY, $CONTENT_GZ_COMPRESSED, $CONTENT_NOT_COMPRESSED, $CONTENT_REST, $CONTENT_ZLIB_COMPRESSED, $date, $DIC, ilFileException\$ID_DEFLATE_METHOD_MISMATCH, ilFileException\$ID_MISMATCH, $ilErr, $usr_id, ilUtil\__extractId(), IL_INST_ID, and ilUtil\ilTempnam().

170  {
171  global $DIC;
172  $ilErr = $DIC['ilErr'];
173 
174  global $DIC;
175  $ilLog = $DIC['ilLog'];
176 
177  switch ($a_name) {
178  case 'File':
179  if (isset($a_attribs["obj_id"])) {
180  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
181  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id) {
182  throw new ilFileException(
183  "Object IDs (xml $read_obj_id and argument " . $this->obj_id . ") do not match!",
185  );
186  }
187  }
188  if (isset($a_attribs["type"])) {
189  $this->file->setFileType($a_attribs["type"]);
190  }
191  $this->file->setVersion($a_attribs["version"]); // Selected version
192  $this->file->setMaxVersion($a_attribs["max_version"]);
193  break;
194  case 'Content': // Old import files
195  case 'Version':
196  if ($a_name === "Version" && !isset($a_attribs["mode"])) {
197  // Old import files
198  $this->version = null;
199  if ($this->date === null) {
200  // Version tag comes after Content tag. Take only first (= Should be latest)
201  $this->date = $a_attribs["date"];
202  $this->usr_id = $a_attribs["usr_id"];
203  $this->versions[0]["date"] = $this->date;
204  $this->versions[0]["usr_id"] = $this->usr_id;
205  }
206  break;
207  }
208 
210  $this->isReadingFile = true;
211  $this->tmpFilename = ilUtil::ilTempnam();
212  #echo $a_attribs["mode"];
213  if (isset($a_attribs["mode"])) {
214  if ($a_attribs["mode"] == "GZIP") {
215  if (!function_exists("gzread")) {
216  throw new ilFileException("Deflating with gzip is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
217  }
218 
220  } elseif ($a_attribs["mode"] == "ZLIB") {
221  if (!function_exists("gzuncompress")) {
222  throw new ilFileException("Deflating with zlib (compress/uncompress) is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
223  }
224 
226  } elseif ($a_attribs["mode"] == "COPY") {
227  $this->mode = ilFileXMLParser::$CONTENT_COPY;
228  } // begin-patch fm
229  elseif ($a_attribs['mode'] == 'REST') {
230  $this->mode = ilFileXMLParser::$CONTENT_REST;
231  }
232  // end-patch fm
233  }
234 
235  if ($a_name === "Version") {
236  $this->version = $a_attribs["version"];
237  $this->max_version = $a_attribs["max_version"];
238  $this->date = $a_attribs["date"];
239  $this->usr_id = $a_attribs["usr_id"];
240  $this->action = $a_attribs["action"];
241  } else {
242  // Old import files
243  //$this->version = $this->file->getVersion();
244  $this->version = 1;
245  $this->file->setVersion($this->version);
246  }
247  }
248  }
const IL_INST_ID
Definition: constants.php:38
$ilErr
Definition: raiseError.php:18
global $DIC
Definition: goto.php:24
Class to report exception.
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static __extractId($ilias_id, $inst_id)
extract ref id from role title, e.g.
+ Here is the call graph for this function:

◆ handlerCharacterData()

ilFileXMLParser::handlerCharacterData (   $a_xml_parser,
  $a_data 
)

handler for character data

Parameters
resource$a_xml_parserxml parser
string$a_datacharacter data

Definition at line 371 of file class.ilFileXMLParser.php.

References $CONTENT_COPY, and $CONTENT_REST.

372  {
373  if ($a_data != "\n") {
374  // begin-patch fm
375  if ($this->isReadingFile && $this->mode != ilFileXMLParser::$CONTENT_COPY
376  && $this->mode != ilFileXMLParser::$CONTENT_REST
377  ) { // begin-patch fm
378  $handle = fopen($this->tmpFilename, "a");
379  fwrite($handle, $a_data);
380  fclose($handle);
381  } else {
382  $this->cdata .= $a_data;
383  }
384  }
385  }

◆ handlerEndTag()

ilFileXMLParser::handlerEndTag (   $a_xml_parser,
  $a_name 
)

handler for end of element

Parameters
resource$a_xml_parserxml parser
string$a_nameelement name

Definition at line 257 of file class.ilFileXMLParser.php.

References $action, $CONTENT_COPY, $CONTENT_GZ_COMPRESSED, $CONTENT_REST, $CONTENT_ZLIB_COMPRESSED, $date, ilFileException\$DECOMPRESSION_FAILED, $DIC, $GLOBALS, $max_version, $tmpFilename, $usr_id, $version, ilFileUtils\_lookupMimeType(), ilFileUtils\fastBase64Decode(), ilFileUtils\fastGunzip(), getImportDirectory(), and ilUtil\ilTempnam().

258  {
259  $this->cdata = trim($this->cdata);
260 
261  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': ' . $this->cdata);
262 
263  switch ($a_name) {
264  case 'File':
265  $this->result = true;
266  break;
267  case 'Filename':
268  if (strlen($this->cdata) == 0) {
269  throw new ilFileException("Filename ist missing!");
270  }
271 
272  $this->file->setFilename($this->cdata);
273  $this->file->setTitle($this->cdata);
274 
275  break;
276  case 'Title':
277  $this->file->setTitle(trim($this->cdata));
278  break;
279  case 'Description':
280  $this->file->setDescription(trim($this->cdata));
281  break;
282  case 'Rating':
283  $this->file->setRating((bool) $this->cdata);
284  break;
285  case 'Content': // Old import files
286  case 'Version':
287  if ($a_name === "Version" && $this->version === null) {
288  // Old import files
289  break;
290  }
291 
292  $GLOBALS['DIC']['ilLog']->write($this->mode);
293  $this->isReadingFile = false;
294  $baseDecodedFilename = ilUtil::ilTempnam();
295  if ($this->mode == ilFileXMLParser::$CONTENT_COPY) {
296  $this->tmpFilename = $this->getImportDirectory() . "/" . self::normalizeRelativePath($this->cdata);
297  } // begin-patch fm
298  elseif ($this->mode == ilFileXMLParser::$CONTENT_REST) {
299  include_once './Services/WebServices/Rest/classes/class.ilRestFileStorage.php';
300  $storage = new ilRestFileStorage();
301  $this->tmpFilename = $storage->getStoredFilePath(self::normalizeRelativePath($this->cdata));
302  if (!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename)) {
303  throw new ilFileException("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
304  }
305  $this->tmpFilename = $baseDecodedFilename;
306  } // end-patch fm
307  else {
308  if (!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename)) {
309  throw new ilFileException("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
310  }
311  if ($this->mode == ilFileXMLParser::$CONTENT_GZ_COMPRESSED) {
312  if (!ilFileUtils::fastGunzip($baseDecodedFilename, $this->tmpFilename)) {
313  throw new ilFileException("Deflating with fastzunzip failed", ilFileException::$DECOMPRESSION_FAILED);
314  }
315  unlink($baseDecodedFilename);
316  } elseif ($this->mode == ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED) {
317  if (!ilFileUtils::fastGunzip($baseDecodedFilename, $this->tmpFilename)) {
318  throw new ilFileException("Deflating with fastDecompress failed", ilFileException::$DECOMPRESSION_FAILED);
319  }
320  unlink($baseDecodedFilename);
321  } else {
322  $this->tmpFilename = $baseDecodedFilename;
323  }
324  }
325 
326  //$this->content = $content;
327  // see #17211
328 
329  if ($this->version == $this->file->getVersion()) {
330  if (is_file($this->tmpFilename)) {
331  $this->file->setFileSize(filesize($this->tmpFilename)); // strlen($this->content));
332  }
333 
334  // if no file type is given => lookup mime type
335  if (!$this->file->getFileType()) {
336  global $DIC;
337  $ilLog = $DIC['ilLog'];
338 
339  #$ilLog->write(__METHOD__.': Trying to detect mime type...');
340  include_once('./Services/Utilities/classes/class.ilFileUtils.php');
341  $this->file->setFileType(ilFileUtils::_lookupMimeType($this->tmpFilename));
342  }
343  }
344 
345  $this->versions[] = [
346  "version" => $this->version,
347  "max_version" => $this->max_version,
348  "tmpFilename" => $this->tmpFilename,
349  "date" => $this->date,
350  "usr_id" => $this->usr_id,
351  "action" => $this->action,
352  ];
353  $this->version = null;
354  $this->date = null;
355  $this->usr_id = null;
356  break;
357  }
358 
359  $this->cdata = '';
360 
361  return;
362  }
static _lookupMimeType($a_file)
global $DIC
Definition: goto.php:24
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
Class to report exception.
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
File storage handling.
static fastBase64Decode($filein, $fileout)
decodes base encoded file row by row to prevent memory exhaust
fastGunzip($in, $out)
fast uncompressing the file with the zlib-extension without memory consumption
getImportDirectory()
Get import directory.
+ Here is the call graph for this function:

◆ normalizeRelativePath()

static ilFileXMLParser::normalizeRelativePath (   $path)
static

Normalize relative directories in a path.

Source: https://github.com/thephpleague/flysystem/blob/master/src/Util.php#L96 Workaround until we have

Parameters
string$path
Returns
string
Exceptions
LogicException

Definition at line 474 of file class.ilFileXMLParser.php.

475  {
476  $path = str_replace('\\', '/', $path);
477 
478  while (preg_match('#\p{C}+|^\./#u', $path)) {
479  $path = preg_replace('#\p{C}+|^\./#u', '', $path);
480  }
481 
482  $parts = [];
483  foreach (explode('/', $path) as $part) {
484  switch ($part) {
485  case '':
486  case '.':
487  break;
488  case '..':
489  array_pop($parts);
490  break;
491  default:
492  $parts[] = $part;
493  break;
494  }
495  }
496 
497  return implode('/', $parts);
498  }

◆ setFileContents()

ilFileXMLParser::setFileContents ( )

update file according to filename and version, does not update history has to be called after (!) file save for new objects, since file storage will be initialised with obj id.

Definition at line 393 of file class.ilFileXMLParser.php.

References $version, ilHistory\_removeEntriesForObject(), and ilLoggerFactory\getLogger().

Referenced by updateFileContents().

394  {
395  // Delete exists version 1 history
396  ilHistory::_removeEntriesForObject($this->file->getId());
397 
398  foreach ($this->versions as $version) {
399  if (!file_exists($version["tmpFilename"])) {
400  if (!isset($version["tmpFilename"])) {
401  continue;
402  }
403  // try to get first file of directory
404  $files = scandir(dirname($version["tmpFilename"]));
405  $version["tmpFilename"] = rtrim(dirname($version["tmpFilename"]),
406  "/") . "/" . $files[2];// because [0] = "." [1] = ".."
407  if (!file_exists($version["tmpFilename"])) {
408  ilLoggerFactory::getLogger('file')->error(__METHOD__ . ' "' . ($version["tmpFilename"]) . '" file not found.');
409 
410  continue;
411  }
412  }
413 
414  if (filesize($version["tmpFilename"]) == 0) {
415  continue;
416  }
417 
418  // imported file version
419  $import_file_version_path = $version["tmpFilename"];
420 
421  $stream = Streams::ofResource(fopen($import_file_version_path, 'rb'));
422  $this->file->appendStream($stream, $this->file->getTitle());
423  }
424  }
static _removeEntriesForObject($a_obj_id)
remove all history entries for an object
static getLogger($a_component_id)
Get component logger.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setHandlers()

ilFileXMLParser::setHandlers (   $a_xml_parser)

set event handlers

Parameters
resourcereference to the xml parser

private

Definition at line 151 of file class.ilFileXMLParser.php.

152  {
153  xml_set_object($a_xml_parser, $this);
154  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
155  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
156  }

◆ setImportDirectory()

ilFileXMLParser::setImportDirectory (   $a_val)

Set import directory.

Parameters
stringimport directory

Definition at line 127 of file class.ilFileXMLParser.php.

128  {
129  $this->importDirectory = $a_val;
130  }

◆ start()

ilFileXMLParser::start ( )

starts parsing an changes object by side effect.

Returns
boolean true, if no errors happend.
Exceptions
ilFileExceptionwhen obj id != - 1 and if it it does not match the id in the xml

Definition at line 454 of file class.ilFileXMLParser.php.

References ilSaxParser\startParsing().

455  {
456  $this->startParsing();
457 
458  return $this->result > 0;
459  }
startParsing()
stores xml data in array
+ Here is the call graph for this function:

◆ updateFileContents()

ilFileXMLParser::updateFileContents ( )

update file according to filename and version and create history entry has to be called after (!) file save for new objects, since file storage will be initialised with obj id.

Definition at line 432 of file class.ilFileXMLParser.php.

References ilHistory\_createEntry(), and setFileContents().

433  {
434  if ($this->setFileContents()) {
435 
436  if ($this->file->getAction() != "" and $this->file->getAction() != null) {
437  ilHistory::_createEntry($this->file->getId(), $this->file->getAction(), $this->file->getFilename() . "," . $this->file->getVersion() . "," . $this->file->getMaxVersion());
438  } else {
439  ilHistory::_createEntry($this->file->getId(), "replace", $this->file->getFilename() . "," . $this->file->getVersion() . "," . $this->file->getMaxVersion());
440  }
441 
442  $this->file->notifyUpdate($this->file->getId(), $this->file->getDescription());
443  }
444  }
static _createEntry( $a_obj_id, $a_action, $a_info_params="", $a_obj_type="", $a_user_comment="", $a_update_last=false)
Creates a new history entry for an object.
setFileContents()
update file according to filename and version, does not update history has to be called after (!) fil...
+ Here is the call graph for this function:

Field Documentation

◆ $action

ilFileXMLParser::$action = null
protected

Definition at line 83 of file class.ilFileXMLParser.php.

Referenced by handlerEndTag().

◆ $CONTENT_COPY

ilFileXMLParser::$CONTENT_COPY = 4
static

Definition at line 27 of file class.ilFileXMLParser.php.

Referenced by handlerBeginTag(), handlerCharacterData(), and handlerEndTag().

◆ $CONTENT_GZ_COMPRESSED

ilFileXMLParser::$CONTENT_GZ_COMPRESSED = 1
static

Definition at line 25 of file class.ilFileXMLParser.php.

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $CONTENT_NOT_COMPRESSED

ilFileXMLParser::$CONTENT_NOT_COMPRESSED = 0
static

Definition at line 24 of file class.ilFileXMLParser.php.

Referenced by handlerBeginTag().

◆ $CONTENT_REST

ilFileXMLParser::$CONTENT_REST = 5
static

Definition at line 29 of file class.ilFileXMLParser.php.

Referenced by handlerBeginTag(), handlerCharacterData(), and handlerEndTag().

◆ $CONTENT_ZLIB_COMPRESSED

ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED = 2
static

Definition at line 26 of file class.ilFileXMLParser.php.

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $date

ilFileXMLParser::$date = null
protected

Definition at line 91 of file class.ilFileXMLParser.php.

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $file

ilFileXMLParser::$file

Definition at line 36 of file class.ilFileXMLParser.php.

Referenced by __construct().

◆ $max_version

ilFileXMLParser::$max_version = null
protected

Definition at line 87 of file class.ilFileXMLParser.php.

Referenced by handlerEndTag().

◆ $mode

ilFileXMLParser::$mode

Definition at line 55 of file class.ilFileXMLParser.php.

Referenced by __construct().

◆ $obj_id

ilFileXMLParser::$obj_id

Definition at line 43 of file class.ilFileXMLParser.php.

Referenced by __construct().

◆ $result

ilFileXMLParser::$result

Definition at line 49 of file class.ilFileXMLParser.php.

◆ $tmpFilename

ilFileXMLParser::$tmpFilename

Definition at line 68 of file class.ilFileXMLParser.php.

Referenced by handlerEndTag().

◆ $usr_id

ilFileXMLParser::$usr_id = null
protected

Definition at line 95 of file class.ilFileXMLParser.php.

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $version

ilFileXMLParser::$version = null
protected

Definition at line 79 of file class.ilFileXMLParser.php.

Referenced by handlerEndTag(), and setFileContents().

◆ $versions

ilFileXMLParser::$versions = []
protected

Definition at line 99 of file class.ilFileXMLParser.php.


The documentation for this class was generated from the following file: