ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ilFileXMLParser Class Reference

Exercise XML Parser which completes/updates a given file by an xml string. More...

+ 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
 

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

Exercise XML Parser which completes/updates a given file by an xml string.

Author
Roland Küstermann rolan.nosp@m.d@ku.nosp@m.ester.nosp@m.mann.nosp@m..com
Version
Id
class.ilObjectXMLParser.php 12811 2006-12-08 18:37:44Z akill

Definition at line 20 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 78 of file class.ilFileXMLParser.php.

References $file, $mode, $obj_id, file, and ilSaxParser\setXMLContent().

79  {
80  parent::__construct();
81  $this->file = $file;
82  $this->setXMLContent($a_xml_data);
83  $this->obj_id = $obj_id;
84  $this->result = false;
85  $this->mode = $mode;
86  }
Reload workbook from saved file
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 105 of file class.ilFileXMLParser.php.

Referenced by handlerEndTag().

106  {
107  return $this->importDirectory;
108  }
+ 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 136 of file class.ilFileXMLParser.php.

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

137  {
138  global $DIC;
139  $ilErr = $DIC['ilErr'];
140 
141  global $DIC;
142  $ilLog = $DIC['ilLog'];
143 
144  switch ($a_name) {
145  case 'File':
146  if (isset($a_attribs["obj_id"])) {
147  $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
148  if ($this->obj_id != -1 && (int) $read_obj_id != -1 && (int) $this->obj_id != (int) $read_obj_id) {
149  throw new ilFileException("Object IDs (xml $read_obj_id and argument " . $this->obj_id . ") do not match!", ilFileException::$ID_MISMATCH);
150  }
151  }
152  if (isset($a_attribs["type"])) {
153  $this->file->setFileType($a_attribs["type"]);
154  }
155  $this->file->setVersion($this->file->getVersion() + 1);
156  break;
157  case 'Content':
158  $this->tmpFilename = ilUtil::ilTempnam();
160  $this->isReadingFile = true;
161  #echo $a_attribs["mode"];
162  if (isset($a_attribs["mode"])) {
163  if ($a_attribs["mode"] == "GZIP") {
164  if (!function_exists("gzread")) {
165  throw new ilFileException("Deflating with gzip is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
166  }
167 
169  } elseif ($a_attribs["mode"] == "ZLIB") {
170  if (!function_exists("gzuncompress")) {
171  throw new ilFileException("Deflating with zlib (compress/uncompress) is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
172  }
173 
175  } elseif ($a_attribs["mode"] == "COPY") {
176  $this->mode = ilFileXMLParser::$CONTENT_COPY;
177  } // begin-patch fm
178  elseif ($a_attribs['mode'] == 'REST') {
179  $this->mode = ilFileXMLParser::$CONTENT_REST;
180  }
181  // end-patch fm
182  }
183  }
184  }
global $ilErr
Definition: raiseError.php:16
global $DIC
Definition: saml.php:7
Class to report exception.
Reload workbook from saved file
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable 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 286 of file class.ilFileXMLParser.php.

References $CONTENT_COPY, and $CONTENT_REST.

287  {
288  if ($a_data != "\n") {
289  // begin-patch fm
290  if ($this->isReadingFile && $this->mode != ilFileXMLParser::$CONTENT_COPY && $this->mode != ilFileXMLParser::$CONTENT_REST) { // begin-patch fm
291  $handle = fopen($this->tmpFilename, "a");
292  fwrite($handle, $a_data);
293  fclose($handle);
294  } else {
295  $this->cdata .= $a_data;
296  }
297  }
298  }

◆ 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 193 of file class.ilFileXMLParser.php.

References $CONTENT_COPY, $CONTENT_GZ_COMPRESSED, $CONTENT_REST, $CONTENT_ZLIB_COMPRESSED, ilFileException\$DECOMPRESSION_FAILED, $DIC, $GLOBALS, $ilLog, ilFileUtils\_lookupMimeType(), ilFileUtils\fastBase64Decode(), ilFileUtils\fastGunzip(), file, getImportDirectory(), and ilUtil\ilTempnam().

194  {
195  $this->cdata = trim($this->cdata);
196 
197  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': ' . $this->cdata);
198 
199  switch ($a_name) {
200  case 'File':
201  $this->result = true;
202  break;
203  case 'Filename':
204  if (strlen($this->cdata) == 0) {
205  throw new ilFileException("Filename ist missing!");
206  }
207 
208  $this->file->setFilename(basename(self::normalizeRelativePath($this->cdata)));
209  $this->file->setTitle($this->cdata);
210 
211  break;
212  case 'Title':
213  $this->file->setTitle(trim($this->cdata));
214  break;
215  case 'Description':
216  $this->file->setDescription(trim($this->cdata));
217  break;
218  case 'Rating':
219  $this->file->setRating((bool) $this->cdata);
220  break;
221  case 'Content':
222  $GLOBALS['DIC']['ilLog']->write($this->mode);
223  $this->isReadingFile = false;
224  $baseDecodedFilename = ilUtil::ilTempnam();
225  if ($this->mode == ilFileXMLParser::$CONTENT_COPY) {
226  $this->tmpFilename = $this->getImportDirectory() . "/" . self::normalizeRelativePath($this->cdata);
227  } // begin-patch fm
228  elseif ($this->mode == ilFileXMLParser::$CONTENT_REST) {
229  include_once './Services/WebServices/Rest/classes/class.ilRestFileStorage.php';
230  $storage = new ilRestFileStorage();
231  $this->tmpFilename = $storage->getStoredFilePath(self::normalizeRelativePath($this->cdata));
232  if (!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename)) {
233  throw new ilFileException("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
234  }
235  $this->tmpFilename = $baseDecodedFilename;
236  } // end-patch fm
237  else {
238  if (!ilFileUtils::fastBase64Decode($this->tmpFilename, $baseDecodedFilename)) {
239  throw new ilFileException("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
240  }
241  if ($this->mode == ilFileXMLParser::$CONTENT_GZ_COMPRESSED) {
242  if (!ilFileUtils::fastGunzip($baseDecodedFilename, $this->tmpFilename)) {
243  throw new ilFileException("Deflating with fastzunzip failed", ilFileException::$DECOMPRESSION_FAILED);
244  }
245  unlink($baseDecodedFilename);
246  } elseif ($this->mode == ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED) {
247  if (!ilFileUtils::fastGunzip($baseDecodedFilename, $this->tmpFilename)) {
248  throw new ilFileException("Deflating with fastDecompress failed", ilFileException::$DECOMPRESSION_FAILED);
249  }
250  unlink($baseDecodedFilename);
251  } else {
252  $this->tmpFilename = $baseDecodedFilename;
253  }
254  }
255  //$this->content = $content;
256  // see #17211
257  if (is_file($this->tmpFilename)) {
258  $this->file->setFileSize(filesize($this->tmpFilename)); // strlen($this->content));
259  }
260 
261  // if no file type is given => lookup mime type
262  if (!$this->file->getFileType()) {
263  global $DIC;
264  $ilLog = $DIC['ilLog'];
265 
266  #$ilLog->write(__METHOD__.': Trying to detect mime type...');
267  include_once('./Services/Utilities/classes/class.ilFileUtils.php');
268  $this->file->setFileType(ilFileUtils::_lookupMimeType($this->tmpFilename));
269  }
270 
271  break;
272  }
273 
274  $this->cdata = '';
275 
276  return;
277  }
global $DIC
Definition: saml.php:7
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
static _lookupMimeType($a_file)
Class to report exception.
Reload workbook from saved file
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable 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 377 of file class.ilFileXMLParser.php.

References $path.

378  {
379  $path = str_replace('\\', '/', $path);
380 
381  while (preg_match('#\p{C}+|^\./#u', $path)) {
382  $path = preg_replace('#\p{C}+|^\./#u', '', $path);
383  }
384 
385  $parts = [];
386  foreach (explode('/', $path) as $part) {
387  switch ($part) {
388  case '':
389  case '.':
390  break;
391  case '..':
392  array_pop($parts);
393  break;
394  default:
395  $parts[] = $part;
396  break;
397  }
398  }
399 
400  return implode('/', $parts);
401  }

◆ 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 306 of file class.ilFileXMLParser.php.

References $filename, file, ilLoggerFactory\getLogger(), ilUtil\makeDir(), and ilFileUtils\rename().

Referenced by updateFileContents().

307  {
308  if (!file_exists($this->tmpFilename)) {
309  ilLoggerFactory::getLogger('file')->error(__METHOD__ . ' "' . $this->tmpFilename . '" file not found.');
310 
311  return;
312  }
313 
314  if (filesize($this->tmpFilename) == 0) {
315  return;
316  }
317 
318  $filedir = $this->file->getDirectory($this->file->getVersion());
319 
320  if (!is_dir($filedir)) {
321  $this->file->createDirectory();
322  ilUtil::makeDir($filedir);
323  }
324 
325  $filename = $filedir . "/" . $this->file->getFileName();
326 
327  if (file_exists($filename)) {
328  unlink($filename);
329  }
330 
331  return ilFileUtils::rename($this->tmpFilename, $filename);
332  }
static rename($a_source, $a_target)
Rename a file.
Reload workbook from saved file
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
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 118 of file class.ilFileXMLParser.php.

119  {
120  xml_set_object($a_xml_parser, $this);
121  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
122  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
123  }

◆ setImportDirectory()

ilFileXMLParser::setImportDirectory (   $a_val)

Set import directory.

Parameters
stringimport directory

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

95  {
96  $this->importDirectory = $a_val;
97  }

◆ 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 357 of file class.ilFileXMLParser.php.

References ilSaxParser\startParsing().

358  {
359  $this->startParsing();
360 
361  return $this->result > 0;
362  }
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 340 of file class.ilFileXMLParser.php.

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

341  {
342  if ($this->setFileContents()) {
343  require_once("./Services/History/classes/class.ilHistory.php");
344  ilHistory::_createEntry($this->file->getId(), "replace", $this->file->getFilename() . "," . $this->file->getVersion());
345  $this->file->addNewsNotification("file_updated");
346  }
347  }
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.
Reload workbook from saved file
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

◆ $CONTENT_COPY

ilFileXMLParser::$CONTENT_COPY = 4
static

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

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

◆ $CONTENT_GZ_COMPRESSED

ilFileXMLParser::$CONTENT_GZ_COMPRESSED = 1
static

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

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $CONTENT_NOT_COMPRESSED

ilFileXMLParser::$CONTENT_NOT_COMPRESSED = 0
static

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

Referenced by handlerBeginTag().

◆ $CONTENT_REST

ilFileXMLParser::$CONTENT_REST = 5
static

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

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

◆ $CONTENT_ZLIB_COMPRESSED

ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED = 2
static

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

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $file

ilFileXMLParser::$file

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

Referenced by __construct().

◆ $mode

ilFileXMLParser::$mode

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

Referenced by __construct().

◆ $obj_id

ilFileXMLParser::$obj_id

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

Referenced by __construct().

◆ $result

ilFileXMLParser::$result

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

◆ $tmpFilename

ilFileXMLParser::$tmpFilename

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


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