Ejemplo n.º 1
0
 public function execute()
 {
     global $CFG;
     if (!function_exists('curl_version')) {
         die("PHP cURL not installed\n");
     }
     /** Ugly hack to get this working. Forking doesn't work here
      * because moodle can't log entries from forked process into db.
      * We need this run in external process because apache doesn't can't
      * give usvalid cookie before moosh process ends.
      */
     $loginasadmin = run_external_command("moosh admin-login", "admin-login failed");
     $target = $CFG->wwwroot . '/admin/index.php';
     $credentials = explode(":", $loginasadmin[0]);
     $cookiename = $credentials[0];
     $cookie = $credentials[1];
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_COOKIE, sprintf('%s=%s', $cookiename, $cookie));
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_URL, $target);
     curl_setopt($ch, CURLOPT_HEADER, true);
     curl_getinfo($ch);
     $result = curl_exec($ch);
     curl_close($ch);
     if (strpos($result, "You are logged in as")) {
         echo "Moodle instance is running.";
         return 0;
     } else {
         echo "Moodle instance unreachable.";
         return 2;
     }
 }
Ejemplo n.º 2
0
 public function execute()
 {
     if (strpos($this->arguments[0], '_') !== false) {
         cli_error("Module name can not contain _");
     }
     //copy newmodule
     $modPath = $this->topDir . '/mod/' . $this->arguments[0];
     if (file_exists($modPath)) {
         cli_problem("Already exists: '{$modPath}'");
         cli_problem("Not creating new module " . $this->arguments[0]);
         exit(1);
     }
     run_external_command("cp -r '{$this->mooshDir}/vendor/moodlehq/moodle-mod_newmodule' '{$modPath}'", "Copying from module template failed");
     if (file_exists("{$modPath}/.git")) {
         run_external_command("rm --interactive=never -r '{$modPath}/.git'", "Removing .git failed");
     }
     //replace newmodule with $this->arguments[0]
     run_external_command("find '{$modPath}' -type f -exec sed 's/newmodule/{$this->arguments[0]}/g' -i {} \\;", "sed command failed");
     //rename lang/en/newmodule.php
     run_external_command("mv '{$modPath}/lang/en/newmodule.php' '{$modPath}/lang/en/{$this->arguments[0]}.php'", "Renaming lang file failed");
     //rename backup files
     run_external_command("mv '{$modPath}/backup/moodle2/backup_newmodule_activity_task.class.php' '{$modPath}/backup/moodle2/backup_{$this->arguments[0]}_activity_task.class.php'", "Renaming backup activity task file failed");
     run_external_command("mv '{$modPath}/backup/moodle2/backup_newmodule_stepslib.php' '{$modPath}/backup/moodle2/backup_{$this->arguments[0]}_stepslib.php'", "Renaming backup stepslib file failed");
     //rename restore files
     run_external_command("mv '{$modPath}/backup/moodle2/restore_newmodule_activity_task.class.php' '{$modPath}/backup/moodle2/restore_{$this->arguments[0]}_activity_task.class.php'", "Renaming restore activity task file failed");
     run_external_command("mv '{$modPath}/backup/moodle2/restore_newmodule_stepslib.php' '{$modPath}/backup/moodle2/restore_{$this->arguments[0]}_stepslib.php'", "Renaming restore stepslib file failed");
 }
Ejemplo n.º 3
0
 public function execute()
 {
     //copy newmodule
     $modPath = $this->topDir . '/availability/condition/' . $this->arguments[0];
     if (file_exists($modPath)) {
         cli_problem("Already exists: '{$modPath}'");
         cli_problem("Not creating new availability condition" . $this->arguments[0]);
         exit(1);
     }
     run_external_command("cp -r '{$this->mooshDir}/vendor/danielneis/moodle-availability_newavailability' '{$modPath}'", "Copying from availability condition template failed");
     if (file_exists("{$modPath}/.git")) {
         run_external_command("rm --interactive=never -r '{$modPath}/.git'", "Removing .git failed");
     }
     //replace newmodule with $this->arguments[0]
     run_external_command("find '{$modPath}' -type f -exec sed 's/newavailability/{$this->arguments[0]}/g' -i {} \\;", "sed command failed");
     //rename lang/en/newmodule.php
     run_external_command("mv '{$modPath}/lang/en/availability_newavailability.php' '{$modPath}/lang/en/availability_{$this->arguments[0]}.php'", "Renaming lang file failed");
     //rename yui/build/moodle-availability_newavailability-form/moodle-availability_newavailability-form.js
     run_external_command("mv '{$modPath}/yui/build/moodle-availability_newavailability-form/moodle-availability_newavailability-form.js' '{$modPath}/yui/build/moodle-availability_newavailability-form/moodle-availability_{$this->arguments[0]}-form.js'", "Renaming yui build js");
     //rename yui/build/moodle-availability_newavailability-form/moodle-availability_newavailability-form-debug.js
     run_external_command("mv '{$modPath}/yui/build/moodle-availability_newavailability-form/moodle-availability_newavailability-form-debug.js' '{$modPath}/yui/build/moodle-availability_newavailability-form/moodle-availability_{$this->arguments[0]}-form-debug.js'", "Renaming yui build debug js");
     //rename yui/build/moodle-availability_newavailability-form/moodle-availability_newavailability-form-min.js
     run_external_command("mv '{$modPath}/yui/build/moodle-availability_newavailability-form/moodle-availability_newavailability-form-min.js' '{$modPath}/yui/build/moodle-availability_newavailability-form/moodle-availability_{$this->arguments[0]}-form-min.js'", "Renaming yui build min js");
     //rename dir yui/build/moodle-availability_newavailability-form
     run_external_command("mv '{$modPath}/yui/build/moodle-availability_newavailability-form' '{$modPath}/yui/build/moodle-availability_{$this->arguments[0]}-form'", "Renaming yui build dir");
 }
Ejemplo n.º 4
0
 public function execute()
 {
     $loader = new Twig_Loader_Filesystem($this->mooshDir . '/templates');
     $twig = new Twig_Environment($loader, array('debug' => true));
     $twig->addExtension(new Twig_Extension_Debug());
     $template = 'form/form-element-' . $this->arguments[0] . '.twig';
     if (!$loader->exists($template)) {
         cli_problem("Template {$template} does not exist");
         exit(1);
     }
     $content = $twig->render($template, array('id' => $this->arguments[1], 'langCategory' => $this->getLangCategory()));
     //do I know where to add the new code?
     $this->loadSession();
     if (isset($this->session['generator.last-file'][$this->cwd]) && file_exists($this->cwd . '/' . $this->session['generator.last-file'][$this->cwd])) {
         $fileName = $this->cwd . '/' . $this->session['generator.last-file'][$this->cwd];
         $fileContent = file_get_contents($fileName);
         //replace /** MOOSH AUTO-GENERATED */ with new code
         $fileContent = str_replace(MOOSH_CODE_MARKER, $content . "\n" . MOOSH_CODE_MARKER, $fileContent);
         file_put_contents($fileName, $fileContent);
         if ($this->defaults['global']['xdotool']) {
             $browser = $this->defaults['global']['browser_string'];
             $active_window = run_external_command("xdotool getwindowfocus", "Couldn't find active window");
             $browser_window = run_external_command("xdotool search --name '{$browser}'", "Couldn't find {$browser}");
             run_external_command("xdotool windowactivate {$browser_window['0']} ", "Couldn't activate window: {$browser}");
             run_external_command("xdotool key F5", "Couldn't refresh browser");
             run_external_command("xdotool windowactivate {$active_window['0']}", "Couldn't activate window: {$active_window['0']}");
         }
     } else {
         echo $content;
     }
 }
Ejemplo n.º 5
0
 public function execute()
 {
     global $CFG, $DB;
     $options = $this->expandedOptions;
     $dataroot = run_external_command("du -s {$CFG->dataroot}", "Couldn't find dataroot directory");
     $pattern = '/\\d*/';
     preg_match($pattern, $dataroot[0], $matches);
     $filedir = run_external_command("du -s {$CFG->dataroot}/filedir", "Couldn't find filedir directory");
     preg_match($pattern, $filedir[0], $dir_matches);
     $sql_query = "SELECT SUM(filesize) AS total FROM {files}";
     $all_files = $DB->get_record_sql($sql_query);
     $sql_query = "SELECT DISTINCT contenthash, SUM(filesize) AS total FROM {files}";
     if (is_a($DB, 'pgsql_native_moodle_database')) {
         $sql_query .= " GROUP BY contenthash";
         $distinct_contenthash = $DB->get_records_sql($sql_query);
         $total = 0;
         foreach ($distinct_contenthash as $k => $v) {
             $total += $v->total;
         }
         $distinctfilestotal = $total;
     } else {
         $distinct_contenthash = $DB->get_record_sql($sql_query);
         $distinctfilestotal = $distinct_contenthash->total;
     }
     $filesbycourse = array();
     if ($courses = get_all_courses()) {
         foreach ($courses as $course) {
             $subcontexts = get_sub_context_ids($course->ctxpath);
             $filesbycourse[$course->id] = array('unique' => 0, 'all' => 0);
             foreach ($subcontexts as $subcontext) {
                 if ($files = get_files($subcontext->id)) {
                     foreach ($files as $file) {
                         $filesbycourse[$course->id]['unique'] += file_is_unique($file->contenthash, $subcontext->id) ? $file->filesize : 0;
                         $filesbycourse[$course->id]['all'] += $file->filesize;
                     }
                 }
             }
         }
     }
     $sortarray = higher_size($filesbycourse);
     $backups = backup_size();
     $data = array('dataroot' => $matches[0], 'filedir' => $dir_matches[0], 'files total' => $all_files->total, 'distinct files total' => $distinctfilestotal);
     foreach ($sortarray as $courseid => $values) {
         $data["Course {$courseid} files total"] = strval($values['all']);
         $data["Course {$courseid} files unique"] = strval($values['unique']);
     }
     foreach ($backups as $key => $values) {
         $data["Backup {$values->username}"] = strval($values->backupsize);
     }
     if ($options['json']) {
         echo json_encode($data);
     } else {
         foreach ($data as $k => $v) {
             echo "{$k}: " . display_size($v) . "\n";
         }
     }
 }
Ejemplo n.º 6
0
    public function execute() {
        $options = $this->expandedOptions;
        $version = str_replace('.', '', $options['version']);
        list($major, $minor) = explode('.', $options['version']);
        $url = str_replace('<version>', $version, self::downloadUrl);
        $url = str_replace('<major>', $major, $url);
        $url = str_replace('<minor>', $minor, $url);

        run_external_command("wget --continue --timestamping '$url'", "Fetching file failed");
    }
Ejemplo n.º 7
0
function get_commands_list($moodle_ver)
{
    run_external_command("cp ../scripts/config{$moodle_ver}.sh config.sh", "Couldn't copy tests config file");
    $file = file_get_contents("config.sh");
    $moosh = __DIR__ . '/../../moosh.php';
    preg_match("/(?<=MOODLEDIR=)(.*)/", $file, $moodledir);
    exec("cd {$moodledir['0']} && {$moosh}", $output);
    $commands_list = array();
    foreach ($output as $line) {
        if (preg_match("/(?<=\t)(.*)/", $line, $result)) {
            $commands_list[] = $result[0];
        }
    }
    return $commands_list;
}
Ejemplo n.º 8
0
 public function execute()
 {
     global $CFG;
     require_once $CFG->libdir . '/adminlib.php';
     // various admin-only functions
     require_once $CFG->libdir . '/upgradelib.php';
     // general upgrade/install related functions
     require_once $CFG->libdir . '/environmentlib.php';
     require_once $CFG->dirroot . '/course/lib.php';
     $this->init();
     $pluginname = $this->arguments[0];
     $pluginversion = $this->arguments[1];
     $downloadurl = $this->get_plugin_url($pluginname, $pluginversion);
     $split = explode('_', $pluginname, 2);
     $type = $split[0];
     $component = $split[1];
     $tempdir = home_dir() . '/.moosh/moodleplugins/';
     $downloadedfile = $tempdir . $component . ".zip";
     if (!file_exists($tempdir)) {
         mkdir($tempdir);
     }
     if (!fopen($downloadedfile, 'w')) {
         echo "Failed to save plugin - check permissions on {$tempdir}.\n";
         return;
     }
     try {
         file_put_contents($downloadedfile, file_get_contents($downloadurl));
     } catch (Exception $e) {
         die("Failed to download plugin from {$downloadurl}. " . $e . "\n");
     }
     $installpath = $this->get_install_path($type);
     $targetpath = $installpath . DIRECTORY_SEPARATOR . $component;
     if (file_exists($targetpath)) {
         if ($this->expandedOptions['delete']) {
             echo "Removing previously installed {$pluginname} from {$targetpath}\n";
             run_external_command("rm -rf {$targetpath}");
         } else {
             die("Something already exists at {$targetpath} - please remove it and try again, or run with the -d option.\n");
         }
     }
     run_external_command("unzip {$downloadedfile} -d {$installpath}");
     run_external_command("rm {$downloadedfile}");
     echo "Installing\n";
     echo "\tname:    {$pluginname}\n";
     echo "\tversion: {$pluginversion}\n";
     upgrade_noncore(true);
     echo "Done\n";
 }
Ejemplo n.º 9
0
 public function execute()
 {
     global $CFG;
     require_once $CFG->libdir . '/adminlib.php';
     // various admin-only functions
     require_once $CFG->libdir . '/upgradelib.php';
     // general upgrade/install related functions
     require_once $CFG->libdir . '/environmentlib.php';
     require_once $CFG->dirroot . '/course/lib.php';
     $pluginname = $this->arguments[0];
     $moodleversion = $this->arguments[1];
     $pluginversion = $this->arguments[2];
     $pluginsfile = home_dir() . '/.moosh/plugins.json';
     $stat = @stat($pluginsfile);
     if (!$stat || time() - $stat['mtime'] > 60 * 60 * 24 || !$stat['size']) {
         die("plugins.json file not found or too old. Run moosh plugin-list to download newest plugins.json file\n");
     }
     $pluginsdata = file_get_contents($pluginsfile);
     $decodeddata = json_decode($pluginsdata);
     $downloadurl = NULL;
     $downloadurl = $this->get_plugin_url($decodeddata, $pluginname, $moodleversion, $pluginversion);
     if (!$downloadurl) {
         die("Couldn't find {$pluginname} {$moodleversion}\n");
     }
     $split = explode('_', $this->arguments[0], 2);
     $tempdir = home_dir() . '/.moosh/moodleplugins/';
     if (!file_exists($tempdir)) {
         mkdir($tempdir);
     }
     if (!fopen($tempdir . $split[1] . ".zip", 'w')) {
         echo "Failed to save plugin.\n";
         return;
     }
     try {
         file_put_contents($tempdir . $split[1] . ".zip", file_get_contents($downloadurl));
     } catch (Exception $e) {
         echo "Failed to download plugin. " . $e . "\n";
         return;
     }
     run_external_command("unzip -o " . $tempdir . $split[1] . ".zip -d " . home_dir() . "/.moosh/moodleplugins/");
     run_external_command("cp -r " . $tempdir . $split[1] . "/ " . $this->get_install_path($split[0], $moodleversion));
     echo "Installing {$pluginname} {$moodleversion}\n";
     upgrade_noncore(true);
     echo "Done\n";
 }
Ejemplo n.º 10
0
 public function execute()
 {
     //copy newmodule
     $modPath = $this->topDir . '/grade/export/' . $this->arguments[0];
     if (file_exists($modPath)) {
         cli_problem("Already exists: '{$modPath}'");
         cli_problem("Not creating new grade export " . $this->arguments[0]);
         exit(1);
     }
     run_external_command("cp -r '{$this->mooshDir}/vendor/danielneis/moodle-gradeexport_newgradeexport' '{$modPath}'", "Copying from grade export template failed");
     if (file_exists("{$modPath}/.git")) {
         run_external_command("rm --interactive=never -r '{$modPath}/.git'", "Removing .git failed");
     }
     //replace newmodule with $this->arguments[0]
     run_external_command("find '{$modPath}' -type f -exec sed 's/newgradeexport/{$this->arguments[0]}/g' -i {} \\;", "sed command failed");
     //rename lang/en/newmodule.php
     run_external_command("mv '{$modPath}/lang/en/gradeexport_newgradeexport.php' '{$modPath}/lang/en/gradeexport_{$this->arguments[0]}.php'", "Renaming lang file failed");
 }
