Created
October 12, 2023 12:03
-
-
Save dvesh3/0e585a16dfbf546bc17a9eef1c5640b3 to your computer and use it in GitHub Desktop.
Pimcore 11: Script to sanitize WYSIWYG fields data (DataObjects, Documents & Translations) ** IMPORTANT: Not tested properly so please backup your data before upgrade
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
<?php | |
// file should be located in project root | |
use Pimcore\Tool\Text; | |
ini_set('max_execution_time', 0); | |
ini_set("memory_limit", "-1"); | |
include_once __DIR__ . "/vendor/autoload.php"; | |
$bootstrap = new \Pimcore\Bootstrap(); | |
$bootstrap->startup(); | |
$pimcoreWysiwygSanitizer = \Pimcore::getContainer()->get(Text::PIMCORE_WYSIWYG_SANITIZER_ID); | |
// Sanitize DataObject WYSIWYG fields example | |
// Adapt as per your Data model to sanitize all persisted WYSIWYG data | |
echo "Sanitizing DataObject WYSIWYG fields....\n"; | |
$offset = 0; | |
$limit = 100; | |
while (true) { | |
$list = new \Pimcore\Model\DataObject\Test\Listing(); | |
$list->setOffset($offset); | |
$list->setLimit($limit); | |
$list = $list->load(); | |
if (!$list) { | |
break; | |
} | |
foreach ($list as $object) { | |
$save = false; | |
//direct WYSIWYG field data | |
if ($wysiwygData = $object->getDescription()) { | |
$object->setDescription($wysiwygData); | |
$save = true; | |
} | |
//WYSIWYG field in localized fields | |
foreach (Pimcore\Tool::getValidLanguages() as $language) { | |
if ($localizedWysiwygData = $object->getLocalizedDescription($language)) { | |
$object->setLocalizedDescription($language, $localizedWysiwygData); | |
$save = true; | |
} | |
} | |
//WYSIWYG field in objectbricks field | |
if ($bricksWysiwygData = $object->getBricks()->getTestBrick()?->getBrickDescription()) { | |
$object->getBricks()->getTestBrick()->setBrickDescription($bricksWysiwygData); | |
$save = true; | |
} | |
//WYSIWYG field in fieldcollections field | |
$collectionArr = $object->getCollections() ?? []; | |
/** @var \Pimcore\Model\DataObject\Fieldcollection\Data\TestCollection $collection */ | |
foreach ($collectionArr as $collection) { | |
if ($collectionWysiwygData = $collection->getCollectionDescription()) { | |
$collection->setCollectionDescription($collectionWysiwygData); | |
$save = true; | |
} | |
} | |
if ($save) { | |
echo sprintf("WYSIWYG fields in Object %d\n", $object->getId()); | |
$object->save(['versionNote' => 'Sanitized WYSIWYG Data']); | |
} | |
} | |
$offset += $limit; | |
Pimcore::collectGarbage(); | |
} | |
//Sanitize Document WYSIWYG fields | |
echo "\nSanitizing Document WYSIWYG editables....\n"; | |
$offset = 0; | |
$limit = 100; | |
while (true) { | |
$list = new \Pimcore\Model\Document\Listing(); | |
$list->setOffset($offset); | |
$list->setLimit($limit); | |
$list->setCondition('`id` = 46'); | |
$list = $list->load(); | |
if (!$list) { | |
break; | |
} | |
/** @var Pimcore\Model\Document $document */ | |
foreach ($list as $document) { | |
$save = false; | |
if (!$document instanceof \Pimcore\Model\Document\PageSnippet) { | |
continue; | |
} | |
$editables = $document->getEditables(); | |
foreach ($editables as $editable) { | |
if (!$editable instanceof \Pimcore\Model\Document\Editable\Wysiwyg) { | |
continue; | |
} | |
$editableData = $editable->getData(); | |
$editable->setDataFromEditmode($editableData); | |
$document->setEditable($editable); | |
$save = true; | |
} | |
if ($save) { | |
echo sprintf("WYSIWYG data in Document %d\n", $document->getId()); | |
$document->save(['versionNote' => 'Sanitized WYSIWYG Data']); | |
} | |
} | |
$offset += $limit; | |
Pimcore::collectGarbage(); | |
} | |
//Sanitize Translation WYSIWYG data | |
echo "\n\nSanitizing Translations WYSIWYG data.....\n"; | |
$offset = 0; | |
$limit = 100; | |
while (true) { | |
$list = new \Pimcore\Model\Translation\Listing(); | |
$list->setOffset($offset); | |
$list->setLimit($limit); | |
$list->setCondition("`key` = 'test'"); | |
$list = $list->load(); | |
if (!$list) { | |
break; | |
} | |
foreach ($list as $transItem) { | |
$save = false; | |
foreach ($transItem->getTranslations() as $lang => $translation) { | |
//process only translation containing html tags | |
if ($translation != strip_tags(html_entity_decode($translation))) { | |
$save = true; | |
break; | |
} | |
} | |
if ($save) { | |
echo sprintf("WYSIWYG data in Translation: %d\n", $transItem->getKey()); | |
$transItem->save(); | |
} | |
} | |
$offset += $limit; | |
Pimcore::collectGarbage(); | |
} | |
echo "\n\nDone WYSIWYG sanitization.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment