Skip to content

Commit

Permalink
MDL-21432 backup - using grouped parser in moodle_backup.xml + minor …
Browse files Browse the repository at this point in the history
…changes in common stuff
  • Loading branch information
stronk7 committed Jul 5, 2010
1 parent 94beb69 commit dc5a2f8
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 5 deletions.
9 changes: 5 additions & 4 deletions backup/backup.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
95 changes: 95 additions & 0 deletions backup/util/helper/backup_general_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,101 @@ public static function array_checksum_recursive($arr) {
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
*
Expand Down
58 changes: 58 additions & 0 deletions backup/util/helper/restore_moodlexml_parser_processor.class.php
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function add_path($path) {
/**
* 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
Expand Down

0 comments on commit dc5a2f8

Please sign in to comment.