diff --git a/Moosh/Command/Moodle39/Dev/StringReplaceEncoded.php b/Moosh/Command/Moodle39/Dev/StringReplaceEncoded.php new file mode 100644 index 00000000..38a1f5a5 --- /dev/null +++ b/Moosh/Command/Moodle39/Dev/StringReplaceEncoded.php @@ -0,0 +1,72 @@ +addArgument('from'); + $this->addArgument('to'); + $this->addArgument('table'); + $this->addArgument('column'); + + $this->addOption('d|dry-run', "don't perform the UPDATE", false); + + } + + + public function execute() + { + //some variables you may want to use + //$this->cwd - the directory where moosh command was executed + //$this->mooshDir - moosh installation directory + //$this->expandedOptions - commandline provided options, merged with defaults + //$this->topDir - top Moodle directory + + global $CFG, $DB, $USER; + + $from = $this->arguments[0]; + $to = $this->arguments[1]; + $table = $this->arguments[2]; + $column = $this->arguments[3]; + $dryrun = $this->expandedOptions['dry-run']; + + $possiblerecords = $DB->get_records_sql("SELECT id, $column FROM {{$table}} WHERE $column != ''"); + foreach ($possiblerecords as $possiblerecord) { + $decoded = base64_decode($possiblerecord->$column); + if (strpos($decoded, $from) === false) { + continue; + } + $unserialized = unserialize_object($decoded); + + // Iterate over all public properties of the object + foreach ($unserialized as $key => $value) { + if (strpos($value, $from) !== false) { + $unserialized->$key = str_replace($from, $to, $value); + if ($this->verbose) { + echo "Replacing in '$key' from\n$value\nto\n{$unserialized->$key}\n"; + } + } + } + $encoded = base64_encode(serialize($unserialized)); + if (!$dryrun) { + $DB->set_field($table, $column, $encoded, ['id' => $possiblerecord->id]); + } + } + + } +} + + diff --git a/moosh.php b/moosh.php index 4a13b655..65919103 100755 --- a/moosh.php +++ b/moosh.php @@ -68,9 +68,6 @@ function cli_error($text, $errorcode = 1) { } $moodle_version = moosh_moodle_version($top_dir); -if (isset($app_options['verbose'])) { - echo "Moodle version detected: $moodle_version\n"; -} $local_dir = home_dir() . DIRECTORY_SEPARATOR . '.moosh'; $viable_versions = moosh_generate_version_list($moodle_version); @@ -342,6 +339,8 @@ function string_exists() { // Some more debug if requested. if ($app_options->has('verbose')) { + echo "Moodle version detected: $moodle_version\n"; + $subcommand->status(); }