forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-46455 logstore_database: added backup/restore support
- Loading branch information
Showing
7 changed files
with
285 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
admin/tool/log/store/database/backup/moodle2/backup_logstore_database_subplugin.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?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 subplugin. | ||
* | ||
* @package logstore_database | ||
* @category backup | ||
* @copyright 2015 Mark Nelson <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
class backup_logstore_database_subplugin extends backup_tool_log_logstore_subplugin { | ||
|
||
/** | ||
* Returns the subplugin structure to attach to the 'logstore' XML element. | ||
* | ||
* @return backup_subplugin_element the subplugin structure to be attached. | ||
*/ | ||
protected function define_logstore_subplugin_structure() { | ||
$subplugin = $this->get_subplugin_element(); | ||
$subpluginwrapper = new backup_nested_element($this->get_recommended_name()); | ||
|
||
// 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( | ||
'eventname', 'component', 'action', 'target', 'objecttable', | ||
'objectid', 'crud', 'edulevel', 'contextid', 'userid', 'relateduserid', | ||
'anonymous', $otherelement, 'timecreated', 'ip', 'realuserid')); | ||
|
||
$subplugin->add_child($subpluginwrapper); | ||
$subpluginwrapper->add_child($subpluginlog); | ||
|
||
// Get the details for the external database. | ||
$manager = new \tool_log\log\manager(); | ||
$store = new \logstore_database\log\store($manager); | ||
$extdb = $store->get_extdb(); | ||
|
||
if (!$extdb) { | ||
return false; | ||
} | ||
|
||
$subpluginlog->set_source_db($extdb); | ||
$subpluginlog->set_source_table($store->get_config_value('dbtable'), array('contextid' => backup::VAR_CONTEXTID)); | ||
|
||
return $subplugin; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
admin/tool/log/store/database/backup/moodle2/restore_logstore_database_subplugin.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Restore implementation for the (tool_log) logstore_database subplugin. | ||
* | ||
* @package logstore_database | ||
* @category backup | ||
* @copyright 2015 Mark Nelson <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
class restore_logstore_database_subplugin extends restore_tool_log_logstore_subplugin { | ||
|
||
/** | ||
* @var moodle_database the external database. | ||
*/ | ||
private static $extdb = null; | ||
|
||
/** | ||
* @var string the external database table name. | ||
*/ | ||
private static $extdbtablename = null; | ||
|
||
/** | ||
* The constructor for this logstore. | ||
* | ||
* @param string $subplugintype the subplugin type. | ||
* @param string $subpluginname the subplugin name. | ||
* @param restore_structure_step $step. | ||
*/ | ||
public function __construct($subplugintype, $subpluginname, $step) { | ||
// Check that the logstore is enabled before setting variables. | ||
$enabledlogstores = explode(',', get_config('tool_log', 'enabled_stores')); | ||
if (in_array('logstore_database', $enabledlogstores)) { | ||
$manager = new \tool_log\log\manager(); | ||
$store = new \logstore_database\log\store($manager); | ||
self::$extdb = $store->get_extdb(); | ||
self::$extdbtablename = $store->get_config_value('dbtable'); | ||
} | ||
|
||
parent::__construct($subplugintype, $subpluginname, $step); | ||
} | ||
|
||
/** | ||
* Returns the subplugin structure to attach to the 'logstore' XML element. | ||
* | ||
* @return restore_path_element[] array of elements to be processed on restore. | ||
*/ | ||
protected function define_logstore_subplugin_structure() { | ||
// If the logstore is not enabled we don't add structures for it. | ||
$enabledlogstores = explode(',', get_config('tool_log', 'enabled_stores')); | ||
if (!in_array('logstore_database', $enabledlogstores)) { | ||
return array(); // The logstore is not enabled, nothing to restore. | ||
} | ||
|
||
$paths = array(); | ||
|
||
$elename = $this->get_namefor('log'); | ||
$elepath = $this->get_pathfor('/logstore_database_log'); | ||
$paths[] = new restore_path_element($elename, $elepath); | ||
|
||
return $paths; | ||
} | ||
|
||
/** | ||
* Process logstore_database_log entries. | ||
* | ||
* This method proceeds to read, complete, remap and, finally, | ||
* discard or save every log entry. | ||
* | ||
* @param array() $data log entry. | ||
* @return null if we are not restoring the log. | ||
*/ | ||
public function process_logstore_database_log($data) { | ||
// Do not bother processing if we can not add it to a database. | ||
if (!self::$extdb || !self::$extdbtablename) { | ||
return; | ||
} | ||
|
||
$data = $this->process_log($data); | ||
|
||
if ($data) { | ||
self::$extdb->insert_record(self::$extdbtablename, $data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.