Last active
March 7, 2023 15:55
-
-
Save roelvanduijnhoven/e70aaf1bb81eaa5d40d2cdb3d8420598 to your computer and use it in GitHub Desktop.
Data for pinned migrations
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 | |
// SELECT website.id | |
// FROM website | |
// INNER JOIN page ON page.websiteId = website.id | |
// INNER JOIN pageContent ON page.pageContentId = pageContent.id | |
// INNER JOIN element nonPinnedElement ON nonPinnedElement.parent_id = pageContent.footerElementId AND nonPinnedElement.pluginName != 'pin' | |
// WHERE website.deletedAt IS NULL | |
// AND website.id IN ( | |
// SELECT website.id | |
// FROM website | |
// INNER JOIN page ON page.websiteId = website.id | |
// GROUP BY website.id | |
// HAVING COUNT(page.id) = 1 | |
// ) | |
// GROUP BY website.id | |
$websiteIds = json_decode(file_get_contents('https://gist.github.com/roelvanduijnhoven/3db188ae7c38fd681e07f0b9b130d9bf/raw/0a6484d96506d7b6036162b2c819abd67feacf25/migration-1-one-page.json'), true); | |
$websiteIds = array_map(fn (array $line) => (int)$line['id'], $websiteIds); | |
$elementService = $container->get(\Page\Service\ElementService::class); | |
$pinService = $container->get(\PluginPin\Service\PinService::class); | |
foreach ($websiteIds as $i => $websiteId) { | |
/** @var \Page\Model\Website $website */ | |
$website = $entityManager->find(\Page\Model\Website::class, $websiteId); | |
$homepage = $website->getHomepage(); | |
$footer = $homepage->getContent()->getFooterIgnoreExperiment(); | |
// If there is no pinned region: pin the first element we encounter first. | |
$pinContainer = $pinService->getContainerAtLocation($homepage->getWebsite(), \Page\Model\PageContent::SECTION_FOOTER); | |
$elementsInFooter = array_values($footer->getChildren()->toArray()); | |
$didSomething = false; | |
if (!$pinContainer) { | |
$didSomething = true; | |
$firstElement = $elementsInFooter[0]; | |
$referenceToPin = $pinService->pinElement($firstElement, $homepage); | |
$elementsInFooter[0] = $referenceToPin; | |
$pinContainer = $pinService->getContainerAtLocation($homepage->getWebsite(), \Page\Model\PageContent::SECTION_FOOTER); | |
} else { | |
$referenceToPinExists = false; | |
} | |
// Now go over the remaining non-pinned elements, and pin them either at the top, or bottom of the pinned region. | |
$beforePin = true; | |
$elementsBeforePin = []; | |
$elementsAfterPin = []; | |
foreach ($elementsInFooter as $child) { | |
if ($child->getPluginName() === \PluginPin\Plugin\PinPlugin::PLUGIN_NAME) { | |
$beforePin = false; | |
continue; | |
} | |
if ($beforePin) { | |
$elementsBeforePin[] = $child; | |
} else { | |
$elementsAfterPin[] = $child; | |
} | |
} | |
if ($beforePin === true) { | |
echo 'This page does not contain a reference to a pinned container. Seems like a broken page? Skipping' . PHP_EOL; | |
continue; | |
} | |
try { | |
if (count($elementsBeforePin) > 0) { | |
$didSomething = true; | |
$elementService->moveElementsInto($elementsBeforePin, $pinContainer); | |
} | |
if (count($elementsAfterPin) > 0) { | |
$didSomething = true; | |
$elementService->moveElementsInto($elementsAfterPin, $pinContainer, count($pinContainer->getChildren()) ? $pinContainer->getChildren()->last() : null); | |
} | |
} catch (\Exception $e) { | |
if ($e->getMessage() !== 'ContainerBrick cannot contain ContainerBricks') { | |
throw $e; | |
} else { | |
echo "Ignored ContainerBrick cannot contain ContainerBricks error for " . $homepage->getWebsite()->getId() . PHP_EOL; | |
continue; | |
} | |
} | |
if ($didSomething) { | |
echo 'Pinned elements to homepage for website ' . $homepage->getWebsite()->getId() . PHP_EOL; | |
} else { | |
echo 'Skipped ' . $homepage->getWebsite()->getId() . PHP_EOL; | |
} | |
if ($i % 100 === 0) { | |
$entityManager->clear(); | |
} | |
} |
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 | |
// SELECT page.id | |
// FROM website | |
// INNER JOIN pageTree ON pageTree.websiteId = website.id | |
// INNER JOIN page ON page.treeId = pageTree.id AND pageTree.homepageId != page.id | |
// INNER JOIN pageContent ON page.pageContentId = pageContent.id | |
// INNER JOIN element nonPinnedElement ON nonPinnedElement.parent_id = pageContent.footerElementId AND nonPinnedElement.pluginName != 'pin' | |
// WHERE website.id IN ( | |
// SELECT website.id | |
// FROM website | |
// INNER JOIN page ON page.websiteId = website.id | |
// INNER JOIN pageContent ON page.pageContentId = pageContent.id | |
// INNER JOIN element nonPinnedElement ON nonPinnedElement.parent_id = pageContent.footerElementId AND nonPinnedElement.pluginName != 'pin' | |
// WHERE website.deletedAt IS NULL | |
// GROUP BY website.id | |
// HAVING COUNT(nonPinnedElement.id) <= 1 | |
// ) | |
$pageIds = json_decode(file_get_contents('https://gist.githubusercontent.com/roelvanduijnhoven/3db188ae7c38fd681e07f0b9b130d9bf/raw/d0b46c6258c4b82a7a16f7e652983cacf94e2ac6/page.json'), true); | |
$pageIds = array_map(fn (array $line) => (int)$line['id'], $pageIds); | |
$elementService = $container->get(\Page\Service\ElementService::class); | |
foreach ($pageIds as $i => $pageId) { | |
/** @var \Page\Model\Page $page */ | |
$page = $entityManager->find(\Page\Model\Page::class, $pageId); | |
$footer = $page->getContent()->getFooterIgnoreExperiment(); | |
$contentSection = $page->getContent()->getContent(); | |
$nonPinnedItemsInFooter = array_filter( | |
$footer->getChildren()->toArray(), | |
fn (\Page\Model\Element $element) => $element->getPlugin() !== \PluginPin\Plugin\PinPlugin::PLUGIN_NAME | |
); | |
if (count($nonPinnedItemsInFooter) === 0) { | |
echo 'Skipped, because page ' . $page->getId() . ' empty for website ' . $page->getWebsite()->getId() . PHP_EOL; | |
continue; | |
} | |
$elementService->moveElementsInto( | |
$nonPinnedItemsInFooter, | |
$contentSection, | |
count($contentSection->getChildren()) > 0 ? $contentSection->getChildren()->last() : null | |
); | |
echo 'Moved elements to content section on page ' . $pageId . ' for website ' . $page->getId() . PHP_EOL; | |
if ($i % 10 === 0) { | |
$entityManager->clear(); | |
} | |
} |
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 | |
// SELECT website.id | |
// FROM website | |
// INNER JOIN pageTree ON pageTree.websiteId = website.id | |
// INNER JOIN page ON page.treeId = pageTree.id AND pageTree.homepageId = page.id | |
// INNER JOIN pageContent ON page.pageContentId = pageContent.id | |
// INNER JOIN element nonPinnedElement ON nonPinnedElement.parent_id = pageContent.footerElementId AND nonPinnedElement.pluginName IN ('social-follow', 'social-share', 'spacer', 'separator') | |
// WHERE website.id IN ( | |
// SELECT website.id | |
// FROM website | |
// INNER JOIN page ON page.websiteId = website.id | |
// INNER JOIN pageContent ON page.pageContentId = pageContent.id | |
// INNER JOIN element nonPinnedElement ON nonPinnedElement.parent_id = pageContent.footerElementId AND nonPinnedElement.pluginName != 'pin' | |
// WHERE website.deletedAt IS NULL | |
// GROUP BY website.id | |
// HAVING COUNT(nonPinnedElement.id) <= 1 | |
// ) | |
$websiteIds = json_decode(file_get_contents('https://gist.githubusercontent.com/roelvanduijnhoven/3db188ae7c38fd681e07f0b9b130d9bf/raw/41f5c1d99ecbcfb438f1bcf22a3330d88a0368d9/migration-3-single-elements-on-homepage-of-certain-type.json'), true); | |
$websiteIds = array_map(fn (array $line) => (int)$line['id'], $websiteIds); | |
$elementService = $container->get(\Page\Service\ElementService::class); | |
$pinService = $container->get(\PluginPin\Service\PinService::class); | |
foreach ($websiteIds as $i => $websiteId) { | |
/** @var \Page\Model\Website $website */ | |
$website = $entityManager->find(\Page\Model\Website::class, $websiteId); | |
$homepage = $website->getHomepage(); | |
$footer = $homepage->getContent()->getFooterIgnoreExperiment(); | |
$pinContainer = $pinService->getContainerAtLocation($homepage->getWebsite(), \Page\Model\PageContent::SECTION_FOOTER); | |
$unpinnedElement = null; | |
foreach ($footer->getChildren() as $element) { | |
if ($element->getPluginName() !== \PluginPin\Plugin\PinPlugin::PLUGIN_NAME) { | |
if ($unpinnedElement) { | |
echo 'There exist multiple non-pinned elements. That is unexpected.' . PHP_EOL; | |
continue 2; | |
} | |
$unpinnedElement = $element; | |
} | |
} | |
if ($unpinnedElement === null) { | |
echo 'There is no non-pinned elements. Continue.' . PHP_EOL; | |
continue; | |
} | |
// If there is no pinned item, we simply pin this thing to all pages. | |
if (!$pinContainer) { | |
$referenceToPin = $pinService->pinElement($unpinnedElement, $homepage); | |
echo 'Pinned element that was previously only on website ' . $website->getId() . PHP_EOL; | |
continue; | |
} | |
// Otherwise, we need to move this element into the pinned section | |
$pinAtTop = $footer->getChildren()->first() === $unpinnedElement; | |
if ($pinAtTop || count($pinContainer->getChildren()) === 0) { | |
$elementService->moveElementsInto([$unpinnedElement], $pinContainer); | |
} else { | |
$elementService->moveElementsInto([$unpinnedElement], $pinContainer, $pinContainer->getChildren()->last()); | |
} | |
echo 'Appended element to pinned part on website ' . $website->getId() . PHP_EOL; | |
if ($i % 10 === 0) { | |
$entityManager->clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment