MDL-21432 backup - using grouped parser in moodle_backup.xml + minor changes in commo...
authorEloy Lafuente <stronk7@moodle.org>
Mon, 5 Jul 2010 22:10:51 +0000 (22:10 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Mon, 5 Jul 2010 22:10:51 +0000 (22:10 +0000)
backup/backup.class.php
backup/util/helper/backup_general_helper.class.php
backup/util/helper/restore_moodlexml_parser_processor.class.php [new file with mode: 0644]
backup/util/xml/parser/processors/simplified_parser_processor.class.php

index 9d0113d..03766fe 100644 (file)
@@ -64,10 +64,11 @@ abstract class backup implements checksumable {
     const STATUS_PLANNED     = 300;
     const STATUS_CONFIGURED  = 400;
     const STATUS_SETTING_UI  = 500;
-    const STATUS_AWAITING    = 600;
-    const STATUS_EXECUTING   = 700;
-    const STATUS_FINISHED_ERR= 800;
-    const STATUS_FINISHED_OK = 900;
+    const STATUS_NEED_PRECHECK=600;
+    const STATUS_AWAITING    = 700;
+    const STATUS_EXECUTING   = 800;
+    const STATUS_FINISHED_ERR= 900;
+    const STATUS_FINISHED_OK =1000;
 
     // Logging levels
     const LOG_DEBUG   = 50;
index 17f0c3e..9a2d681 100644 (file)
@@ -56,6 +56,101 @@ abstract class backup_general_helper extends backup_helper {
         return $checksum;
     }
 
+    /**
+     * Load and format all the needed information from moodle_backup.xml
+     *
+     * This function loads and process all the moodle_backup.xml
+     * information, composing a big information structure that will
+     * be the used by the plan builder in order to generate the
+     * appropiate tasks / steps / settings
+     */
+    public static function get_backup_information($tempdir) {
+        global $CFG;
+
+        $info = new stdclass(); // Final information goes here
+
+        $moodlefile = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle_backup.xml';
+        if (!file_exists($moodlefile)) { // Shouldn't happen ever, but...
+            throw new backup_helper_exception('missing_moodle_backup_xml_file');
+        }
+        // Load the entire file to in-memory array
+        $xmlparser = new progressive_parser();
+        $xmlparser->set_file($moodlefile);
+        $xmlprocessor = new restore_moodlexml_parser_processor();
+        $xmlparser->set_processor($xmlprocessor);
+        $xmlparser->process();
+        $infoarr = $xmlprocessor->get_all_chunks();
+        if (count($infoarr) !== 1) { // Shouldn't happen ever, but...
+            throw new backup_helper_exception('problem_parsing_moodle_backup_xml_file');
+        }
+        $infoarr = $infoarr[0]['tags']; // for commodity
+
+        // Let's build info
+        $info->moodle_version = $infoarr['moodle_version'];
+        $info->moodle_release = $infoarr['moodle_release'];
+        $info->backup_version = $infoarr['backup_version'];
+        $info->backup_release = $infoarr['backup_release'];
+        $info->backup_date    = $infoarr['backup_date'];
+        $info->original_wwwroot         = $infoarr['original_wwwroot'];
+        $info->original_site_identifier = $infoarr['original_site_identifier'];
+        $info->original_course_id       = $infoarr['original_course_id'];
+        $info->type   =  $infoarr['details']['detail'][0]['type'];
+        $info->format =  $infoarr['details']['detail'][0]['format'];
+        $info->mode   =  $infoarr['details']['detail'][0]['mode'];
+
+        // Now the contents
+        $contentsarr = $infoarr['contents'];
+        if (isset($contentsarr['course']) && isset($contentsarr['course'][0])) {
+            $info->course = new stdclass();
+            $info->course = (object)$contentsarr['course'][0];
+            $info->course->settings = array();
+        }
+        if (isset($contentsarr['sections']) && isset($contentsarr['sections']['section'])) {
+            $sectionarr = $contentsarr['sections']['section'];
+            $sections = array();
+            foreach ($sectionarr as $section) {
+                $section = (object)$section;
+                $section->settings = array();
+                $sections[basename($section->directory)] = $section;
+            }
+            $info->sections = $sections;
+        }
+        if (isset($contentsarr['activities']) && isset($contentsarr['activities']['activity'])) {
+            $activityarr = $contentsarr['activities']['activity'];
+            $activities = array();
+            foreach ($activityarr as $activity) {
+                $activity = (object)$activity;
+                $activity->settings = array();
+                $activities[basename($activity->directory)] = $activity;
+            }
+            $info->activities = $activities;
+        }
+        $info->root_settings = array(); // For root settings
+
+        // Now the settings, putting each one under its owner
+        $settingsarr = $infoarr['settings']['setting'];
+        foreach($settingsarr as $setting) {
+            switch ($setting['level']) {
+                case 'root':
+                    $info->root_settings[$setting['name']] = $setting['value'];
+                    break;
+                case 'course':
+                    $info->course->settings[$setting['name']] = $setting['value'];
+                    break;
+                case 'section':
+                    $info->sections[$setting['section']]->settings[$setting['name']] = $setting['value'];
+                    break;
+                case 'activity':
+                    $info->activities[$setting['activity']]->settings[$setting['name']] = $setting['value'];
+                    break;
+                default: // Shouldn't happen
+                    throw new backup_helper_exception('wrong_setting_level_moodle_backup_xml_file', $setting['level']);
+            }
+        }
+
+        return $info;
+    }
+
     /**
      * Given one temp/backup/xxx dir, detect its format
      *
diff --git a/backup/util/helper/restore_moodlexml_parser_processor.class.php b/backup/util/helper/restore_moodlexml_parser_processor.class.php
new file mode 100644 (file)
index 0000000..f95dfb0
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-helper
+ * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once($CFG->dirroot.'/backup/util/xml/parser/processors/grouped_parser_processor.class.php');
+
+/**
+ * helper implementation of grouped_parser_processor that will
+ * return all the information present in the moodle_backup.xml file
+ * accumulating it for later generation of controller->info
+*
+ * TODO: Complete phpdocs
+ */
+class restore_moodlexml_parser_processor extends grouped_parser_processor {
+
+    protected $accumchunks;
+
+    public function __construct() {
+        $this->accumchunks = array();
+        parent::__construct();
+        // Let's add all the paths we are interested on
+        $this->add_path('/moodle_backup/information', true); // Everything will be grouped below this
+        $this->add_path('/moodle_backup/information/details/detail');
+        $this->add_path('/moodle_backup/information/contents/activities/activity');
+        $this->add_path('/moodle_backup/information/contents/sections/section');
+        $this->add_path('/moodle_backup/information/contents/course');
+        $this->add_path('/moodle_backup/information/settings/setting');
+    }
+
+    protected function dispatch_chunk($data) {
+        $this->accumchunks[] = $data;
+    }
+
+    public function get_all_chunks() {
+        return $this->accumchunks;
+    }
+
+}
index 19a2e57..a204a6d 100644 (file)
@@ -61,7 +61,7 @@ abstract class simplified_parser_processor extends progressive_parser_processor
     /**
      * Get the already simplified chunk and dispatch it
      */
-    abstract public function dispatch_chunk($data);
+    abstract protected function dispatch_chunk($data);
 
     /**
      * Get one chunk of parsed data and make it simpler