From 809fdb83b1bf8e2d6573dc4044f45a10c7464e89 Mon Sep 17 00:00:00 2001 From: James Pratt Date: Fri, 15 Nov 2013 12:18:06 +0700 Subject: [PATCH] MDL-41754 (1) Progress tracking : moved from backup and restore to core --- backup/backup.php | 4 +- backup/controller/backup_controller.class.php | 2 +- backup/controller/base_controller.class.php | 10 ++-- .../controller/restore_controller.class.php | 6 +-- backup/import.php | 2 +- backup/moodle2/backup_stepslib.php | 4 +- backup/moodle2/restore_stepslib.php | 2 +- backup/restore.php | 6 +-- .../dbops/backup_controller_dbops.class.php | 4 +- .../dbops/backup_structure_dbops.class.php | 4 +- backup/util/dbops/restore_dbops.class.php | 28 +++++------ backup/util/helper/backup_helper.class.php | 20 ++++---- backup/util/includes/backup_includes.php | 4 -- backup/util/includes/restore_includes.php | 4 -- backup/util/plan/backup_plan.class.php | 2 +- backup/util/plan/base_plan.class.php | 2 +- backup/util/plan/base_task.class.php | 2 +- backup/util/plan/restore_plan.class.php | 2 +- .../plan/restore_structure_step.class.php | 4 +- .../backup_structure_processor.class.php | 6 +-- backup/util/ui/restore_ui.class.php | 10 ++-- backup/util/ui/restore_ui_stage.class.php | 14 +++--- .../xml/parser/progressive_parser.class.php | 16 ++++--- .../classes/progress/base.php | 47 ++++++++++--------- .../classes/progress/display.php | 28 ++++++----- .../classes/progress/display_if_slow.php | 24 ++++++---- .../classes/progress/null.php | 8 +++- .../progress => lib}/tests/progress_test.php | 32 ++++++------- 28 files changed, 153 insertions(+), 144 deletions(-) rename backup/util/progress/core_backup_progress.class.php => lib/classes/progress/base.php (89%) rename backup/util/progress/core_backup_display_progress.class.php => lib/classes/progress/display.php (86%) rename backup/util/progress/core_backup_display_progress_if_slow.class.php => lib/classes/progress/display_if_slow.php (85%) rename backup/util/progress/core_backup_null_progress.class.php => lib/classes/progress/null.php (88%) rename {backup/util/progress => lib}/tests/progress_test.php (92%) diff --git a/backup/backup.php b/backup/backup.php index 0f3bf7add3f..688726daee3 100644 --- a/backup/backup.php +++ b/backup/backup.php @@ -97,7 +97,7 @@ echo $OUTPUT->header(); // Prepare a progress bar which can display optionally during long-running // operations while setting up the UI. -$slowprogress = new core_backup_display_progress_if_slow(get_string('preparingui', 'backup')); +$slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup')); $previous = optional_param('previous', false, PARAM_BOOL); if ($backup->get_stage() == backup_ui::STAGE_SCHEMA && !$previous) { @@ -121,7 +121,7 @@ if ($backup->get_stage() == backup_ui::STAGE_FINAL) { // Display an extra backup step bar so that we can show the 'processing' step first. echo html_writer::start_div('', array('id' => 'executionprogress')); echo $renderer->progress_bar($backup->get_progress_bar()); - $backup->get_controller()->set_progress(new core_backup_display_progress()); + $backup->get_controller()->set_progress(new \core\progress\display()); // Prepare logger and add to end of chain. $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO); diff --git a/backup/controller/backup_controller.class.php b/backup/controller/backup_controller.class.php index 345831c0845..cd124e588b6 100644 --- a/backup/controller/backup_controller.class.php +++ b/backup/controller/backup_controller.class.php @@ -110,7 +110,7 @@ class backup_controller extends base_controller { // By default there is no progress reporter. Interfaces that wish to // display progress must set it. - $this->progress = new core_backup_null_progress(); + $this->progress = new \core\progress\null(); // Instantiate the output_controller singleton and active it if interactive and inmediate $oc = output_controller::get_instance(); diff --git a/backup/controller/base_controller.class.php b/backup/controller/base_controller.class.php index 4b017b73688..09987965e5f 100644 --- a/backup/controller/base_controller.class.php +++ b/backup/controller/base_controller.class.php @@ -24,7 +24,7 @@ */ abstract class base_controller extends backup implements loggable { /** - * @var core_backup_progress Progress reporting object. + * @var \core\progress\base Progress reporting object. */ protected $progress; @@ -37,7 +37,7 @@ abstract class base_controller extends backup implements loggable { * Gets the progress reporter, which can be used to report progress within * the backup or restore process. * - * @return core_backup_progress Progress reporting object + * @return \core\progress\base Progress reporting object */ public function get_progress() { return $this->progress; @@ -46,9 +46,9 @@ abstract class base_controller extends backup implements loggable { /** * Sets the progress reporter. * - * @param core_backup_progress $progress Progress reporting object + * @param \core\progress\base $progress Progress reporting object */ - public function set_progress(core_backup_progress $progress) { + public function set_progress(\core\progress\base $progress) { $this->progress = $progress; } @@ -82,4 +82,4 @@ abstract class base_controller extends backup implements loggable { public function log($message, $level, $a = null, $depth = null, $display = false) { backup_helper::log($message, $level, $a, $depth, $display, $this->logger); } -} \ No newline at end of file +} diff --git a/backup/controller/restore_controller.class.php b/backup/controller/restore_controller.class.php index f4d39585211..822992ce4f7 100644 --- a/backup/controller/restore_controller.class.php +++ b/backup/controller/restore_controller.class.php @@ -70,10 +70,10 @@ class restore_controller extends base_controller { * @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ] * @param int $userid * @param int $target backup::TARGET_[ NEW_COURSE | CURRENT_ADDING | CURRENT_DELETING | EXISTING_ADDING | EXISTING_DELETING ] - * @param core_backup_progress $progress Optional progress monitor + * @param \core\progress\base $progress Optional progress monitor */ public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target, - core_backup_progress $progress = null) { + \core\progress\base $progress = null) { $this->tempdir = $tempdir; $this->courseid = $courseid; $this->interactive = $interactive; @@ -111,7 +111,7 @@ class restore_controller extends base_controller { if ($progress) { $this->progress = $progress; } else { - $this->progress = new core_backup_null_progress(); + $this->progress = new \core\progress\null(); } $this->progress->start_progress('Constructing restore_controller'); diff --git a/backup/import.php b/backup/import.php index 7a320573dcb..b2f34ab2b51 100644 --- a/backup/import.php +++ b/backup/import.php @@ -95,7 +95,7 @@ if ($backup->get_stage() == backup_ui::STAGE_FINAL) { echo $renderer->progress_bar($backup->get_progress_bar()); // Start the progress display - we split into 2 chunks for backup and restore. - $progress = new core_backup_display_progress(); + $progress = new \core\progress\display(); $progress->start_progress('', 2); $backup->get_controller()->set_progress($progress); diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index 46862a700d7..4e79cc529a3 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -1748,13 +1748,13 @@ class backup_zip_contents extends backup_execution_step implements file_progress // Start tracking progress if necessary. if (!$this->startedprogress) { $reporter->start_progress('extract_file_to_dir', ($max == file_progress::INDETERMINATE) - ? core_backup_progress::INDETERMINATE : $max); + ? \core\progress\base::INDETERMINATE : $max); $this->startedprogress = true; } // Pass progress through to whatever handles it. $reporter->progress(($progress == file_progress::INDETERMINATE) - ? core_backup_progress::INDETERMINATE : $progress); + ? \core\progress\base::INDETERMINATE : $progress); } } diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index cba5771ccfd..1045d9465d3 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -3564,7 +3564,7 @@ class restore_create_question_files extends restore_execution_step { // Track progress, as this task can take a long time. $progress = $this->task->get_progress(); - $progress->start_progress($this->get_name(), core_backup_progress::INDETERMINATE); + $progress->start_progress($this->get_name(), \core\progress\base::INDETERMINATE); // Let's process only created questions $questionsrs = $DB->get_recordset_sql("SELECT bi.itemid, bi.newitemid, bi.parentitemid, q.qtype diff --git a/backup/restore.php b/backup/restore.php index 96121639d19..3ac6f8d422c 100644 --- a/backup/restore.php +++ b/backup/restore.php @@ -34,7 +34,7 @@ echo $OUTPUT->header(); // Prepare a progress bar which can display optionally during long-running // operations while setting up the UI. -$slowprogress = new core_backup_display_progress_if_slow(get_string('preparingui', 'backup')); +$slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup')); // Overall, allow 10 units of progress. $slowprogress->start_progress('', 10); @@ -90,7 +90,7 @@ $slowprogress->end_progress(); if (!$restore->is_independent()) { // Use a temporary (disappearing) progress bar to show the precheck progress if any. - $precheckprogress = new core_backup_display_progress_if_slow(get_string('preparingdata', 'backup')); + $precheckprogress = new \core\progress\display_if_slow(get_string('preparingdata', 'backup')); $restore->get_controller()->set_progress($precheckprogress); if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) { try { @@ -99,7 +99,7 @@ if (!$restore->is_independent()) { // Show the current restore state (header with bolded item). echo $renderer->progress_bar($restore->get_progress_bar()); // Start displaying the actual progress bar percentage. - $restore->get_controller()->set_progress(new core_backup_display_progress()); + $restore->get_controller()->set_progress(new \core\progress\display()); // Prepare logger. $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO); $restore->get_controller()->add_logger($logger); diff --git a/backup/util/dbops/backup_controller_dbops.class.php b/backup/util/dbops/backup_controller_dbops.class.php index d9715d1e4f2..a6cfb558d89 100644 --- a/backup/util/dbops/backup_controller_dbops.class.php +++ b/backup/util/dbops/backup_controller_dbops.class.php @@ -350,10 +350,10 @@ abstract class backup_controller_dbops extends backup_dbops { * to track progress in processing (in case this task takes a long time). * * @param string $backupid Backup ID - * @param core_backup_progress $progress Optional progress monitor + * @param \core\progress\base $progress Optional progress monitor */ public static function get_moodle_backup_information($backupid, - core_backup_progress $progress = null) { + \core\progress\base $progress = null) { // Start tracking progress if required (for load_controller). if ($progress) { diff --git a/backup/util/dbops/backup_structure_dbops.class.php b/backup/util/dbops/backup_structure_dbops.class.php index 61a903a9919..6202906e90e 100644 --- a/backup/util/dbops/backup_structure_dbops.class.php +++ b/backup/util/dbops/backup_structure_dbops.class.php @@ -133,9 +133,9 @@ abstract class backup_structure_dbops extends backup_dbops { * * @param string $backupid Backup ID * @param string $itemname Item name - * @param core_backup_progress $progress Progress tracker + * @param \core\progress\base $progress Progress tracker */ - public static function move_annotations_to_final($backupid, $itemname, core_backup_progress $progress) { + public static function move_annotations_to_final($backupid, $itemname, \core\progress\base $progress) { global $DB; $progress->start_progress('move_annotations_to_final'); $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname)); diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 00fb8f3616f..079b63c702e 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -112,10 +112,10 @@ abstract class restore_dbops { * * @param string $restoreid Restore id * @param string $inforeffile File path - * @param core_backup_progress $progress Progress tracker + * @param \core\progress\base $progress Progress tracker */ public static function load_inforef_to_tempids($restoreid, $inforeffile, - core_backup_progress $progress = null) { + \core\progress\base $progress = null) { if (!file_exists($inforeffile)) { // Shouldn't happen ever, but... throw new backup_helper_exception('missing_inforef_xml_file', $inforeffile); @@ -123,7 +123,7 @@ abstract class restore_dbops { // Set up progress tracking (indeterminate). if (!$progress) { - $progress = new core_backup_null_progress(); + $progress = new \core\progress\null(); } $progress->start_progress('Loading inforef.xml file'); @@ -419,10 +419,10 @@ abstract class restore_dbops { * * @param string $restoreid Restore id * @param string $usersfile File path - * @param core_backup_progress $progress Progress tracker + * @param \core\progress\base $progress Progress tracker */ public static function load_users_to_tempids($restoreid, $usersfile, - core_backup_progress $progress = null) { + \core\progress\base $progress = null) { if (!file_exists($usersfile)) { // Shouldn't happen ever, but... throw new backup_helper_exception('missing_users_xml_file', $usersfile); @@ -430,7 +430,7 @@ abstract class restore_dbops { // Set up progress tracking (indeterminate). if (!$progress) { - $progress = new core_backup_null_progress(); + $progress = new \core\progress\null(); } $progress->start_progress('Loading users into temporary table'); @@ -861,13 +861,13 @@ abstract class restore_dbops { * @param int|null $olditemid * @param int|null $forcenewcontextid explicit value for the new contextid (skip mapping) * @param bool $skipparentitemidctxmatch - * @param core_backup_progress $progress Optional progress reporter + * @param \core\progress\base $progress Optional progress reporter * @return array of result object */ public static function send_files_to_pool($basepath, $restoreid, $component, $filearea, $oldcontextid, $dfltuserid, $itemname = null, $olditemid = null, $forcenewcontextid = null, $skipparentitemidctxmatch = false, - core_backup_progress $progress = null) { + \core\progress\base $progress = null) { global $DB, $CFG; $backupinfo = backup_general_helper::get_backup_information(basename($basepath)); @@ -1084,10 +1084,10 @@ abstract class restore_dbops { * @param string $basepath Base path of unzipped backup * @param string $restoreid Restore ID * @param int $userid Default userid for files - * @param core_backup_progress $progress Object used for progress tracking + * @param \core\progress\base $progress Object used for progress tracking */ public static function create_included_users($basepath, $restoreid, $userid, - core_backup_progress $progress) { + \core\progress\base $progress) { global $CFG, $DB; $progress->start_progress('Creating included users'); @@ -1481,10 +1481,10 @@ abstract class restore_dbops { * @param int $courseid Course id * @param int $userid User id * @param bool $samesite True if restore is to same site - * @param core_backup_progress $progress Progress reporter + * @param \core\progress\base $progress Progress reporter */ public static function precheck_included_users($restoreid, $courseid, $userid, $samesite, - core_backup_progress $progress) { + \core\progress\base $progress) { global $CFG, $DB; // To return any problem found @@ -1570,10 +1570,10 @@ abstract class restore_dbops { * @param int $courseid Course id * @param int $userid User id * @param bool $samesite True if restore is to same site - * @param core_backup_progress $progress Optional progress tracker + * @param \core\progress\base $progress Optional progress tracker */ public static function process_included_users($restoreid, $courseid, $userid, $samesite, - core_backup_progress $progress = null) { + \core\progress\base $progress = null) { global $DB; // Just let precheck_included_users() to do all the hard work diff --git a/backup/util/helper/backup_helper.class.php b/backup/util/helper/backup_helper.class.php index c2d59aae048..963e471b247 100644 --- a/backup/util/helper/backup_helper.class.php +++ b/backup/util/helper/backup_helper.class.php @@ -46,9 +46,9 @@ abstract class backup_helper { * progress reports. * * @param string $backupid Backup id - * @param core_backup_progress $progress Optional progress reporting object + * @param \core\progress\base $progress Optional progress reporting object */ - static public function clear_backup_dir($backupid, core_backup_progress $progress = null) { + static public function clear_backup_dir($backupid, \core\progress\base $progress = null) { global $CFG; if (!self::delete_dir_contents($CFG->tempdir . '/backup/' . $backupid, '', $progress)) { throw new backup_helper_exception('cannot_empty_backup_temp_dir'); @@ -63,9 +63,9 @@ abstract class backup_helper { * progress reports. * * @param string $backupid Backup id - * @param core_backup_progress $progress Optional progress reporting object + * @param \core\progress\base $progress Optional progress reporting object */ - static public function delete_backup_dir($backupid, core_backup_progress $progress = null) { + static public function delete_backup_dir($backupid, \core\progress\base $progress = null) { global $CFG; self::clear_backup_dir($backupid, $progress); return rmdir($CFG->tempdir . '/backup/' . $backupid); @@ -81,9 +81,9 @@ abstract class backup_helper { * * @param string $dir Directory to delete * @param string $excludedir Exclude this directory - * @param core_backup_progress $progress Optional progress reporting object + * @param \core\progress\base $progress Optional progress reporting object */ - static public function delete_dir_contents($dir, $excludeddir='', core_backup_progress $progress = null) { + static public function delete_dir_contents($dir, $excludeddir='', \core\progress\base $progress = null) { global $CFG; if ($progress) { @@ -154,9 +154,9 @@ abstract class backup_helper { * progress reports. * * @param int $deletefrom Time to delete from - * @param core_backup_progress $progress Optional progress reporting object + * @param \core\progress\base $progress Optional progress reporting object */ - static public function delete_old_backup_dirs($deletefrom, core_backup_progress $progress = null) { + static public function delete_old_backup_dirs($deletefrom, \core\progress\base $progress = null) { global $CFG; $status = true; @@ -213,12 +213,12 @@ abstract class backup_helper { * * @param int $backupid * @param string $filepath zip file containing the backup - * @param core_backup_progress $progress Optional progress monitor + * @param \core\progress\base $progress Optional progress monitor * @return stored_file if created, null otherwise * * @throws moodle_exception in case of any problems */ - static public function store_backup_file($backupid, $filepath, core_backup_progress $progress = null) { + static public function store_backup_file($backupid, $filepath, \core\progress\base $progress = null) { global $CFG; // First of all, get some information from the backup_controller to help us decide diff --git a/backup/util/includes/backup_includes.php b/backup/util/includes/backup_includes.php index 2a4f22266d9..87d237d690b 100644 --- a/backup/util/includes/backup_includes.php +++ b/backup/util/includes/backup_includes.php @@ -72,10 +72,6 @@ require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php'); require_once($CFG->dirroot . '/backup/util/loggers/core_backup_html_logger.class.php'); require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php'); require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php'); -require_once($CFG->dirroot . '/backup/util/progress/core_backup_progress.class.php'); -require_once($CFG->dirroot . '/backup/util/progress/core_backup_null_progress.class.php'); -require_once($CFG->dirroot . '/backup/util/progress/core_backup_display_progress.class.php'); -require_once($CFG->dirroot . '/backup/util/progress/core_backup_display_progress_if_slow.class.php'); require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php'); require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php'); require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php'); diff --git a/backup/util/includes/restore_includes.php b/backup/util/includes/restore_includes.php index fd223c53c0d..ec0f8eb9801 100644 --- a/backup/util/includes/restore_includes.php +++ b/backup/util/includes/restore_includes.php @@ -61,10 +61,6 @@ require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php'); require_once($CFG->dirroot . '/backup/util/loggers/core_backup_html_logger.class.php'); require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php'); require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php'); -require_once($CFG->dirroot . '/backup/util/progress/core_backup_progress.class.php'); -require_once($CFG->dirroot . '/backup/util/progress/core_backup_null_progress.class.php'); -require_once($CFG->dirroot . '/backup/util/progress/core_backup_display_progress.class.php'); -require_once($CFG->dirroot . '/backup/util/progress/core_backup_display_progress_if_slow.class.php'); require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php'); require_once($CFG->dirroot . '/backup/util/factories/restore_factory.class.php'); require_once($CFG->dirroot . '/backup/util/helper/backup_helper.class.php'); diff --git a/backup/util/plan/backup_plan.class.php b/backup/util/plan/backup_plan.class.php index 3d537d2cf43..2fcb4692ba3 100644 --- a/backup/util/plan/backup_plan.class.php +++ b/backup/util/plan/backup_plan.class.php @@ -91,7 +91,7 @@ class backup_plan extends base_plan implements loggable { * Gets the progress reporter, which can be used to report progress within * the backup or restore process. * - * @return core_backup_progress Progress reporting object + * @return \core\progress\base Progress reporting object */ public function get_progress() { return $this->controller->get_progress(); diff --git a/backup/util/plan/base_plan.class.php b/backup/util/plan/base_plan.class.php index c465b2efa79..07f380576d8 100644 --- a/backup/util/plan/base_plan.class.php +++ b/backup/util/plan/base_plan.class.php @@ -185,7 +185,7 @@ abstract class base_plan implements checksumable, executable { * Gets the progress reporter, which can be used to report progress within * the backup or restore process. * - * @return core_backup_progress Progress reporting object + * @return \core\progress\base Progress reporting object */ public abstract function get_progress(); diff --git a/backup/util/plan/base_task.class.php b/backup/util/plan/base_task.class.php index e167b89dd6c..19cb072b25b 100644 --- a/backup/util/plan/base_task.class.php +++ b/backup/util/plan/base_task.class.php @@ -126,7 +126,7 @@ abstract class base_task implements checksumable, executable, loggable { * Gets the progress reporter, which can be used to report progress within * the backup or restore process. * - * @return core_backup_progress Progress reporting object + * @return \core\progress\base Progress reporting object */ public function get_progress() { return $this->plan->get_progress(); diff --git a/backup/util/plan/restore_plan.class.php b/backup/util/plan/restore_plan.class.php index e173e0efc86..6f5b5b5bf91 100644 --- a/backup/util/plan/restore_plan.class.php +++ b/backup/util/plan/restore_plan.class.php @@ -98,7 +98,7 @@ class restore_plan extends base_plan implements loggable { * Gets the progress reporter, which can be used to report progress within * the backup or restore process. * - * @return core_backup_progress Progress reporting object + * @return \core\progress\base Progress reporting object */ public function get_progress() { return $this->controller->get_progress(); diff --git a/backup/util/plan/restore_structure_step.class.php b/backup/util/plan/restore_structure_step.class.php index 300f92d485d..e4685b09615 100644 --- a/backup/util/plan/restore_structure_step.class.php +++ b/backup/util/plan/restore_structure_step.class.php @@ -103,7 +103,7 @@ abstract class restore_structure_step extends restore_step { // Set up progress tracking. $progress = $this->get_task()->get_progress(); - $progress->start_progress($this->get_name(), core_backup_progress::INDETERMINATE); + $progress->start_progress($this->get_name(), \core\progress\base::INDETERMINATE); $xmlparser->set_progress($progress); // And process it, dispatch to target methods in step will start automatically @@ -229,7 +229,7 @@ abstract class restore_structure_step extends restore_step { // the execute() method here, which does set up progress like this.) $progress = $this->get_task()->get_progress(); if (!$progress->is_in_progress_section() || - $progress->get_current_max() !== core_backup_progress::INDETERMINATE) { + $progress->get_current_max() !== \core\progress\base::INDETERMINATE) { $progress = null; } diff --git a/backup/util/structure/backup_structure_processor.class.php b/backup/util/structure/backup_structure_processor.class.php index 88cfeca3cee..2cc41dca4b2 100644 --- a/backup/util/structure/backup_structure_processor.class.php +++ b/backup/util/structure/backup_structure_processor.class.php @@ -38,7 +38,7 @@ class backup_structure_processor extends base_processor { protected $vars; // array of backup::VAR_XXX => helper value pairs to be used by source specifications /** - * @var core_backup_progress Progress tracker (null if none) + * @var \core\progress\base Progress tracker (null if none) */ protected $progress; @@ -46,9 +46,9 @@ class backup_structure_processor extends base_processor { * Constructor. * * @param xml_writer $writer XML writer to save data - * @param core_backup_progress $progress Progress tracker (optional) + * @param c\core\progress\base$progress Progress tracker (optional) */ - public function __construct(xml_writer $writer, core_backup_progress $progress = null) { + public function __construct(xml_writer $writer, \core\progress\base $progress = null) { $this->writer = $writer; $this->progress = $progress; $this->vars = array(); diff --git a/backup/util/ui/restore_ui.class.php b/backup/util/ui/restore_ui.class.php index 251cbc17e07..cd749724cfe 100644 --- a/backup/util/ui/restore_ui.class.php +++ b/backup/util/ui/restore_ui.class.php @@ -51,7 +51,7 @@ class restore_ui extends base_ui { protected $stage = null; /** - * @var core_backup_progress Progress indicator (where there is no controller) + * @var \core\progress\base Progress indicator (where there is no controller) */ protected $progressreporter = null; @@ -146,11 +146,11 @@ class restore_ui extends base_ui { * there are long-running tasks even though there is no restore controller * in use. * - * @return core_backup_null_progress + * @return \core\progress\null */ public function get_progress_reporter() { if (!$this->progressreporter) { - $this->progressreporter = new core_backup_null_progress(); + $this->progressreporter = new \core\progress\null(); } return $this->progressreporter; } @@ -158,9 +158,9 @@ class restore_ui extends base_ui { /** * Sets the progress reporter that will be returned by get_progress_reporter. * - * @param core_backup_progress $progressreporter Progress reporter + * @param c\core\progress\base$progressreporter Progress reporter */ - public function set_progress_reporter(core_backup_progress $progressreporter) { + public function set_progress_reporter(\core\progress\base $progressreporter) { $this->progressreporter = $progressreporter; } diff --git a/backup/util/ui/restore_ui_stage.class.php b/backup/util/ui/restore_ui_stage.class.php index ef559fda131..63dc00ea359 100644 --- a/backup/util/ui/restore_ui_stage.class.php +++ b/backup/util/ui/restore_ui_stage.class.php @@ -94,7 +94,7 @@ abstract class restore_ui_stage extends base_ui_stage { */ abstract class restore_ui_independent_stage { /** - * @var core_backup_progress Optional progress reporter + * @var \core\progress\base Optional progress reporter */ private $progressreporter; @@ -117,11 +117,11 @@ abstract class restore_ui_independent_stage { * in use. There is a similar function in restore_ui. but that class is not * used on some stages. * - * @return core_backup_null_progress + * @return \core\progress\null */ public function get_progress_reporter() { if (!$this->progressreporter) { - $this->progressreporter = new core_backup_null_progress(); + $this->progressreporter = new \core\progress\null(); } return $this->progressreporter; } @@ -129,9 +129,9 @@ abstract class restore_ui_independent_stage { /** * Sets the progress reporter that will be returned by get_progress_reporter. * - * @param core_backup_progress $progressreporter Progress reporter + * @param \core\progress\base $progressreporter Progress reporter */ - public function set_progress_reporter(core_backup_progress $progressreporter) { + public function set_progress_reporter(\core\progress\base $progressreporter) { $this->progressreporter = $progressreporter; } @@ -272,13 +272,13 @@ class restore_ui_stage_confirm extends restore_ui_independent_stage implements f // Start tracking progress if necessary. if (!$this->startedprogress) { $reporter->start_progress('extract_file_to_dir', - ($max == file_progress::INDETERMINATE) ? core_backup_progress::INDETERMINATE : $max); + ($max == file_progress::INDETERMINATE) ? \core\progress\base::INDETERMINATE : $max); $this->startedprogress = true; } // Pass progress through to whatever handles it. $reporter->progress( - ($progress == file_progress::INDETERMINATE) ? core_backup_progress::INDETERMINATE : $progress); + ($progress == file_progress::INDETERMINATE) ? \core\progress\base::INDETERMINATE : $progress); } /** diff --git a/backup/util/xml/parser/progressive_parser.class.php b/backup/util/xml/parser/progressive_parser.class.php index 882852af211..a5ebc4d6238 100644 --- a/backup/util/xml/parser/progressive_parser.class.php +++ b/backup/util/xml/parser/progressive_parser.class.php @@ -29,7 +29,7 @@ * attributes and case folding and works only with UTF-8 content. It's one * progressive push parser because, intead of loading big crunchs of information * in memory, it "publishes" (pushes) small information in a "propietary array format" througt - * the corresponding @progressive_parser_procesor, that will be the responsibe for + * the corresponding @progressive_parser_processor, that will be the responsibe for * returning information into handy formats to higher levels. * * Note that, while this progressive parser is able to process any XML file, it is @@ -37,7 +37,7 @@ * the expected behaviour) so information belonging to the same path can be returned in * different chunks if there are inner levels/paths in the middle. Be warned! * - * The "propietary array format" that the parser publishes to the @progressive_parser_procesor + * The "propietary array format" that the parser publishes to the @progressive_parser_processor * is this: * array ( * 'path' => path where the tags belong to, @@ -56,7 +56,11 @@ class progressive_parser { protected $xml_parser; // PHP's low level XML SAX parser protected $file; // full path to file being progressively parsed | => mutually exclusive protected $contents; // contents being progressively parsed | - protected $procesor; // progressive_parser_procesor to be used to publish processed information + + /** + * @var progressive_parser_processor to be used to publish processed information + */ + protected $processor; protected $level; // level of the current tag protected $path; // path of the current tag @@ -68,7 +72,7 @@ class progressive_parser { protected $currtag; // name/value/attributes of the tag being processed /** - * @var core_backup_progress Progress tracker called for each action + * @var \core\progress\base Progress tracker called for each action */ protected $progress; @@ -129,9 +133,9 @@ class progressive_parser { * * The caller should have already called start_progress on the progress tracker. * - * @param core_backup_progress $progress Progress tracker + * @param \core\progress\base $progress Progress tracker */ - public function set_progress(core_backup_progress $progress) { + public function set_progress(\core\progress\base $progress) { $this->progress = $progress; } diff --git a/backup/util/progress/core_backup_progress.class.php b/lib/classes/progress/base.php similarity index 89% rename from backup/util/progress/core_backup_progress.class.php rename to lib/classes/progress/base.php index 7204e585750..d1eaf91e131 100644 --- a/backup/util/progress/core_backup_progress.class.php +++ b/lib/classes/progress/base.php @@ -14,17 +14,21 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core\progress; + +defined('MOODLE_INTERNAL') || die(); + /** - * Base class for handling progress information during a backup and restore. + * Base class for handling progress information. * * Subclasses should generally override the current_progress function which * summarises all progress information. * - * @package core_backup + * @package core_progress * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -abstract class core_backup_progress { +abstract class base { /** * @var int Constant indicating that the number of progress calls is unknown. */ @@ -66,7 +70,7 @@ abstract class core_backup_progress { protected $currents = array(); /** - * @var int Array of counts within parent progress entry (ignored for first) + * @var int[] Array of counts within parent progress entry (ignored for first) */ protected $parentcounts = array(); @@ -88,16 +92,16 @@ abstract class core_backup_progress { * @param string $description Description to display * @param int $max Maximum value of progress for this section * @param int $parentcount How many progress points this section counts for - * @throws coding_exception If max is invalid + * @throws \coding_exception If max is invalid */ public function start_progress($description, $max = self::INDETERMINATE, $parentcount = 1) { if ($max != self::INDETERMINATE && $max < 0) { - throw new coding_exception( + throw new \coding_exception( 'start_progress() max value cannot be negative'); } if ($parentcount < 1) { - throw new coding_exception( + throw new \coding_exception( 'start_progress() parent progress count must be at least 1'); } if (!empty($this->descriptions)) { @@ -105,13 +109,13 @@ abstract class core_backup_progress { if ($prevmax !== self::INDETERMINATE) { $prevcurrent = end($this->currents); if ($prevcurrent + $parentcount > $prevmax) { - throw new coding_exception( + throw new \coding_exception( 'start_progress() parent progress would exceed max'); } } } else { if ($parentcount != 1) { - throw new coding_exception( + throw new \coding_exception( 'start_progress() progress count must be 1 when no parent'); } } @@ -130,11 +134,11 @@ abstract class core_backup_progress { * If there is a parent progress section, its progress will be increased * automatically to reflect the end of the child section. * - * @throws coding_exception If progress hasn't been started + * @throws \coding_exception If progress hasn't been started */ public function end_progress() { if (!count($this->descriptions)) { - throw new coding_exception('end_progress() without start_progress()'); + throw new \coding_exception('end_progress() without start_progress()'); } array_pop($this->descriptions); array_pop($this->maxes); @@ -164,7 +168,7 @@ abstract class core_backup_progress { * INDETERMINATE. Otherwise it must not be indeterminate. * * @param int $progress Progress so far - * @throws coding_exception If progress value is invalid + * @throws \coding_exception If progress value is invalid */ public function progress($progress = self::INDETERMINATE) { // Ignore too-frequent progress calls (more than once per second). @@ -176,7 +180,7 @@ abstract class core_backup_progress { // Check we are inside a progress section. $max = end($this->maxes); if ($max === false) { - throw new coding_exception( + throw new \coding_exception( 'progress() without start_progress'); } @@ -184,20 +188,20 @@ abstract class core_backup_progress { if ($progress === self::INDETERMINATE) { // Indeterminate progress. if ($max !== self::INDETERMINATE) { - throw new coding_exception( + throw new \coding_exception( 'progress() INDETERMINATE, expecting value'); } } else { // Determinate progress. $current = end($this->currents); if ($max === self::INDETERMINATE) { - throw new coding_exception( + throw new \coding_exception( 'progress() with value, expecting INDETERMINATE'); } else if ($progress < 0 || $progress > $max) { - throw new coding_exception( + throw new \coding_exception( 'progress() value out of range'); } else if ($progress < $current) { - throw new coding_Exception( + throw new \coding_exception( 'progress() value may not go backwards'); } $this->currents[key($this->currents)] = $progress; @@ -236,24 +240,25 @@ abstract class core_backup_progress { /** * Checks max value of current progress section. * - * @return int Current max value (may be core_backup_progress::INDETERMINATE) - * @throws coding_exception If not in a progress section + * @return int Current max value (may be \core\progress\base::INDETERMINATE) + * @throws \coding_exception If not in a progress section */ public function get_current_max() { $max = end($this->maxes); if ($max === false) { - throw new coding_exception('Not inside progress section'); + throw new \coding_exception('Not inside progress section'); } return $max; } /** + * @throws \coding_exception * @return string Current progress section description */ public function get_current_description() { $description = end($this->descriptions); if ($description === false) { - throw new coding_exception('Not inside progress section'); + throw new \coding_exception('Not inside progress section'); } return $description; } diff --git a/backup/util/progress/core_backup_display_progress.class.php b/lib/classes/progress/display.php similarity index 86% rename from backup/util/progress/core_backup_display_progress.class.php rename to lib/classes/progress/display.php index f893b431053..4e3b46ff501 100644 --- a/backup/util/progress/core_backup_display_progress.class.php +++ b/lib/classes/progress/display.php @@ -14,23 +14,27 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core\progress; + +defined('MOODLE_INTERNAL') || die(); + /** * Progress handler that uses a standard Moodle progress bar to display * progress. The Moodle progress bar cannot show indeterminate progress, * so we do extra output in addition to the bar. * - * @package core_backup + * @package core_progress * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_backup_display_progress extends core_backup_progress { +class display extends base { /** * @var int Number of wibble states (state0...stateN-1 classes in CSS) */ const WIBBLE_STATES = 13; /** - * @var progress_bar Current progress bar. + * @var \progress_bar Current progress bar. */ private $bar; @@ -54,8 +58,8 @@ class core_backup_display_progress extends core_backup_progress { } /** - * By default, the progress section names do not display because (in backup) - * these are usually untranslated and incomprehensible. To make them + * By default, the progress section names do not display because + * these will probably be untranslated and incomprehensible. To make them * display, call this method. * * @param bool $displaynames True to display names @@ -69,15 +73,15 @@ class core_backup_display_progress extends core_backup_progress { * * Called in constructor and in update_progress if required. * - * @throws coding_exception If already started + * @throws \coding_exception If already started */ public function start_html() { if ($this->bar) { - throw new coding_exception('Already started'); + throw new \coding_exception('Already started'); } - $this->bar = new progress_bar(); + $this->bar = new \progress_bar(); $this->bar->create(); - echo html_writer::start_div('wibbler'); + echo \html_writer::start_div('wibbler'); } /** @@ -92,13 +96,13 @@ class core_backup_display_progress extends core_backup_progress { $this->bar = null; // End wibbler div. - echo html_writer::end_div(); + echo \html_writer::end_div(); } /** * When progress is updated, updates the bar. * - * @see core_backup_progress::update_progress() + * @see \core\progress\base::update_progress() */ public function update_progress() { // If finished... @@ -114,7 +118,7 @@ class core_backup_display_progress extends core_backup_progress { // (up to once per second). if (time() != $this->lastwibble) { $this->lastwibble = time(); - echo html_writer::div('', 'wibble state' . $this->currentstate); + echo \html_writer::div('', 'wibble state' . $this->currentstate); // Go on to next colour. $this->currentstate += $this->direction; diff --git a/backup/util/progress/core_backup_display_progress_if_slow.class.php b/lib/classes/progress/display_if_slow.php similarity index 85% rename from backup/util/progress/core_backup_display_progress_if_slow.class.php rename to lib/classes/progress/display_if_slow.php index fdf8cfc6f98..c0833003b86 100644 --- a/backup/util/progress/core_backup_display_progress_if_slow.class.php +++ b/lib/classes/progress/display_if_slow.php @@ -14,20 +14,24 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core\progress; + +defined('MOODLE_INTERNAL') || die(); + /** * Progress handler that uses a standard Moodle progress bar to display - * progress. Same as core_backup_display_progress, but the bar does not + * progress. Same as \core\progress\display, but the bar does not * appear until a certain time has elapsed, and disappears automatically * after it finishes. * * The bar can be re-used, i.e. if you end all sections it will disappear, * but if you start all sections, a new bar will be output. * - * @package core_backup + * @package core_progress * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_backup_display_progress_if_slow extends core_backup_display_progress { +class display_if_slow extends display { /** * @var int Waits this many seconds before displaying progress bar */ @@ -71,16 +75,16 @@ class core_backup_display_progress_if_slow extends core_backup_display_progress * Starts displaying the progress bar, with optional heading and a special * div so it can be hidden later. * - * @see core_backup_display_progress::start_html() + * @see \core\progress\display::start_html() */ public function start_html() { global $OUTPUT; - $this->id = 'core_backup_display_progress_if_slow' . self::$nextid; + $this->id = 'core_progress_display_if_slow' . self::$nextid; self::$nextid++; // Containing div includes a CSS class so that it can be themed if required, // and an id so it can be automatically hidden at end. - echo html_writer::start_div('core_backup_display_progress_if_slow', + echo \html_writer::start_div('core_progress_display_if_slow', array('id' => $this->id)); // Display optional heading. @@ -96,7 +100,7 @@ class core_backup_display_progress_if_slow extends core_backup_display_progress * When progress is updated, after a certain time, starts actually displaying * the progress bar. * - * @see core_backup_progress::update_progress() + * @see \core\progress\base::update_progress() */ public function update_progress() { // If we haven't started yet, consider starting. @@ -116,12 +120,12 @@ class core_backup_display_progress_if_slow extends core_backup_display_progress /** * Finishes parent display then closes div and hides it. * - * @see core_backup_display_progress::end_html() + * @see \core\progress\display::end_html() */ public function end_html() { parent::end_html(); - echo html_writer::end_div(); - echo html_writer::script('document.getElementById("' . $this->id . + echo \html_writer::end_div(); + echo \html_writer::script('document.getElementById("' . $this->id . '").style.display = "none"'); } } diff --git a/backup/util/progress/core_backup_null_progress.class.php b/lib/classes/progress/null.php similarity index 88% rename from backup/util/progress/core_backup_null_progress.class.php rename to lib/classes/progress/null.php index 3e02369751c..f332a30b0f0 100644 --- a/backup/util/progress/core_backup_null_progress.class.php +++ b/lib/classes/progress/null.php @@ -14,14 +14,18 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core\progress; + +defined('MOODLE_INTERNAL') || die(); + /** * Progress handler that ignores progress entirely. * - * @package core_backup + * @package core_progress * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_backup_null_progress extends core_backup_progress { +class null extends base { public function update_progress() { // Do nothing. } diff --git a/backup/util/progress/tests/progress_test.php b/lib/tests/progress_test.php similarity index 92% rename from backup/util/progress/tests/progress_test.php rename to lib/tests/progress_test.php index 1e4f8c8033a..fa50404d7f5 100644 --- a/backup/util/progress/tests/progress_test.php +++ b/lib/tests/progress_test.php @@ -17,7 +17,7 @@ /** * Unit tests for the progress classes. * - * @package core_backup + * @package core_progress * @category phpunit * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -25,20 +25,16 @@ defined('MOODLE_INTERNAL') || die(); -// Include all the needed stuff. -global $CFG; -require_once($CFG->dirroot . '/backup/util/progress/core_backup_progress.class.php'); - /** * Progress tests. */ -class backup_progress_testcase extends basic_testcase { +class core_progress_testcase extends basic_testcase { /** * Tests for basic use with simple numeric progress. */ public function test_basic() { - $progress = new core_backup_mock_progress(); + $progress = new core_mock_progress(); // Check values of empty progress things. $this->assertFalse($progress->is_in_progress_section()); @@ -59,7 +55,7 @@ class backup_progress_testcase extends basic_testcase { core_php_time_limit::get_and_clear_unit_test_data(); $progress->progress(2); $this->assertTrue($progress->was_update_called()); - $this->assertEquals(array(core_backup_progress::TIME_LIMIT_WITHOUT_PROGRESS), + $this->assertEquals(array(\core\progress\base::TIME_LIMIT_WITHOUT_PROGRESS), core_php_time_limit::get_and_clear_unit_test_data()); // Check the new value. @@ -86,7 +82,7 @@ class backup_progress_testcase extends basic_testcase { */ public function test_nested() { // Outer progress goes from 0 to 10. - $progress = new core_backup_mock_progress(); + $progress = new core_mock_progress(); $progress->start_progress('hello', 10); // Get up to 4, check position. @@ -163,7 +159,7 @@ class backup_progress_testcase extends basic_testcase { * Tests the feature for 'weighting' nested progress. */ public function test_nested_weighted() { - $progress = new core_backup_mock_progress(); + $progress = new core_mock_progress(); $progress->start_progress('', 10); // First nested child has 2 units of its own and is worth 1 unit. @@ -183,7 +179,7 @@ class backup_progress_testcase extends basic_testcase { $this->assert_min_max(0.4, 0.4, $progress); // Next indeterminate child is worth 6 units. - $progress->start_progress('', core_backup_progress::INDETERMINATE, 6); + $progress->start_progress('', \core\progress\base::INDETERMINATE, 6); $progress->step_time(); $progress->progress(); $this->assert_min_max(0.4, 1.0, $progress); @@ -196,7 +192,7 @@ class backup_progress_testcase extends basic_testcase { * to be similar. */ public function test_realistic() { - $progress = new core_backup_mock_progress(); + $progress = new core_mock_progress(); $progress->start_progress('parent', 100); $progress->start_progress('child', 1); $progress->progress(1); @@ -210,7 +206,7 @@ class backup_progress_testcase extends basic_testcase { * zero entries. */ public function test_zero() { - $progress = new core_backup_mock_progress(); + $progress = new core_mock_progress(); $progress->start_progress('parent', 100); $progress->progress(1); $this->assert_min_max(0.01, 0.01, $progress); @@ -229,7 +225,7 @@ class backup_progress_testcase extends basic_testcase { * Tests for any exceptions due to invalid calls. */ public function test_exceptions() { - $progress = new core_backup_mock_progress(); + $progress = new core_mock_progress(); // Check errors when empty. try { @@ -268,7 +264,7 @@ class backup_progress_testcase extends basic_testcase { // Indeterminate when value expected. $progress->start_progress('hello', 10); try { - $progress->progress(core_backup_progress::INDETERMINATE); + $progress->progress(\core\progress\base::INDETERMINATE); $this->fail(); } catch (coding_exception $e) { $this->assertEquals(1, preg_match('~expecting value~', $e->getMessage())); @@ -326,9 +322,9 @@ class backup_progress_testcase extends basic_testcase { * * @param number $min Expected min progress * @param number $max Expected max progress - * @param core_backup_mock_progress $progress + * @param core_mock_progress $progress */ - private function assert_min_max($min, $max, core_backup_mock_progress $progress) { + private function assert_min_max($min, $max, core_mock_progress $progress) { $this->assertEquals(array($min, $max), $progress->get_progress_proportion_range()); } @@ -338,7 +334,7 @@ class backup_progress_testcase extends basic_testcase { * Helper class that records when update_progress is called and allows time * stepping. */ -class core_backup_mock_progress extends core_backup_progress { +class core_mock_progress extends \core\progress\base { private $updatecalled = false; private $time = 1;