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

12458 wordpress import authors #641

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
146 changes: 110 additions & 36 deletions Model/Import/AbstractImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ abstract class AbstractImport extends \Magento\Framework\Model\AbstractModel
*/
protected $_storeManager;

/**
* @var \Laminas\Db\Adapter\Adapter
*/
protected $dbAdapter;

/**
* @var \Magefan\BlogAuthor\Model\AuthorFactory
*/
Expand All @@ -113,6 +108,21 @@ abstract class AbstractImport extends \Magento\Framework\Model\AbstractModel
*/
protected $productRepository;

/**
* @var \Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactory
*/
protected $connectionFactory;

/**
* @var \Magento\Framework\Filesystem
*/
protected $fileSystem;

/**
* @var \Magento\Framework\Filesystem\Io\File
*/
protected $file;

/**
* AbstractImport constructor.
* @param \Magento\Framework\Model\Context $context
Expand All @@ -122,6 +132,9 @@ abstract class AbstractImport extends \Magento\Framework\Model\AbstractModel
* @param \Magefan\Blog\Model\TagFactory $tagFactory
* @param \Magefan\Blog\Model\CommentFactory $commentFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactory $connectionFactory
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\Filesystem\Io\File $file
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
Expand All @@ -136,6 +149,7 @@ public function __construct(
\Magefan\Blog\Model\TagFactory $tagFactory,
\Magefan\Blog\Model\CommentFactory $commentFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactory $connectionFactory,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Filesystem\Io\File $file,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
Expand All @@ -149,6 +163,7 @@ public function __construct(
$this->_tagFactory = $tagFactory;
$this->_commentFactory = $commentFactory;
$this->_storeManager = $storeManager;
$this->connectionFactory = $connectionFactory;
$this->fileSystem = $filesystem;
$this->file = $file;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
Expand Down Expand Up @@ -237,47 +252,36 @@ protected function prepareIdentifier($identifier)
*/
public function getPrefix()
{
$adapter = $this->getDbAdapter();
$connection = $this->getDbConnection();

$_pref = '';

if ($this->getData('prefix')) {
$_pref = $adapter->getPlatform()->quoteValue(
$this->getData('prefix')
);
$_pref = $connection->quote($this->getData('prefix'));
$_pref = trim($_pref, "'");
} else {
$_pref = '';
}

return $_pref;
}

/**
* @return \Laminas\Db\Adapter\Adapter
* @return \Magento\Framework\DB\Adapter\AdapterInterface
*/
protected function getDbAdapter()
protected function getDbConnection()
{
if (null === $this->dbAdapter) {
$connectionConf = [
'driver' => 'Pdo_Mysql',
'database' => $this->getData('dbname'),
'username' => $this->getData('uname'),
'password' => $this->getData('pwd'),
'charset' => 'utf8',
];

if ($this->getData('dbhost')) {
$connectionConf['host'] = $this->getData('dbhost');
}

$this->dbAdapter = new \Laminas\Db\Adapter\Adapter($connectionConf);

try {
$this->dbAdapter->query('SELECT 1')->execute();
} catch (\Exception $e) {
throw new \Exception("Failed connect to the database.");
}

$connectionConf = [
'driver' => 'Pdo_Mysql',
'dbname' => $this->getData('dbname'),
'username' => $this->getData('uname'),
'password' => $this->getData('pwd'),
'charset' => 'utf8',
];

if ($this->getData('dbhost')) {
$connectionConf['host'] = $this->getData('dbhost');
}
return $this->dbAdapter;

return $this->connectionFactory->create($connectionConf);
}

protected function getFeaturedImgBySrc($src)
Expand All @@ -287,6 +291,7 @@ protected function getFeaturedImgBySrc($src)
$this->file->mkdir($mediaPath, 0775);

$imageName = explode('?', $src);
$imageName = explode('#', $src);
$imageName = explode('/', $imageName[0]);
$imageName = end($imageName);
$imageName = str_replace(['%20', ' '], '-', $imageName);
Expand All @@ -302,7 +307,13 @@ protected function getFeaturedImgBySrc($src)
if (!$hasFormat) {
$imageName .= '.jpg';
}


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$formatIdentifier = $objectManager->get(\Magefan\Blog\Model\ResourceModel\PageIdentifierGenerator::class);
$imageNameWithoutFormat = explode('.', $imageName);
$preparedImageName = $formatIdentifier->formatIdentifier($imageNameWithoutFormat[0]);
$imageName = $preparedImageName . '.' . $imageNameWithoutFormat[1];

$imagePath = $mediaPath . '/' . $imageName;
$imageSource = false;
if (!$this->file->fileExists($imagePath)) {
Expand All @@ -323,4 +334,67 @@ protected function getFeaturedImgBySrc($src)
return false;
}
}

protected function parseContent($content)
{
$content = str_replace('<!--more-->', '<!-- pagebreak -->', $content);

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$fileSystem = $objectManager->create(\Magento\Framework\Filesystem::class);
$mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath() . '/wysiwyg/blog';
@mkdir($mediaPath, 0777, true);

foreach (['/src="(.*)"/Ui', '/src=\'(.*)\'/Ui'] as $patern) {

$matches = [];
preg_match_all($patern, $content, $matches);


if (!empty($matches[1])) {

foreach ($matches[1] as $src) {
//$src = $matches[1];
$imageName = explode('?', $src);
$imageName = explode('#', $src);
$imageName = explode('/', $imageName[0]);
$imageName = end($imageName);
$imageName = str_replace(['%20', ' '], '-', $imageName);
$imageName = urldecode($imageName);
$imagePath = $mediaPath . '/' . $imageName;
if (!file_exists($imagePath)) {

if ($imageSource = @file_get_contents($src)) {
file_put_contents(
$imagePath,
$imageSource
);
}
} else {
$imageSource = true;
}

if ($imageSource) {
$content = str_replace($src, '{{media url=\'wysiwyg/blog/' . $imageName . '\'}}', $content);
} else {
$content = str_replace($src, 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==', $content);
}
}
}

}

$content = preg_replace(
'/(srcset=".*")/Ui',
's',
$content
);
$content = preg_replace(
'/(srcset=\'.*\')/Ui',
's',
$content
);

return $content;
}
}
82 changes: 52 additions & 30 deletions Model/Import/Aw.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ class Aw extends AbstractImport

public function execute()
{
$adapter = $this->getDbAdapter();
$connection = $this->getDbConnection();
$_pref = $this->getPrefix();

$sql = 'SELECT * FROM '.$_pref.'aw_blog_cat LIMIT 1';
$select = $connection->select()
->from($_pref.'aw_blog_cat')->limit(1);

try {
$adapter->query($sql)->execute();
$connection->fetchAll($select);
} catch (\Exception $e) {
throw new \Exception(__('AheadWorks Blog Extension not detected.'), 1);
}
Expand All @@ -35,23 +37,28 @@ public function execute()
$oldCategories = [];

/* Import categories */
$sql = 'SELECT
t.cat_id as old_id,
t.title as title,
t.identifier as identifier,
t.sort_order as position,
t.meta_keywords as meta_keywords,
t.meta_description as meta_description
FROM '.$_pref.'aw_blog_cat t';

$result = $adapter->query($sql)->execute();

$select = $connection->select()
->from(['t' => $_pref . 'aw_blog_cat'],[
'old_id' => 'cat_id',
'title' => 'title',
'identifier' => 'identifier',
'position' => 'sort_order',
'meta_keywords' => 'meta_keywords',
'meta_description' => 'meta_description'
]);
$result = $connection->fetchAll($select);
foreach ($result as $data) {
/* Prepare category data */

/* Find store ids */
$data['store_ids'] = [];
$s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_cat_store WHERE cat_id = "'.$data['old_id'].'"';
$s_result = $adapter->query($s_sql)->execute();

$s_select = $connection->select()
->from($_pref . 'aw_blog_cat_store', ['store_id'])
->where('cat_id = ?', (int)$data['old_id']);
$s_result = $connection->fetchAll($s_select);

foreach ($s_result as $s_data) {
$data['store_ids'][] = $s_data['store_id'];
}
Expand Down Expand Up @@ -92,12 +99,13 @@ public function execute()
$oldTags = [];
$existingTags = [];

$sql = 'SELECT
t.id as old_id,
t.tag as title
FROM '.$_pref.'aw_blog_tags t';
$select = $connection->select()
->from(['t' => $_pref . 'aw_blog_tags'], [
'old_id' => 'id',
'title' => 'tag'
]);
$result = $connection->fetchAll($select);

$result = $adapter->query($sql)->execute();
foreach ($result as $data) {
/* Prepare tag data */
/*
Expand Down Expand Up @@ -132,15 +140,22 @@ public function execute()
}

/* Import posts */
$sql = 'SELECT * FROM '.$_pref.'aw_blog';
$result = $adapter->query($sql)->execute();

$select = $connection->select()
->from($_pref . 'aw_blog');
$result = $connection->fetchAll($select);

foreach ($result as $data) {
/* Find post categories*/
$postCategories = [];
$c_sql = 'SELECT cat_id as category_id FROM '.
$_pref.'aw_blog_post_cat WHERE post_id = "'.$data['post_id'].'"';
$c_result = $adapter->query($c_sql)->execute();

$c_select = $connection->select()
->from($_pref . 'aw_blog_post_cat', ['category_id' => 'cat_id'])
->where('post_id = ?', (int)$data['post_id']);
;

$c_result = $connection->fetchAll($c_select);

foreach ($c_result as $c_data) {
$oldId = $c_data['category_id'];
if (isset($oldCategories[$oldId])) {
Expand All @@ -151,8 +166,13 @@ public function execute()

/* Find store ids */
$data['store_ids'] = [];
$s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_store WHERE post_id = "'.$data['post_id'].'"';
$s_result = $adapter->query($s_sql)->execute();

$s_select = $connection->select()
->from($_pref . 'aw_blog_store', ['store_id'])
->where('post_id = ?', (int)$data['post_id']);

$s_result = $connection->fetchAll($s_select);

foreach ($s_result as $s_data) {
$data['store_ids'][] = $s_data['store_id'];
}
Expand Down Expand Up @@ -193,8 +213,11 @@ public function execute()
$post->setData($data)->save();

/* find post comment s*/
$sql = 'SELECT * FROM '.$_pref.'aw_blog_comment WHERE `post_id` = ' . $post->getOldId();
$resultComments = $adapter->query($sql)->execute();
$select = $connection->select();
$select->from($_pref . 'aw_blog_comment')
->where('post_id = ?', $post->getOldId());

$resultComments = $connection->fetchAll($select);
foreach ($resultComments as $comments) {
$commentParentId = 0;

Expand Down Expand Up @@ -244,6 +267,5 @@ public function execute()
unset($post);
}
/* end */
$adapter->getDriver()->getConnection()->disconnect();
}
}
Loading