Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WP media files #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,39 @@
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\PageObjectCollection;
use PortlandLabs\Concrete5\MigrationTool\Importer\Sanitizer\PagePathSanitizer;
use PortlandLabs\Concrete5\MigrationTool\Importer\Wordpress\ElementParserInterface;
use \Concrete\Core\File\Importer;
use \Concrete\Core\File\Service\File as FileHelper;
use \Concrete\Core\Tree\Node\Type\FileFolder as FileFolder;

defined('C5_EXECUTE') or die("Access Denied.");

class Page implements ElementParserInterface
{
private $simplexml;
private $namespaces;
private $folderID;
private $files;

public function __construct()
{
$this->blockImporter = \Core::make('migration/manager/import/wordpress_block');
}

public function getObjectCollection(\SimpleXMLElement $element, array $namespaces)
{
public function getObjectCollection(\SimpleXMLElement $element, array $namespaces, $batch)
{
$this->simplexml = $element;
$this->namespaces = $namespaces;
$this->folderID = $batch->getFileFolderID();

$collection = new PageObjectCollection();
$pages = $this->createParentPages();

$files = array();
foreach ($this->getFileNodes() as $node) {
$files[] = $this->parseFile($node);
}
$this->files = $files;

foreach ($this->getPageNodes() as $node) {
$pages[] = $this->parsePage($node);
}
Expand All @@ -47,6 +60,18 @@ public function getObjectCollection(\SimpleXMLElement $element, array $namespace
return $collection;
}

public function getFileNodes()
{
$files = array();
foreach ($this->simplexml->channel->item as $item) {
$nodeType = $this->getPageType($item);
if ($nodeType == 'attachment') {
$files[] = $item;
}
}
return $files;
}

public function getPageNodes()
{
$pages = array();
Expand All @@ -56,7 +81,6 @@ public function getPageNodes()
$pages[] = $item;
}
}

return $pages;
}

Expand All @@ -65,7 +89,7 @@ private function getItemType(\SimpleXMLElement $node)
$wp = $node->children($this->namespaces['wp']);

return (string) $wp->post_type;
}
}

private function getPageType($node)
{
Expand All @@ -78,34 +102,56 @@ private function getPageType($node)
case 'page':
$pageType = 'page';
break;
case 'attachment':
$pageType = 'attachment';
break;
}

return isset($pageType) ? $pageType : $itemType;
}

protected function parseFile($node)
{
$fileURL = (string)$node->guid;
$fileName = basename($fileURL);
$fileContent = FileHelper::getContents($fileURL);
if ($fileContent !== false) {
if(FileHelper::append(FileHelper::getTemporaryDirectory() . '/' . $fileName, $fileContent)) {
$importer = new Importer();
$result = $importer->import(FileHelper::getTemporaryDirectory() . '/' . $fileName, $fileName);
if($this->folderID != null) {
$folder = FileFolder::getByID($this->folderID);
$result->getFile()->getFileNodeObject()->move($folder);
}
FileHelper::clear(FileHelper::getTemporaryDirectory() . '/' . $fileName);
return array("old"=>$fileURL,"new"=>$result->getDownloadUrl());
}
}
}

protected function parsePage($node)
{
$pageType = $this->getPageType($node);

$page = new \PortlandLabs\Concrete5\MigrationTool\Entity\Import\Page();
$page->setName((string) html_entity_decode($node->title));
$page->setPublicDate((string) $node->xpath('wp:post_date_gmt')[0]);
$page->setDescription((string) html_entity_decode($node->description));
$page->setType($pageType);
$page->setTemplate('blank');
$page = new \PortlandLabs\Concrete5\MigrationTool\Entity\Import\Page();
$page->setName((string) html_entity_decode($node->title));
$page->setPublicDate((string) $node->xpath('wp:post_date_gmt')[0]);
$page->setDescription((string) html_entity_decode($node->description));
$page->setType($pageType);
$page->setTemplate('blank');

$page->setOriginalPath($this->createOriginalPath($node));
$page->setBatchPath($this->createBatchPath($page->getOriginalPath(), $pageType));
$page->setOriginalPath($this->createOriginalPath($node));
$page->setBatchPath($this->createBatchPath($page->getOriginalPath(), $pageType));

// $page->setUser($this->getUser($node));
// TODO remove temporary user assignment
$page->setUser('admin');
// TODO remove temporary user assignment
$page->setUser('admin');

$area = $this->parseArea($node);
$area->setPage($page);
$page->areas->add($area);
$area = $this->parseArea($node);
$area->setPage($page);
$page->areas->add($area);

return $page;
return $page;
}

private function createOriginalPath($node)
Expand Down Expand Up @@ -192,6 +238,9 @@ private function parseBlocks($node)
$block = new Block();
$block->setType('Content');
$block->setName('Content');
foreach($this->files as $file) {
$node->children('content', true)->encoded = str_replace($file["old"],$file["new"],$node->children('content', true)->encoded);
}
$value = $this->blockImporter->driver('unmapped')->parse($node);
$block->setBlockValue($value);
$block->setPosition(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

interface ElementParserInterface
{
public function getObjectCollection(\SimpleXMLElement $xml, array $namespaces);
public function getObjectCollection(\SimpleXMLElement $xml, array $namespaces, $batch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function addContentObjectCollectionsToBatch($file = null, Batch $batch)
// $simplexml = simplexml_load_file($file);

foreach ($manager->getDrivers() as $driver) {
$collection = $driver->getObjectCollection($this->wxr, $this->namespaces);
$collection = $driver->getObjectCollection($this->wxr, $this->namespaces, $batch);
if ($collection) {
if (!($collection instanceof ObjectCollection)) {
throw new \RuntimeException(t('Driver %s getObjectCollection did not return an object of the ObjectCollection type', get_class($driver)));
Expand Down