Ejemplo n.º 11
0
 public function execute()
 {
     $userprofilename = $this->arguments[0];
     if (!preg_match('/[^a-z_0-9]/i', $userprofilename)) {
         cli_error('Agrument is not valid userprofile name');
     }
     $userprofilepath = $this->topDir . '/user/profile/field/' . $userprofilename;
     if (file_exists($userprofilepath)) {
         cli_error("Already exists: '{$userprofilepath}'");
     }
     run_external_command("cp -r '{$this->mooshDir}/vendor/moodlehq/moodle-user_profile_field' '{$userprofilepath}'", "Copying from module template failed");
     if (file_exists("{$userprofilepath}/.git")) {
         run_external_command("rm --interactive=never -r '{$userprofilepath}/.git'", "Removing .git failed");
     }
     // //replace newblock with $this->arguments[0]
     run_external_command("find '{$userprofilepath}' -type f -exec sed 's/myprofilefield/{$this->arguments[0]}/g' -i {} \\;", "sed command failed");
     //rename lang/en/block_newblock.php
     run_external_command("mv '{$userprofilepath}/lang/en/profilefield_myprofilefield.php' '{$userprofilepath}/lang/en/profilefield_{$this->arguments[0]}.php'", "Renaming lang file failed");
 }
Ejemplo n.º 12
0
 public function execute()
 {
     $options = $this->expandedOptions;
     if (!$options['version']) {
         $releasepage = file_get_contents('https://download.moodle.org/releases/latest/');
         preg_match('(https:\\/\\/download.moodle.org\\/download.php\\/stable[0-9].\\/moodle-latest-[0-9].\\.tgz)', $releasepage, $downloadurl);
         $downloadpage = file_get_contents($downloadurl[0]);
         preg_match('(\\/download\\.php\\/direct\\/stable[0-9].\\/moodle-latest-[0-9].\\.tgz)', $downloadpage, $downloadurl);
         $url = 'https://download.moodle.org' . $downloadurl[0];
         run_external_command("wget --continue --timestamping '{$url}'", "Fetching file failed");
         die;
     }
     $version = explode('.', $options['version']);
     $url = str_replace('<version>', $version[0] . $version[1], self::downloadUrl);
     $url = str_replace('<major>', $version[0], $url);
     $url = str_replace('<minor>', $version[1], $url);
     $url = str_replace('<point>', $version[2], $url);
     run_external_command("wget --continue --timestamping '{$url}'", "Fetching file failed");
 }
Ejemplo n.º 13
0
 public function execute()
 {
     //copy newmodule
     $modPath = $this->topDir . '/question/type/' . $this->arguments[0];
     if (file_exists($modPath)) {
         cli_problem("Already exists: '{$modPath}'");
         cli_problem("Not creating new question type " . $this->arguments[0]);
         exit(1);
     }
     run_external_command("cp -r '{$this->mooshDir}/vendor/jamiepratt/moodle-qtype_TEMPLATE' '{$modPath}'", "Copying from qtype template failed");
     if (file_exists("{$modPath}/.git")) {
         run_external_command("rm --interactive=never -r '{$modPath}/.git'", "Removing .git failed");
     }
     //replace newmodule with $this->arguments[0]
     run_external_command("find '{$modPath}' -type f -exec sed 's/YOURQTYPENAME/{$this->arguments[0]}/g' -i {} \\;", "sed command failed");
     //rename lang/en/qtype_YOURQTYPENAME.php
     run_external_command("mv '{$modPath}/lang/en/qtype_YOURQTYPENAME.php' '{$modPath}/lang/en/qtype_{$this->arguments[0]}.php'", "Renaming lang file failed");
     //rename edit_YOURQTYPENAME_form.php
     run_external_command("mv '{$modPath}/edit_YOURQTYPENAME_form.php' '{$modPath}/edit_{$this->arguments[0]}_form.php'", "Renaming lang file failed");
 }