| @@ -40,8 +40,9 @@ | ||
| 40 | 40 | protected $record_offset = 0; |
| 41 | 41 | protected $chunk_size = 8192; |
| 42 | 42 | protected $depth = 0; |
| 43 | 43 | protected $node_list = array(); |
| 44 | + protected $namespace_list = null; | |
| 44 | 45 | protected $chunk; |
| 45 | 46 | protected $chunk_offset = 0; |
| 46 | 47 | |
| 47 | 48 | protected $record_counter = 0; |
| @@ -192,8 +193,9 @@ | ||
| 192 | 193 | |
| 193 | 194 | $last_percent = 0; |
| 194 | 195 | $size = intval($this->get_file_size($this->getFileHandle())); |
| 195 | 196 | $this->config->set('process', 0); |
| 197 | + $this->namespace_list = []; | |
| 196 | 198 | |
| 197 | 199 | rewind($this->getFileHandle()); |
| 198 | 200 | |
| 199 | 201 | while (!feof($this->getFileHandle())) { |
| @@ -200,10 +202,10 @@ | ||
| 200 | 202 | |
| 201 | 203 | $this->chunk .= $this->getChunk(); |
| 202 | 204 | $this->chunk = $this->read_chunk_xml_nodes($this->chunk); |
| 203 | 205 | |
| 204 | - // only read the first 1mb of file | |
| 205 | - if ($this->is_processing && ($this->record_counter > 0 || ftell($this->getFileHandle()) > $this->process_max_size)) { | |
| 206 | + // only read the first 1mb of file when sampling | |
| 207 | + if ($this->is_processing && ftell($this->getFileHandle()) > $this->process_max_size) { | |
| 206 | 208 | break; |
| 207 | 209 | } |
| 208 | 210 | |
| 209 | 211 | $current_pos = intval(ftell($this->getFileHandle())); |
| @@ -224,8 +226,10 @@ | ||
| 224 | 226 | } |
| 225 | 227 | } |
| 226 | 228 | } |
| 227 | 229 | |
| 230 | + $this->config->set('namespace_list', $this->namespace_list); | |
| 231 | + | |
| 228 | 232 | // if file is incomplete do we try and fix the record, or skip it? |
| 229 | 233 | if (!$this->is_processing && true === $this->record_opened) { |
| 230 | 234 | $this->record_opened = false; |
| 231 | 235 | $this->last_record_incomplete = true; |
| @@ -398,8 +402,13 @@ | ||
| 398 | 402 | } |
| 399 | 403 | |
| 400 | 404 | $this->open_nodes[] = $node_name; |
| 401 | 405 | $this->record_open_nodes(); |
| 406 | + | |
| 407 | + // find namespaces | |
| 408 | + if (preg_match_all('/xmlns:([a-zA-Z0-9_-]+)="([^"]+)"/', $node, $matches) > 0) { | |
| 409 | + $this->namespace_list = array_merge($this->namespace_list, array_combine($matches[1], $matches[2])); | |
| 410 | + } | |
| 402 | 411 | } elseif ($depth_delta < 0) { |
| 403 | 412 | |
| 404 | 413 | array_pop($this->open_nodes); |
| 405 | 414 | if ($this->isRecordEnd($node_name)) { |
| @@ -464,9 +473,9 @@ | ||
| 464 | 473 | // clear sub namespaces |
| 465 | 474 | $record = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $record); |
| 466 | 475 | |
| 467 | 476 | // No need to check within cdata tags |
| 468 | - $temp = preg_replace('/<!\[CDATA\[.*?\]\]>/', '', $record); | |
| 477 | + $temp = preg_replace('/<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>/', '', $record); | |
| 469 | 478 | |
| 470 | 479 | // $new_regex = '/<([^:!\/]+):[^ ]+[^>]*>/'; |
| 471 | 480 | // $new_regex = '/<([^!\/>]+):[^>]+>/'; // breaking due to attr="http://", would match the : |
| 472 | 481 | $new_regex = '/<([^!\/">]+):[^>]+>/'; |
| @@ -481,9 +490,22 @@ | ||
| 481 | 490 | $namespace = $namespace_parts[count($namespace_parts) - 1]; |
| 482 | 491 | |
| 483 | 492 | if (!in_array($namespace, $unique_matches)) { |
| 484 | 493 | $unique_matches[] = $namespace; |
| 485 | - $namespace_attrs .= ' xmlns:' . $namespace . '="false"'; | |
| 494 | + | |
| 495 | + // load namespace list if needed | |
| 496 | + if (is_null($this->namespace_list) && !is_null($this->config)) { | |
| 497 | + $this->namespace_list = $this->config->get('namespace_list'); | |
| 498 | + if (!is_array($this->namespace_list)) { | |
| 499 | + $this->namespace_list = array(); | |
| 500 | + } | |
| 501 | + } | |
| 502 | + | |
| 503 | + if (isset($this->namespace_list[$namespace])) { | |
| 504 | + $namespace_attrs .= ' xmlns:' . $namespace . '="' . $this->namespace_list[$namespace] . '"'; | |
| 505 | + } else { | |
| 506 | + $namespace_attrs .= ' xmlns:' . $namespace . '="false"'; | |
| 507 | + } | |
| 486 | 508 | } |
| 487 | 509 | |
| 488 | 510 | $temp = substr($temp, $offset + strlen($match)); |
| 489 | 511 | } |