From 5539e698629ad9c8ec2e2c20467958c9f8ef0640 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 11 Feb 2011 12:02:18 +0100 Subject: [PATCH] MDL-26229 restore - parser improvements (repetitions and skipped tags) --- .../simplified_parser_processor.class.php | 11 ++++++++--- .../util/xml/parser/progressive_parser.class.php | 14 ++++++++++++-- .../util/xml/parser/simpletest/fixtures/test4.xml | 3 +++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/backup/util/xml/parser/processors/simplified_parser_processor.class.php b/backup/util/xml/parser/processors/simplified_parser_processor.class.php index 9dbe9b8a0fc07..e87ac9d556fc5 100644 --- a/backup/util/xml/parser/processors/simplified_parser_processor.class.php +++ b/backup/util/xml/parser/processors/simplified_parser_processor.class.php @@ -104,13 +104,18 @@ public function process_chunk($data) { $alltagswhitespace = false; continue; } + // If the path including the tag name matches another selected path - // (registered or parent) delete it, another chunk will contain that info + // (registered or parent) and is null or begins with linefeed, we know it's part + // of another chunk, delete it, another chunk will contain that info if ($this->path_is_selected($path . '/' . $key) || $this->path_is_selected_parent($path . '/' . $key)) { - unset($data['tags'][$key]); - continue; + if (!isset($value['cdata']) || substr($value['cdata'], 0, 1) === "\n") { + unset($data['tags'][$key]); + continue; + } } + // Convert to simple name => value array $data['tags'][$key] = isset($value['cdata']) ? $value['cdata'] : null; diff --git a/backup/util/xml/parser/progressive_parser.class.php b/backup/util/xml/parser/progressive_parser.class.php index 11f3c9c682ef6..91bc85fc4b2d2 100644 --- a/backup/util/xml/parser/progressive_parser.class.php +++ b/backup/util/xml/parser/progressive_parser.class.php @@ -201,7 +201,12 @@ protected function start_tag($parser, $tag, $attributes) { // Entering a new inner level, publish all the information available if ($this->level > $this->prevlevel) { if (!empty($this->currtag) && (!empty($this->currtag['attrs']) || !empty($this->currtag['cdata']))) { - $this->topush['tags'][$this->currtag['name']] = $this->currtag; + // We always add the last not-empty repetition. Empty ones are ignored. + if (isset($this->topush['tags'][$this->currtag['name']]) && trim($this->currtag['cdata']) === '') { + // Do nothing, the tag already exists and the repetition is empty + } else { + $this->topush['tags'][$this->currtag['name']] = $this->currtag; + } } if (!empty($this->topush['tags'])) { $this->publish($this->topush); @@ -233,7 +238,12 @@ protected function end_tag($parser, $tag) { // Ending rencently started tag, add value to current tag if ($this->level == $this->prevlevel) { $this->currtag['cdata'] = $this->postprocess_cdata($this->accum); - $this->topush['tags'][$this->currtag['name']] = $this->currtag; + // We always add the last not-empty repetition. Empty ones are ignored. + if (isset($this->topush['tags'][$this->currtag['name']]) && trim($this->currtag['cdata']) === '') { + // Do nothing, the tag already exists and the repetition is empty + } else { + $this->topush['tags'][$this->currtag['name']] = $this->currtag; + } $this->currtag = array(); } diff --git a/backup/util/xml/parser/simpletest/fixtures/test4.xml b/backup/util/xml/parser/simpletest/fixtures/test4.xml index f5865c47da9af..4a4fa67416c6d 100755 --- a/backup/util/xml/parser/simpletest/fixtures/test4.xml +++ b/backup/util/xml/parser/simpletest/fixtures/test4.xml @@ -106,6 +106,9 @@ 4 4 5 + + +