MDL-41754 (1) Progress tracking : moved from backup and restore to core

This commit is contained in:
James Pratt
2014-01-27 17:45:50 +07:00
parent 8e32861786
commit 809fdb83b1
28 changed files with 153 additions and 144 deletions
+2 -2
View File
@@ -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);
@@ -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();
+5 -5
View File
@@ -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);
}
}
}
@@ -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');
+1 -1
View File
@@ -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);
+2 -2
View File
@@ -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);
}
}
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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);
@@ -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) {
@@ -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));
+14 -14
View File
@@ -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
+10 -10
View File
@@ -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
-4
View File
@@ -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');
@@ -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');
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -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();
@@ -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;
}
@@ -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();
+5 -5
View File
@@ -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;
}
+7 -7
View File
@@ -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);
}
/**
@@ -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;
}
@@ -14,17 +14,21 @@
// 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\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;
}
@@ -14,23 +14,27 @@
// 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\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;
@@ -14,20 +14,24 @@
// 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\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"');
}
}
@@ -14,14 +14,18 @@
// 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\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.
}
@@ -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;