-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New command moosh string-replace-encoded
- Loading branch information
Tomek Muras
committed
Apr 26, 2024
1 parent
84ac425
commit 85782a0
Showing
2 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters