MDL-26229 restore - parser improvements (repetitions and skipped tags)
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Fri, 11 Feb 2011 11:02:18 +0000 (12:02 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Sat, 12 Feb 2011 01:19:02 +0000 (02:19 +0100)
backup/util/xml/parser/processors/simplified_parser_processor.class.php
backup/util/xml/parser/progressive_parser.class.php
backup/util/xml/parser/simpletest/fixtures/test4.xml

index 9dbe9b8..e87ac9d 100644 (file)
@@ -104,13 +104,18 @@ abstract class simplified_parser_processor extends progressive_parser_processor
                     $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;
 
index 11f3c9c..91bc85f 100644 (file)
@@ -201,7 +201,12 @@ class progressive_parser {
         // 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 @@ class progressive_parser {
         // 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();
         }
 
index f5865c4..4a4fa67 100755 (executable)
       <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>