public function queue_import_xml($filename)
{
only_admin_access();
if (!is_file($filename)) {
return array('error' => 'You have not provided a existing backup to restore.');
}
$chunk_size = $this->batch_size;
libxml_use_internal_errors(true);
$chunks_folder = $this->get_chunks_location();
$content_items = array();
$chunk_size = $this->batch_size;
$i = 0;
$xml_paths = $this->xml_paths;
$content_batch = '';
foreach ($xml_paths as $xml_key => $xml_path) {
$XMLReader = new \XMLReader();
$xml_file_path = $filename;
$XMLReader->open($xml_file_path);
// Move to the first "[item name]" node in the file.
while ($XMLReader->read() && $XMLReader->name != $xml_path) {
//$xml_str = $XMLReader->readOuterXML();
// d($xml_str);
}
// Now that we're at the right depth, hop to the next "[item name]" until the end of tree/file.
while ($XMLReader->name === $xml_path) {
$xml_str = $XMLReader->readOuterXML();
if ($xml_str != '') {
//$content_batch = $content_batch . $xml_str . "\n";
$content_batch = $xml_str;
//if ($i % $chunk_size == 0) {
$file_name = 'import_chunk_xml_' . md5($content_batch);
$file_location = $chunks_folder . $file_name;
if (!is_file($file_location)) {
$content_batch = str_replace('content:encoded', 'content', $content_batch);
$content_batch = str_replace('<' . $xml_path, '<item', $content_batch);
$content_batch = str_replace('</' . $xml_path, '</item', $content_batch);
$rss_stub = '<?xml version="1.0"?>' . "\n";
file_put_contents($file_location, $rss_stub . $content_batch);
}
$content_batch = '';
// }
++$i;
$XMLReader->next($xml_path);
}
}
//$XMLReader->close();
}
$file_name = 'import_chunk_xml_' . md5($content_batch);
$file_location = $chunks_folder . $file_name;
if (!is_file($file_location)) {
file_put_contents($file_location, $content_batch);
}
return array('success' => $i . ' xml items will be imported');
}