Skip to content

Instantly share code, notes, and snippets.

@dvesh3
Last active October 9, 2023 12:19
Show Gist options
  • Save dvesh3/96e68c1fe0e9d03996e09857105813c3 to your computer and use it in GitHub Desktop.
Save dvesh3/96e68c1fe0e9d03996e09857105813c3 to your computer and use it in GitHub Desktop.
Pimcore 11: Script to migrate data for replacing o_ prefixes ** IMPORTANT: Not tested properly so please backup your data before upgrade
<?php
// file should be located in project root
use Pimcore\Tool\Serialize;
use Pimcore\Tool\Storage;
ini_set('max_execution_time', 0);
ini_set("memory_limit", "-1");
include_once __DIR__ . "/vendor/autoload.php";
$bootstrap = new \Pimcore\Bootstrap();
$bootstrap->startup();
function migrateO_Properties(int $id, string &$data): bool
{
$update = false;
$propertyMappings = [
'o_id' => 'id',
'o_path' => 'path',
'o_creationDate' => 'creationDate',
'o_userOwner' => 'userOwner',
'o_versionCount' => 'versionCount',
'o_locked' => 'locked',
'o_parent' => 'parent',
'o_properties' => 'properties',
'o_userModification' => 'userModification',
'o_modificationDate' => 'modificationDate',
'o_parentId' => 'parentId',
];
foreach ($propertyMappings as $oldProperty => $newProperty) {
if (strpos($data, $oldProperty) !== false) {
$data = str_replace($oldProperty, $newProperty, $data);
$update = true;
}
}
if ($update) {
echo sprintf("Updating: serialized data for recycled item: %s\n", $id);
$data = preg_replace_callback(
'/s:([0-9]+):\"(.*?)\";/',
function ($matches) { return "s:".strlen($matches[2]).':"'.$matches[2].'";'; },
$data
);
return true;
}
return false;
}
$offset = 0;
$limit = 100;
$storage = Storage::get('recycle_bin');
while (true) {
$list = new \Pimcore\Model\Element\Recyclebin\Item\Listing();
$list->setOffset($offset);
$list->setLimit($limit);
$list->setCondition("`type` = 'object'");
$list = $list->load();
if (!$list) {
break;
}
foreach ($list as $item) {
$filePath = $item->getStoreageFile();
$data = $storage->read($item->getStoreageFile());
if (!$data) {
echo sprintf('RecycleBin (ID %S): cannot read recycled data.\n ', $item->getId());
continue;
}
echo sprintf("Migrating: recycled item: %s\n", $item->getId());
if (migrateO_Properties($item->getId(), $data)) {
$storage->write($item->getStoreageFile(), $data);
}
$offset += $limit;
Pimcore::collectGarbage();
}
}
echo "Done Recyclebin migration.";
<?php
// file should be located in project root
use Pimcore\Logger;
use Pimcore\Model\Version\Adapter\VersionStorageAdapterInterface;
use Pimcore\Tool\Serialize;
ini_set('max_execution_time', 0);
ini_set("memory_limit", "-1");
include_once __DIR__ . "/vendor/autoload.php";
$bootstrap = new \Pimcore\Bootstrap();
$bootstrap->startup();
function migrateO_Properties(int $id, string &$data): bool
{
$update = false;
$propertyMappings = [
'o_id' => 'id',
'o_path' => 'path',
'o_creationDate' => 'creationDate',
'o_userOwner' => 'userOwner',
'o_versionCount' => 'versionCount',
'o_locked' => 'locked',
'o_parent' => 'parent',
'o_properties' => 'properties',
'o_userModification' => 'userModification',
'o_modificationDate' => 'modificationDate',
'o_parentId' => 'parentId',
];
foreach ($propertyMappings as $oldProperty => $newProperty) {
if (strpos($data, $oldProperty) !== false) {
$data = str_replace($oldProperty, $newProperty, $data);
$update = true;
}
}
if ($update) {
echo sprintf("Updating: serialized data for Version ID: %s\n", $id);
$data = preg_replace_callback(
'/s:([0-9]+):\"(.*?)\";/',
function ($matches) { return "s:".strlen($matches[2]).':"'.$matches[2].'";'; },
$data
);
return true;
}
return false;
}
$offset = 0;
$limit = 100;
$storageAdapter = \Pimcore::getContainer()->get(VersionStorageAdapterInterface::class);
while (true) {
$list = new \Pimcore\Model\Version\Listing();
$list->setOffset($offset);
$list->setLimit($limit);
$list->setCondition("`ctype` = 'object'");
$list = $list->load();
if (!$list) {
break;
}
foreach ($list as $version) {
$data = null;
if ($version->getSerialized()) {
$data = $storageAdapter->loadMetaData($version);
}
if (!$data) {
echo sprintf("Version (ID % of Object %d): cannot read version data.\n", $version->getId(), $version->getCid());
continue;
}
echo sprintf("Migrating: Object %d, version ID: %s\n", $version->getCid(), $version->getId());
if (migrateO_Properties($version->getId(), $data)) {
$version->setData($data);
$version->save();
}
}
$offset += $limit;
Pimcore::collectGarbage();
}
echo "Done Versions migration.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment