Extract getFileAsSimpleXmlElement()

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2024-03-18 16:31:45 +01:00
parent 866564bc9d
commit 553ea7bce2

View File

@ -65,39 +65,7 @@ class ImportXml extends ImportPlugin
{
$GLOBALS['error'] ??= null;
$buffer = '';
/**
* Read in the file via Import::getNextChunk so that
* it can process compressed files
*/
/** @infection-ignore-all */
while (! ImportSettings::$finished && ! $GLOBALS['error'] && ! ImportSettings::$timeoutPassed) {
$data = $this->import->getNextChunk($importHandle);
if ($data === false) {
/* subtract data we didn't handle yet and stop processing */
ImportSettings::$offset -= strlen($buffer);
break;
}
if ($data === true) {
continue;
}
/* Append new data to buffer */
$buffer .= $data;
}
/**
* Load the XML string
*
* The option LIBXML_COMPACT is specified because it can
* result in increased performance without the need to
* alter the code in any way. It's basically a freebee.
*/
$xml = @simplexml_load_string($buffer, SimpleXMLElement::class, LIBXML_COMPACT);
unset($buffer);
$xml = $this->getFileAsSimpleXmlElement($importHandle);
/**
* The XML was malformed
@ -224,6 +192,8 @@ class ImportXml extends ImportPlugin
$tables[$tableName] = $table;
}
unset($xml);
$tables = array_values($tables);
foreach ($tables as $table) {
@ -234,8 +204,6 @@ class ImportXml extends ImportPlugin
$analyses = array_map($this->import->analyzeTable(...), $tables);
}
unset($xml);
/**
* Only build SQL from data if there is data present.
* Set values to NULL if they were not present
@ -268,4 +236,39 @@ class ImportXml extends ImportPlugin
return $sqlStatements;
}
private function getFileAsSimpleXmlElement(File|null $importHandle): false|SimpleXMLElement
{
$buffer = '';
/**
* Read in the file via Import::getNextChunk so that
* it can process compressed files
*/
/** @infection-ignore-all */
while (! ImportSettings::$finished && ! $GLOBALS['error'] && ! ImportSettings::$timeoutPassed) {
$data = $this->import->getNextChunk($importHandle);
if ($data === false) {
/* subtract data we didn't handle yet and stop processing */
ImportSettings::$offset -= strlen($buffer);
break;
}
if ($data === true) {
continue;
}
/* Append new data to buffer */
$buffer .= $data;
}
/**
* Load the XML string
*
* The option LIBXML_COMPACT is specified because it can
* result in increased performance without the need to
* alter the code in any way. It's basically a freebee.
*/
return @simplexml_load_string($buffer, SimpleXMLElement::class, LIBXML_COMPACT);
}
}