Skip to content

Commit

Permalink
New command moosh string-replace-encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomek Muras committed Apr 26, 2024
1 parent 84ac425 commit 85782a0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
72 changes: 72 additions & 0 deletions Moosh/Command/Moodle39/Dev/StringReplaceEncoded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* moosh - Moodle Shell
*
* @copyright 2012 onwards Tomasz Muras
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace Moosh\Command\Moodle39\Dev;

use Moosh\MooshCommand;

class StringReplaceEncoded extends MooshCommand
{
public function __construct()
{
// moosh string-replace-encoded from to table column
parent::__construct('replace-encoded', 'string');

$this->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]);
}
}

}
}


5 changes: 2 additions & 3 deletions moosh.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 85782a0

Please sign in to comment.