mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
MDL-41827 tool_generator: Adding a file size limit
This commit is contained in:
parent
09373a5a62
commit
53e6d79c39
@ -51,6 +51,11 @@ abstract class tool_generator_backend {
|
||||
*/
|
||||
protected $fixeddataset;
|
||||
|
||||
/**
|
||||
* @var int|bool Maximum number of bytes for file.
|
||||
*/
|
||||
protected $filesizelimit;
|
||||
|
||||
/**
|
||||
* @var bool True if displaying progress
|
||||
*/
|
||||
@ -81,10 +86,11 @@ abstract class tool_generator_backend {
|
||||
*
|
||||
* @param int $size Size as numeric index
|
||||
* @param bool $fixeddataset To use fixed or random data
|
||||
* @param int|bool $filesizelimit The max number of bytes for a generated file
|
||||
* @param bool $progress True if progress information should be displayed
|
||||
* @throws coding_exception If parameters are invalid
|
||||
*/
|
||||
public function __construct($size, $fixeddataset = false, $progress = true) {
|
||||
public function __construct($size, $fixeddataset = false, $filesizelimit = false, $progress = true) {
|
||||
|
||||
// Check parameter.
|
||||
if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) {
|
||||
@ -94,6 +100,7 @@ abstract class tool_generator_backend {
|
||||
// Set parameters.
|
||||
$this->size = $size;
|
||||
$this->fixeddataset = $fixeddataset;
|
||||
$this->filesizelimit = $filesizelimit;
|
||||
$this->progress = $progress;
|
||||
}
|
||||
|
||||
|
@ -100,15 +100,15 @@ class tool_generator_course_backend extends tool_generator_backend {
|
||||
* @param string $shortname Course shortname
|
||||
* @param int $size Size as numeric index
|
||||
* @param bool $fixeddataset To use fixed or random data
|
||||
* @param int|bool $filesizelimit The max number of bytes for a generated file
|
||||
* @param bool $progress True if progress information should be displayed
|
||||
* @return int Course id
|
||||
*/
|
||||
public function __construct($shortname, $size, $fixeddataset = false, $progress = true) {
|
||||
public function __construct($shortname, $size, $fixeddataset = false, $filesizelimit = false, $progress = true) {
|
||||
|
||||
// Set parameters.
|
||||
$this->shortname = $shortname;
|
||||
|
||||
parent::__construct($size, $fixeddataset, $progress);
|
||||
parent::__construct($size, $fixeddataset, $filesizelimit, $progress);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -345,7 +345,7 @@ class tool_generator_course_backend extends tool_generator_backend {
|
||||
|
||||
// Generate random binary data (different for each file so it
|
||||
// doesn't compress unrealistically).
|
||||
$data = self::get_random_binary(self::$paramsmallfilesize[$this->size]);
|
||||
$data = self::get_random_binary($this->limit_filesize(self::$paramsmallfilesize[$this->size]));
|
||||
|
||||
$fs->create_file_from_string($filerecord, $data);
|
||||
$this->dot($i, $count);
|
||||
@ -362,6 +362,7 @@ class tool_generator_course_backend extends tool_generator_backend {
|
||||
* @return Random data
|
||||
*/
|
||||
private static function get_random_binary($length) {
|
||||
|
||||
$data = microtime(true);
|
||||
if (strlen($data) > $length) {
|
||||
// Use last digits of data.
|
||||
@ -382,8 +383,9 @@ class tool_generator_course_backend extends tool_generator_backend {
|
||||
|
||||
// Work out how many files and how many blocks to use (up to 64KB).
|
||||
$count = self::$parambigfilecount[$this->size];
|
||||
$blocks = ceil(self::$parambigfilesize[$this->size] / 65536);
|
||||
$blocksize = floor(self::$parambigfilesize[$this->size] / $blocks);
|
||||
$filesize = $this->limit_filesize(self::$parambigfilesize[$this->size]);
|
||||
$blocks = ceil($filesize / 65536);
|
||||
$blocksize = floor($filesize / $blocks);
|
||||
|
||||
$this->log('createbigfiles', $count, true);
|
||||
|
||||
@ -504,4 +506,20 @@ class tool_generator_course_backend extends tool_generator_backend {
|
||||
return $userid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restricts the binary file size if necessary
|
||||
*
|
||||
* @param int $length The total length
|
||||
* @return int The limited length if a limit was specified.
|
||||
*/
|
||||
private function limit_filesize($length) {
|
||||
|
||||
// Limit to $this->filesizelimit.
|
||||
if (is_numeric($this->filesizelimit) && $length > $this->filesizelimit) {
|
||||
$length = floor($this->filesizelimit);
|
||||
}
|
||||
|
||||
return $length;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -61,15 +61,16 @@ class tool_generator_site_backend extends tool_generator_backend {
|
||||
* @param int $size Size as numeric index
|
||||
* @param bool $bypasscheck If debugging level checking was skipped.
|
||||
* @param bool $fixeddataset To use fixed or random data
|
||||
* @param int|bool $filesizelimit The max number of bytes for a generated file
|
||||
* @param bool $progress True if progress information should be displayed
|
||||
* @return int Course id
|
||||
*/
|
||||
public function __construct($size, $bypasscheck, $fixeddataset = false, $progress = true) {
|
||||
public function __construct($size, $bypasscheck, $fixeddataset = false, $filesizelimit = false, $progress = true) {
|
||||
|
||||
// Set parameters.
|
||||
$this->bypasscheck = $bypasscheck;
|
||||
|
||||
parent::__construct($size, $fixeddataset, $progress);
|
||||
parent::__construct($size, $fixeddataset, $filesizelimit, $progress);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,6 +35,7 @@ list($options, $unrecognized) = cli_get_params(
|
||||
'shortname' => false,
|
||||
'size' => false,
|
||||
'fixeddataset' => false,
|
||||
'filesizelimit' => false,
|
||||
'bypasscheck' => false,
|
||||
'quiet' => false
|
||||
),
|
||||
@ -52,11 +53,12 @@ Not for use on live sites; only normally works if debugging is set to DEVELOPER
|
||||
level.
|
||||
|
||||
Options:
|
||||
--shortname Shortname of course to create (required)
|
||||
--size Size of course to create XS, S, M, L, XL, or XXL (required)
|
||||
--fixeddataset Use a fixed data set instead of randomly generated data
|
||||
--bypasscheck Bypasses the developer-mode check (be careful!)
|
||||
--quiet Do not show any output
|
||||
--shortname Shortname of course to create (required)
|
||||
--size Size of course to create XS, S, M, L, XL, or XXL (required)
|
||||
--fixeddataset Use a fixed data set instead of randomly generated data
|
||||
--filesizelimit Limits the size of the generated files to the specified bytes
|
||||
--bypasscheck Bypasses the developer-mode check (be careful!)
|
||||
--quiet Do not show any output
|
||||
|
||||
-h, --help Print out this help
|
||||
|
||||
@ -76,6 +78,7 @@ if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) {
|
||||
$shortname = $options['shortname'];
|
||||
$sizename = $options['size'];
|
||||
$fixeddataset = $options['fixeddataset'];
|
||||
$filesizelimit = $options['filesizelimit'];
|
||||
|
||||
// Check size.
|
||||
try {
|
||||
@ -93,5 +96,5 @@ if ($error = tool_generator_course_backend::check_shortname_available($shortname
|
||||
session_set_user(get_admin());
|
||||
|
||||
// Do backend code to generate course.
|
||||
$backend = new tool_generator_course_backend($shortname, $size, $fixeddataset, empty($options['quiet']));
|
||||
$backend = new tool_generator_course_backend($shortname, $size, $fixeddataset, $filesizelimit, empty($options['quiet']));
|
||||
$id = $backend->make();
|
||||
|
@ -34,6 +34,7 @@ list($options, $unrecognized) = cli_get_params(
|
||||
'help' => false,
|
||||
'size' => false,
|
||||
'fixeddataset' => false,
|
||||
'filesizelimit' => false,
|
||||
'bypasscheck' => false,
|
||||
'quiet' => false
|
||||
),
|
||||
@ -57,10 +58,11 @@ Consider that, depending on the size you select, this CLI tool can really genera
|
||||
$sitesizes
|
||||
|
||||
Options:
|
||||
--size Size of the generated site, this value affects the number of courses and their size. Accepted values: XS, S, M, L, XL, or XXL (required)
|
||||
--fixeddataset Use a fixed data set instead of randomly generated data
|
||||
--bypasscheck Bypasses the developer-mode check (be careful!)
|
||||
--quiet Do not show any output
|
||||
--size Size of the generated site, this value affects the number of courses and their size. Accepted values: XS, S, M, L, XL, or XXL (required)
|
||||
--fixeddataset Use a fixed data set instead of randomly generated data
|
||||
--filesizelimit Limits the size of the generated files to the specified bytes
|
||||
--bypasscheck Bypasses the developer-mode check (be careful!)
|
||||
--quiet Do not show any output
|
||||
|
||||
-h, --help Print out this help
|
||||
|
||||
@ -79,6 +81,7 @@ if (empty($options['bypasscheck']) && !$CFG->debugdeveloper) {
|
||||
// Get options.
|
||||
$sizename = $options['size'];
|
||||
$fixeddataset = $options['fixeddataset'];
|
||||
$filesizelimit = $options['filesizelimit'];
|
||||
|
||||
// Check size.
|
||||
try {
|
||||
@ -91,5 +94,5 @@ try {
|
||||
session_set_user(get_admin());
|
||||
|
||||
// Do backend code to generate site.
|
||||
$backend = new tool_generator_site_backend($size, $options['bypasscheck'], $fixeddataset, empty($options['quiet']));
|
||||
$backend = new tool_generator_site_backend($size, $options['bypasscheck'], $fixeddataset, $filesizelimit, empty($options['quiet']));
|
||||
$backend->make();
|
||||
|
Loading…
x
Reference in New Issue
Block a user