Skip to content

Commit

Permalink
MDL-26229 restore - parser improvements (repetitions and skipped tags)
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Feb 12, 2011
1 parent d911c72 commit 5539e69
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 12 additions & 2 deletions backup/util/xml/parser/progressive_parser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

Expand Down
3 changes: 3 additions & 0 deletions backup/util/xml/parser/simpletest/fixtures/test4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
<name>4</name><!-- Only last will be processed. We don't allow repeated final tags in our parser -->
<value>4</value>
<value>5</value><!-- Only last will be processed. We don't allow repeated final tags in our parser -->
<value>

</value><!-- If one tag already is set and the repeated is empty, the original value is kept -->
</othertest>
</glossary>
</activity>

0 comments on commit 5539e69

Please sign in to comment.