Removing relics of the previous plan/tasks/steps based implementation

Kudos to Mark Nielsen for his initial work on this project. In his code,
Mark described the valuable logic of the conversion using a framework
mirroring plan/tasks/steps based architecture of the backup and restore.
My attempt to simplify it led to the current handlers-based
architecture.
This commit is contained in:
David Mudrak 2011-05-13 03:09:26 +02:00
parent 5f3185feb8
commit a5ea28b32d
7 changed files with 0 additions and 1254 deletions

View File

@ -1,830 +0,0 @@
<?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 core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* A general/abstract moodle1 structure step converter
*/
abstract class moodle1_structure_step extends convert_structure_step {
/**
* @var xml_writer
*/
protected $xmlwriter = NULL;
/**
* Return the relative path to the XML file that
* this step writes out to. Example: course/course.xml
*
* @abstract
* @return string
*/
abstract public function get_xml_filename();
/**
* Opens the XML writer - after calling, one is free
* to use $xmlwriter
*
* @return void
*/
public function open_xml_writer() {
if (!$this->xmlwriter instanceof xml_writer) {
$fullpath = $this->get_basepath() . '/' . $this->get_xml_filename();
$directory = pathinfo($fullpath, PATHINFO_DIRNAME);
if (!check_dir_exists($directory)) {
throw new backup_exception('failedtomakeconvertdir'); // @todo Define this string
}
$this->xmlwriter = new xml_writer(
new file_xml_output($fullpath)
);
$this->xmlwriter->start();
}
}
/**
* Close the XML writer
*
* At the moment, must close all tags before calling
*
* @return void
*/
public function close_xml_writer() {
if ($this->xmlwriter instanceof xml_writer) {
$this->xmlwriter->stop();
unset($this->xmlwriter);
$this->xmlwriter = NULL;
// var_dump(file_get_contents($this->get_basepath().'/'.$this->get_xml_filename())); // DEBUG
}
}
/**
* Return deprecated fields
*
* @return array
*/
public function get_deprecated() {
return array();
}
/**
* Return renamed fields
*
* The key is the old name and the value is the new name.
*
* @return array
*/
public function get_renamed() {
return array();
}
/**
* Return new fields and their values
*
* The key is the new field name and the value
* is the value to write to the XML file.
*
* @return array
*/
public function get_new() {
return array();
}
/**
* Last chance to modify the datum before
* it is written to the XML file.
*
* @param string $name The tag/field name
* @param mixed $datum The data belonging to $name
* @return mixed
*/
public function mutate_datum($name, $datum) {
return $datum;
}
/**
* General data converter
*
* Can remove deprecated fields, rename fields,
* and manipulate data values before writing
* to the XML file.
*
* @param array $data The array of data to process
* @return void
*/
public function convert_data(array $data) {
// print_object($data); // DEBUG
$this->open_xml_writer();
$deprecated = $this->get_deprecated();
$renamed = $this->get_renamed();
foreach ($data as $name => $datum) {
$name = strtolower($name);
if (in_array($name, $deprecated)) {
continue;
}
if (array_key_exists($name, $renamed)) {
$name = $renamed[$name];
}
$this->xmlwriter->full_tag($name, $this->mutate_datum($name, $datum));
}
foreach ($this->get_new() as $name => $datum) {
$this->xmlwriter->full_tag($name, $datum);
}
}
}
class moodle1_info_structure_step extends moodle1_structure_step {
/**
* @var array
*/
protected $info = array();
/**
* @var array
*/
protected $details = array();
/**
* @var array
*/
protected $mods = array();
/**
* @var string
*/
protected $currentmod;
/**
* Function that will return the structure to be processed by this convert_step.
* Must return one array of @convert_path_element elements
*/
protected function define_structure() {
return array(
new convert_path_element('info', '/MOODLE_BACKUP/INFO'),
new convert_path_element('details', '/MOODLE_BACKUP/INFO/DETAILS'),
new convert_path_element('mod', '/MOODLE_BACKUP/INFO/DETAILS/MOD'),
new convert_path_element('instance', '/MOODLE_BACKUP/INFO/DETAILS/MOD/INSTANCES/INSTANCE'),
);
}
/**
* Return the relative path to the XML file that
* this step writes out to. Example: course/course.xml
*
* @return string
*/
public function get_xml_filename() {
return 'moodle_backup.xml';
}
public function get_new() {
global $DB;
$course = $DB->get_record('backup_ids_temp', array('itemname' => 'course'), '*', MUST_EXIST);
$info = unserialize(base64_decode($course->info));
return array(
'mnet_remoteusers' => 0,
'original_site_identifier_hash' => '?', // @todo What is this?
'original_course_id' => $course->itemid,
'original_course_fullname' => $info['fullname'],
'original_course_shortname' => $info['shortname'],
'original_course_startdate' => $info['startdate'],
'original_course_contextid' => $info['contextid'],
'original_system_contextid' => convert_helper::get_contextid(0, 'system', $this->get_convertid()),
);
}
public function get_renamed() {
return parent::get_renamed();
}
public function get_deprecated() {
return parent::get_deprecated();
}
public function convert_info($data) {
// print_object($data); // DEBUG
$this->info = $data;
}
public function convert_details($data) {
// print_object($data); // DEBUG
$this->details = $data;
}
public function convert_mod($data) {
// print_object($data); // DEBUG
$this->currentmod = $data['NAME'];
$this->mods[$this->currentmod] = array();
}
public function convert_instance($data) {
// print_object($data); // DEBUG
$this->mods[$this->currentmod][$data['ID']] = $data;
}
public function execute_after_convert() {
global $DB;
$rootsettings = array(
'filename' => $this->info['NAME'],
'users' => 0, // @todo Add support for users
'anonymize' => 0, // @todo Correct?
'role_assignments' => 0, // @todo Add support for users
'user_files' => 0, // @todo Add support for users
'activities' => 1,
'blocks' => 1,
'filters' => 0, // @todo Add support for filters
'comments' => 0,
'userscompletion' => 0,
'logs' => 0,
'grade_histories' => 0, // @todo Add support for users
);
$settings = array();
foreach ($rootsettings as $name => $value) {
$settings[] = array(
'level' => 'root',
'name' => $name,
'value' => $value,
);
}
$this->open_xml_writer();
$this->xmlwriter->begin_tag('moodle_backup');
$this->xmlwriter->begin_tag('information');
$this->convert_data($this->info);
$this->xmlwriter->begin_tag('details');
$this->xmlwriter->begin_tag('detail', array('backup_id' => $this->get_convertid()));
$this->xmlwriter->full_tag('type', 'course');
$this->xmlwriter->full_tag('format', 'moodle2');
$this->xmlwriter->full_tag('interactive', 0); // @todo Correct?
$this->xmlwriter->full_tag('mode', backup::MODE_GENERAL);
$this->xmlwriter->full_tag('execution', 1); // @todo Correct?
$this->xmlwriter->full_tag('executiontime', 0);
$this->xmlwriter->end_tag('detail');
$this->xmlwriter->end_tag('details');
$this->xmlwriter->begin_tag('contents');
$this->xmlwriter->begin_tag('activities');
foreach ($this->mods as $type => $instances) {
$records = $DB->get_records('backup_ids_temp', array('backupid' => $this->get_convertid(), 'itemname' => $type));
foreach ($records as $record) {
$info = unserialize(base64_decode($record->info));
$key = "{$record->itemname}_{$record->parentitemid}";
if (array_key_exists($record->itemid, $instances)) {
$title = $instances[$record->itemid]['NAME'];
$included = (int) $instances[$record->itemid]['INCLUDED'];
} else {
$title = 'UNKNOWN';
$included = 1;
}
$settings[] = array(
'level' => 'activity',
'activity' => $key,
'name' => "{$key}_included",
'value' => $included,
);
$this->xmlwriter->begin_tag('activity');
$this->xmlwriter->full_tag('moduleid', $record->parentitemid);
$this->xmlwriter->full_tag('sectionid', $info['sectionid']);
$this->xmlwriter->full_tag('modulename', $record->itemname);
$this->xmlwriter->full_tag('title', $title);
$this->xmlwriter->full_tag('directory', "activities/$key");
$this->xmlwriter->end_tag('activity');
}
}
$this->xmlwriter->end_tag('activities');
$this->xmlwriter->begin_tag('sections');
$records = $DB->get_records('backup_ids_temp', array('backupid' => $this->get_convertid(), 'itemname' => 'section'));
foreach ($records as $record) {
$info = unserialize(base64_decode($record->info));
$settings[] = array(
'level' => 'section',
'section' => "section_$record->itemid",
'name' => "section_{$record->itemid}_userinfo",
'value' => 0, // @todo Add support for user info
);
$this->xmlwriter->begin_tag('section');
$this->xmlwriter->full_tag('sectionid', $record->itemid);
$this->xmlwriter->full_tag('title', $info['title']);
$this->xmlwriter->full_tag('directory', $info['directory']);
$this->xmlwriter->end_tag('section');
}
$this->xmlwriter->end_tag('sections');
$course = $DB->get_record('backup_ids_temp', array('itemname' => 'course'), '*', MUST_EXIST);
$info = unserialize(base64_decode($course->info));
$this->xmlwriter->begin_tag('course');
$this->xmlwriter->full_tag('courseid', $course->itemid);
$this->xmlwriter->full_tag('title', $info['fullname']);
$this->xmlwriter->full_tag('directory', 'course');
$this->xmlwriter->end_tag('course');
$this->xmlwriter->end_tag('contents');
$this->xmlwriter->begin_tag('settings');
foreach ($settings as $setting) {
$this->xmlwriter->begin_tag('setting');
foreach ($setting as $name => $value) {
$this->xmlwriter->full_tag($name, $value);
}
$this->xmlwriter->end_tag('setting');
}
$this->xmlwriter->end_tag('settings');
$this->xmlwriter->end_tag('information');
$this->xmlwriter->end_tag('moodle_backup');
$this->close_xml_writer();
}
}
class moodle1_course_structure_step extends moodle1_structure_step {
/**
* @var array
*/
protected $course = array();
/**
* @var array
*/
protected $category = array();
/**
* Function that will return the structure to be processed by this convert_step.
* Must return one array of @convert_path_element elements
*/
protected function define_structure() {
return array(
new convert_path_element('course', '/MOODLE_BACKUP/COURSE/HEADER'),
new convert_path_element('category', '/MOODLE_BACKUP/COURSE/HEADER/CATEGORY'),
);
}
/**
* Return the relative path to the XML file that
* this step writes out to. Example: course/course.xml
*
* @return string
*/
public function get_xml_filename() {
return 'course/course.xml';
}
public function get_new() {
return array(
'summaryformat' => 1,
'legacyfiles' => 1, // @todo I think this is correct
'requested' => 0, // @todo Not really new, but maybe never backed up?
'restrictmodules' => 0,
'enablecompletion' => 0,
'completionstartonenrol' => 0,
'completionnotify' => 0,
);
}
public function get_deprecated() {
return array(
'roles_overrides',
'roles_assignments',
'cost',
'currancy',
'defaultrole',
'enrol',
'enrolenddate',
'enrollable',
'enrolperiod',
'enrolstartdate',
'expirynotify',
'expirythreshold',
'guest',
'notifystudents',
'password',
'student',
'students',
'teacher',
'teachers',
'metacourse',
);
}
/**
* This is actually called twice because category is defined
* right after ID in the XML... any way around that? Only
* idea is to patch Moodle 1.9
*
* @throws backup_exception
* @param $data
* @return void
*/
public function convert_course($data) {
// print_object($data); // DEBUG
$this->course = array_merge($this->course, $data);
}
public function convert_category($data) {
// print_object($data); // DEBUG
$this->category = $data;
}
public function execute_after_convert() {
global $DB;
$this->open_xml_writer();
$contextid = convert_helper::get_contextid($this->course['ID'], 'course');
$this->xmlwriter->begin_tag('course', array(
'id' => $this->course['ID'],
'contextid' => $contextid,
));
$this->convert_data($this->course);
$DB->insert_record('backup_ids_temp', (object) array(
'backupid' => $this->get_convertid(),
'itemname' => 'course',
'itemid' => $this->course['ID'],
'info' => base64_encode(serialize(array(
'fullname' => $this->course['FULLNAME'],
'shortname' => $this->course['SHORTNAME'],
'startdate' => $this->course['STARTDATE'],
'contextid' => $contextid,
))),
));
$this->xmlwriter->begin_tag('category', array('id' => $this->category['ID']));
$this->xmlwriter->full_tag('name', $this->category['NAME']);
$this->xmlwriter->full_tag('description', NULL);
$this->xmlwriter->end_tag('category');
$this->xmlwriter->full_tag('tags', NULL);
$this->xmlwriter->full_tag('allowed_modules', NULL);
$this->xmlwriter->end_tag('course');
$this->close_xml_writer();
}
}
/**
* Converts all of the sections
*/
class moodle1_section_structure_step extends moodle1_structure_step {
/**
* Section ID
* @var int
*/
protected $id;
/**
* Module ID sequence
* @var array
*/
protected $sequence = array();
/**
* Section data array
* @var array
*/
protected $data = array();
/**
* Return the relative path to the XML file that
* this step writes out to. Example: course/course.xml
*
* @return string
*/
public function get_xml_filename() {
return "sections/section_$this->id/section.xml";
}
/**
* Function that will return the structure to be processed by this convert_step.
* Must return one array of @convert_path_element elements
*/
protected function define_structure() {
$paths = array();
$paths[] = new convert_path_element('section', '/MOODLE_BACKUP/COURSE/SECTIONS/SECTION');
$paths[] = new convert_path_element('mod', '/MOODLE_BACKUP/COURSE/SECTIONS/SECTION/MODS/MOD');
return $paths;
}
/**
* This grabs section ID and stores data.
* We must process the section's modules before
* the section can be written out to XML
*
* @param $data
* @return void
*/
public function convert_section($data) {
if (!empty($this->id)) {
$this->write_section_xml();
}
// print_object($data); // DEBUG
$this->id = $data['ID'];
$this->data = $data;
}
/**
* Writes out section XML
*
* @return void
*/
public function write_section_xml() {
global $DB;
$this->open_xml_writer();
$this->xmlwriter->begin_tag('section', array('id' => $this->data['ID']));
$this->xmlwriter->full_tag('number', $this->data['NUMBER']);
$this->xmlwriter->full_tag('name', NULL);
$this->xmlwriter->full_tag('summary', $this->data['SUMMARY']);
$this->xmlwriter->full_tag('summaryformat', 1);
$this->xmlwriter->full_tag('sequence', implode(',', $this->sequence));
$this->xmlwriter->full_tag('visible', $this->data['VISIBLE']);
$this->xmlwriter->end_tag('section');
$this->close_xml_writer();
$DB->insert_record('backup_ids_temp', (object) array(
'backupid' => $this->get_convertid(),
'itemname' => 'section',
'itemid' => $this->data['ID'],
'info' => base64_encode(serialize(array(
'title' => $this->data['NUMBER'], // @todo Correct?
'directory' => pathinfo($this->get_xml_filename(), PATHINFO_DIRNAME),
))),
));
// Reset
$this->id = 0;
$this->sequence = array();
$this->data = array();
}
/**
* Converting section module data - store
* id for section sequence then throw all the
* module data in the database for later use.
*
* @param $data
* @return void
*/
public function convert_mod($data) {
global $DB;
// print_object($data); // DEBUG
unset(
$data['ROLES_OVERRIDES'],
$data['ROLES_ASSIGNMENTS']
);
$this->sequence[] = $data['ID'];
$info = array();
$info['sectionid'] = $this->id;
$info['sectionnumber'] = $this->data['NUMBER'];
foreach ($data as $name => $value) {
$info[strtolower($name)] = $value;
}
$temp = new stdClass;
$temp->itemname = $data['TYPE'];
$temp->itemid = $data['INSTANCE'];
$temp->parentitemid = $data['ID'];
$temp->info = base64_encode(serialize($info));
$DB->insert_record('backup_ids_temp', $temp);
}
/**
* Write out any section still left in memory
*/
public function execute_after_convert() {
if (!empty($this->id)) {
$this->write_section_xml();
}
}
}
/**
* Writes out an activity's module.xml file
*/
class moodle1_module_structure_step extends moodle1_structure_step {
/**
* Module name
*
* @var string
*/
protected $type;
/**
* The current Module ID
*
* @var int
*/
protected $moduleid;
/**
* @param string $type The module name
*/
public function __construct($name, $type, convert_task $task = null) {
$this->type = $type;
parent::__construct($name, $task);
}
/**
* Function that will return the structure to be processed by this convert_step.
* Must return one array of @convert_path_element elements
*/
protected function define_structure() {
return array(); // @todo Hack?
}
/**
* Return the relative path to the XML file that
* this step writes out to. Example: course/course.xml
*
* @return string
*/
public function get_xml_filename() {
return "activities/{$this->type}_{$this->moduleid}/module.xml";
}
public function get_new() {
return array(
'visibleold' => 1,
'completion' => 0,
'completiongradeitemnumber' => NULL,
'completionview' => 0,
'completionexpected' => 0,
'availablefrom' => 0,
'availableuntil' => 0,
'showavailability' => 1,
'availability_info' => NULL,
);
}
public function get_renamed() {
return array(
'type' => 'modulename',
);
}
public function get_deprecated() {
return array('id', 'instance');
}
public function execute_after_convert() {
global $DB;
$records = $DB->get_records('backup_ids_temp', array('backupid' => $this->get_convertid(), 'itemname' => $this->type));
foreach ($records as $record) {
$this->moduleid = $record->parentitemid;
$this->open_xml_writer();
$this->xmlwriter->begin_tag('module', array('id' => $this->moduleid, 'version' => '?')); // @todo What to do for version?
$this->convert_data(unserialize(base64_decode($record->info)));
$this->xmlwriter->end_tag('module');
$this->close_xml_writer();
}
}
}
/**
* Write out a block's block.xml file
*/
class moodle1_block_structure_step extends moodle1_structure_step {
/**
* The block's instance ID
*
* @var int
*/
protected $instanceid;
/**
* The block's name
*
* @var string
*/
protected $blockname;
/**
* Function that will return the structure to be processed by this convert_step.
* Must return one array of @convert_path_element elements
*/
protected function define_structure() {
return array(
new convert_path_element('block', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK'),
);
}
/**
* Return the relative path to the XML file that
* this step writes out to. Example: course/course.xml
*
* @return string
*/
public function get_xml_filename() {
return "course/blocks/{$this->blockname}_{$this->instanceid}/block.xml";
}
public function get_new() {
return array(
'showinsubcontexts' => 0,
'subpagepattern' => NULL,
'block_positions' => NULL,
);
}
public function get_renamed() {
return array(
'name' => 'blockname',
'pageid' => 'parentcontextid',
'pagetype' => 'pagetypepattern',
'position' => 'defaultregion',
'weight' => 'defaultweight',
);
}
public function get_deprecated() {
return array(
'id',
'visible',
'roles_overrides',
'roles_assignments',
);
}
public function mutate_datum($name, $datum) {
if ($name == 'pagetypepattern') {
if ($datum == 'course-view') {
$datum = 'course-view-*';
}
} else if ($name == 'defaultregion') {
if ($datum == 'r') {
$datum = BLOCK_POS_RIGHT;
} else {
$datum = BLOCK_POS_LEFT;
}
}
return $datum;
}
public function convert_block($data) {
$this->instanceid = $data['ID'];
$this->blockname = $data['NAME'];
$contextid = convert_helper::get_contextid($this->instanceid, "block_$this->name", $this->get_convertid());
// Map PAGEID to context ID
if ($data['PAGETYPE'] == 'course-view') {
$data['PAGEID'] = convert_helper::get_contextid($data['PAGEID'], 'course', $this->get_convertid());
} else if (strpos($data['PAGETYPE'], 'mod-') === 0) { // EG: mod-quiz-view
$parts = explode('-', $data['PAGETYPE']);
$data['PAGEID'] = convert_helper::get_contextid($data['PAGEID'], $parts[1], $this->get_convertid());
}
$this->open_xml_writer();
$this->xmlwriter->begin_tag('block', array('id' => $this->instanceid, 'contextid' => $contextid));
$this->convert_data($data);
$this->xmlwriter->end_tag('block');
$this->close_xml_writer();
}
}

View File

@ -1,115 +0,0 @@
<?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 core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
abstract class convert_file_manager {
public static function convert_course_files() {
}
public static function convert_file($converter, $filepath, $mod_context) {
global $DB;
// make a dummy record in the temp tabke to get auto gen FILEID
$backupid = $converter->get_id();
$dummy = new stdClass;
$dummy->backupid = $backupid;
$dummy->itemname = random_string(32);
if(!$fileid = $DB->insert_record('backup_ids_temp', $dummy)) {
// Replace with a converter_exception?
$info = convert_helper::obj_to_readable($dummy);
throw new Exception(sprintf("Could not insert dummy record with info: (%s)", $info));
}
// TODO: make this look at moddata as well
$converter_path = $converter->get_convertdir();
$oldpath = $converter->get_tempdir();
$converted_course_files = "$oldpath/course_files";
// Start processing the file now
$pathinfo = pathinfo($filepath);
// Try to get the base
$base = str_replace($converted_course_files, '', $pathinfo['dirname']);
// if the $base is the same as before, then it is root
if($base == $pathinfo['dirname']) {
$path = '/';
} else if(is_dir($filepath)) {
$path = '/'. $pathinfo['basename'] . '/';
} else {
$path = $base . '/';
}
// TODO: Might need to spoof more fields
$file_data = new stdClass;
$file_data->id = $fileid;
$file_data->contenthash = sha1_file($filepath);
$file_data->mod_context = $mod_context;
$file_data->filename = is_dir($filepath) ? '.' : $pathinfo['basename'];
$file_data->filesize = is_dir($filepath) ? '0' : filesize($filepath);
$file_data->mimetype = is_dir($filepath) ? '$@NULL@$' : mimeinfo('type', $pathinfo['basename']);
$file_data->created = time();
$file_data->modified = time();
// Move in-memory file data to the real deal on the server
$hashdir = substr($file_data->contenthash, 0, 2);
$folders = array(
"$converted_path/files",
"$converted_path/files/$hashdir"
);
if(!array_reduce($folders, function($in, $folder) {
return $in and (file_exists($folder) or mkdir($folder));
}, true)) {
throw new Exception("Could not create temp dirs for file");
}
// Move file
$new_path = $folders[1]."/$file_data->contenthash";
if(!rename($filepath, $new_path) {
throw new Exception(sprintf("Could not move file into %s", $new_path));
}
// Time to insert this record now
$fs_record = new stdClass;
$fs_record->id = $fileid;
$fs_record->backupid = $backupid;
$fs_ercord->itemname = 'file';
$fs_record->itemid = $fileid
$fs_record->info = serialize($file_data);
if(!$DB->update_record('backup_ids_temp', $fs_record)) {
$info = convert_helper::obj_to_readable($fs_record);
throw new Exception(sprintf("Could not update with real info: (%s)", $info));
}
// TODO: register inforef
return true;
}
public static function get_temp_file_info($info) {
global $DB;
}
public static function create_files_xml() {
}
}

View File

@ -1,40 +0,0 @@
<?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 core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Executable step for converters
*/
abstract class convert_execution_step extends convert_step {
public function execute() {
return $this->define_execution();
}
/**
* Function that will contain all the code to be executed
*/
abstract protected function define_execution();
}

View File

@ -1,85 +0,0 @@
<?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 core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Convert plan
*/
class convert_plan extends base_plan implements loggable {
/**
* @var plan_converter
*/
protected $converter;
public function __construct(plan_converter $converter) {
$this->converter = $converter;
parent::__construct('convert_plan');
}
/**
* This function will be responsible for handling the params, and to call
* to the corresponding logger->process() once all modifications in params
* have been performed
*/
public function log($message, $level, $a = null, $depth = null, $display = false) {
// TODO: Implement log() method.
}
public function get_basepath() {
return $this->converter->get_workdir_path();
}
/**
* @return plan_converter
*/
public function get_converter() {
return $this->converter;
}
public function get_converterid() {
return $this->converter->get_id();
}
/**
* Function responsible for building the tasks of any plan
* with their corresponding settings
* (must set the $built property to true)
*/
public function build() {
// This seems circular for no real reason....
$this->converter->build_plan();
$this->built = true;
}
/**
* Execute the after_restore methods of all the executed tasks in the plan
*/
public function execute_after_convert() {
// Simply iterate over each task in the plan and delegate to them the execution
foreach ($this->tasks as $task) {
$task->execute_after_convert();
}
}
}

View File

@ -1,59 +0,0 @@
<?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 core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Convert step
*
* @throws backup_exception
*/
abstract class convert_step extends base_step {
public function __construct($name, convert_task $task = null) {
parent::__construct($name, $task);
}
protected function get_convertid() {
if (!$this->task instanceof convert_task) {
throw new backup_exception('not_specified_convert_task'); // @todo Define string
}
return $this->task->get_convertid();
}
/**
* @throws backup_exception
* @return plan_converter
*/
protected function get_converter() {
if (!$this->task instanceof convert_task) {
throw new backup_exception('not_specified_convert_task'); // @todo Define string
}
return $this->task->get_converter();
}
public function execute_after_convert() {
// Default nothing
}
}

View File

@ -1,68 +0,0 @@
<?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 core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
abstract class convert_structure_step extends convert_step {
final public function execute() {
if (!$this->execute_condition()) { // Check any condition to execute this
return;
}
// Get restore_path elements array adapting and preparing it for processing
$structure = $this->define_structure();
if (!is_array($structure)) {
throw new restore_step_exception('restore_step_structure_not_array', $this->get_name()); // @todo Change exception
}
$this->get_converter()->add_structures($this, $structure);
}
/**
* As far as restore structure steps are implementing restore_plugin stuff, they need to
* have the parent task available for wrapping purposes (get course/context....)
*/
public function get_task() {
return $this->task;
}
/**
* To conditionally decide if one step will be executed or no
*
* For steps needing to be executed conditionally, based in dynamic
* conditions (at execution time vs at declaration time) you must
* override this function. It will return true if the step must be
* executed and false if not
*/
protected function execute_condition() {
return true;
}
/**
* Function that will return the structure to be processed by this convert_step.
* Must return one array of @convert_path_element elements
*/
abstract protected function define_structure();
}

View File

@ -1,57 +0,0 @@
<?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 core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Convert task
*/
abstract class convert_task extends base_task {
public function __construct($name, convert_plan $plan = null) {
parent::__construct($name, $plan);
}
public function get_convertid() {
return $this->plan->get_converterid();
}
/**
* @return plan_converter
*/
public function get_converter() {
return $this->plan->get_converter();
}
protected function define_settings() {
// None
}
public function execute_after_convert() {
if ($this->executed) {
foreach ($this->steps as $step) {
$step->execute_after_convert();
}
}
}
}