ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
ilFileXMLParser Class Reference
+ Inheritance diagram for ilFileXMLParser:
+ Collaboration diagram for ilFileXMLParser:

Public Member Functions

 __construct (public ilObjFile $file, string $a_xml_data, public int $obj_id=-1, public int $mode=0)
 Constructor. More...
 
 setImportDirectory (?string $a_val)
 Set import directory. More...
 
 getImportDirectory ()
 Get import directory. More...
 
 setHandlers ($a_xml_parser)
 set event handlers More...
 
 handlerBeginTag ($a_xml_parser, string $a_name, array $a_attribs)
 handler for begin of element More...
 
 handlerEndTag ($a_xml_parser, string $a_name)
 handler for end of element More...
 
 handlerCharacterData ($a_xml_parser, string $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 (?string $path_to_file='', ?bool $throw_exception=false)
 
 setXMLContent (string $a_xml_content)
 
 getXMLContent ()
 
 getInputType ()
 
 startParsing ()
 stores xml data in array More...
 
 createParser ()
 
 setHandlers ($a_xml_parser)
 
 parse ($a_xml_parser, $a_fp=null)
 

Static Public Member Functions

static normalizeRelativePath (string $path)
 Normalize relative directories in a path. More...
 

Data Fields

bool $result = false
 result of parsing and updating More...
 
string $tmpFilename = null
 file of temporary file where we store the file content instead of in memory More...
 
- Data Fields inherited from ilSaxParser
string $xml_file
 
bool $throw_exception = false
 

Static Public Attributes

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

Protected Attributes

int $version = null
 
string $action = null
 
int $max_version = null
 
int $date = null
 
int $usr_id = null
 
array $versions = []
 
string $import_directory = null
 
string $cdata = null
 
- Protected Attributes inherited from ilSaxParser
ilLanguage $lng = null
 

Private Member Functions

 fastBase64Decode (string $filein, string $fileout)
 
 fastGunzip (string $in, string $out)
 

Additional Inherited Members

- Protected Member Functions inherited from ilSaxParser
 openXMLFile ()
 
 handleError (string $message)
 
 setThrowException (bool $throw_exception)
 

Detailed Description

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

Constructor & Destructor Documentation

◆ __construct()

ilFileXMLParser::__construct ( public ilObjFile  $file,
string  $a_xml_data,
public int  $obj_id = -1,
public int  $mode = 0 
)

Constructor.

Parameters
$fileExercise object which has been parsed
$obj_idthis will be matched against the id in the xml in case we want to update an exercise
$modeContent compression mode, defaults to no compression

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

76 {
78 $this->setXMLContent($a_xml_data);
79 }
setXMLContent(string $a_xml_content)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc

References ILIAS\GlobalScreen\Provider\__construct(), and ilSaxParser\setXMLContent().

+ Here is the call graph for this function:

Member Function Documentation

◆ fastBase64Decode()

ilFileXMLParser::fastBase64Decode ( string  $filein,
string  $fileout 
)
private

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

429 : bool
430 {
431 $fh = fopen($filein, 'rb');
432 $fh2 = fopen($fileout, 'wb');
433 stream_filter_append($fh2, 'convert.base64-decode');
434
435 while (!feof($fh)) {
436 $chunk = fgets($fh);
437 if ($chunk === false) {
438 break;
439 }
440 fwrite($fh2, $chunk);
441 }
442 fclose($fh);
443 fclose($fh2);
444
445 return true;
446 }

Referenced by handlerEndTag().

+ Here is the caller graph for this function:

◆ fastGunzip()

ilFileXMLParser::fastGunzip ( string  $in,
string  $out 
)
private

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

448 : bool
449 {
450 if (!file_exists($in)) {
451 return false;
452 }
453 if (!is_readable($in)) {
454 return false;
455 }
456 if (!file_exists($out) && !is_writable(dirname($out))) {
457 return false;
458 }
459 if (file_exists($out) && !is_writable($out)) {
460 return false;
461 }
462 $in_file = gzopen($in, "rb");
463 $out_file = fopen($out, "wb");
464
465 while (!gzeof($in_file)) {
466 $buffer = gzread($in_file, 4096);
467 fwrite($out_file, $buffer, 4096);
468 }
469
470 gzclose($in_file);
471 fclose($out_file);
472
473 return true;
474 }
$out
Definition: buildRTE.php:24

References $out.

Referenced by handlerEndTag().

+ Here is the caller graph for this function:

◆ getImportDirectory()

ilFileXMLParser::getImportDirectory ( )

Get import directory.

Returns
mixed|null import directory

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

96 : ?string
97 {
99 }

References $import_directory.

Referenced by handlerEndTag().

+ Here is the caller graph for this function:

◆ handlerBeginTag()

ilFileXMLParser::handlerBeginTag (   $a_xml_parser,
string  $a_name,
array  $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 124 of file class.ilFileXMLParser.php.

124 : void
125 {
126 global $DIC;
127
128 global $DIC;
129
130 switch ($a_name) {
131 case 'File':
132 if (isset($a_attribs["obj_id"])) {
133 $read_obj_id = ilUtil::__extractId($a_attribs["obj_id"], IL_INST_ID);
134 if ($this->obj_id != -1 && (int) $read_obj_id != -1 && $this->obj_id !== (int) $read_obj_id) {
135 throw new ilFileException(
136 "Object IDs (xml $read_obj_id and argument " . $this->obj_id . ") do not match!",
138 );
139 }
140 }
141
142 break;
143 case 'Content': // Old import files
144 case 'Version':
145 if ($a_name === "Version" && !isset($a_attribs["mode"])) {
146 // Old import files
147 $this->version = null;
148 if ($this->date === null) {
149 // Version tag comes after Content tag. Take only first (= Should be latest)
150 $this->date = $a_attribs["date"];
151 $this->usr_id = (int) $a_attribs["usr_id"];
152 $this->versions[0]["date"] = $this->date;
153 $this->versions[0]["usr_id"] = $this->usr_id;
154 }
155 break;
156 }
157
159 #echo $a_attribs["mode"];
160 if (isset($a_attribs["mode"])) {
161 if ($a_attribs["mode"] == "GZIP") {
162 if (!function_exists("gzread")) {
163 throw new ilFileException(
164 "Deflating with gzip is not supported",
166 );
167 }
168
170 } elseif ($a_attribs["mode"] == "ZLIB") {
171 if (!function_exists("gzuncompress")) {
172 throw new ilFileException(
173 "Deflating with zlib (compress/uncompress) is not supported",
175 );
176 }
177
179 } elseif ($a_attribs["mode"] == "COPY") {
180 $this->mode = ilFileXMLParser::$CONTENT_COPY;
181 } // begin-patch fm
182 elseif ($a_attribs['mode'] == 'REST') {
183 $this->mode = ilFileXMLParser::$CONTENT_REST;
184 }
185 // end-patch fm
186 }
187
188 if ($a_name === "Version") {
189 $this->version = (int) $a_attribs["version"];
190 $this->max_version = (int) $a_attribs["max_version"];
191 $this->date = (int) $a_attribs["date"];
192 $this->usr_id = (int) $a_attribs["usr_id"];
193 $this->action = (string) $a_attribs["action"];
194 }
195 }
196 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static int $ID_DEFLATE_METHOD_MISMATCH
static int $CONTENT_GZ_COMPRESSED
static int $CONTENT_ZLIB_COMPRESSED
static int $CONTENT_NOT_COMPRESSED
static __extractId(string $ilias_id, int $inst_id)
extract ref id from role title, e.g.
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: shib_login.php:26

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

Referenced by setHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handlerCharacterData()

ilFileXMLParser::handlerCharacterData (   $a_xml_parser,
string  $a_data 
)

handler for character data

Parameters
resource$a_xml_parserxml parser
string$a_datacharacter data

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

316 : void
317 {
318 if ($a_data !== "\n") {
319 // begin-patch fm
320 if ($this->mode !== ilFileXMLParser::$CONTENT_COPY
321 && $this->mode !== ilFileXMLParser::$CONTENT_REST
322 ) { // begin-patch fm
323 $this->cdata .= $a_data;
324 } else {
325 $this->cdata .= $a_data;
326 }
327 }
328 }

References $CONTENT_COPY, and $CONTENT_REST.

Referenced by setHandlers().

+ Here is the caller graph for this function:

◆ handlerEndTag()

ilFileXMLParser::handlerEndTag (   $a_xml_parser,
string  $a_name 
)

handler for end of element

Parameters
resource$a_xml_parserxml parser
string$a_nameelement name

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

204 : void
205 {
206 $this->cdata = trim($this->cdata ?? '');
207
208 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': ' . $this->cdata);
209
210 switch ($a_name) {
211 case 'File':
212 $this->result = true;
213 break;
214 case 'Filename':
215 $this->file->setFilename($this->cdata ?? 'unkown');
216 $this->file->setTitle($this->cdata ?? 'unkown');
217
218 break;
219 case 'Title':
220 $this->file->setTitle(trim($this->cdata));
221 break;
222 case 'Description':
223 $this->file->setDescription(trim($this->cdata));
224 break;
225 case 'Rating':
226 $this->file->setRating((bool) $this->cdata);
227 break;
228 case 'ImportantInformation':
229 $this->file->setImportantInfo((string) $this->cdata);
230 break;
231 case 'OnClickMode':
232 $this->file->setOnclickMode((int) $this->cdata);
233 break;
234 case 'Content': // Old import files
235 case 'Version':
236 if ($a_name === "Version" && $this->version === null) {
237 // Old import files
238 break;
239 }
240
241 $baseDecodedFilename = ilFileUtils::ilTempnam();
242 if ($this->mode === ilFileXMLParser::$CONTENT_COPY) {
243 $this->tmpFilename = $this->getImportDirectory() . "/" . self::normalizeRelativePath($this->cdata);
244 } // begin-patch fm
245 elseif ($this->mode === ilFileXMLParser::$CONTENT_REST) {
246 $storage = new ilRestFileStorage();
247 $this->tmpFilename = $storage->getStoredFilePath(self::normalizeRelativePath($this->cdata));
248 if (!$this->fastBase64Decode($this->tmpFilename, $baseDecodedFilename)) {
249 throw new ilFileException("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
250 }
251 $this->tmpFilename = $baseDecodedFilename;
252 } // end-patch fm
253 else {
254 if (!$this->fastBase64Decode($this->tmpFilename, $baseDecodedFilename)) {
255 throw new ilFileException("Base64-Decoding failed", ilFileException::$DECOMPRESSION_FAILED);
256 }
257 if ($this->mode === ilFileXMLParser::$CONTENT_GZ_COMPRESSED) {
258 if (!$this->fastGunzip($baseDecodedFilename, $this->tmpFilename)) {
259 throw new ilFileException(
260 "Deflating with fastzunzip failed",
262 );
263 }
264 unlink($baseDecodedFilename);
265 } elseif ($this->mode === ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED) {
266 if (!$this->fastGunzip($baseDecodedFilename, $this->tmpFilename)) {
267 throw new ilFileException(
268 "Deflating with fastDecompress failed",
270 );
271 }
272 unlink($baseDecodedFilename);
273 } else {
274 $this->tmpFilename = $baseDecodedFilename;
275 }
276 }
277
278 //$this->content = $content;
279 // see #17211
280
281 if ($this->version == $this->file->getVersion()) {
282 if (is_file($this->tmpFilename)) {
283 $this->file->setFileSize(filesize($this->tmpFilename)); // strlen($this->content));
284 }
285
286 // if no file type is given => lookup mime type
287 if ($this->file->getFileType() === '' || $this->file->getFileType() === '0') {
288 global $DIC;
289 $this->file->setFileType(MimeType::getMimeType($this->tmpFilename));
290 }
291 }
292
293 $this->versions[] = [
294 "version" => $this->version,
295 "max_version" => $this->max_version,
296 "tmpFilename" => $this->tmpFilename,
297 "date" => $this->date,
298 "usr_id" => $this->usr_id,
299 "action" => $this->action,
300 ];
301 $this->version = null;
302 $this->date = null;
303 $this->usr_id = null;
304 break;
305 }
306
307 $this->cdata = '';
308 }
static int $DECOMPRESSION_FAILED
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
fastGunzip(string $in, string $out)
getImportDirectory()
Get import directory.
string $tmpFilename
file of temporary file where we store the file content instead of in memory
static normalizeRelativePath(string $path)
Normalize relative directories in a path.
fastBase64Decode(string $filein, string $fileout)
File storage handling.
$GLOBALS["DIC"]
Definition: wac.php:54

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

Referenced by setHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ normalizeRelativePath()

static ilFileXMLParser::normalizeRelativePath ( string  $path)
static

Normalize relative directories in a path.

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

Exceptions
LogicException

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

403 : string
404 {
405 $path = str_replace('\\', '/', $path);
406
407 while (preg_match('#\p{C}+|^\./#u', (string) $path)) {
408 $path = preg_replace('#\p{C}+|^\./#u', '', (string) $path);
409 }
410
411 $parts = [];
412 foreach (explode('/', (string) $path) as $part) {
413 switch ($part) {
414 case '':
415 case '.':
416 break;
417 case '..':
418 array_pop($parts);
419 break;
420 default:
421 $parts[] = $part;
422 break;
423 }
424 }
425
426 return implode('/', $parts);
427 }
$path
Definition: ltiservices.php:30
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61

References $parts, and $path.

Referenced by handlerEndTag().

+ Here is the caller graph for this function:

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

335 : void
336 {
337 foreach ($this->versions as $version) {
338 if (!file_exists($version["tmpFilename"])) {
339 if (!isset($version["tmpFilename"])) {
340 continue;
341 }
342 // try to get first file of directory
343 $files = scandir(dirname((string) $version["tmpFilename"]));
344 $version["tmpFilename"] = rtrim(
345 dirname((string) $version["tmpFilename"]),
346 "/"
347 ) . "/" . $files[2];// because [0] = "." [1] = ".."
348 if (!file_exists($version["tmpFilename"])) {
349 ilLoggerFactory::getLogger('file')->error(
350 __METHOD__ . ' "' . ($version["tmpFilename"]) . '" file not found.'
351 );
352
353 continue;
354 }
355 }
356
357 if (filesize($version["tmpFilename"]) == 0) {
358 continue;
359 }
360
361 // imported file version
362 $import_file_version_path = $version["tmpFilename"];
363
364 $stream = Streams::ofResource(fopen($import_file_version_path, 'rb'));
365 $this->file->appendStream($stream, $this->file->getTitle());
366 }
367 }
static getLogger(string $a_component_id)
Get component logger.

References $version, and ilLoggerFactory\getLogger().

+ Here is the call graph for this function:

◆ setHandlers()

ilFileXMLParser::setHandlers (   $a_xml_parser)

set event handlers

Parameters
resourcereference to the xml parser

@access private

Reimplemented from ilSaxParser.

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

108 : void
109 {
110 xml_set_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
111 xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
112 }
handlerCharacterData($a_xml_parser, string $a_data)
handler for character data
handlerEndTag($a_xml_parser, string $a_name)
handler for end of element
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
handler for begin of element

References handlerBeginTag(), handlerCharacterData(), and handlerEndTag().

+ Here is the call graph for this function:

◆ setImportDirectory()

ilFileXMLParser::setImportDirectory ( ?string  $a_val)

Set import directory.

Parameters
stringimport directory

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

86 : void
87 {
88 $this->import_directory = $a_val;
89 }

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

386 : bool
387 {
388 $this->startParsing();
389
390 return $this->result > 0;
391 }
startParsing()
stores xml data in array

References ilSaxParser\startParsing().

+ 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 374 of file class.ilFileXMLParser.php.

374 : void
375 {
376 // removed
377 }

Field Documentation

◆ $action

◆ $cdata

string ilFileXMLParser::$cdata = null
protected

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

◆ $CONTENT_COPY

int ilFileXMLParser::$CONTENT_COPY = 4
static

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

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

◆ $CONTENT_GZ_COMPRESSED

int ilFileXMLParser::$CONTENT_GZ_COMPRESSED = 1
static

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

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $CONTENT_NOT_COMPRESSED

int ilFileXMLParser::$CONTENT_NOT_COMPRESSED = 0
static

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

Referenced by handlerBeginTag().

◆ $CONTENT_REST

int ilFileXMLParser::$CONTENT_REST = 5
static

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

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

◆ $CONTENT_ZLIB_COMPRESSED

int ilFileXMLParser::$CONTENT_ZLIB_COMPRESSED = 2
static

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

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $date

int ilFileXMLParser::$date = null
protected

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

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $import_directory

string ilFileXMLParser::$import_directory = null
protected

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

Referenced by getImportDirectory().

◆ $max_version

int ilFileXMLParser::$max_version = null
protected

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

Referenced by handlerEndTag().

◆ $result

bool ilFileXMLParser::$result = false

result of parsing and updating

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

◆ $tmpFilename

string ilFileXMLParser::$tmpFilename = null

file of temporary file where we store the file content instead of in memory

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

Referenced by handlerEndTag().

◆ $usr_id

int ilFileXMLParser::$usr_id = null
protected

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

Referenced by handlerBeginTag(), and handlerEndTag().

◆ $version

int ilFileXMLParser::$version = null
protected

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

Referenced by handlerEndTag(), and setFileContents().

◆ $versions

array ilFileXMLParser::$versions = []
protected

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


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