From: stronk7 <stronk7> Date: Fri, 16 May 2003 23:57:35 +0000 (+0000) Subject: Begin parsing xml data. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=785ae439eb20f73ffeae5be4208d4254904ccb28;p=moodle.git Begin parsing xml data. Info tags. --- diff --git a/backup/restore_precheck.html b/backup/restore_precheck.html index 1f72111546..8060138aa1 100644 --- a/backup/restore_precheck.html +++ b/backup/restore_precheck.html @@ -59,8 +59,16 @@ //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 diff --git a/backup/restorelib.php b/backup/restorelib.php index 96272f6df2..f5433b9056 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -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) { @@ -46,4 +46,91 @@ 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(" ",$this->level*2)."<".$tagName."><br>\n"; + $this->level++; + } + + function endElement($parser, $tagName) { + if (trim($this->content)) { + //echo utf8_decode(str_repeat(" ",$this->level*2).$this->content."<br>\n"); + } + $this->level--; + //echo str_repeat(" ",$this->level*2)."</".$tagName."><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; + } + } ?>