MDL-46455 backup: Keep external database logstore code self-contained.

General backup/restore does not need to handle external data sources natively - so
any changes needed to achieve this should be contained to the plugin that needs it.
This commit is contained in:
Damyon Wiese 2015-09-21 13:39:51 +08:00 committed by Mark Nelson
parent bcfa51ca41
commit 51311962c4
4 changed files with 105 additions and 30 deletions

View File

@ -0,0 +1,98 @@
<?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/>.
/**
* Backup implementation for the (tool_log) logstore_database nested element.
*
* @package logstore_database
* @category backup
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Custom subclass of backup_nested_element that iterates over an external DB connection.
*
* @package logstore_database
* @category backup
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_logstore_database_nested_element extends backup_nested_element {
/**
* @var \moodle_database $sourcedb
*/
protected $sourcedb;
/**
* Constructor - instantiates one backup_nested_element, specifying its basic info.
*
* @param string $name name of the element
* @param array $attributes attributes this element will handle (optional, defaults to null)
* @param array $finalelements this element will handle (optional, defaults to null)
*/
public function __construct($name, $attributes = null, $finalelements = null) {
global $DB;
parent::__construct($name, $attributes, $finalelements);
$this->sourcedb = $DB;
}
/**
* For sql or table datasources, this will iterate over the "external" DB connection
* stored in this class instead of the default $DB. All other cases use the parent default.
* @param object $processor the processor
*/
protected function get_iterator($processor) {
if ($this->get_source_table() !== null) { // It's one table, return recordset iterator.
return $this->get_source_db()->get_recordset(
$this->get_source_table(),
backup_structure_dbops::convert_params_to_values($this->procparams, $processor),
$this->get_source_table_sortby()
);
} else if ($this->get_source_sql() !== null) { // It's one sql, return recordset iterator.
return $this->get_source_db()->get_recordset_sql(
$this->get_source_sql(),
backup_structure_dbops::convert_params_to_values($this->procparams, $processor)
);
}
return parent::get_iterator($processor);
}
/**
* Set the database we want to use.
*
* @param \moodle_database $db
*/
public function set_source_db($db) {
$this->sourcedb = $db;
}
/**
* Get the database we want to use.
*
* @return \moodle_database $db
*/
public function get_source_db() {
return $this->sourcedb;
}
}

View File

@ -24,6 +24,7 @@
*/
defined('MOODLE_INTERNAL') || die();
require_once('backup_logstore_database_nested_element.php');
class backup_logstore_database_subplugin extends backup_tool_log_logstore_subplugin {
@ -39,7 +40,7 @@ class backup_logstore_database_subplugin extends backup_tool_log_logstore_subplu
// Create the custom (base64 encoded, xml safe) 'other' final element.
$otherelement = new base64_encode_final_element('other');
$subpluginlog = new backup_nested_element('logstore_database_log', array('id'), array(
$subpluginlog = new backup_logstore_database_nested_element('logstore_database_log', array('id'), array(
'eventname', 'component', 'action', 'target', 'objecttable',
'objectid', 'crud', 'edulevel', 'contextid', 'userid', 'relateduserid',
'anonymous', $otherelement, 'timecreated', 'ip', 'realuserid'));

View File

@ -33,6 +33,8 @@
abstract class backup_structure_dbops extends backup_dbops {
public static function get_iterator($element, $params, $processor) {
global $DB;
// Check we are going to get_iterator for one backup_nested_element
if (! $element instanceof backup_nested_element) {
throw new base_element_struct_exception('backup_nested_element_expected');
@ -48,17 +50,17 @@ abstract class backup_structure_dbops extends backup_dbops {
return new backup_array_iterator($element->get_source_array());
} else if ($element->get_source_table() !== null) { // It's one table, return recordset iterator
return $element->get_source_db()->get_recordset($element->get_source_table(), self::convert_params_to_values($params, $processor), $element->get_source_table_sortby());
return $DB->get_recordset($element->get_source_table(), self::convert_params_to_values($params, $processor), $element->get_source_table_sortby());
} else if ($element->get_source_sql() !== null) { // It's one sql, return recordset iterator
return $element->get_source_db()->get_recordset_sql($element->get_source_sql(), self::convert_params_to_values($params, $processor));
return $DB->get_recordset_sql($element->get_source_sql(), self::convert_params_to_values($params, $processor));
} else { // No sources, supress completely, using null iterator
return new backup_null_iterator();
}
}
protected static function convert_params_to_values($params, $processor) {
public static function convert_params_to_values($params, $processor) {
$newparams = array();
foreach ($params as $key => $param) {
$newvalue = null;

View File

@ -41,11 +41,6 @@ class backup_nested_element extends base_nested_element implements processable {
protected $results; // Logs the results we encounter during the process.
protected $logs; // Some log messages that could be retrieved later.
/**
* @var \moodle_database $dbtouse
*/
protected $dbtouse;
/**
* Constructor - instantiates one backup_nested_element, specifying its basic info.
*
@ -54,8 +49,6 @@ class backup_nested_element extends base_nested_element implements processable {
* @param array $final_elements this element will handle (optional, defaults to null)
*/
public function __construct($name, $attributes = null, $final_elements = null) {
global $DB;
parent::__construct($name, $attributes, $final_elements);
$this->var_array = null;
$this->table = null;
@ -68,7 +61,6 @@ class backup_nested_element extends base_nested_element implements processable {
$this->counter = 0;
$this->results = array();
$this->logs = array();
$this->dbtouse = $DB;
}
/**
@ -201,15 +193,6 @@ class backup_nested_element extends base_nested_element implements processable {
$this->var_array = $arr;
}
/**
* Set the database we want to use.
*
* @param \moodle_database $db
*/
public function set_source_db($db) {
$this->dbtouse = $db;
}
public function set_source_table($table, $params, $sortby = null) {
if (!is_array($params)) { // Check we are passing array
throw new base_element_struct_exception('setsourcerequiresarrayofparams');
@ -277,15 +260,6 @@ class backup_nested_element extends base_nested_element implements processable {
return $this->var_array;
}
/**
* Get the database we want to use.
*
* @return \moodle_database $db
*/
public function get_source_db() {
return $this->dbtouse;
}
public function get_source_table() {
return $this->table;
}