### Eclipse Workspace Patch 1.0
#P moodle
Index: lib/xmldb/classes/XMLDBTable.class.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/xmldb/classes/XMLDBTable.class.php,v
retrieving revision 1.20
diff -u -r1.20 XMLDBTable.class.php
--- lib/xmldb/classes/XMLDBTable.class.php	20 Apr 2007 00:10:53 -0000	1.20
+++ lib/xmldb/classes/XMLDBTable.class.php	5 Jul 2007 13:50:07 -0000
@@ -498,6 +498,7 @@
                 $result = false;
             }
         /// Check previous & next are ok (duplicates and existing fields)
+            $this->fixPrevNext($this->fields);
             if ($result && !$this->checkPreviousNextValues($this->fields)) {
                 $this->errormsg = 'Some FIELDS previous/next values are incorrect';
                 $this->debug($this->errormsg);
@@ -542,6 +543,7 @@
                 $result = false;
             }
         /// Check previous & next are ok (duplicates and existing keys)
+            $this->fixPrevNext($this->keys);
             if ($result && !$this->checkPreviousNextValues($this->keys)) {
                 $this->errormsg = 'Some KEYS previous/next values are incorrect';
                 $this->debug($this->errormsg);
@@ -585,6 +587,7 @@
                 $result = false;
             }
         /// Check previous & next are ok (duplicates and existing INDEXES)
+            $this->fixPrevNext($this->indexes);
             if ($result && !$this->checkPreviousNextValues($this->indexes)) {
                 $this->errormsg = 'Some INDEXES previous/next values are incorrect';
                 $this->debug($this->errormsg);
Index: lib/xmldb/classes/XMLDBObject.class.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/xmldb/classes/XMLDBObject.class.php,v
retrieving revision 1.5
diff -u -r1.5 XMLDBObject.class.php
--- lib/xmldb/classes/XMLDBObject.class.php	20 Apr 2007 00:10:53 -0000	1.5
+++ lib/xmldb/classes/XMLDBObject.class.php	5 Jul 2007 13:50:06 -0000
@@ -190,6 +190,37 @@
     }
 
     /**
+     * Reconstruct previous/next attributes.
+     */
+    function fixPrevNext(&$arr) {
+        global $CFG;
+
+        if (empty($CFG->xmldbreconstructprevnext)) {
+            return false;
+        }
+        $tweaked = false;
+
+        $prev = null;
+        foreach ($arr as $key=>$el) {
+            $prev_value = $arr[$key]->previous;
+            $next_value = $arr[$key]->next;
+
+            $arr[$key]->next     = null;;
+            $arr[$key]->previous = null;;
+            if ($prev !== null) {
+                $arr[$prev]->next    = $arr[$key]->name;
+                $arr[$key]->previous = $arr[$prev]->name;
+            }
+            $prev = $key;
+
+            if ($prev_value != $arr[$key]->previous or $next_value != $arr[$key]->next) {
+                $tweaked = true;
+            }
+        }
+
+        return $tweaked;
+    }
+    /**
      * This function will check that all the elements in one array
      * have a consistent info in their previous/next fields
      */
Index: lib/xmldb/classes/XMLDBStructure.class.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/xmldb/classes/XMLDBStructure.class.php,v
retrieving revision 1.10
diff -u -r1.10 XMLDBStructure.class.php
--- lib/xmldb/classes/XMLDBStructure.class.php	20 Apr 2007 00:10:53 -0000	1.10
+++ lib/xmldb/classes/XMLDBStructure.class.php	5 Jul 2007 13:50:07 -0000
@@ -395,6 +395,7 @@
                 $result = false;
             }
         /// Check previous & next are ok (duplicates and existing tables)
+            $this->fixPrevNext($this->tables);
             if ($result && !$this->checkPreviousNextValues($this->tables)) {
                 $this->errormsg = 'Some TABLES previous/next values are incorrect';
                 $this->debug($this->errormsg);
@@ -435,6 +436,7 @@
                 $result = false;
             }
         /// Check previous & next are ok (duplicates and existing statements)
+            $this->fixPrevNext($this->statements);
             if ($result && !$this->checkPreviousNextValues($this->statements)) {
                 $this->errormsg = 'Some STATEMENTS previous/next values are incorrect';
                 $this->debug($this->errormsg);
