From 028b8dc3e9b8400b74dde25ebf28c25e2e921dcc Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 26 Aug 2013 17:23:21 +0800 Subject: [PATCH] MDL-41882 tool_generator: Adding maketestsite script Uses maketestcourse.php to set up a test site. --- admin/tool/generator/classes/site_backend.php | 209 ++++++++++++++++++ admin/tool/generator/cli/maketestsite.php | 96 ++++++++ .../tool/generator/lang/en/tool_generator.php | 9 +- admin/tool/generator/version.php | 2 +- 4 files changed, 314 insertions(+), 2 deletions(-) create mode 100644 admin/tool/generator/classes/site_backend.php create mode 100644 admin/tool/generator/cli/maketestsite.php diff --git a/admin/tool/generator/classes/site_backend.php b/admin/tool/generator/classes/site_backend.php new file mode 100644 index 00000000000..f1d08ff2726 --- /dev/null +++ b/admin/tool/generator/classes/site_backend.php @@ -0,0 +1,209 @@ +. + +/** + * tool_generator site backend. + * + * @package tool_generator + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/backend.php'); + +/** + * Backend code for the site generator. + * + * @package tool_generator + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tool_generator_site_backend extends tool_generator_backend { + + /** + * @var string The course's shortname prefix. + */ + const SHORTNAMEPREFIX = 'testcourse_'; + + /** + * @var bool If the debugging level checking was skipped. + */ + protected $bypasscheck; + + /** + * @var array Multidimensional array where the first level is the course size and the second the site size. + */ + protected static $sitecourses = array( + array(2, 8, 64, 256, 1024, 4096), + array(1, 4, 8, 16, 32, 64), + array(0, 0, 1, 4, 8, 16), + array(0, 0, 0, 1, 0, 0), + array(0, 0, 0, 0, 1, 0), + array(0, 0, 0, 0, 0, 1) + ); + + /** + * Constructs object ready to make the site. + * + * @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 bool $progress True if progress information should be displayed + * @return int Course id + */ + public function __construct($size, $bypasscheck, $fixeddataset = false, $progress = true) { + + // Set parameters. + $this->bypasscheck = $bypasscheck; + + parent::__construct($size, $fixeddataset, $progress); + } + + /** + * Gets a list of size choices supported by this backend. + * + * @return array List of size (int) => text description for display + */ + public static function get_size_choices() { + $options = array(); + for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) { + $options[$size] = get_string('sitesize_' . $size, 'tool_generator'); + } + return $options; + } + + /** + * Runs the entire 'make' process. + * + * @return int Course id + */ + public function make() { + global $DB, $CFG; + + raise_memory_limit(MEMORY_EXTRA); + + if ($this->progress && !CLI_SCRIPT) { + echo html_writer::start_tag('ul'); + } + + $entirestart = microtime(true); + + // Create courses. + $prevchdir = getcwd(); + chdir($CFG->dirroot); + $ncourse = self::get_last_testcourse_id(); + foreach (self::$sitecourses as $coursesize => $ncourses) { + for ($i = 1; $i <= $ncourses[$this->size]; $i++) { + // Non language-dependant shortname. + $ncourse++; + $this->run_create_course(self::SHORTNAMEPREFIX . $ncourse, $coursesize); + } + } + chdir($prevchdir); + + // Store last course id to return it (will be the bigger one). + $lastcourseid = $DB->get_field('course', 'id', array('shortname' => self::SHORTNAMEPREFIX . $ncourse)); + + // Log total time. + $this->log('sitecompleted', round(microtime(true) - $entirestart, 1)); + + if ($this->progress && !CLI_SCRIPT) { + echo html_writer::end_tag('ul'); + } + + return $lastcourseid; + } + + /** + * Creates a course with the specified shortname, coursesize and the provided maketestsite options. + * + * @param string $shortname The course shortname + * @param int $coursesize One of the possible course sizes. + * @return void + */ + protected function run_create_course($shortname, $coursesize) { + + // We are in $CFG->dirroot. + $command = 'php admin/tool/generator/cli/maketestcourse.php'; + + $options = array( + '--shortname="' . $shortname . '"', + '--size="' . get_string('shortsize_' . $coursesize, 'tool_generator') . '"' + ); + + if (!$this->progress) { + $options[] = '--quiet'; + } + + // Extend options. + $optionstoextend = array( + 'fixeddataset' => 'fixeddataset', + 'bypasscheck' => 'bypasscheck', + ); + + // Getting an options string. + foreach ($optionstoextend as $attribute => $option) { + if (!empty($this->{$attribute})) { + $options[] = '--' . $option; + } + } + $options = implode(' ', $options); + if ($this->progress) { + system($command . ' ' . $options, $exitcode); + } else { + passthru($command . ' ' . $options, $exitcode); + } + + if ($exitcode != 0) { + exit($exitcode); + } + } + + /** + * Obtains the last unique sufix (numeric) using the test course prefix. + * + * @return int The last generated numeric value. + */ + protected static function get_last_testcourse_id() { + global $DB; + + $params = array(); + $params['shortnameprefix'] = $DB->sql_like_escape(self::SHORTNAMEPREFIX) . '%'; + $like = $DB->sql_like('shortname', ':shortnameprefix'); + + if (!$testcourses = $DB->get_records_select('course', $like, $params, '', 'shortname')) { + return 0; + } + // SQL order by is not appropiate here as is ordering strings. + $shortnames = array_keys($testcourses); + collatorlib::asort($shortnames, collatorlib::SORT_NATURAL); + $shortnames = array_reverse($shortnames); + + // They come ordered by shortname DESC, so non-numeric values will be the first ones. + $prefixnchars = strlen(self::SHORTNAMEPREFIX); + foreach ($shortnames as $shortname) { + $sufix = substr($shortname, $prefixnchars); + if (preg_match('/^[\d]+$/', $sufix)) { + return $sufix; + } + } + // If all sufixes are not numeric this is the first make test site run. + return 0; + } + +} diff --git a/admin/tool/generator/cli/maketestsite.php b/admin/tool/generator/cli/maketestsite.php new file mode 100644 index 00000000000..504f04a9305 --- /dev/null +++ b/admin/tool/generator/cli/maketestsite.php @@ -0,0 +1,96 @@ +. + +/** + * CLI interface for creating a test site. + * + * @package tool_generator + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define('CLI_SCRIPT', true); +define('NO_OUTPUT_BUFFERING', true); + +require(__DIR__ . '/../../../../config.php'); +require_once($CFG->libdir. '/clilib.php'); +require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/generator/classes/site_backend.php'); + +// CLI options. +list($options, $unrecognized) = cli_get_params( + array( + 'help' => false, + 'size' => false, + 'fixeddataset' => false, + 'bypasscheck' => false, + 'quiet' => false + ), + array( + 'h' => 'help' + ) +); + +$sitesizes = '* ' . implode(PHP_EOL . '* ', tool_generator_site_backend::get_size_choices()); + +// Display help. +if (!empty($options['help']) || empty($options['size'])) { + echo " +Utility to generate a standard test site data set. + +Not for use on live sites; only normally works if debugging is set to DEVELOPER +level. + +Consider that, depending on the size you select, this CLI tool can really generate a lot of data, aproximated sizes: + +$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 + +-h, --help Print out this help + +Example from Moodle root directory: +\$sudo -u www-data /usr/bin/php admin/tool/generator/cli/maketestsite.php --size=S +"; + // Exit with error unless we're showing this because they asked for it. + exit(empty($options['help']) ? 1 : 0); +} + +// Check debugging is set to developer level. +if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) { + cli_error(get_string('error_notdebugging', 'tool_generator')); +} + +// Get options. +$sizename = $options['size']; +$fixeddataset = $options['fixeddataset']; + +// Check size. +try { + $size = tool_generator_site_backend::size_for_name($sizename); +} catch (coding_exception $e) { + cli_error("Invalid size ($sizename). Use --help for help."); +} + +// Switch to admin user account. +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->make(); diff --git a/admin/tool/generator/lang/en/tool_generator.php b/admin/tool/generator/lang/en/tool_generator.php index 753ecfde805..a204ce8aed7 100644 --- a/admin/tool/generator/lang/en/tool_generator.php +++ b/admin/tool/generator/lang/en/tool_generator.php @@ -54,7 +54,7 @@ $string['error_notdebugging'] = 'Not available on this server because debugging $string['firstname'] = 'Test course user'; $string['fullname'] = 'Test course: {$a->size}'; $string['maketestcourse'] = 'Make test course'; -$string['pluginname'] = 'Random course generator'; +$string['pluginname'] = 'Development data generator'; $string['progress_createcourse'] = 'Creating course {$a}'; $string['progress_checkaccounts'] = 'Checking user accounts ({$a})'; $string['progress_coursecompleted'] = 'Course completed ({$a}s)'; @@ -64,11 +64,18 @@ $string['progress_createforum'] = 'Creating forum ({$a} posts)'; $string['progress_createpages'] = 'Creating pages ({$a})'; $string['progress_createsmallfiles'] = 'Creating small files ({$a})'; $string['progress_enrol'] = 'Enrolling users into course ({$a})'; +$string['progress_sitecompleted'] = 'Site completed ({$a}s)'; $string['shortsize_0'] = 'XS'; $string['shortsize_1'] = 'S'; $string['shortsize_2'] = 'M'; $string['shortsize_3'] = 'L'; $string['shortsize_4'] = 'XL'; $string['shortsize_5'] = 'XXL'; +$string['sitesize_0'] = 'XS (~10MB; 3 courses, created in ~30 seconds)'; +$string['sitesize_1'] = 'S (~50MB; 8 courses, created in ~2 minutes)'; +$string['sitesize_2'] = 'M (~200MB; 73 courses, created in ~10 minutes)'; +$string['sitesize_3'] = 'L (~1\'5GB; 277 courses, created in ~1\'5 hours)'; +$string['sitesize_4'] = 'XL (~10GB; 1065 courses, created in ~5 hours)'; +$string['sitesize_5'] = 'XXL (~20GB; 4177 courses, created in ~10 hours)'; $string['size'] = 'Size of course'; $string['smallfiles'] = 'Small files'; diff --git a/admin/tool/generator/version.php b/admin/tool/generator/version.php index 0a413897b18..8430d34c0f9 100644 --- a/admin/tool/generator/version.php +++ b/admin/tool/generator/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2013080700; +$plugin->version = 2013091200; $plugin->requires = 2013050100; $plugin->component = 'tool_generator';