Search & Replace against collections

We’ve been using cockpit in a test environment with some WYSIWIG fields. These fields contain absolute URLs which will need to be mass-replaced when we migrate. (e.g. “https://test.domain.com/this_thing” will need to be replaced with “https://www.domain.com/this_thing”) inside some Collections and Singletons.

What’s the best way to iterate through Collection entries and Singletons to do a search/replace?

simple approach: create a migrate script e.g. migrate.php.

<?php

include('{cockpit-folder}/bootstrap.php');

$collection = '{collectionname}';
$records = cockpit('collections')->find($collection); // get all entries

foreach ($records as $record) {
    $record['wysiwyg'] = str_replace('xyz', 'zyx', $record['wysiwyg']);

    cockpit('collections')->save($collection, $record);
}
1 Like