Skip to content

Commit 630d8ea

Browse files
author
Gregor Pollak
committed
Initial release of 1.0.1 on public repository
1 parent bdd0af1 commit 630d8ea

File tree

10 files changed

+669
-1
lines changed

10 files changed

+669
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
.idea/

Console/Command/DumpCmsData.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/*
3+
* RocketWeb
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Open Software License (OSL 3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://opensource.org/licenses/osl-3.0.php
11+
*
12+
* @category RocketWeb
13+
* @copyright Copyright (c) 2020 RocketWeb (http://rocketweb.com)
14+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
15+
* @author Rocket Web Inc.
16+
*/
17+
namespace RocketWeb\CmsImportExport\Console\Command;
18+
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Input\InputOption;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
23+
class DumpCmsData extends \Symfony\Component\Console\Command\Command
24+
{
25+
private const INPUT_KEY_TYPE = 'type';
26+
private const INPUT_TYPE_VALUES = ['block', 'page', 'all'];
27+
private const INPUT_KEY_IDENTIFIER = 'identifier';
28+
private \RocketWeb\CmsImportExport\Model\Service\DumpCmsDataService $dumpCmsDataService;
29+
30+
public function __construct(
31+
\RocketWeb\CmsImportExport\Model\Service\DumpCmsDataService $dumpCmsDataService,
32+
string $name = null
33+
) {
34+
parent::__construct($name);
35+
$this->dumpCmsDataService = $dumpCmsDataService;
36+
}
37+
38+
protected function configure()
39+
{
40+
$this->setName('cms:dump:data');
41+
$this->setDescription('Dumps cms pages/blocks to var/sync_cms_data for further import');
42+
$this->setDefinition([
43+
new InputOption(
44+
self::INPUT_KEY_TYPE,
45+
't',
46+
InputOption::VALUE_REQUIRED,
47+
'Which type are we dumping - block/page/all'
48+
),
49+
new InputOption(
50+
self::INPUT_KEY_IDENTIFIER,
51+
'i',
52+
InputOption::VALUE_OPTIONAL,
53+
'identifier to process (one or CSV list)'
54+
)
55+
]);
56+
parent::configure();
57+
}
58+
59+
protected function execute(InputInterface $input, OutputInterface $output): void
60+
{
61+
$type = $input->getOption(self::INPUT_KEY_TYPE);
62+
if ($type === null) {
63+
throw new \RuntimeException("Type ([-t|--type) is required");
64+
}
65+
if (!in_array($type, self::INPUT_TYPE_VALUES)) {
66+
throw new \RuntimeException('Unsupported type for export');
67+
}
68+
$types = [];
69+
if ($type == 'all' || $type == 'block') {
70+
$types[] = 'block';
71+
}
72+
if ($type == 'all' || $type == 'page') {
73+
$types[] = 'page';
74+
}
75+
76+
$identifiers = $input->getOption(self::INPUT_KEY_IDENTIFIER);
77+
if ($identifiers !== null) {
78+
$identifiers = explode(',', $identifiers);
79+
}
80+
81+
$this->dumpCmsDataService->execute($types, $identifiers);
82+
}
83+
}

Console/Command/ImportCmsData.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/*
3+
* RocketWeb
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Open Software License (OSL 3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://opensource.org/licenses/osl-3.0.php
11+
*
12+
* @category RocketWeb
13+
* @copyright Copyright (c) 2020 RocketWeb (http://rocketweb.com)
14+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
15+
* @author Rocket Web Inc.
16+
*/
17+
18+
19+
namespace RocketWeb\CmsImportExport\Console\Command;
20+
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Input\InputOption;
23+
use Symfony\Component\Console\Output\OutputInterface;
24+
25+
class ImportCmsData extends \Symfony\Component\Console\Command\Command
26+
{
27+
private const INPUT_KEY_TYPE = 'type';
28+
private const INPUT_TYPE_VALUES = ['block', 'page', 'all'];
29+
private const INPUT_KEY_IDENTIFIER = 'identifier';
30+
private \RocketWeb\CmsImportExport\Model\Service\ImportCmsDataService $importCmsDataService;
31+
32+
public function __construct(
33+
\RocketWeb\CmsImportExport\Model\Service\ImportCmsDataService $importCmsDataService,
34+
string $name = null
35+
) {
36+
parent::__construct($name);
37+
$this->importCmsDataService = $importCmsDataService;
38+
}
39+
40+
protected function configure()
41+
{
42+
$this->setName('cms:import:data');
43+
$this->setDescription('Import cms pages/blocks from var/sync_cms_data');
44+
$this->setDefinition([
45+
new InputOption(
46+
self::INPUT_KEY_TYPE,
47+
't',
48+
InputOption::VALUE_REQUIRED,
49+
'Which type are we importing - block/page/all'
50+
),
51+
new InputOption(
52+
self::INPUT_KEY_IDENTIFIER,
53+
'i',
54+
InputOption::VALUE_OPTIONAL,
55+
'identifier to process (one or CSV list)'
56+
)
57+
]);
58+
parent::configure();
59+
}
60+
61+
protected function execute(InputInterface $input, OutputInterface $output): void
62+
{
63+
$type = $input->getOption(self::INPUT_KEY_TYPE);
64+
if ($type === null) {
65+
throw new \RuntimeException("Type ([-t|--type) is required");
66+
}
67+
if (!in_array($type, self::INPUT_TYPE_VALUES)) {
68+
throw new \RuntimeException('Unsupported type for import');
69+
}
70+
$types = [];
71+
if ($type == 'all' || $type == 'block') {
72+
$types[] = 'block';
73+
}
74+
if ($type == 'all' || $type == 'page') {
75+
$types[] = 'page';
76+
}
77+
78+
$identifiers = $input->getOption(self::INPUT_KEY_IDENTIFIER);
79+
if ($identifiers !== null) {
80+
$identifiers = explode(',', $identifiers);
81+
}
82+
83+
$this->importCmsDataService->execute($types, $identifiers);
84+
}
85+
}

Model/Service/DumpCmsDataService.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* RocketWeb
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Open Software License (OSL 3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://opensource.org/licenses/osl-3.0.php
11+
*
12+
* @category RocketWeb
13+
* @copyright Copyright (c) 2020 RocketWeb (http://rocketweb.com)
14+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
15+
* @author Rocket Web Inc.
16+
*/
17+
18+
namespace RocketWeb\CmsImportExport\Model\Service;
19+
20+
use Magento\Framework\App\Filesystem\DirectoryList;
21+
use Magento\Framework\Filesystem\Directory\WriteInterface;
22+
23+
class DumpCmsDataService
24+
{
25+
private \Magento\Cms\Api\PageRepositoryInterface $pageRepository;
26+
private \Magento\Cms\Api\BlockRepositoryInterface $blockRepository;
27+
private \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder;
28+
private \Magento\Framework\Filesystem\DirectoryList $directoryList;
29+
private \Magento\Framework\Filesystem $filesystem;
30+
private \Magento\Framework\Serialize\SerializerInterface $serializer;
31+
private \Magento\Catalog\Model\CategoryList $categoryList;
32+
private array $blockIdentifiers = [];
33+
34+
public function __construct(
35+
\Magento\Cms\Api\PageRepositoryInterface $pageRepository,
36+
\Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
37+
\Magento\Catalog\Model\CategoryList $categoryList,
38+
\Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
39+
\Magento\Framework\Filesystem\DirectoryList $directoryList,
40+
\Magento\Framework\Filesystem $filesystem,
41+
\Magento\Framework\Serialize\SerializerInterface $serializer
42+
) {
43+
$this->pageRepository = $pageRepository;
44+
$this->blockRepository = $blockRepository;
45+
$this->criteriaBuilder = $criteriaBuilder;
46+
$this->directoryList = $directoryList;
47+
$this->filesystem = $filesystem;
48+
$this->serializer = $serializer;
49+
$this->categoryList = $categoryList;
50+
}
51+
52+
public function execute(array $types, ?array $identifiers)
53+
{
54+
$varDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
55+
$varPath = $this->directoryList->getPath(DirectoryList::VAR_DIR);
56+
$workingDirPath = $varPath . '/sync_cms_data';
57+
if ($varDirectory->isExist($workingDirPath)) {
58+
$varDirectory->delete($workingDirPath);
59+
}
60+
61+
foreach ($types as $type) {
62+
if ($type == 'block') {
63+
$this->dumpBlocks($workingDirPath . '/cms/blocks/', $varDirectory, $identifiers);
64+
} else if ($type == 'page') {
65+
$this->dumpPages($workingDirPath . '/cms/pages/', $varDirectory, $identifiers);
66+
}
67+
}
68+
}
69+
70+
private function write(WriteInterface $writeDirectory, string $filePath, string $content): void
71+
{
72+
$stream = $writeDirectory->openFile($filePath, 'w+');
73+
$stream->lock();
74+
$stream->write($content);
75+
$stream->unlock();
76+
$stream->close();
77+
}
78+
79+
private function dumpPages(string $path, WriteInterface $varDirectory, ?array $identifiers): void
80+
{
81+
$searchCriteria = $this->criteriaBuilder;
82+
if ($identifiers) {
83+
$searchCriteria->addFilter('identifier', $identifiers, 'in');
84+
}
85+
86+
$pagesList = $this->pageRepository->getList($searchCriteria->create());
87+
$pages = $pagesList->getItems();
88+
89+
foreach ($pages as $page) {
90+
$identifier = str_replace('/', '|', trim($page->getIdentifier()));
91+
if (strpos($identifier, '.html') !== false) {
92+
$identifier = str_replace('.html', '_html', $identifier);
93+
}
94+
$htmlPath = $path . $identifier . '.html';
95+
$this->write($varDirectory, $htmlPath, $page->getContent());
96+
$jsonPath = $path . $identifier . '.json';
97+
$jsonContent = [
98+
'title' => $page->getTitle(),
99+
'is_active' => $page->isActive(),
100+
'page_layout' => $page->getPageLayout(),
101+
'identifier' => $page->getIdentifier(),
102+
'content_heading' => $page->getContentHeading(),
103+
104+
];
105+
$this->write($varDirectory, $jsonPath, $this->serializer->serialize($jsonContent));
106+
}
107+
}
108+
109+
private function dumpBlocks(string $path, WriteInterface $varDirectory, ?array $identifiers): void
110+
{
111+
$searchCriteria = $this->criteriaBuilder;
112+
if ($identifiers) {
113+
$searchCriteria->addFilter('identifier', $identifiers, 'in');
114+
}
115+
116+
$blocksList = $this->blockRepository->getList($searchCriteria->create());
117+
$blocks = $blocksList->getItems();
118+
119+
foreach ($blocks as $block) {
120+
if (strpos($block->getIdentifier(), 'series_build_cms_') !== false
121+
|| strpos($block->getIdentifier(), '-block-') !== false
122+
) {
123+
// Skipping all generated CMS blocks from old system
124+
continue;
125+
}
126+
$this->blockIdentifiers[$block->getId()] = $block->getIdentifier();
127+
$htmlPath = $path . trim($block->getIdentifier()) . '.html';
128+
$this->write($varDirectory, $htmlPath, $block->getContent());
129+
$jsonPath = $path . trim($block->getIdentifier()) . '.json';
130+
$jsonContent = [
131+
'title' => $block->getTitle(),
132+
'identifier' => $block->getIdentifier(),
133+
'stores' => [1],
134+
'is_active' => $block->isActive()
135+
];
136+
$this->write($varDirectory, $jsonPath, $this->serializer->serialize($jsonContent));
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)