Skip to content

Instantly share code, notes, and snippets.

@biplobice
Last active June 29, 2017 08:49
Show Gist options
  • Save biplobice/6f84435afc0f7ab4335d5ed1cb80c92b to your computer and use it in GitHub Desktop.
Save biplobice/6f84435afc0f7ab4335d5ed1cb80c92b to your computer and use it in GitHub Desktop.
PHP script to import proxy blocks from legacy version and remove proxy blocks from new version 8.X
<?php
Route::register('/import', function () {
/*=============== Just for checking ============*/
// Get block type
$bt = BlockType::getByHandle(BLOCK_HANDLE_SCRAPBOOK_PROXY);
echo 'Active proxy blocks: ' . $bt->getCount(true) . '<br>';
$list = new PageList();
$list->filterByBlockType($bt);
Log::info('getQuery');
echo 'Proxy blocks used on ' . count($list->getResults()) . ' pages.<br>';
/*==============================================*/
/*============== Import XML data ===============*/
$f = File::getByID('11038');
if (!is_object($f)) {
$this->error->add(t('File not found.'));
} else {
$fileURl = $f->getURL();
}
$url_headers = @get_headers($fileURl);
if($url_headers[0] == 'HTTP/1.1 200 OK') {
$simplexml = simplexml_load_file($fileURl);
} else {
exit("failed to load XML");
}
if (!$simplexml->pages->page) {
return 'Invalid XML file';
}
$paths = [];
foreach ($simplexml->pages->page as $node) {
$sanitizer = new PortlandLabs\Concrete5\MigrationTool\Importer\Sanitizer\PagePathSanitizer();
$paths[] = $originalPath = $sanitizer->sanitize((string) $node['path']);
$page = Page::getByPath($originalPath);
\Log::info(var_export($originalPath, true));
$bt = BlockType::getByHandle((string)$node->area->block['type']);
$a = (string)$node->area['name'];
if (!is_object($page) || !is_object($bt)) {
continue;
}
$controller = $bt->getController();
$controller->import($page, $a, $node->area->block);
}
// Display status
$paths = array_unique($paths);
foreach ($paths as $path) {
echo '<a href="' . URL::to($path) . '" target="_blank">' . $path . '</a><br>';
}
echo '<br>' . count($simplexml->pages->page) .' blocks updated on ' . count($paths) . ' pages. <br>';
/*==============================================*/
/*============ Delete proxy blocks =============*/
// Delete proxy blocks
$db = Database::connection();
$q = "SELECT b.bID, btCSD.bOriginalID FROM Blocks b
LEFT JOIN btCoreScrapbookDisplay btCSD ON b.bID = btCSD.bID
WHERE btID=?
AND EXISTS (
SELECT 1 FROM CollectionVersionBlocks cvb
INNER JOIN CollectionVersions cv ON cv.cID=cvb.cID AND cv.cvID=cvb.cvID
WHERE b.bID=cvb.bID AND cv.cvIsApproved=1
)
AND(btCSD.bOriginalID NOT IN (SELECT bID FROM Blocks))";
$v = array(BlockType::getByHandle(BLOCK_HANDLE_SCRAPBOOK_PROXY)->getBlockTypeID());
$r = $db->fetchAll($q, $v);
foreach ($r as $item) {
$b = Block::getByID($item['bID']);
$b->deleteBlock(true);
}
echo count($r) . 'proxy blocks deleted. <br>';
/*==============================================*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment