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

12016-All-import-scripts-improvements #619

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
103 changes: 25 additions & 78 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,11 @@ abstract class AbstractImport extends \Magento\Framework\Model\AbstractModel
*/
protected $productRepository;

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

/**
* AbstractImport constructor.
* @param \Magento\Framework\Model\Context $context
Expand All @@ -136,6 +136,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 +150,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,90 +239,35 @@ 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()
{
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.");
}

}
return $this->dbAdapter;
}

protected function getFeaturedImgBySrc($src)
protected function getDbConnection()
{
$mediaPath = $this->fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath() . '/magefan_blog';

$this->file->mkdir($mediaPath, 0775);

$imageName = explode('?', $src);
$imageName = explode('/', $imageName[0]);
$imageName = end($imageName);
$imageName = str_replace(['%20', ' '], '-', $imageName);
$imageName = urldecode($imageName);

$hasFormat = false;
foreach (['jpg','jpeg', 'png', 'gif', 'webp'] as $format) {
if (false !== stripos($imageName, $format)) {
$hasFormat = true;
break;
}
}
if (!$hasFormat) {
$imageName .= '.jpg';
}

$imagePath = $mediaPath . '/' . $imageName;
$imageSource = false;
if (!$this->file->fileExists($imagePath)) {
try {
$imageData = $this->file->read($src);
$this->file->write($imagePath, $imageData);
$imageSource = true;
} catch (\Exception $e) {
$imageSource = false;
}
} else {
$imageSource = true;
$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');
}

if ($imageSource) {
return 'magefan_blog/' . $imageName;
} else {
return false;
}
return $this->connectionFactory->create($connectionConf);
}
}
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