Skip to content

Commit f32b107

Browse files
Merge pull request #19064 from kamil-tekiela/Refactor-import-12
Extract getFileAsSimpleXmlElement()
2 parents 1c7c48b + 94f0d05 commit f32b107

1 file changed

Lines changed: 40 additions & 39 deletions

File tree

src/Plugins/Import/ImportXml.php

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,7 @@ public function doImport(File|null $importHandle = null): array
6565
{
6666
$GLOBALS['error'] ??= null;
6767

68-
$buffer = '';
69-
70-
/**
71-
* Read in the file via Import::getNextChunk so that
72-
* it can process compressed files
73-
*/
74-
/** @infection-ignore-all */
75-
while (! ImportSettings::$finished && ! $GLOBALS['error'] && ! ImportSettings::$timeoutPassed) {
76-
$data = $this->import->getNextChunk($importHandle);
77-
if ($data === false) {
78-
/* subtract data we didn't handle yet and stop processing */
79-
ImportSettings::$offset -= strlen($buffer);
80-
break;
81-
}
82-
83-
if ($data === true) {
84-
continue;
85-
}
86-
87-
/* Append new data to buffer */
88-
$buffer .= $data;
89-
}
90-
91-
/**
92-
* Load the XML string
93-
*
94-
* The option LIBXML_COMPACT is specified because it can
95-
* result in increased performance without the need to
96-
* alter the code in any way. It's basically a freebee.
97-
*/
98-
$xml = @simplexml_load_string($buffer, SimpleXMLElement::class, LIBXML_COMPACT);
99-
100-
unset($buffer);
68+
$xml = $this->getFileAsSimpleXmlElement($importHandle);
10169

10270
/**
10371
* The XML was malformed
@@ -152,13 +120,13 @@ public function doImport(File|null $importHandle = null): array
152120
/**
153121
* CREATE code included (by default: no)
154122
*/
155-
$structPresent = false;
123+
$structPresent = isset($namespaces['pma']);
156124

157125
/**
158126
* Retrieve the structure information
159127
*/
160128
$create = [];
161-
if (isset($namespaces['pma'])) {
129+
if ($structPresent) {
162130
/**
163131
* Get structures for all tables
164132
*
@@ -186,8 +154,6 @@ public function doImport(File|null $importHandle = null): array
186154
}
187155
}
188156
}
189-
190-
$structPresent = true;
191157
}
192158

193159
/**
@@ -224,6 +190,8 @@ public function doImport(File|null $importHandle = null): array
224190
$tables[$tableName] = $table;
225191
}
226192

193+
unset($xml);
194+
227195
$tables = array_values($tables);
228196

229197
foreach ($tables as $table) {
@@ -234,8 +202,6 @@ public function doImport(File|null $importHandle = null): array
234202
$analyses = array_map($this->import->analyzeTable(...), $tables);
235203
}
236204

237-
unset($xml);
238-
239205
/**
240206
* Only build SQL from data if there is data present.
241207
* Set values to NULL if they were not present
@@ -268,4 +234,39 @@ public function doImport(File|null $importHandle = null): array
268234

269235
return $sqlStatements;
270236
}
237+
238+
private function getFileAsSimpleXmlElement(File|null $importHandle): false|SimpleXMLElement
239+
{
240+
$buffer = '';
241+
242+
/**
243+
* Read in the file via Import::getNextChunk so that
244+
* it can process compressed files
245+
*/
246+
/** @infection-ignore-all */
247+
while (! ImportSettings::$finished && ! $GLOBALS['error'] && ! ImportSettings::$timeoutPassed) {
248+
$data = $this->import->getNextChunk($importHandle);
249+
if ($data === false) {
250+
/* subtract data we didn't handle yet and stop processing */
251+
ImportSettings::$offset -= strlen($buffer);
252+
break;
253+
}
254+
255+
if ($data === true) {
256+
continue;
257+
}
258+
259+
/* Append new data to buffer */
260+
$buffer .= $data;
261+
}
262+
263+
/**
264+
* Load the XML string
265+
*
266+
* The option LIBXML_COMPACT is specified because it can
267+
* result in increased performance without the need to
268+
* alter the code in any way. It's basically a freebee.
269+
*/
270+
return @simplexml_load_string($buffer, SimpleXMLElement::class, LIBXML_COMPACT);
271+
}
271272
}

0 commit comments

Comments
 (0)