]> git.mjollnir.org Git - moodle.git/commitdiff
Begin parsing xml data.
authorstronk7 <stronk7>
Fri, 16 May 2003 23:57:35 +0000 (23:57 +0000)
committerstronk7 <stronk7>
Fri, 16 May 2003 23:57:35 +0000 (23:57 +0000)
Info tags.

backup/restore_precheck.html
backup/restorelib.php

index 1f7211154657b48b5eab9123ba2180c0d3a096d4..8060138aa1aff7b316ca1ba88530bb3aa5047233 100644 (file)
 
     //Now check for the moodle.xml file
     if ($status) {
+        $xml_file  = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
         echo "<li>Checking backup file";
-        $status = restore_check_moodle_file ($CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml");
+        $status = restore_check_moodle_file ($xml_file);
+    }
+
+    //Now read the info tag (all)
+    if ($status) {
+        echo "<li>Reading info from file";
+        //Reading info from file
+        $info = restore_read_xml_info ($xml_file);
     }
 
     //End the main ul
index 96272f6df2960abfac18570e7b491934f593fe9c..f5433b9056671e1fc618cc2d698b1176489f57cf 100644 (file)
@@ -1,7 +1,7 @@
 <?PHP //$Id$
     //Functions used in restore
    
-    //This function unzips a zip file in the samen directory that it is
+    //This function unzips a zip file in the same directory that it is
     //It automatically uses pclzip or command line unzip
     function restore_unzip ($file,$moodle_home) {
         
 
         return $status;  
     }   
+
+    //This function read the xml file and store it data from the info section in an array
+    function restore_read_xml_info ($xml_file) {
+
+        //We call the main read_xml function, with todo = INFO
+        $info = restore_read_xml ($xml_file,"INFO",false);
+
+        return $info;
+
+        echo "finished";
+    }
+
+    //=====================================================================================
+    //==                                                                                 ==
+    //==                         XML Functions (SAX)                                     ==
+    //==                                                                                 ==
+    //=====================================================================================
+
+    //This is the class used to do all the xml parse
+    class MoodleParser {
+
+        var $level = 0;        //Level we are
+        var $tree = array();   //Array of levels we are
+        var $content = "";     //Content under current level
+        var $todo = "";        //What we hav to do when parsing
+        var $info = array();   //Information collected (todo = INFO)
+        var $preferences = ""; //Preferences about what to load !!
+        var $finished = false; //Flag to say xml_parse to stop
+
+        function startElement($parser, $tagName, $attrs) {
+            $this->tree[$this->level] = $tagName;
+            //echo str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br>\n";
+            $this->level++;
+        }
+
+        function endElement($parser, $tagName) {
+            if (trim($this->content)) {
+                //echo utf8_decode(str_repeat("&nbsp;",$this->level*2).$this->content."<br>\n");
+            }
+            $this->level--;
+            //echo str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br>\n";
+            $this->tree[$this->level] = "";
+            $this->content = "";
+
+            //Stop parsing if todo = INFO and tagName = INFO
+            //Speed up a lot (avoid parse all)
+            //echo $tagName."<br>";
+            if (($this->todo == "INFO") and ($tagName == "INFO")) {
+                $this->finished = true;
+            }
+        }
+
+        function characterData($parser, $data) {
+            $this->content .= $data;
+        }
+    }
+    
+    //This function executes the MoodleParser
+    function restore_read_xml ($xml_file,$todo,$preferences) {
+
+        $status = true;
+
+        $xml_parser = xml_parser_create();
+        $moodle_parser = new MoodleParser();
+        $moodle_parser->todo = $todo;
+        $moodle_parser->preferences = $preferences;
+        xml_set_object($xml_parser,&$moodle_parser);
+        xml_set_element_handler($xml_parser, "startElement", "endElement");
+        xml_set_character_data_handler($xml_parser, "characterData");
+        $fp = fopen($xml_file,"r")
+            or $status = false;
+        if ($status) {
+            while ($data = fread($fp, 4096) and !$moodle_parser->finished)
+                    xml_parse($xml_parser, $data, feof($fp))
+                            or die(sprintf("XML error: %s at line %d",
+                            xml_error_string(xml_get_error_code($xml_parser)),
+                                    xml_get_current_line_number($xml_parser)));
+            fclose($fp);
+        }
+        xml_parser_free($xml_parser);
+
+        if ($Status) {
+            return $info;
+        } else {
+            return $status;
+        }
+    }
 ?>