mirror of
https://github.com/moodle/moodle.git
synced 2025-04-15 13:33:52 +02:00
MDL-15670 Database export/import library - by Andrei Bautu, minor tweaks only - big thanks!
This commit is contained in:
parent
4ce763762e
commit
7cd18ebabf
@ -5,7 +5,7 @@
|
||||
<xs:sequence>
|
||||
<xs:element maxOccurs="unbounded" name="table" type="tableType" />
|
||||
</xs:sequence>
|
||||
<xs:attribute name="version" type="xs:positiveInteger" use="required" />
|
||||
<xs:attribute name="version" type="xs:float" use="required" />
|
||||
<xs:attribute name="timestamp" type="xs:dateTime" use="required" />
|
||||
<xs:attribute name="comment" type="xs:string" />
|
||||
</xs:complexType>
|
||||
|
380
admin/dbtransfer/exportlib.php
Normal file
380
admin/dbtransfer/exportlib.php
Normal file
@ -0,0 +1,380 @@
|
||||
<?php //$Id$
|
||||
|
||||
/**
|
||||
* General database import classes
|
||||
* @author Andrei Bautu
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package dbtransfer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base class for database export operations. This class defines basic callbacks
|
||||
* for export operations and implements the @see export_database and
|
||||
* @export_table methods generic export processing. In general, subclasses will
|
||||
* override callback methods to provide specific output and (optionally)
|
||||
* @see export_database to add functionality.
|
||||
* Between a single pair of calls to @see begin_database_export and
|
||||
* @see finish_database_export, multiple non-overlapping pairs of calls may
|
||||
* be made to @see begin_table_export and @see finish_database_export for
|
||||
* different tables. Between one pair of calls to @see begin_table_export and
|
||||
* @see finish_database_export multiple calls may be made to
|
||||
* @see export_table_data for the same table.
|
||||
*/
|
||||
abstract class database_exporter {
|
||||
/** Connection to the source database (a @see moodle_database object). */
|
||||
protected $mdb;
|
||||
/** Database manager of the source database (a @see database_manager object). */
|
||||
protected $manager;
|
||||
/** Source database schema in XMLDB format (a @see xmldb_structure object). */
|
||||
protected $schema;
|
||||
/**
|
||||
* Boolean flag - whether or not to check that XML database schema matches
|
||||
* the RDBMS database schema before exporting (used by
|
||||
* @see export_database).
|
||||
*/
|
||||
protected $check_schema;
|
||||
|
||||
/**
|
||||
* Object constructor.
|
||||
*
|
||||
* @param moodle_database $mdb Connection to the source database (a
|
||||
* @see moodle_database object).
|
||||
* @param xmldb_structure $schema Source database schema in XMLDB format (a
|
||||
* @see xmldb_structure object). Use null to load the schema from the
|
||||
* system's install.xml files.
|
||||
* @param boolean $check_schema - whether or not to check that XML database
|
||||
* schema matches the RDBMS database schema before exporting (used by
|
||||
* @see export_database).
|
||||
*/
|
||||
public function __construct(moodle_database $mdb, xmldb_structure $schema=null, $check_schema=true) {
|
||||
if (is_null($schema)) {
|
||||
$schema = database_manager::get_install_xml_schema();
|
||||
}
|
||||
$this->mdb = $mdb;
|
||||
$this->manager = $mdb->get_manager();
|
||||
$this->schema = $schema;
|
||||
$this->check_schema = $check_schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once database per export
|
||||
* operation, before any other export operations. Subclasses should export
|
||||
* basic database information (version and timestamp).
|
||||
*
|
||||
* @param float $version the version of the system which generating the data
|
||||
* @param string $timestamp the timestamp of the data (in ISO 8601) format.
|
||||
* @param string $description a user description of the data.
|
||||
* @return void
|
||||
*/
|
||||
public function begin_database_export($version, $timestamp, $description) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once per table export operation,
|
||||
* before any other table export operations. Subclasses should export
|
||||
* basic database information (name and schema's hash).
|
||||
*
|
||||
* @param xmldb_table $table - XMLDB object for the exported table
|
||||
* @return void
|
||||
*/
|
||||
public function begin_table_export(xmldb_table $table) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once per table export operation,
|
||||
* after all other table export operations.
|
||||
*
|
||||
* @param xmldb_table $table - XMLDB object for the exported table
|
||||
*/
|
||||
public function finish_table_export(xmldb_table $table) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once database per export
|
||||
* operation, after all database export operations.
|
||||
*/
|
||||
public function finish_database_export() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once per record export operation,
|
||||
* only between @see begin_table_export and @see finish_table_export calls.
|
||||
* It will insert table data. Subclasses should export basic record
|
||||
* information (data values).
|
||||
*
|
||||
* @param xmldb_table $table - XMLDB object of the table from which data was retrived
|
||||
* @param object $data - data object (fields and values from record)
|
||||
* @return void
|
||||
*/
|
||||
public abstract function export_table_data(xmldb_table $table, $data);
|
||||
|
||||
/**
|
||||
* Generic method to export the database. It checks the schema (if
|
||||
* @see $check_schema is true), queries the database and calls
|
||||
* appropiate callbacks.
|
||||
*
|
||||
* @exception export_exception if any checking (e.g. database schema) fails
|
||||
*
|
||||
* @param string $description a user description of the data.
|
||||
*/
|
||||
public function export_database($description=null) {
|
||||
global $CFG;
|
||||
|
||||
if ($this->check_schema && $this->manager->check_database_schema($this->schema)) {
|
||||
//TODO put message in error lang
|
||||
throw new export_exception('XMLDB schema does not match database schema.');
|
||||
}
|
||||
$tables = $this->schema->getTables();
|
||||
$this->begin_database_export($CFG->version, date('c'), $description);
|
||||
foreach ($tables as $table) {
|
||||
$rs = $this->mdb->get_recordset_sql('SELECT * FROM {'.$table->getName().'}');
|
||||
//TODO remove this when dml will have exceptions
|
||||
if (!$rs) {
|
||||
//TODO put message in error lang
|
||||
throw new export_exception('An error occured while reading the database.');
|
||||
}
|
||||
$this->begin_table_export($table);
|
||||
foreach ($rs as $row) {
|
||||
$this->export_table_data($table, $row);
|
||||
}
|
||||
$this->finish_table_export($table);
|
||||
}
|
||||
$this->finish_database_export();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* XML format exporter class.
|
||||
* Provides logic for writing XML tags and data inside appropiate callbacks.
|
||||
* Subclasses should define XML data sinks.
|
||||
*/
|
||||
abstract class xml_database_exporter extends database_exporter {
|
||||
/**
|
||||
* Generic output method. Subclasses should implement it with code specific
|
||||
* to the target XML sink.
|
||||
*/
|
||||
protected abstract function output($text);
|
||||
|
||||
/**
|
||||
* Callback function. Outputs open XML PI and moodle_database opening tag.
|
||||
*
|
||||
* @param float $version the version of the system which generating the data
|
||||
* @param string $timestamp the timestamp of the data (in ISO 8601) format.
|
||||
* @param string $description a user description of the data.
|
||||
* @return void
|
||||
*/
|
||||
public function begin_database_export($version, $timestamp, $description) {
|
||||
$this->output('<?xml version="1.0" encoding="utf-8"?>');
|
||||
//TODO add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" and schema information
|
||||
$this->output('<moodle_database version="'.$version.'" timestamp="'.$timestamp.'"'.(empty ($description) ? '' : ' comment="'.htmlspecialchars($description, ENT_QUOTES).'"').'>');
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Outputs table opening tag.
|
||||
*
|
||||
* @param xmldb_table $table - XMLDB object for the exported table
|
||||
* @return void
|
||||
*/
|
||||
public function begin_table_export(xmldb_table $table) {
|
||||
$this->output('<table name="'.$table->getName().'" schemaHash="'.$table->getHash().'">');
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Outputs table closing tag.
|
||||
*
|
||||
* @param xmldb_table $table - XMLDB object for the exported table
|
||||
*/
|
||||
public function finish_table_export(xmldb_table $table) {
|
||||
$this->output('</table>');
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Outputs moodle_database closing tag.
|
||||
*/
|
||||
public function finish_database_export() {
|
||||
$this->output('</moodle_database>');
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Outputs record tag with field subtags and data.
|
||||
*
|
||||
* @param xmldb_table $table - XMLDB object of the table from which data was retrived
|
||||
* @param object $data - data object (fields and values from record)
|
||||
* @return void
|
||||
*/
|
||||
public function export_table_data(xmldb_table $table, $data) {
|
||||
$this->output('<record>');
|
||||
foreach ($data as $key => $value) {
|
||||
if (is_null($value)) {
|
||||
$this->output('<field name="'.$key.'" value="null" />');
|
||||
} else {
|
||||
$this->output('<field name="'.$key.'">'.htmlspecialchars($value, ENT_NOQUOTES).'</field>');
|
||||
}
|
||||
}
|
||||
$this->output('</record>');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XML format exporter class to file storage.
|
||||
*/
|
||||
class file_xml_database_exporter extends xml_database_exporter {
|
||||
/** Path to the XML data file. */
|
||||
protected $filepath;
|
||||
/** File descriptor for the output file. */
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* Object constructor.
|
||||
*
|
||||
* @param string $filepath - path to the XML data file. Use null for PHP
|
||||
* output stream.
|
||||
* @param moodle_database $mdb Connection to the source database
|
||||
* @see xml_database_exporter::__construct()
|
||||
* @param xmldb_structure $schema Source database schema in XMLDB format
|
||||
* @see xml_database_exporter::__construct()
|
||||
* @param boolean $check_schema - whether or not to check that XML database
|
||||
* @see xml_database_exporter::__construct()
|
||||
*/
|
||||
function __construct($filepath, moodle_database $mdb, xmldb_structure $schema=null, $check_schema=true) {
|
||||
parent::__construct($mdb, $schema, $check_schema);
|
||||
if (is_null($filepath)) {
|
||||
$filepath = 'php://output';
|
||||
}
|
||||
$this->filepath = $filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific output method for the file XML sink.
|
||||
*/
|
||||
protected function output($text) {
|
||||
fwrite($this->file, $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific implementation for file exporting the database: it opens output stream, calls
|
||||
* superclass @see database_exporter::export_database() and closes output stream.
|
||||
*
|
||||
* @exception export_exception if any checking (e.g. database schema) fails
|
||||
*
|
||||
* @param string $description a user description of the data.
|
||||
*/
|
||||
public function export_database($description=null) {
|
||||
$this->file = fopen($this->filepath, 'wb');
|
||||
parent::export_database($description);
|
||||
fclose($this->file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XML format exporter class to memory storage (i.e. a string).
|
||||
*/
|
||||
class string_xml_database_exporter extends xml_database_exporter {
|
||||
/** String with XML data. */
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Specific output method for the memory XML sink.
|
||||
*/
|
||||
protected function output($text) {
|
||||
$this->data .= $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the output of the exporters
|
||||
* @return string XML data from exporter
|
||||
*/
|
||||
public function get_output() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific implementation for memory exporting the database: it clear the buffer
|
||||
* and calls superclass @see database_exporter::export_database().
|
||||
*
|
||||
* @exception export_exception if any checking (e.g. database schema) fails
|
||||
* @param string $description a user description of the data.
|
||||
* @return void
|
||||
*/
|
||||
public function export_database($description=null) {
|
||||
$this->data = '';
|
||||
parent::export_database($description);
|
||||
}
|
||||
}
|
||||
|
||||
class database_mover extends database_exporter {
|
||||
/** Importer object used to transfer data. */
|
||||
protected $importer;
|
||||
|
||||
/**
|
||||
* Object constructor.
|
||||
*
|
||||
* @param moodle_database $mdb_target Connection to the target database (a
|
||||
* @see moodle_database object).
|
||||
* @param moodle_database $mdb Connection to the source database (a
|
||||
* @see moodle_database object).
|
||||
* @param xmldb_structure $schema Source database schema in XMLDB format (a
|
||||
* @see xmldb_structure object). Use null to load the schema from the
|
||||
* system's install.xml files.
|
||||
* @param boolean $check_schema - whether or not to check that XML database
|
||||
* schema matches the RDBMS database schema before exporting (used by
|
||||
* @see export_database).
|
||||
*/
|
||||
public function __construct(moodle_database $mdb_target, moodle_database $mdb_source, xmldb_structure $schema=null, $check_schema=true) {
|
||||
parent::__construct($mdb_source, $schema, $check_schema);
|
||||
$this->importer = new database_importer($mdb_target, $schema, $check_schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Calls importer's begin_database_import callback method.
|
||||
*
|
||||
* @param float $version the version of the system which generating the data
|
||||
* @param string $timestamp the timestamp of the data (in ISO 8601) format.
|
||||
* @param string $description a user description of the data.
|
||||
* @return void
|
||||
*/
|
||||
public function begin_database_export($version, $timestamp, $description) {
|
||||
$this->importer->init_database();
|
||||
$this->importer->begin_database_import($version, $timestamp, $description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Calls importer's begin_table_import callback method.
|
||||
*
|
||||
* @param xmldb_table $table - XMLDB object for the exported table
|
||||
* @return void
|
||||
*/
|
||||
public function begin_table_export(xmldb_table $table) {
|
||||
$this->importer->begin_table_import($table->getName(), $table->getHash());
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Calls importer's import_table_data callback method.
|
||||
*
|
||||
* @param xmldb_table $table - XMLDB object of the table from which data
|
||||
* was retrived
|
||||
* @param object $data - data object (fields and values from record)
|
||||
* @return void
|
||||
*/
|
||||
public function export_table_data(xmldb_table $table, $data) {
|
||||
$this->importer->import_table_data($table->getName(), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Calls importer's finish_table_import callback method.
|
||||
* @param xmldb_table $table - XMLDB object for the exported table
|
||||
* @return void
|
||||
*/
|
||||
public function finish_table_export(xmldb_table $table) {
|
||||
$this->importer->finish_table_import($table->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Calls importer's finish_database_import callback method.
|
||||
* @return void
|
||||
*/
|
||||
public function finish_database_export() {
|
||||
$this->importer->finish_database_import();
|
||||
}
|
||||
}
|
351
admin/dbtransfer/importlib.php
Normal file
351
admin/dbtransfer/importlib.php
Normal file
@ -0,0 +1,351 @@
|
||||
<?php //$Id$
|
||||
|
||||
/**
|
||||
* General database import classes
|
||||
* @author Andrei Bautu
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package dbtransfer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base class for database import operations. This class implements
|
||||
* basic callbacks for import operations and defines the @see import_database
|
||||
* method as a common method for all importers. In general, subclasses will
|
||||
* override import_database and call other methods in appropriate moments.
|
||||
* Between a single pair of calls to @see begin_database_import and
|
||||
* @see finish_database_import, multiple non-overlapping pairs of calls may
|
||||
* be made to @see begin_table_import and @see finish_database_import for
|
||||
* different tables.
|
||||
* Between one pair of calls to @see begin_table_import and
|
||||
* @see finish_database_import multiple calls may be made to
|
||||
* @see import_table_data for the same table.
|
||||
* This class can be used directly, if the standard control flow (defined above)
|
||||
* is respected.
|
||||
*/
|
||||
class database_importer {
|
||||
/** Connection to the target database (a @see moodle_database object). */
|
||||
protected $mdb;
|
||||
/** Database manager of the target database (a @see database_manager object). */
|
||||
protected $manager;
|
||||
/** Target database schema in XMLDB format (a @see xmldb_structure object). */
|
||||
protected $schema;
|
||||
/**
|
||||
* Boolean flag - whether or not to check that XML database schema matches
|
||||
* the RDBMS database schema before importing (used by
|
||||
* @see begin_database_import).
|
||||
*/
|
||||
protected $check_schema;
|
||||
|
||||
/**
|
||||
* Object constructor.
|
||||
*
|
||||
* @param moodle_database $mdb Connection to the target database (a
|
||||
* @see moodle_database object). Use null to use the curent $DB connection.
|
||||
* @param xmldb_structure $schema Target database schema in XMLDB format (a
|
||||
* @see xmldb_structure object). Use null to load the schema from the
|
||||
* system's install.xml files.
|
||||
* @param boolean $check_schema - whether or not to check that XML database
|
||||
* schema matches the RDBMS database schema before importing (inside
|
||||
* @see begin_database_import).
|
||||
*/
|
||||
public function __construct(moodle_database $mdb, xmldb_structure $schema=null, $check_schema=true) {
|
||||
if (is_null($schema)) {
|
||||
$schema = database_manager::get_install_xml_schema();
|
||||
}
|
||||
$this->mdb = $mdb;
|
||||
$this->manager = $mdb->get_manager();
|
||||
$this->schema = $schema;
|
||||
$this->check_schema = $check_schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once database per import
|
||||
* operation, before any database changes are made. It will check the database
|
||||
* schema if @see check_schema is true
|
||||
*
|
||||
* @exception import_exception if any checking (e.g. database schema, Moodle
|
||||
* version) fails
|
||||
*
|
||||
* @param float $version the version of the system which generated the data
|
||||
* @param string $timestamp the timestamp of the data (in ISO 8601) format.
|
||||
* @return void
|
||||
*/
|
||||
public function begin_database_import($version, $timestamp) {
|
||||
global $CFG;
|
||||
|
||||
if (round($version, 2) !== round($CFG->version, 2)) { // version might be in decimal format too
|
||||
//TODO put message in error lang
|
||||
throw new import_exception('Current Moodle version does not match exported Moodle version.');
|
||||
}
|
||||
if ($this->check_schema && $this->manager->check_database_schema($this->schema)) {
|
||||
//TODO put message in error lang
|
||||
throw new import_exception('XMLDB schema does not match database schema.');
|
||||
}
|
||||
$this->mdb->begin_sql();
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once per table import operation,
|
||||
* before any table changes are made. It will delete all table data.
|
||||
*
|
||||
* @exception import_exception an unknown table import is attempted
|
||||
* @exception ddl_table_missing_exception if the table is missing
|
||||
*
|
||||
* @param string $tablename - the name of the table that will be imported
|
||||
* @param string $schemaHash - the hash of the xmldb_table schema of the table
|
||||
* @return void
|
||||
*/
|
||||
public function begin_table_import($tablename, $schemaHash) {
|
||||
$table = $this->schema->getTable($tablename);
|
||||
if (is_null($table)) {
|
||||
//TODO put message in error lang
|
||||
throw new import_exception('Unknown table in import data');
|
||||
}
|
||||
if ($schemaHash != $table->getHash()) {
|
||||
throw new import_exception('XMLDB schema does not match database schema.');
|
||||
}
|
||||
// this should not happen, unless someone drops tables after import started
|
||||
if (!$this->manager->table_exists($table)) {
|
||||
// in the future, missing tables will be recreated with
|
||||
//$this->manager->create_table($table);
|
||||
throw new ddl_table_missing_exception($tablename);
|
||||
}
|
||||
$this->mdb->delete_records($tablename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once per table import operation,
|
||||
* after all table changes are made. It will reset table sequences if any.
|
||||
* @param string $tablename
|
||||
* @return void
|
||||
*/
|
||||
public function finish_table_import($tablename) {
|
||||
$table = $this->schema->getTable($tablename);
|
||||
$fields = $table->getFields();
|
||||
foreach ($fields as $field) {
|
||||
if ($field->getSequence()) {
|
||||
//TODO make sure that there aren't tables using two sequences (probably there is none)
|
||||
$this->mdb->reset_sequence($tablename);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once database per import
|
||||
* operation, after all database changes are made. It will commit changes.
|
||||
* @return void
|
||||
*/
|
||||
public function finish_database_import() {
|
||||
$this->mdb->commit_sql();
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Should be called only once per record import operation, only
|
||||
* between @see begin_table_import and @see finish_table_import calls.
|
||||
* It will insert table data.
|
||||
*
|
||||
* @exception dml_exception if data insert operation failed
|
||||
*
|
||||
* @param string $tablename - the name of the table in which data will be
|
||||
* imported
|
||||
* @param object $data - data object (fields and values will be inserted
|
||||
* into table)
|
||||
* @return void
|
||||
*/
|
||||
public function import_table_data($tablename, $data) {
|
||||
$this->mdb->import_record($tablename, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* All subclases must call this
|
||||
* @return void
|
||||
*/
|
||||
public function init_database() {
|
||||
if (!$this->mdb->get_tables()) {
|
||||
// not tables yet, time to create all tables
|
||||
$this->manager->install_from_xmldb_structure($this->schema);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XML format importer class (uses SAX for speed and low memory footprint).
|
||||
* Provides logic for parsing XML data and calling appropiate callbacks.
|
||||
* Subclasses should define XML data sources.
|
||||
*/
|
||||
abstract class xml_database_importer extends database_importer {
|
||||
/**
|
||||
* Creates and setups a SAX parser. Subclasses should use this method to
|
||||
* create the XML parser.
|
||||
*
|
||||
* @return resource XML parser resource.
|
||||
*/
|
||||
protected function get_parser() {
|
||||
$parser = xml_parser_create();
|
||||
xml_set_object($parser, $this);
|
||||
xml_set_element_handler($parser, 'tag_open', 'tag_close');
|
||||
xml_set_character_data_handler($parser, 'cdata');
|
||||
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
|
||||
return $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Called by the XML parser for opening tags processing.
|
||||
*
|
||||
* @param resource $parser XML parser resource.
|
||||
* @param string $tag name of opening tag
|
||||
* @param array $attributes set of opening tag XML attributes
|
||||
* @return void
|
||||
*/
|
||||
protected function tag_open($parser, $tag, $attributes) {
|
||||
switch ($tag) {
|
||||
case 'moodle_database' :
|
||||
$this->begin_database_import($attributes['version'], $attributes['timestamp']);
|
||||
break;
|
||||
case 'table' :
|
||||
$this->current_table = $attributes['name'];
|
||||
$this->begin_table_import($this->current_table, $attributes['schemaHash']);
|
||||
break;
|
||||
case 'record' :
|
||||
$this->current_row = new object();
|
||||
break;
|
||||
case 'field' :
|
||||
$this->current_field = $attributes['name'];
|
||||
$this->current_data = @$attributes['value'] == 'null' ? null : '';
|
||||
break;
|
||||
default :
|
||||
//TODO put message in error lang
|
||||
throw new import_exception('XML content not valid for import operation.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Called by the XML parser for closing tags processing.
|
||||
*
|
||||
* @param resource $parser XML parser resource.
|
||||
* @param string $tag name of opening tag
|
||||
* @return void
|
||||
*/
|
||||
protected function tag_close($parser, $tag) {
|
||||
switch ($tag) {
|
||||
case 'moodle_database' :
|
||||
$this->finish_database_import();
|
||||
break;
|
||||
case 'table' :
|
||||
$this->finish_table_import($this->current_table);
|
||||
unset ($this->current_table);
|
||||
break;
|
||||
case 'record' :
|
||||
$this->import_table_data($this->current_table, $this->current_row);
|
||||
unset ($this->current_row);
|
||||
break;
|
||||
case 'field' :
|
||||
$field = $this->current_field;
|
||||
unset ($this->current_field);
|
||||
$this->current_row-> $field = $this->current_data;
|
||||
unset ($this->current_data);
|
||||
break;
|
||||
default :
|
||||
//TODO put message in error lang
|
||||
throw new import_exception('XML content not valid for import operation.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function. Called by the XML parser for character data processing.
|
||||
*
|
||||
* @param resource $parser XML parser resource.
|
||||
* @param string $data character data to be processed
|
||||
* @return void
|
||||
*/
|
||||
protected function cdata($parser, $cdata) {
|
||||
if (isset($this->current_field)) {
|
||||
$this->current_data .= $cdata;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Common import method
|
||||
* @return void
|
||||
*/
|
||||
public abstract function import_database();
|
||||
}
|
||||
|
||||
/**
|
||||
* XML format importer class from file storage.
|
||||
*/
|
||||
class file_xml_database_importer extends xml_database_importer {
|
||||
/** Path to the XML data file. */
|
||||
protected $filepath;
|
||||
|
||||
/**
|
||||
* Object constructor.
|
||||
*
|
||||
* @param string $filepath - path to the XML data file. Use null for PHP
|
||||
* input stream.
|
||||
* @param moodle_database $mdb Connection to the target database
|
||||
* @see xml_database_importer::__construct()
|
||||
* @param xmldb_structure $schema Target database schema in XMLDB format
|
||||
* @see xml_database_importer::__construct()
|
||||
* @param boolean $check_schema - whether or not to check that XML database
|
||||
* @see xml_database_importer::__construct()
|
||||
*/
|
||||
public function __construct($filepath, moodle_database $mdb, xmldb_structure $schema=null) {
|
||||
parent::__construct($mdb, $schema);
|
||||
if (is_null($filepath)) {
|
||||
$filepath = 'php://input';
|
||||
}
|
||||
$this->filepath = $filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Common import method: it opens the file storage, creates the parser, feeds
|
||||
* the XML parser with data, releases the parser and closes the file storage.
|
||||
* @return void
|
||||
*/
|
||||
public function import_database() {
|
||||
$this->init_database();
|
||||
$file = fopen($this->filepath, 'r');
|
||||
$parser = $this->get_parser();
|
||||
while (($data = fread($file, 65536)) && xml_parse($parser, $data, feof($file)));
|
||||
xml_parser_free($parser);
|
||||
fclose($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* XML format importer class from memory storage (i.e. string).
|
||||
*/
|
||||
class string_xml_database_importer extends xml_database_importer {
|
||||
/** String with XML data. */
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Object constructor.
|
||||
*
|
||||
* @param string data - string with XML data
|
||||
* @param moodle_database $mdb Connection to the target database
|
||||
* @see xml_database_importer::__construct()
|
||||
* @param xmldb_structure $schema Target database schema in XMLDB format
|
||||
* @see xml_database_importer::__construct()
|
||||
* @param boolean $check_schema - whether or not to check that XML database
|
||||
* @see xml_database_importer::__construct()
|
||||
*/
|
||||
public function __construct($data, moodle_database $mdb, xmldb_structure $schema=null) {
|
||||
parent::__construct($mdb, $schema);
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Common import method: it creates the parser, feeds the XML parser with
|
||||
* data, releases the parser.
|
||||
* @return void
|
||||
*/
|
||||
public function import_database() {
|
||||
$this->init_database();
|
||||
$parser = $this->get_parser();
|
||||
xml_parse($parser, $this->data, true);
|
||||
xml_parser_free($parser);
|
||||
}
|
||||
}
|
35
admin/dbtransfer/lib.php
Normal file
35
admin/dbtransfer/lib.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php //$Id$
|
||||
|
||||
/**
|
||||
* Database transfer related code.
|
||||
* @author Andrei Bautu
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package dbtransfer
|
||||
*/
|
||||
|
||||
require_once $CFG->libdir.'/xmldb/xmldb_structure.php';
|
||||
require_once $CFG->libdir.'/ddl/database_manager.php';
|
||||
require_once $CFG->dirroot.'/'.$CFG->admin.'/dbtransfer/exportlib.php';
|
||||
require_once $CFG->dirroot.'/'.$CFG->admin.'/dbtransfer/importlib.php';
|
||||
|
||||
/**
|
||||
* Exception class for export operations.
|
||||
* @see moodle_exception
|
||||
* TODO subclass for specific purposes
|
||||
*/
|
||||
class export_exception extends moodle_exception {
|
||||
function __construct($errorcode, $a=null, $debuginfo=null) {
|
||||
parent::__construct($errorcode, '', '', $a, $debuginfo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception class for import operations.
|
||||
* @see moodle_exception
|
||||
* TODO subclass for specific purposes
|
||||
*/
|
||||
class import_exception extends moodle_exception {
|
||||
function __construct($errorcode, $a=null, $debuginfo=null) {
|
||||
parent::__construct($errorcode, '', '', $a, $debuginfo);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user