first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,165 @@
<?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-factories
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Non instantiable factory class providing different backup object instances
*
* This class contains various methods available in order to easily
* create different parts of the backup architecture in an easy way
*
* TODO: Finish phpdocs
*/
abstract class backup_factory {
public static function get_destination_chain($type, $id, $mode, $execution) {
return null;
}
public static function get_logger_chain($interactive, $execution, $backupid) {
global $CFG;
$dfltloglevel = backup::LOG_WARNING; // Default logging level
if ($CFG->debugdeveloper) { // Debug developer raises default logging level
$dfltloglevel = backup::LOG_DEBUG;
}
$enabledloggers = array(); // Array to store all enabled loggers
// Create error_log_logger, observing $CFG->backup_error_log_logger_level,
// defaulting to $dfltloglevel
$elllevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : $dfltloglevel;
$enabledloggers[] = new error_log_logger($elllevel);
// Create output_indented_logger, observing $CFG->backup_output_indented_logger_level and $CFG->debugdisplay,
// defaulting to LOG_ERROR. Only if interactive and inmediate
if ($CFG->debugdisplay && $interactive == backup::INTERACTIVE_YES && $execution == backup::EXECUTION_INMEDIATE) {
$oillevel = isset($CFG->backup_output_indented_logger_level) ? $CFG->backup_output_indented_logger_level : backup::LOG_ERROR;
$enabledloggers[] = new output_indented_logger($oillevel, false, false);
}
// Create file_logger, observing $CFG->backup_file_logger_level
// defaulting to $dfltloglevel
$backuptempdir = make_backup_temp_directory(''); // Need to ensure that $CFG->backuptempdir already exists.
$fllevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : $dfltloglevel;
$enabledloggers[] = new file_logger($fllevel, true, true, $backuptempdir . '/' . $backupid . '.log');
// Create database_logger, observing $CFG->backup_database_logger_level and defaulting to LOG_WARNING
// and pointing to the backup_logs table
$dllevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : $dfltloglevel;
$columns = array('backupid' => $backupid);
$enabledloggers[] = new database_logger($dllevel, 'timecreated', 'loglevel', 'message', 'backup_logs', $columns);
// Create extra file_logger, observing $CFG->backup_file_logger_extra and $CFG->backup_file_logger_extra_level
// defaulting to $fllevel (normal file logger)
if (isset($CFG->backup_file_logger_extra)) {
$flelevel = isset($CFG->backup_file_logger_extra_level) ? $CFG->backup_file_logger_extra_level : $fllevel;
$enabledloggers[] = new file_logger($flelevel, true, true, $CFG->backup_file_logger_extra);
}
// Build the chain
$loggers = null;
foreach ($enabledloggers as $currentlogger) {
if ($loggers == null) {
$loggers = $currentlogger;
} else {
$lastlogger->set_next($currentlogger);
}
$lastlogger = $currentlogger;
}
return $loggers;
}
/**
* Given one format and one course module id, return the corresponding
* backup_xxxx_activity_task()
*/
public static function get_backup_activity_task($format, $moduleid) {
global $CFG, $DB;
// Check moduleid exists
if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
throw new backup_task_exception('activity_task_coursemodule_not_found', $moduleid);
}
$classname = 'backup_' . $coursemodule->modname . '_activity_task';
return new $classname($coursemodule->name, $moduleid);
}
/**
* Given one format, one block id and, optionally, one moduleid, return the corresponding backup_xxx_block_task()
*/
public static function get_backup_block_task($format, $blockid, $moduleid = null) {
global $CFG, $DB;
// Check blockid exists
if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
throw new backup_task_exception('block_task_block_instance_not_found', $blockid);
}
// Set default block backup task
$classname = 'backup_default_block_task';
$testname = 'backup_' . $block->blockname . '_block_task';
// If the block has custom backup/restore task class (testname), use it
if (class_exists($testname)) {
$classname = $testname;
}
return new $classname($block->blockname, $blockid, $moduleid);
}
/**
* Given one format and one section id, return the corresponding backup_section_task()
*/
public static function get_backup_section_task($format, $sectionid) {
global $DB;
// Check section exists
if (!$section = $DB->get_record('course_sections', array('id' => $sectionid))) {
throw new backup_task_exception('section_task_section_not_found', $sectionid);
}
return new backup_section_task((string)$section->name === '' ? $section->section : $section->name, $sectionid);
}
/**
* Given one format and one course id, return the corresponding backup_course_task()
*/
public static function get_backup_course_task($format, $courseid) {
global $DB;
// Check course exists
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
throw new backup_task_exception('course_task_course_not_found', $courseid);
}
return new backup_course_task($course->shortname, $courseid);
}
/**
* Dispatches the creation of the @backup_plan to the proper format builder
*/
public static function build_plan($controller) {
backup_plan_builder::build_plan($controller);
}
}
@@ -0,0 +1,60 @@
<?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();
/**
* Factory class to create new instances of backup converters
*/
abstract class convert_factory {
/**
* Instantinates the given converter operating on a given directory
*
* @throws coding_exception
* @param $name The converter name
* @param $tempdir The temp directory to operate on
* @param base_logger|null if the conversion should be logged, use this logger
* @return base_converter
*/
public static function get_converter($name, $tempdir, $logger = null) {
global $CFG;
$name = clean_param($name, PARAM_SAFEDIR);
$classfile = "$CFG->dirroot/backup/converter/$name/lib.php";
$classname = "{$name}_converter";
if (!file_exists($classfile)) {
throw new coding_exception("Converter factory error: class file not found $classfile");
}
require_once($classfile);
if (!class_exists($classname)) {
throw new coding_exception("Converter factory error: class not found $classname");
}
return new $classname($tempdir, $logger);
}
}
@@ -0,0 +1,69 @@
<?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-factories
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Non instantiable factory class providing different restore object instances
*
* This class contains various methods available in order to easily
* create different parts of the restore architecture in an easy way
*
* TODO: Finish phpdocs
*/
abstract class restore_factory {
public static function get_restore_activity_task($info) {
$classname = 'restore_' . $info->modulename . '_activity_task';
if (class_exists($classname)) {
return new $classname($info->title, $info);
}
}
public static function get_restore_block_task($blockname, $basepath) {
$classname = 'restore_default_block_task';
$testname = 'restore_' . $blockname . '_block_task';
// If the block has custom backup/restore task class (testname), use it
if (class_exists($testname)) {
$classname = $testname;
}
return new $classname($blockname, $basepath);
}
public static function get_restore_section_task($info) {
return new restore_section_task($info->title, $info);
}
public static function get_restore_course_task($info, $courseid) {
global $DB;
// Check course exists
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
throw new restore_task_exception('course_task_course_not_found', $courseid);
}
return new restore_course_task($info->title, $info);
}
}
@@ -0,0 +1,129 @@
<?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/>.
namespace core_backup;
use backup;
use backup_factory;
use database_logger;
use error_log_logger;
use file_logger;
use output_indented_logger;
defined('MOODLE_INTERNAL') || die();
// Include all the needed stuff
global $CFG;
require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php');
require_once($CFG->dirroot . '/backup/backup.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/base_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/error_log_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php');
require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php');
/**
* @package core_backup
* @category test
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class factories_test extends \advanced_testcase {
public function setUp(): void {
global $CFG;
parent::setUp();
$this->resetAfterTest(true);
$CFG->backup_error_log_logger_level = backup::LOG_NONE;
$CFG->backup_output_indented_logger_level = backup::LOG_NONE;
$CFG->backup_file_logger_level = backup::LOG_NONE;
$CFG->backup_database_logger_level = backup::LOG_NONE;
unset($CFG->backup_file_logger_extra);
$CFG->backup_file_logger_level_extra = backup::LOG_NONE;
}
/**
* test get_logger_chain() method
*/
public function test_backup_factory(): void {
global $CFG;
// Default instantiate, all levels = backup::LOG_NONE
// With debugdisplay enabled
$CFG->debugdisplay = true;
$logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
$this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger
$this->assertEquals($logger1->get_level(), backup::LOG_NONE);
$logger2 = $logger1->get_next();
$this->assertTrue($logger2 instanceof output_indented_logger); // 2nd logger is output_indented_logger
$this->assertEquals($logger2->get_level(), backup::LOG_NONE);
$logger3 = $logger2->get_next();
$this->assertTrue($logger3 instanceof file_logger); // 3rd logger is file_logger
$this->assertEquals($logger3->get_level(), backup::LOG_NONE);
$logger4 = $logger3->get_next();
$this->assertTrue($logger4 instanceof database_logger); // 4th logger is database_logger
$this->assertEquals($logger4->get_level(), backup::LOG_NONE);
$logger5 = $logger4->get_next();
$this->assertTrue($logger5 === null);
// With debugdisplay disabled
$CFG->debugdisplay = false;
$logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
$this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger
$this->assertEquals($logger1->get_level(), backup::LOG_NONE);
$logger2 = $logger1->get_next();
$this->assertTrue($logger2 instanceof file_logger); // 2nd logger is file_logger
$this->assertEquals($logger2->get_level(), backup::LOG_NONE);
$logger3 = $logger2->get_next();
$this->assertTrue($logger3 instanceof database_logger); // 3rd logger is database_logger
$this->assertEquals($logger3->get_level(), backup::LOG_NONE);
$logger4 = $logger3->get_next();
$this->assertTrue($logger4 === null);
// Instantiate with debugging enabled and $CFG->backup_error_log_logger_level not set
$CFG->debugdisplay = true;
unset($CFG->backup_error_log_logger_level);
$logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
$this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger
$this->assertEquals($logger1->get_level(), backup::LOG_DEBUG); // and must have backup::LOG_DEBUG level
// Set $CFG->backup_error_log_logger_level to backup::LOG_WARNING and test again
$CFG->backup_error_log_logger_level = backup::LOG_WARNING;
$logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
$this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger
$this->assertEquals($logger1->get_level(), backup::LOG_WARNING); // and must have backup::LOG_WARNING level
// Instantiate in non-interactive mode, output_indented_logger must be out
$logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_NO, backup::EXECUTION_INMEDIATE, 'test');
$logger2 = $logger1->get_next();
$this->assertTrue($logger2 instanceof file_logger); // 2nd logger is file_logger (output_indented_logger skiped)
// Define extra file logger and instantiate, should be 5th and last logger
$CFG->backup_file_logger_extra = $CFG->tempdir.'/test.html';
$CFG->backup_file_logger_level_extra = backup::LOG_NONE;
$logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
$logger2 = $logger1->get_next();
$logger3 = $logger2->get_next();
$logger4 = $logger3->get_next();
$logger5 = $logger4->get_next();
$this->assertTrue($logger5 instanceof file_logger); // 5rd logger is file_logger (extra)
$this->assertEquals($logger3->get_level(), backup::LOG_NONE);
$logger6 = $logger5->get_next();
$this->assertTrue($logger6 === null);
}
}