From b25b59eec9c925bf8fb5b1562bdf572794ab4bd1 Mon Sep 17 00:00:00 2001 From: "ivan.hrytsai" Date: Mon, 14 Oct 2024 17:12:30 +0300 Subject: [PATCH 01/11] All-import-scripts-improvements --- Model/Import/Aw.php | 92 +++++++++++----- Model/Import/Aw2.php | 90 +++++++++++----- Model/Import/Mageplaza.php | 100 +++++++++-------- Model/Import/Mageplaza1.php | 170 ++++++++++++++++++++--------- Model/Import/Mirasvit.php | 155 +++++++++++++++++--------- Model/Import/Wordpress.php | 210 +++++++++++++++++++++--------------- 6 files changed, 535 insertions(+), 282 deletions(-) diff --git a/Model/Import/Aw.php b/Model/Import/Aw.php index ae9aafe3..b2d88784 100644 --- a/Model/Import/Aw.php +++ b/Model/Import/Aw.php @@ -22,9 +22,12 @@ public function execute() $adapter = $this->getDbAdapter(); $_pref = $this->getPrefix(); - $sql = 'SELECT * FROM '.$_pref.'aw_blog_cat LIMIT 1'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref.'aw_blog_cat')->limit(1); + try { - $adapter->query($sql)->execute(); + $sql->prepareStatementForSqlObject($select)->execute(); } catch (\Exception $e) { throw new \Exception(__('AheadWorks Blog Extension not detected.'), 1); } @@ -35,23 +38,32 @@ 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(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'aw_blog_cat']) + ->columns([ + 'old_id' => 'cat_id', + 'title' => 'title', + 'identifier' => 'identifier', + 'position' => 'sort_order', + 'meta_keywords' => 'meta_keywords', + 'meta_description' => 'meta_description' + ]); + $result = $sql->prepareStatementForSqlObject($select)->execute(); 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_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $s_sql->select(); + $select->from($_pref . 'aw_blog_cat_store') + ->columns(['store_id']) + ->where(['cat_id = ?' => (int)$data['old_id']]); + $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; } @@ -92,12 +104,15 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = 'SELECT - t.id as old_id, - t.tag as title - FROM '.$_pref.'aw_blog_tags t'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'aw_blog_tags']) + ->columns([ + 'old_id' => 'id', + 'title' => 'tag' + ]); + $result = $sql->prepareStatementForSqlObject($select)->execute(); - $result = $adapter->query($sql)->execute(); foreach ($result as $data) { /* Prepare tag data */ /* @@ -132,15 +147,25 @@ public function execute() } /* Import posts */ - $sql = 'SELECT * FROM '.$_pref.'aw_blog'; - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'aw_blog'); + $result = $sql->prepareStatementForSqlObject($select)->execute(); 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_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $c_sql->select(); + $select->from($_pref . 'aw_blog_post_cat') + ->columns(['category_id' => 'cat_id']) + ->where(['post_id = ?' => (int)$data['post_id']]); + ; + + $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; if (isset($oldCategories[$oldId])) { @@ -151,8 +176,15 @@ 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_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $s_sql->select(); + $select->from($_pref . 'aw_blog_store') + ->columns(['store_id']) + ->where(['post_id = ?' => (int)$data['post_id']]); + + $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; } @@ -193,8 +225,12 @@ 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(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'aw_blog_comment') + ->where(['post_id = ?' => $post->getOldId()]); + + $resultComments = $sql->prepareStatementForSqlObject($select)->execute(); foreach ($resultComments as $comments) { $commentParentId = 0; diff --git a/Model/Import/Aw2.php b/Model/Import/Aw2.php index 0b34e6fa..d7e2dabb 100644 --- a/Model/Import/Aw2.php +++ b/Model/Import/Aw2.php @@ -23,9 +23,12 @@ public function execute() $adapter = $this->getDbAdapter(); $_pref = $this->getPrefix(); - $sql = 'SELECT * FROM '.$_pref.'aw_blog_category LIMIT 1'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'aw_blog_category')->limit(1); + try { - $adapter->query($sql)->execute(); + $sql->prepareStatementForSqlObject($select)->execute(); } catch (\Exception $e) { throw new \Exception(__('AheadWorks Blog Extension not detected.'), 1); } @@ -35,27 +38,35 @@ public function execute() $oldCategories = []; /* Import categories */ - $sql = 'SELECT - t.id as old_id, - t.name as title, - t.url_key as identifier, - t.sort_order as position, - t.meta_description as meta_description - FROM '.$_pref.'aw_blog_category t'; - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'aw_blog_category']) + ->columns([ + 'old_id' => 'id', + 'title' => 'name', + 'identifier' => 'url_key', + 'position' => 'sort_order', + 'meta_description' => 'meta_description' + ]); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); foreach ($result as $data) { /* Prepare category data */ /* Find store ids */ $data['store_ids'] = []; - $s_sql = 'SELECT - `store_id` - FROM - '.$_pref.'`aw_blog_category_store` - WHERE - `category_id` = '. ((int)$data['old_id']) . ";"; - $s_result = $adapter->query($s_sql)->execute(); + + $s_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $s_sql->select(); + $select->from($_pref . 'aw_blog_category_store') + ->columns([ + 'store_id' + ])->where(['category_id' => ((int)$data['old_id'])]); + + $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; } @@ -96,12 +107,16 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = 'SELECT - t.id as old_id, - t.name as title - FROM '.$_pref.'aw_blog_tag t'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'aw_blog_tag']) + ->columns([ + 'old_id' => 'id', + 'title' => 'name' + ]); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); - $result = $adapter->query($sql)->execute(); foreach ($result as $data) { /* Prepare tag data */ /* @@ -136,14 +151,26 @@ public function execute() } /* Import posts */ - $sql = 'SELECT * FROM '.$_pref.'aw_blog_post'; - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'aw_blog_post') + ->columns(['*']); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); foreach ($result as $data) { /* Find post categories*/ $postCategories = []; - $c_sql = 'SELECT category_id FROM '.$_pref.'aw_blog_post_category WHERE post_id = "'.$data['id'].'"'; - $c_result = $adapter->query($c_sql)->execute(); + + $c_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $c_sql->select(); + $select->from($_pref . 'aw_blog_post_category') + ->columns(['category_id']) + ->where(['post_id = ?' => (int)$data['id']]); + + $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; if (isset($oldCategories[$oldId])) { @@ -154,8 +181,15 @@ public function execute() /* Find store ids */ $data['store_ids'] = []; - $s_sql = 'SELECT store_id FROM '.$_pref.'aw_blog_post_store WHERE post_id = "'.$data['id'].'"'; - $s_result = $adapter->query($s_sql)->execute(); + + $s_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $s_sql->select(); + $select->from($_pref . 'aw_blog_post_store') + ->columns(['store_id']) + ->where(['post_id = ?' => (int)$data['id']]); + + $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; } diff --git a/Model/Import/Mageplaza.php b/Model/Import/Mageplaza.php index 973dec7f..0ab8f841 100644 --- a/Model/Import/Mageplaza.php +++ b/Model/Import/Mageplaza.php @@ -21,9 +21,12 @@ public function execute() $adapter = $this->getDbAdapter(); $_pref = $this->getPrefix(); - $sql = 'SELECT * FROM ' . $_pref . 'mageplaza_blog_category LIMIT 1'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'mageplaza_blog_category') + ->limit(1); try { - $adapter->query($sql)->execute(); + $sql->prepareStatementForSqlObject($select)->execute(); } catch (\Exception $e) { throw new \Exception(__('Mageplaza Blog Extension not detected.'), 1); } @@ -32,22 +35,22 @@ public function execute() $oldCategories = []; /* Import categories */ - $sql = 'SELECT - t.category_id as old_id, - t.name as title, - t.url_key as identifier, - t.position as position, ' . - /* - t.meta_title as meta_title, - t.meta_keywords as meta_keywords, - t.meta_description as meta_description, - */ - 't.description as content, - t.parent_id as parent_id, - t.enabled as is_active, - t.store_ids as store_ids - FROM ' . $_pref . 'mageplaza_blog_category t'; - $result = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + + $select = $sql->select(); + + $select->from(['t' => $_pref . 'mageplaza_blog_category']) + ->columns([ + 'old_id' => 'category_id', + 'title' => 'name', + 'identifier' => 'url_key', + 'position' => 'position', + 'content' => 'description', + 'parent_id' => 'parent_id', + 'is_active' => 'enabled', + 'store_ids' => 'store_ids', + ]); + $result = $sql->prepareStatementForSqlObject($select)->execute(); foreach ($result as $data) { /* Prepare category data */ @@ -113,20 +116,18 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = 'SELECT - t.tag_id as old_id, - t.name as title, - t.url_key as identifier, - t.description as content, ' . - /* - t.meta_title as meta_title, - t.meta_description as meta_description, - t.meta_keywords as meta_keywords, - */ - 't.enabled as is_active - FROM ' . $_pref . 'mageplaza_blog_tag t'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'mageplaza_blog_tag']) + ->columns([ + 'old_id' => 'tag_id', + 'title' => 'name', + 'identifier' => 'url_key', + 'content' => 'description', + 'is_active' => 'enabled', + ]); + $result = $sql->prepareStatementForSqlObject($select)->execute(); - $result = $adapter->query($sql)->execute(); foreach ($result as $data) { /* Prepare tag data */ /* @@ -161,14 +162,23 @@ public function execute() } /* Import posts */ - $sql = 'SELECT * FROM ' . $_pref . 'mageplaza_blog_post'; - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'mageplaza_blog_post'); + $result = $sql->prepareStatementForSqlObject($select)->execute(); + foreach ($result as $data) { /* Find post categories*/ $postCategories = []; - $c_sql = 'SELECT category_id FROM ' . $_pref . - 'mageplaza_blog_post_category WHERE post_id = "'.$data['post_id'].'"'; - $c_result = $adapter->query($c_sql)->execute(); + + $c_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $c_sql->select(); + $select->from($_pref . 'mageplaza_blog_post_category') + ->columns(['category_id']) + ->where(['post_id = ?' => $data['post_id']]); + $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; if (isset($oldCategories[$oldId])) { @@ -179,9 +189,12 @@ public function execute() /* Find post tags*/ $postTags = []; - $t_sql = 'SELECT tag_id FROM ' . $_pref . 'mageplaza_blog_post_tag WHERE post_id = "'.$data['post_id'].'"'; - - $t_result = $adapter->query($t_sql)->execute(); + $t_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $t_sql->select(); + $select->from($_pref . 'mageplaza_blog_post_tag') + ->columns(['tag_id']) + ->where(['post_id = ?' => $data['post_id']]); + $t_result = $t_sql->prepareStatementForSqlObject($select)->execute(); foreach ($t_result as $t_data) { $oldId = $t_data['tag_id']; @@ -222,9 +235,12 @@ public function execute() $post->setData($data)->save(); /* find post comment s*/ - $sql = 'SELECT * FROM ' . $_pref . - 'mageplaza_blog_comment WHERE `post_id` = ' . $post->getOldId(); - $resultComments = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'mageplaza_blog_comment') + ->where(['post_id = ?' => $post->getOldId()]); + $resultComments = $sql->prepareStatementForSqlObject($select)->execute(); foreach ($resultComments as $comments) { $commentData = [ diff --git a/Model/Import/Mageplaza1.php b/Model/Import/Mageplaza1.php index cf5b1b63..c8baa462 100644 --- a/Model/Import/Mageplaza1.php +++ b/Model/Import/Mageplaza1.php @@ -33,9 +33,12 @@ public function execute() $adapter = $this->getDbAdapter(); $_pref = $this->getPrefix(); - $sql = 'SELECT * FROM ' . $_pref . 'mageplaza_betterblog_post LIMIT 1'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'mageplaza_betterblog_post')->limit(1); + try { - $adapter->query($sql)->execute(); + $sql->prepareStatementForSqlObject($select); } catch (\Exception $e) { throw new \Exception(__('Mageplaza Blog Extension not detected.'), 1); } @@ -44,19 +47,24 @@ public function execute() $oldCategories = []; /* Import categories */ - $sql = 'SELECT - t.entity_id as old_id, - t.name as title, - t.url_key as identifier, - t.position as position, - t.meta_title as meta_title, - t.meta_keywords as meta_keywords, - t.meta_description as meta_description, - t.description as content, - t.parent_id as parent_id, - t.status as is_active - FROM ' . $_pref . 'mageplaza_betterblog_category t'; - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'mageplaza_betterblog_category']) + ->columns([ + 'old_id' => 'entity_id', + 'title' => 'name', + 'identifier' => 'url_key', + 'position' => 'position', + 'meta_title' => 'meta_title', + 'meta_keywords' => 'meta_keywords', + 'meta_description' => 'meta_description', + 'content' => 'description', + 'parent_id' => 'parent_id', + 'is_active' => 'status', + ]); + $result = $sql->prepareStatementForSqlObject($select)->execute(); + foreach ($result as $data) { /* Prepare category data */ @@ -70,8 +78,15 @@ public function execute() /* Find store ids */ $data['store_ids'] = []; - $s_sql = 'SELECT store_id FROM '.$_pref.'mageplaza_betterblog_category_store WHERE category_id = "'.$data['old_id'].'"'; - $s_result = $adapter->query($s_sql)->execute(); + + $s_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $s_sql->select(); + $select->from($_pref . 'mageplaza_betterblog_category_store') + ->columns(['store_id']) + ->where(['category_id = ?' => (int)$data['old_id']]); + + $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; } @@ -132,18 +147,22 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = 'SELECT - t.entity_id as old_id, - t.name as title, - t.url_key as identifier, - t.description as content, - t.meta_title as meta_title, - t.meta_description as meta_description, - t.meta_keywords as meta_keywords, - t.status as is_active - FROM ' . $_pref . 'mageplaza_betterblog_tag t'; - - $result = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'mageplaza_betterblog_tag']) + ->columns([ + 'old_id' => 'entity_id', + 'title' => 'name', + 'identifier' => 'url_key', + 'content' => 'description', + 'meta_title' => 'meta_title', + 'meta_description' => 'meta_description', + 'meta_keywords' => 'meta_keywords', + 'is_active' => 'status', + ]); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); + foreach ($result as $data) { if (!$data['title']) { @@ -153,8 +172,15 @@ public function execute() /* Find store ids */ $data['store_ids'] = []; - $s_sql = 'SELECT store_id FROM '.$_pref.'mageplaza_betterblog_tag_store WHERE tag_id = "'.$data['old_id'].'"'; - $s_result = $adapter->query($s_sql)->execute(); + + $s_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $s_sql->select(); + $select->from($_pref . 'mageplaza_betterblog_tag_store') + ->columns(['store_id']) + ->where(['tag_id = ?' => (int)$data['old_id']]); + + $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; } @@ -179,14 +205,30 @@ public function execute() } /* Import posts */ - $sql = 'SELECT entity_id as post_id, created_at, updated_at FROM ' . $_pref . 'mageplaza_betterblog_post'; - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'mageplaza_betterblog_post') + ->columns([ + 'post_id' => 'entity_id', + 'created_at', + 'updated_at' + ]); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); + foreach ($result as $data) { /* Find post categories*/ $postCategories = []; - $c_sql = 'SELECT category_id FROM ' . $_pref . - 'mageplaza_betterblog_post_category WHERE post_id = "'.$data['post_id'].'"'; - $c_result = $adapter->query($c_sql)->execute(); + + $c_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $c_sql->select(); + $select->from($_pref . 'mageplaza_betterblog_post_category') + ->columns(['category_id']) + ->where(['post_id = ?' => (int)$data['post_id']]); + + $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; if (isset($oldCategories[$oldId])) { @@ -197,9 +239,14 @@ public function execute() /* Find post tags*/ $postTags = []; - $t_sql = 'SELECT tag_id FROM ' . $_pref . 'mageplaza_betterblog_post_tag WHERE post_id = "'.$data['post_id'].'"'; - $t_result = $adapter->query($t_sql)->execute(); + $t_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $t_sql->select(); + $select->from($_pref . 'mageplaza_betterblog_post_tag') + ->columns(['tag_id']) + ->where(['post_id = ?' => (int)$data['post_id']]); + + $t_result = $t_sql->prepareStatementForSqlObject($select)->execute(); foreach ($t_result as $t_data) { $oldId = $t_data['tag_id']; @@ -240,9 +287,14 @@ public function execute() $post->setData($data)->save(); /* find post comment s*/ - $sql = 'SELECT * FROM ' . $_pref . - 'mageplaza_betterblog_post_comment WHERE `post_id` = ' . $post->getOldId(); - $resultComments = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'mageplaza_betterblog_post_comment') + ->columns(['*']) + ->where(['post_id = ?' => (int)$post->getOldId()]); + + $resultComments = $sql->prepareStatementForSqlObject($select)->execute(); foreach ($resultComments as $comments) { $commentData = [ @@ -299,9 +351,16 @@ private function getPostAttrValue($postId, $attributeCode) $attribute = $this->getBlogAttributeId($attributeCode); - $sql = 'SELECT value FROM ' . $_pref . 'mageplaza_betterblog_post_' . $attribute['backend_type'] . ' WHERE entity_id = "' . ((int)$postId) . '" - AND attribute_id = "' . ((int)$attribute['attribute_id']) . '" LIMIT 1'; - $result = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'mageplaza_betterblog_post_' . $attribute['backend_type']) + ->columns(['value']) + ->where(['entity_id = ?' => (int)$postId]) + ->where(['attribute_id = ?' => (int)$attribute['attribute_id']]) + ->limit(1); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); + foreach ($result as $data) { return $data['value']; } @@ -319,8 +378,15 @@ private function getBlogEntityId() $adapter = $this->getDbAdapter(); $_pref = $this->getPrefix(); - $sql = 'SELECT entity_type_id FROM ' . $_pref . 'eav_entity_type WHERE entity_type_code = "mageplaza_betterblog_post" LIMIT 1'; - $result = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'eav_entity_type') + ->columns(['entity_type_id']) + ->where(['entity_type_code = ?' => 'mageplaza_betterblog_post']) + ->limit(1); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); + foreach ($result as $data) { $this->blogEntityId = (int)$data['entity_type_id']; break; @@ -346,9 +412,15 @@ private function getBlogAttributeId($attributeCode) $adapter = $this->getDbAdapter(); $_pref = $this->getPrefix(); - $sql = 'SELECT * FROM ' . $_pref . 'eav_attribute WHERE entity_type_id = "' . $this->getBlogEntityId() . '" - AND attribute_code = "' . $attributeCode . '" LIMIT 1'; - $result = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'eav_attribute') + ->where(['entity_type_id = ?' => $this->getBlogEntityId()]) + ->where(['attribute_code = ?' => $attributeCode]) + ->limit(1); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); + foreach ($result as $data) { $this->blogAttributes[$attributeCode] = $data; break; diff --git a/Model/Import/Mirasvit.php b/Model/Import/Mirasvit.php index 48f27653..566f2d47 100644 --- a/Model/Import/Mirasvit.php +++ b/Model/Import/Mirasvit.php @@ -24,9 +24,12 @@ public function execute() $adapter = $this->getDbAdapter(); $_pref = $this->getPrefix(); - $sql = 'SELECT * FROM ' . $_pref . 'mst_blog_post_entity LIMIT 1'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'mst_blog_post_entity')->limit(1); + try { - $adapter->query($sql)->execute(); + $sql->prepareStatementForSqlObject($select)->execute(); } catch (\Exception $e) { throw new \Exception(__('Mirasvit Blog Extension not detected.'), 1); } @@ -35,12 +38,19 @@ public function execute() $oldCategories = []; /* Import categories */ - $sql = 'SELECT - t.entity_id as old_id, - t.position as position, - t.parent_id as parent_id - FROM ' . $_pref . 'mst_blog_category_entity t'; - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'mst_blog_category_entity']) + ->columns([ + 'old_id' => 'entity_id', + 'position' => 'position', + 'parent_id' => 'parent_id' + ]); + $statement = $sql->prepareStatementForSqlObject($select); + $result = $statement->execute(); + + foreach ($result as $data) { /* Prepare category data */ @@ -117,14 +127,18 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = 'SELECT - t.tag_id as old_id, - t.name as title, - t.url_key as identifier, - 1 as is_active - FROM ' . $_pref . 'mst_blog_tag t'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'mst_blog_tag']) + ->columns([ + 'old_id' => 'tag_id', + 'title' => 'name', + 'identifier' => 'url_key', + 'is_active' => new \Laminas\Db\Sql\Predicate\Literal('1') + ]); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); - $result = $adapter->query($sql)->execute(); foreach ($result as $data) { /* Prepare tag data */ /* @@ -159,15 +173,22 @@ public function execute() } /* Import posts */ - $sql = 'SELECT - t.entity_id as old_id, - t.author_id as author_id, - t.created_at as creation_time, - t.created_at as publish_time, - t.updated_at as update_time - FROM ' . $_pref . 'mst_blog_post_entity t WHERE type="post"'; - - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref . 'mst_blog_post_entity']) + ->columns([ + 'old_id' => 'entity_id', + 'author_id' => 'author_id', + 'creation_time' => 'created_at', + 'publish_time' => 'created_at', + 'update_time' => 'updated_at', + ]) + ->where(['type' => 'post']); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); + + foreach ($result as $data) { $map = [ @@ -202,13 +223,15 @@ public function execute() /* Find post categories*/ $postCategories = []; - $c_sql = 'SELECT - category_id - FROM - ' . $_pref . 'mst_blog_category_post - WHERE - post_id = "'.$data['old_id'].'"'; - $c_result = $adapter->query($c_sql)->execute(); + + $c_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $c_sql->select(); + $select->from(['c' => $_pref . 'mst_blog_category_post']) + ->columns(['category_id']) + ->where(['post_id' => $data['old_id']]); + + $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; if (isset($oldCategories[$oldId])) { @@ -220,9 +243,13 @@ public function execute() /* Find post tags*/ $postTags = []; - $t_sql = 'SELECT tag_id FROM ' . $_pref . 'mst_blog_tag_post WHERE post_id = "'.$data['old_id'].'"'; + $t_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $t_sql->select(); + $select->from(['tp' => $_pref . 'mst_blog_tag_post']) + ->columns(['tag_id']) + ->where(['post_id' => $data['old_id']]); - $t_result = $adapter->query($t_sql)->execute(); + $t_result = $t_sql->prepareStatementForSqlObject($select)->execute(); foreach ($t_result as $t_data) { $oldId = $t_data['tag_id']; @@ -236,9 +263,14 @@ public function execute() /* Find post products*/ $data['links'] = []; $postProducts = []; - $t_sql = 'SELECT product_id FROM ' . $_pref . 'mst_blog_post_product WHERE post_id = "'.$data['old_id'].'"'; - $t_result = $adapter->query($t_sql)->execute(); + $t_sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $t_sql->select(); + $select->from($_pref . 'mst_blog_post_product') + ->columns(['product_id']) + ->where(['post_id' => $data['old_id']]); + + $t_result = $t_sql->prepareStatementForSqlObject($select)->execute(); foreach ($t_result as $t_data) { $id = $t_data['product_id']; @@ -251,8 +283,14 @@ public function execute() /* Find store ids */ $storeIds = []; - $sql2 = 'SELECT store_id FROM ' . $_pref . 'mst_blog_store_post WHERE post_id=' . $data['old_id']; - $result2 = $adapter->query($sql2)->execute(); + + $sql2 = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql2->select(); + $select->from($_pref . 'mst_blog_store_post') + ->columns(['store_id']) + ->where(['post_id' => $data['old_id']]); + + $result2 = $sql2->prepareStatementForSqlObject($select)->execute(); foreach ($result2 as $data2) { $storeIds[] = $data2['store_id']; } @@ -279,11 +317,14 @@ protected function getAttributValue($entitytTypeCode, $entitytId, $attributeCode $_pref = $this->getPrefix(); if (!isset($this->entityTypeId[$entitytTypeCode])) { - $sql = 'SELECT - entity_type_id - FROM ' . $_pref . 'eav_entity_type WHERE entity_type_code="' . $entitytTypeCode . '"'; + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'eav_entity_type') + ->columns(['entity_type_id']) + ->where(['entity_type_code' => $entitytTypeCode]); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); - $result = $adapter->query($sql)->execute(); if (count($result)) { foreach ($result as $data) { $this->entityTypeId[$entitytTypeCode] = $data['entity_type_id']; @@ -302,10 +343,14 @@ protected function getAttributValue($entitytTypeCode, $entitytId, $attributeCode if (!isset($this->entityTypeAttributes[$entitytTypeCode])) { $this->entityTypeAttributes[$entitytTypeCode] = []; - $sql = 'SELECT - * - FROM ' . $_pref . 'eav_attribute WHERE entity_type_id=' . $entityTypeId; - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'eav_attribute') + ->where(['entity_type_id' => $entityTypeId]); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); + foreach ($result as $data) { $this->entityTypeAttributes[$entitytTypeCode][$data['attribute_code']] = $data; } @@ -317,12 +362,20 @@ protected function getAttributValue($entitytTypeCode, $entitytId, $attributeCode $attribute = $this->entityTypeAttributes[$entitytTypeCode][$attributeCode]; - $sql = 'SELECT - value - FROM ' . $_pref . 'mst_'.$entitytTypeCode.'_entity_' . $attribute['backend_type'] . ' WHERE store_id = 0 - AND attribute_id = ' . $attribute['attribute_id'] . ' - AND entity_id=' . $entitytId; - $result = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + + $select->from($_pref . 'mst_' . $entitytTypeCode . '_entity_' . $attribute['backend_type']) + ->columns(['value']) + ->where([ + 'store_id' => 0, + 'attribute_id' => $attribute['attribute_id'], + 'entity_id' => $entitytId, + ]); + + $result = $sql->prepareStatementForSqlObject($select)->execute(); + + if (count($result)) { foreach ($result as $data) { return $data['value']; diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index d1722c14..c9b70b01 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -24,16 +24,25 @@ public function execute() $oldCategories = []; /* Import categories */ - $sql = 'SELECT - t.term_id as old_id, - t.name as title, - t.slug as identifier, - tt.parent as parent_id - FROM '.$_pref.'terms t - LEFT JOIN '.$_pref.'term_taxonomy tt on t.term_id = tt.term_id - WHERE tt.taxonomy = "category" AND t.slug <> "uncategorized"'; - - $result = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref.'terms']) + ->columns([ + 'old_id' => 'term_id', + 'title' => 'name', + 'identifier' => 'slug' + ]) + ->join( + ['tt' => $_pref.'term_taxonomy'], + 't.term_id = tt.term_id', + ['parent_id' => 'parent'], + $select::JOIN_LEFT + ) + ->where(['tt.taxonomy' => 'category']) + ->where->notEqualTo('t.slug', 'uncategorized'); +// $result = $adapter->query($sql)->execute(); + $result = $adapter->query($sql->buildSqlString($select))->execute(); foreach ($result as $data) { /* Prepare category data */ /* @@ -104,16 +113,24 @@ public function execute() $tags = []; $oldTags = []; - $sql = 'SELECT - t.term_id as old_id, - t.name as title, - t.slug as identifier, - tt.parent as parent_id - FROM '.$_pref.'terms t - LEFT JOIN '.$_pref.'term_taxonomy tt on t.term_id = tt.term_id - WHERE tt.taxonomy = "post_tag" AND t.slug <> "uncategorized"'; - - $result = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['t' => $_pref.'terms']) + ->columns([ + 'old_id' => 'term_id', + 'title' => 'name', + 'identifier' => 'slug' + ]) + ->join( + ['tt' => $_pref.'term_taxonomy'], + 't.term_id = tt.term_id', + ['parent_id' => 'parent'], + $select::JOIN_LEFT + ) + ->where(['tt.taxonomy' => 'post_tag']) + ->where->notEqualTo('t.slug', 'uncategorized'); + + $result = $adapter->query($sql->buildSqlString($select))->execute(); foreach ($result as $data) { /* Prepare tag data */ /* @@ -147,18 +164,32 @@ public function execute() } /* Import posts */ - $sql = 'SELECT * FROM '.$_pref.'posts WHERE `post_type` = "post"'; - $result = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref.'posts') + ->where(['post_type' => 'post']); + + $result = $adapter->query($sql->buildSqlString($select))->execute(); foreach ($result as $data) { /* find post categories*/ $postCategories = []; - $sql = 'SELECT tt.term_id as term_id FROM '.$_pref.'term_relationships tr - LEFT JOIN '.$_pref.'term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id - WHERE tr.`object_id` = "'.$data['ID'].'" AND tt.taxonomy = "category"'; - - $result2 = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['tr' => $_pref . 'term_relationships']) + ->columns(['term_id' => 'tt.term_id']) + ->join( + ['tt' => $_pref . 'term_taxonomy'], + 'tr.term_taxonomy_id = tt.term_taxonomy_id', + [] + ) + ->where([ + 'tr.object_id' => $data['ID'], + 'tt.taxonomy' => 'category' + ]); + + $result2 = $adapter->query($sql->buildSqlString($select))->execute(); foreach ($result2 as $data2) { $oldTermId = $data2['term_id']; if (isset($oldCategories[$oldTermId])) { @@ -169,11 +200,21 @@ public function execute() /* find post tags*/ $postTags = []; - $sql = 'SELECT tt.term_id as term_id FROM '.$_pref.'term_relationships tr - LEFT JOIN '.$_pref.'term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id - WHERE tr.`object_id` = "'.$data['ID'].'" AND tt.taxonomy = "post_tag"'; - - $result2 = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['tr' => $_pref . 'term_relationships']) + ->columns(['term_id' => 'tt.term_id']) + ->join( + ['tt' => $_pref . 'term_taxonomy'], + 'tr.term_taxonomy_id = tt.term_taxonomy_id', + [] + ) + ->where([ + 'tr.object_id' => $data['ID'], + 'tt.taxonomy' => 'post_tag' + ]); + + $result2 = $adapter->query($sql->buildSqlString($select))->execute(); foreach ($result2 as $data2) { $oldTermId = $data2['term_id']; if (isset($oldTags[$oldTermId])) { @@ -183,30 +224,27 @@ public function execute() $data['featured_img'] = ''; - $sql = 'SELECT wm2.meta_value as featured_img - FROM - '.$_pref.'posts p1 - LEFT JOIN - '.$_pref.'postmeta wm1 - ON ( - wm1.post_id = p1.id - AND wm1.meta_value IS NOT NULL - AND wm1.meta_key = "_thumbnail_id" - ) - LEFT JOIN - '.$_pref.'postmeta wm2 - ON ( - wm1.meta_value = wm2.post_id - AND wm2.meta_key = "_wp_attached_file" - AND wm2.meta_value IS NOT NULL - ) - WHERE - p1.ID="'.$data['ID'].'" - AND p1.post_type="post" - ORDER BY - p1.post_date DESC'; - - $result2 = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['p1' => $_pref . 'posts']) + ->columns(['featured_img' => 'wm2.meta_value']) + ->join( + ['wm1' => $_pref . 'postmeta'], + 'wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = "_thumbnail_id"', + [] + ) + ->join( + ['wm2' => $_pref . 'postmeta'], + 'wm1.meta_value = wm2.post_id AND wm2.meta_key = "_wp_attached_file" AND wm2.meta_value IS NOT NULL', + [] + ) + ->where([ + 'p1.ID = ?' => $data['ID'], + 'p1.post_type = ?' => 'post' + ]) + ->order('p1.post_date DESC'); + + $result2 = $adapter->query($sql->buildSqlString($select))->execute(); foreach ($result2 as $data2) { if ($data2['featured_img']) { $data['featured_img'] = \Magefan\Blog\Model\Post::BASE_MEDIA_PATH . '/' . $data2['featured_img']; @@ -216,23 +254,22 @@ public function execute() if (empty($data['featured_img'])) { - $sql = 'SELECT wm1.meta_value as featured_img - FROM - '.$_pref.'posts p1 - LEFT JOIN - '.$_pref.'postmeta wm1 - ON ( - wm1.post_id = p1.id - AND wm1.meta_value IS NOT NULL - AND wm1.meta_key = "dfiFeatured" - ) - WHERE - p1.ID="'.$data['ID'].'" - AND p1.post_type="post" - ORDER BY - p1.post_date DESC'; - - $result2 = $adapter->query($sql)->execute(); + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from(['p1' => $_pref . 'posts']) + ->columns(['featured_img' => 'wm1.meta_value']) + ->join( + ['wm1' => $_pref . 'postmeta'], + 'wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = "dfiFeatured"', + [] + ) + ->where([ + 'p1.ID = ?' => $data['ID'], + 'p1.post_type = ?' => 'post' + ]) + ->order('p1.post_date DESC'); + + $result2 = $adapter->query($sql->buildSqlString($select))->execute(); foreach ($result2 as $data2) { if ($data2['featured_img']) { $serializeInterface = \Magento\Framework\App\ObjectManager::getInstance() @@ -255,8 +292,13 @@ public function execute() } /* Find Meta Data */ - $sql = 'SELECT * FROM `'.$_pref.'postmeta` WHERE `post_id` = ' . ((int)$data['ID']); - $metaResult = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'postmeta') + ->where(['post_id = ?' => (int)$data['ID']]); + + $metaResult = $adapter->query($sql->buildSqlString($select))->execute(); foreach ($metaResult as $metaData) { $metaValue = trim(isset($metaData['meta_value']) ? $metaData['meta_value'] : ''); @@ -325,15 +367,15 @@ public function execute() $post->setData($data)->save(); /* find post comment s*/ - $sql = 'SELECT - * - FROM - '.$_pref.'comments - WHERE - `comment_approved`=1 - AND - `comment_post_ID` = ' . $wordpressPostId; - $resultComments = $adapter->query($sql)->execute(); + + $sql = new \Laminas\Db\Sql\Sql($adapter); + $select = $sql->select(); + $select->from($_pref . 'comments') + ->where(['comment_approved = ?' => 1]) + ->where(['comment_post_ID = ?' => $wordpressPostId]); + + $resultComments = $adapter->query($sql->buildSqlString($select))->execute(); + $commentParents = []; foreach ($resultComments as $comments) { From a6cd821c67a39797d238e8b63448a6f51eddc1d6 Mon Sep 17 00:00:00 2001 From: "ivan.hrytsai" Date: Tue, 15 Oct 2024 11:19:59 +0300 Subject: [PATCH 02/11] selet-improvements --- Model/Import/AbstractImport.php | 25 +++++++ Model/Import/Aw.php | 68 ++++++++---------- Model/Import/Aw2.php | 66 +++++++---------- Model/Import/Mageplaza.php | 61 +++++++--------- Model/Import/Mageplaza1.php | 110 +++++++++++----------------- Model/Import/Mirasvit.php | 120 +++++++++++++------------------ Model/Import/Wordpress.php | 122 +++++++++++++------------------- 7 files changed, 244 insertions(+), 328 deletions(-) diff --git a/Model/Import/AbstractImport.php b/Model/Import/AbstractImport.php index 7534c5e5..8f148af4 100644 --- a/Model/Import/AbstractImport.php +++ b/Model/Import/AbstractImport.php @@ -113,6 +113,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 @@ -136,6 +141,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\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = [], @@ -147,6 +153,7 @@ public function __construct( $this->_tagFactory = $tagFactory; $this->_commentFactory = $commentFactory; $this->_storeManager = $storeManager; + $this->connectionFactory = $connectionFactory; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->_authorFactory = $authorFactory ?: $objectManager->get(\Magefan\Blog\Api\AuthorInterfaceFactory::class); @@ -276,4 +283,22 @@ protected function getDbAdapter() } return $this->dbAdapter; } + + protected function getDbConnection() + { + $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->connectionFactory->create($connectionConf); + } + } diff --git a/Model/Import/Aw.php b/Model/Import/Aw.php index b2d88784..3b0f61ab 100644 --- a/Model/Import/Aw.php +++ b/Model/Import/Aw.php @@ -20,14 +20,14 @@ class Aw extends AbstractImport public function execute() { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref.'aw_blog_cat')->limit(1); + $select = $connection->select() + ->from($_pref.'aw_blog_cat')->limit(1); try { - $sql->prepareStatementForSqlObject($select)->execute(); + $connection->fetchAll($select); } catch (\Exception $e) { throw new \Exception(__('AheadWorks Blog Extension not detected.'), 1); } @@ -39,10 +39,8 @@ public function execute() /* Import categories */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'aw_blog_cat']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref . 'aw_blog_cat'],[ 'old_id' => 'cat_id', 'title' => 'title', 'identifier' => 'identifier', @@ -50,19 +48,17 @@ public function execute() 'meta_keywords' => 'meta_keywords', 'meta_description' => 'meta_description' ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare category data */ /* Find store ids */ $data['store_ids'] = []; - $s_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $s_sql->select(); - $select->from($_pref . 'aw_blog_cat_store') - ->columns(['store_id']) - ->where(['cat_id = ?' => (int)$data['old_id']]); - $s_result = $s_sql->prepareStatementForSqlObject($select)->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']; @@ -104,14 +100,12 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'aw_blog_tags']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref . 'aw_blog_tags'], [ 'old_id' => 'id', 'title' => 'tag' ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare tag data */ @@ -148,23 +142,20 @@ public function execute() /* Import posts */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'aw_blog'); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $select = $connection->select() + ->from($_pref . 'aw_blog'); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Find post categories*/ $postCategories = []; - $c_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $c_sql->select(); - $select->from($_pref . 'aw_blog_post_cat') - ->columns(['category_id' => 'cat_id']) - ->where(['post_id = ?' => (int)$data['post_id']]); + $c_select = $connection->select() + ->from($_pref . 'aw_blog_post_cat', ['category_id' => 'cat_id']) + ->where('post_id = ?', (int)$data['post_id']); ; - $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + $c_result = $connection->fetchAll($c_select); foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; @@ -177,13 +168,11 @@ public function execute() /* Find store ids */ $data['store_ids'] = []; - $s_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $s_sql->select(); - $select->from($_pref . 'aw_blog_store') - ->columns(['store_id']) - ->where(['post_id = ?' => (int)$data['post_id']]); + $s_select = $connection->select() + ->from($_pref . 'aw_blog_store', ['store_id']) + ->where('post_id = ?', (int)$data['post_id']); - $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + $s_result = $connection->fetchAll($s_select); foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; @@ -225,12 +214,11 @@ public function execute() $post->setData($data)->save(); /* find post comment s*/ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); + $select = $connection->select(); $select->from($_pref . 'aw_blog_comment') - ->where(['post_id = ?' => $post->getOldId()]); + ->where('post_id = ?', $post->getOldId()); - $resultComments = $sql->prepareStatementForSqlObject($select)->execute(); + $resultComments = $connection->fetchAll($select); foreach ($resultComments as $comments) { $commentParentId = 0; diff --git a/Model/Import/Aw2.php b/Model/Import/Aw2.php index d7e2dabb..915d18dc 100644 --- a/Model/Import/Aw2.php +++ b/Model/Import/Aw2.php @@ -21,14 +21,14 @@ class Aw2 extends AbstractImport public function execute() { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'aw_blog_category')->limit(1); + $select = $connection->select() + ->from($_pref . 'aw_blog_category')->limit(1); try { - $sql->prepareStatementForSqlObject($select)->execute(); + $connection->fetchAll($select); } catch (\Exception $e) { throw new \Exception(__('AheadWorks Blog Extension not detected.'), 1); } @@ -39,18 +39,17 @@ public function execute() /* Import categories */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'aw_blog_category']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref . 'aw_blog_category'],[ 'old_id' => 'id', 'title' => 'name', 'identifier' => 'url_key', 'position' => 'sort_order', 'meta_description' => 'meta_description' - ]); + ]) + ->columns(); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare category data */ @@ -58,14 +57,11 @@ public function execute() /* Find store ids */ $data['store_ids'] = []; - $s_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $s_sql->select(); - $select->from($_pref . 'aw_blog_category_store') - ->columns([ - 'store_id' - ])->where(['category_id' => ((int)$data['old_id'])]); + $s_select = $connection->select() + ->from($_pref . 'aw_blog_category_store', ['store_id']) + ->where('category_id = ?', (int)$data['old_id']); - $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + $s_result = $connection->fetchAll($s_select); foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; @@ -107,15 +103,13 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'aw_blog_tag']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref . 'aw_blog_tag'], [ 'old_id' => 'id', 'title' => 'name' ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare tag data */ @@ -152,24 +146,20 @@ public function execute() /* Import posts */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'aw_blog_post') - ->columns(['*']); + $select = $connection->select(); + $select->from($_pref . 'aw_blog_post', ['*']); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Find post categories*/ $postCategories = []; - $c_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $c_sql->select(); - $select->from($_pref . 'aw_blog_post_category') - ->columns(['category_id']) - ->where(['post_id = ?' => (int)$data['id']]); + $c_select = $connection->select() + ->from($_pref . 'aw_blog_post_category', ['category_id']) + ->where('post_id = ?', (int)$data['id']); - $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + $c_result = $connection->fetchAll($c_select); foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; @@ -182,13 +172,11 @@ public function execute() /* Find store ids */ $data['store_ids'] = []; - $s_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $s_sql->select(); - $select->from($_pref . 'aw_blog_post_store') - ->columns(['store_id']) - ->where(['post_id = ?' => (int)$data['id']]); + $s_select = $connection->select() + ->from($_pref . 'aw_blog_post_store', ['store_id']) + ->where('post_id = ?', (int)$data['id']); - $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + $s_result = $connection->fetchAll($s_select); foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; diff --git a/Model/Import/Mageplaza.php b/Model/Import/Mageplaza.php index 0ab8f841..fe40229b 100644 --- a/Model/Import/Mageplaza.php +++ b/Model/Import/Mageplaza.php @@ -19,14 +19,13 @@ class Mageplaza extends AbstractImport public function execute() { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'mageplaza_blog_category') + $select = $connection->select()->from($_pref . 'mageplaza_blog_category') ->limit(1); try { - $sql->prepareStatementForSqlObject($select)->execute(); + $connection->fetchAll($select); } catch (\Exception $e) { throw new \Exception(__('Mageplaza Blog Extension not detected.'), 1); } @@ -35,12 +34,9 @@ public function execute() $oldCategories = []; /* Import categories */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - - $select->from(['t' => $_pref . 'mageplaza_blog_category']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref . 'mageplaza_blog_category'], [ 'old_id' => 'category_id', 'title' => 'name', 'identifier' => 'url_key', @@ -50,7 +46,7 @@ public function execute() 'is_active' => 'enabled', 'store_ids' => 'store_ids', ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select);; foreach ($result as $data) { /* Prepare category data */ @@ -116,18 +112,17 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'mageplaza_blog_tag']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref . 'mageplaza_blog_tag'], [ 'old_id' => 'tag_id', 'title' => 'name', 'identifier' => 'url_key', 'content' => 'description', - 'is_active' => 'enabled', + 'is_active' => 'enabled' ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); +//var_dump($result); foreach ($result as $data) { /* Prepare tag data */ /* @@ -146,6 +141,7 @@ public function execute() /* Initial saving */ if (!isset($existingTags[$data['title']])) { $tag = $this->_tagFactory->create(); +// var_dump($data);exit(); $tag->setData($data)->save(); $this->_importedTagsCount++; $tags[$tag->getId()] = $tag; @@ -163,21 +159,17 @@ public function execute() /* Import posts */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'mageplaza_blog_post'); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $select = $connection->select()->from($_pref . 'mageplaza_blog_post'); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Find post categories*/ $postCategories = []; - $c_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $c_sql->select(); - $select->from($_pref . 'mageplaza_blog_post_category') - ->columns(['category_id']) - ->where(['post_id = ?' => $data['post_id']]); - $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + $select = $connection->select() + ->from($_pref . 'mageplaza_blog_post_category',['category_id']) + ->where('post_id = ?', $data['post_id']); + $c_result = $connection->fetchAll($select); foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; @@ -189,12 +181,9 @@ public function execute() /* Find post tags*/ $postTags = []; - $t_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $t_sql->select(); - $select->from($_pref . 'mageplaza_blog_post_tag') - ->columns(['tag_id']) - ->where(['post_id = ?' => $data['post_id']]); - $t_result = $t_sql->prepareStatementForSqlObject($select)->execute(); + $t_select = $connection->select()->from($_pref . 'mageplaza_blog_post_tag',['tag_id']) + ->where('post_id = ?', $data['post_id']); + $t_result = $connection->fetchAll($t_select); foreach ($t_result as $t_data) { $oldId = $t_data['tag_id']; @@ -236,11 +225,9 @@ public function execute() /* find post comment s*/ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'mageplaza_blog_comment') - ->where(['post_id = ?' => $post->getOldId()]); - $resultComments = $sql->prepareStatementForSqlObject($select)->execute(); + $select = $connection->select()->from($_pref . 'mageplaza_blog_comment') + ->where('post_id = ?' , $post->getOldId()); + $resultComments = $connection->fetchAll($select); foreach ($resultComments as $comments) { $commentData = [ diff --git a/Model/Import/Mageplaza1.php b/Model/Import/Mageplaza1.php index c8baa462..be16b921 100644 --- a/Model/Import/Mageplaza1.php +++ b/Model/Import/Mageplaza1.php @@ -31,14 +31,13 @@ class Mageplaza1 extends AbstractImport public function execute() { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'mageplaza_betterblog_post')->limit(1); + $select = $connection->select()->from($_pref . 'mageplaza_betterblog_post')->limit(1); try { - $sql->prepareStatementForSqlObject($select); + $connection->fetchAll($select); } catch (\Exception $e) { throw new \Exception(__('Mageplaza Blog Extension not detected.'), 1); } @@ -48,10 +47,7 @@ public function execute() /* Import categories */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'mageplaza_betterblog_category']) - ->columns([ + $select = $connection->select()->from(['t' => $_pref . 'mageplaza_betterblog_category'], [ 'old_id' => 'entity_id', 'title' => 'name', 'identifier' => 'url_key', @@ -63,7 +59,7 @@ public function execute() 'parent_id' => 'parent_id', 'is_active' => 'status', ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare category data */ @@ -79,13 +75,11 @@ public function execute() /* Find store ids */ $data['store_ids'] = []; - $s_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $s_sql->select(); - $select->from($_pref . 'mageplaza_betterblog_category_store') - ->columns(['store_id']) - ->where(['category_id = ?' => (int)$data['old_id']]); + $s_select = $connection->select()->from($_pref . 'mageplaza_betterblog_category_store', ['store_id']) + ->columns() + ->where('category_id = ?', (int)$data['old_id']); - $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + $s_result = $connection->fetchAll($s_select); foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; @@ -147,10 +141,7 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'mageplaza_betterblog_tag']) - ->columns([ + $select = $connection->select()->from(['t' => $_pref . 'mageplaza_betterblog_tag'], [ 'old_id' => 'entity_id', 'title' => 'name', 'identifier' => 'url_key', @@ -161,7 +152,7 @@ public function execute() 'is_active' => 'status', ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { @@ -173,13 +164,10 @@ public function execute() /* Find store ids */ $data['store_ids'] = []; - $s_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $s_sql->select(); - $select->from($_pref . 'mageplaza_betterblog_tag_store') - ->columns(['store_id']) - ->where(['tag_id = ?' => (int)$data['old_id']]); + $select = $connection->select()->from($_pref . 'mageplaza_betterblog_tag_store', ['store_id']) + ->where('tag_id = ?', (int)$data['old_id']); - $s_result = $s_sql->prepareStatementForSqlObject($select)->execute(); + $s_result = $connection->fetchAll($select); foreach ($s_result as $s_data) { $data['store_ids'][] = $s_data['store_id']; @@ -206,28 +194,24 @@ public function execute() /* Import posts */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'mageplaza_betterblog_post') - ->columns([ + $select = $connection->select() + ->from($_pref . 'mageplaza_betterblog_post', [ 'post_id' => 'entity_id', 'created_at', 'updated_at' ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Find post categories*/ $postCategories = []; - $c_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $c_sql->select(); - $select->from($_pref . 'mageplaza_betterblog_post_category') - ->columns(['category_id']) - ->where(['post_id = ?' => (int)$data['post_id']]); + $select = $connection->select() + ->from($_pref . 'mageplaza_betterblog_post_category', ['category_id']) + ->where('post_id = ?', (int)$data['post_id']); - $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + $c_result = $connection->fetchAll($select); foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; @@ -240,13 +224,10 @@ public function execute() /* Find post tags*/ $postTags = []; - $t_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $t_sql->select(); - $select->from($_pref . 'mageplaza_betterblog_post_tag') - ->columns(['tag_id']) - ->where(['post_id = ?' => (int)$data['post_id']]); + $select = $connection->select()->from($_pref . 'mageplaza_betterblog_post_tag', ['tag_id']) + ->where('post_id = ?', (int)$data['post_id']); - $t_result = $t_sql->prepareStatementForSqlObject($select)->execute(); + $t_result = $connection->fetchAll($select); foreach ($t_result as $t_data) { $oldId = $t_data['tag_id']; @@ -288,13 +269,10 @@ public function execute() /* find post comment s*/ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'mageplaza_betterblog_post_comment') - ->columns(['*']) - ->where(['post_id = ?' => (int)$post->getOldId()]); + $select = $connection->select()->from($_pref . 'mageplaza_betterblog_post_comment', ['*']) + ->where('post_id = ?', (int)$post->getOldId()); - $resultComments = $sql->prepareStatementForSqlObject($select)->execute(); + $resultComments = $connection->fetchAll($select); foreach ($resultComments as $comments) { $commentData = [ @@ -347,19 +325,17 @@ public function execute() private function getPostAttrValue($postId, $attributeCode) { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); $attribute = $this->getBlogAttributeId($attributeCode); - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'mageplaza_betterblog_post_' . $attribute['backend_type']) - ->columns(['value']) - ->where(['entity_id = ?' => (int)$postId]) - ->where(['attribute_id = ?' => (int)$attribute['attribute_id']]) + $select = $connection->select()->from($_pref . 'mageplaza_betterblog_post_' . $attribute['backend_type'], ['value']) + ->where('entity_id = ?', (int)$postId) + ->where('attribute_id = ?', (int)$attribute['attribute_id']) ->limit(1); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { return $data['value']; @@ -376,16 +352,14 @@ private function getBlogEntityId() { if (null === $this->blogEntityId) { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'eav_entity_type') - ->columns(['entity_type_id']) - ->where(['entity_type_code = ?' => 'mageplaza_betterblog_post']) + $select = $connection->select()->from($_pref . 'eav_entity_type', ['entity_type_id']) + ->where('entity_type_code = ?', 'mageplaza_betterblog_post') ->limit(1); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { $this->blogEntityId = (int)$data['entity_type_id']; @@ -410,16 +384,16 @@ private function getBlogAttributeId($attributeCode) $this->blogAttributes[$attributeCode] = []; $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'eav_attribute') - ->where(['entity_type_id = ?' => $this->getBlogEntityId()]) - ->where(['attribute_code = ?' => $attributeCode]) + $select = $connection->select() + ->from($_pref . 'eav_attribute') + ->where('entity_type_id = ?', $this->getBlogEntityId()) + ->where('attribute_code = ?', $attributeCode) ->limit(1); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { $this->blogAttributes[$attributeCode] = $data; diff --git a/Model/Import/Mirasvit.php b/Model/Import/Mirasvit.php index 566f2d47..f69a8edc 100644 --- a/Model/Import/Mirasvit.php +++ b/Model/Import/Mirasvit.php @@ -22,14 +22,14 @@ class Mirasvit extends AbstractImport public function execute() { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'mst_blog_post_entity')->limit(1); + $select = $connection->select() + ->from($_pref . 'mst_blog_post_entity')->limit(1); try { - $sql->prepareStatementForSqlObject($select)->execute(); + $connection->fetchAll($select); } catch (\Exception $e) { throw new \Exception(__('Mirasvit Blog Extension not detected.'), 1); } @@ -39,17 +39,13 @@ public function execute() /* Import categories */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'mst_blog_category_entity']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref . 'mst_blog_category_entity'], [ 'old_id' => 'entity_id', 'position' => 'position', 'parent_id' => 'parent_id' ]); - $statement = $sql->prepareStatementForSqlObject($select); - $result = $statement->execute(); - + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare category data */ @@ -127,17 +123,15 @@ public function execute() $oldTags = []; $existingTags = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'mst_blog_tag']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref . 'mst_blog_tag'], [ 'old_id' => 'tag_id', 'title' => 'name', 'identifier' => 'url_key', - 'is_active' => new \Laminas\Db\Sql\Predicate\Literal('1') + 'is_active' => new \Zend_Db_Expr('1') ]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare tag data */ @@ -174,19 +168,17 @@ public function execute() /* Import posts */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref . 'mst_blog_post_entity']) - ->columns([ - 'old_id' => 'entity_id', + $select = $connection->select() + ->from(['t' => $_pref . 'mst_blog_post_entity'], [ + 'old_id' => 'entity_id', 'author_id' => 'author_id', 'creation_time' => 'created_at', 'publish_time' => 'created_at', 'update_time' => 'updated_at', ]) - ->where(['type' => 'post']); + ->where('type = ?', 'post'); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { @@ -224,13 +216,11 @@ public function execute() /* Find post categories*/ $postCategories = []; - $c_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $c_sql->select(); - $select->from(['c' => $_pref . 'mst_blog_category_post']) - ->columns(['category_id']) - ->where(['post_id' => $data['old_id']]); + $c_select = $connection->select() + ->from(['c' => $_pref . 'mst_blog_category_post'], ['category_id']) + ->where('post_id = ?', $data['old_id']); - $c_result = $c_sql->prepareStatementForSqlObject($select)->execute(); + $c_result = $connection->fetchAll($c_select); foreach ($c_result as $c_data) { $oldId = $c_data['category_id']; @@ -243,13 +233,12 @@ public function execute() /* Find post tags*/ $postTags = []; - $t_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $t_sql->select(); - $select->from(['tp' => $_pref . 'mst_blog_tag_post']) - ->columns(['tag_id']) - ->where(['post_id' => $data['old_id']]); - $t_result = $t_sql->prepareStatementForSqlObject($select)->execute(); + $t_select = $connection->select() + ->from(['tp' => $_pref . 'mst_blog_tag_post'], ['tag_id']) + ->where('post_id = ?', $data['old_id']); + + $t_result = $connection->fetchAll($t_select); foreach ($t_result as $t_data) { $oldId = $t_data['tag_id']; @@ -264,13 +253,11 @@ public function execute() $data['links'] = []; $postProducts = []; - $t_sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $t_sql->select(); - $select->from($_pref . 'mst_blog_post_product') - ->columns(['product_id']) - ->where(['post_id' => $data['old_id']]); + $t_select = $connection->select() + ->from($_pref . 'mst_blog_post_product', ['product_id']) + ->where('post_id = ?', $data['old_id']); - $t_result = $t_sql->prepareStatementForSqlObject($select)->execute(); + $t_result = $connection->fetchAll($t_select); foreach ($t_result as $t_data) { $id = $t_data['product_id']; @@ -284,13 +271,11 @@ public function execute() /* Find store ids */ $storeIds = []; - $sql2 = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql2->select(); - $select->from($_pref . 'mst_blog_store_post') - ->columns(['store_id']) - ->where(['post_id' => $data['old_id']]); + $select2 = $connection->select() + ->from($_pref . 'mst_blog_store_post', ['store_id']) + ->where('post_id = ?', $data['old_id']); - $result2 = $sql2->prepareStatementForSqlObject($select)->execute(); + $result2 = $connection->fetchAll($select2); foreach ($result2 as $data2) { $storeIds[] = $data2['store_id']; } @@ -314,16 +299,16 @@ public function execute() protected function getAttributValue($entitytTypeCode, $entitytId, $attributeCode) { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); if (!isset($this->entityTypeId[$entitytTypeCode])) { - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'eav_entity_type') - ->columns(['entity_type_id']) - ->where(['entity_type_code' => $entitytTypeCode]); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $select = $connection->select() + ->from($_pref . 'eav_entity_type', ['entity_type_id']) + ->where('entity_type_code = ?', $entitytTypeCode); + + $result = $connection->fetchAll($select); if (count($result)) { foreach ($result as $data) { @@ -344,12 +329,11 @@ protected function getAttributValue($entitytTypeCode, $entitytId, $attributeCode if (!isset($this->entityTypeAttributes[$entitytTypeCode])) { $this->entityTypeAttributes[$entitytTypeCode] = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'eav_attribute') - ->where(['entity_type_id' => $entityTypeId]); + $select = $connection->select() + ->from($_pref . 'eav_attribute') + ->where('entity_type_id = ?', $entityTypeId); - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { $this->entityTypeAttributes[$entitytTypeCode][$data['attribute_code']] = $data; @@ -362,19 +346,13 @@ protected function getAttributValue($entitytTypeCode, $entitytId, $attributeCode $attribute = $this->entityTypeAttributes[$entitytTypeCode][$attributeCode]; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - - $select->from($_pref . 'mst_' . $entitytTypeCode . '_entity_' . $attribute['backend_type']) - ->columns(['value']) - ->where([ - 'store_id' => 0, - 'attribute_id' => $attribute['attribute_id'], - 'entity_id' => $entitytId, - ]); - - $result = $sql->prepareStatementForSqlObject($select)->execute(); + $select = $connection->select() + ->from($_pref . 'mst_' . $entitytTypeCode . '_entity_' . $attribute['backend_type'], ['value']) + ->where('store_id = ?', 0) + ->where('attribute_id = ?', $attribute['attribute_id']) + ->where('entity_id = ?', $entitytId); + $result = $connection->fetchAll($select); if (count($result)) { foreach ($result as $data) { diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index c9b70b01..64e57cca 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -18,6 +18,7 @@ class Wordpress extends AbstractImport public function execute() { $adapter = $this->getDbAdapter(); + $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); $categories = []; @@ -25,24 +26,20 @@ public function execute() /* Import categories */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref.'terms']) - ->columns([ + $select = $connection->select() + ->from(['t' => $_pref.'terms'], [ 'old_id' => 'term_id', 'title' => 'name', 'identifier' => 'slug' ]) - ->join( + ->joinLeft( ['tt' => $_pref.'term_taxonomy'], 't.term_id = tt.term_id', - ['parent_id' => 'parent'], - $select::JOIN_LEFT + ['parent_id' => 'parent'] ) - ->where(['tt.taxonomy' => 'category']) - ->where->notEqualTo('t.slug', 'uncategorized'); -// $result = $adapter->query($sql)->execute(); - $result = $adapter->query($sql->buildSqlString($select))->execute(); + ->where('tt.taxonomy = ?', 'category') + ->where('t.slug != ?', 'uncategorized'); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare category data */ /* @@ -113,24 +110,22 @@ public function execute() $tags = []; $oldTags = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['t' => $_pref.'terms']) - ->columns([ + + $select = $connection->select() + ->from(['t' => $_pref.'terms'], [ 'old_id' => 'term_id', 'title' => 'name', 'identifier' => 'slug' ]) - ->join( + ->joinLeft( ['tt' => $_pref.'term_taxonomy'], 't.term_id = tt.term_id', - ['parent_id' => 'parent'], - $select::JOIN_LEFT + ['parent_id' => 'parent'] ) - ->where(['tt.taxonomy' => 'post_tag']) - ->where->notEqualTo('t.slug', 'uncategorized'); + ->where('tt.taxonomy = ?', 'post_tag') + ->where('t.slug != ?', 'uncategorized'); - $result = $adapter->query($sql->buildSqlString($select))->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare tag data */ /* @@ -165,31 +160,26 @@ public function execute() /* Import posts */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref.'posts') - ->where(['post_type' => 'post']); + $select = $connection->select() + ->from($_pref.'posts') + ->where('post_type = ?', 'post'); - $result = $adapter->query($sql->buildSqlString($select))->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* find post categories*/ $postCategories = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['tr' => $_pref . 'term_relationships']) - ->columns(['term_id' => 'tt.term_id']) + $select2 = $connection->select() + ->from(['tr' => $_pref . 'term_relationships'], ['term_id' => 'tt.term_id']) ->join( ['tt' => $_pref . 'term_taxonomy'], 'tr.term_taxonomy_id = tt.term_taxonomy_id', [] ) - ->where([ - 'tr.object_id' => $data['ID'], - 'tt.taxonomy' => 'category' - ]); + ->where('tr.object_id = ?', $data['ID']) + ->where('tt.taxonomy = ?', 'category'); - $result2 = $adapter->query($sql->buildSqlString($select))->execute(); + $result2 = $connection->fetchAll($select2); foreach ($result2 as $data2) { $oldTermId = $data2['term_id']; if (isset($oldCategories[$oldTermId])) { @@ -200,21 +190,17 @@ public function execute() /* find post tags*/ $postTags = []; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['tr' => $_pref . 'term_relationships']) - ->columns(['term_id' => 'tt.term_id']) + $select2 = $connection->select() + ->from(['tr' => $_pref . 'term_relationships'],['term_id' => 'tt.term_id']) ->join( ['tt' => $_pref . 'term_taxonomy'], 'tr.term_taxonomy_id = tt.term_taxonomy_id', [] ) - ->where([ - 'tr.object_id' => $data['ID'], - 'tt.taxonomy' => 'post_tag' - ]); + ->where('tr.object_id = ?', $data['ID']) + ->where('tt.taxonomy = ?', 'post_tag'); - $result2 = $adapter->query($sql->buildSqlString($select))->execute(); + $result2 = $connection->fetchAll($select2); foreach ($result2 as $data2) { $oldTermId = $data2['term_id']; if (isset($oldTags[$oldTermId])) { @@ -224,10 +210,8 @@ public function execute() $data['featured_img'] = ''; - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['p1' => $_pref . 'posts']) - ->columns(['featured_img' => 'wm2.meta_value']) + $select2 = $connection->select() + ->from(['p1' => $_pref . 'posts'],['featured_img' => 'wm2.meta_value']) ->join( ['wm1' => $_pref . 'postmeta'], 'wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = "_thumbnail_id"', @@ -238,13 +222,11 @@ public function execute() 'wm1.meta_value = wm2.post_id AND wm2.meta_key = "_wp_attached_file" AND wm2.meta_value IS NOT NULL', [] ) - ->where([ - 'p1.ID = ?' => $data['ID'], - 'p1.post_type = ?' => 'post' - ]) + ->where('p1.ID = ?', $data['ID']) + ->where('p1.post_type = ?', 'post') ->order('p1.post_date DESC'); - $result2 = $adapter->query($sql->buildSqlString($select))->execute(); + $result2 = $connection->fetchAll($select2); foreach ($result2 as $data2) { if ($data2['featured_img']) { $data['featured_img'] = \Magefan\Blog\Model\Post::BASE_MEDIA_PATH . '/' . $data2['featured_img']; @@ -254,22 +236,18 @@ public function execute() if (empty($data['featured_img'])) { - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from(['p1' => $_pref . 'posts']) - ->columns(['featured_img' => 'wm1.meta_value']) + $select2 = $connection->select() + ->from(['p1' => $_pref . 'posts'], ['featured_img' => 'wm1.meta_value']) ->join( ['wm1' => $_pref . 'postmeta'], 'wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = "dfiFeatured"', [] ) - ->where([ - 'p1.ID = ?' => $data['ID'], - 'p1.post_type = ?' => 'post' - ]) + ->where('p1.ID = ?', $data['ID']) + ->where('p1.post_type = ?', 'post') ->order('p1.post_date DESC'); - $result2 = $adapter->query($sql->buildSqlString($select))->execute(); + $result2 = $connection->fetchAll($select2); foreach ($result2 as $data2) { if ($data2['featured_img']) { $serializeInterface = \Magento\Framework\App\ObjectManager::getInstance() @@ -293,12 +271,11 @@ public function execute() /* Find Meta Data */ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'postmeta') - ->where(['post_id = ?' => (int)$data['ID']]); + $select = $connection->select() + ->from($_pref . 'postmeta') + ->where('post_id = ?', (int)$data['ID']); - $metaResult = $adapter->query($sql->buildSqlString($select))->execute(); + $metaResult = $connection->fetchAll($select); foreach ($metaResult as $metaData) { $metaValue = trim(isset($metaData['meta_value']) ? $metaData['meta_value'] : ''); @@ -368,13 +345,12 @@ public function execute() /* find post comment s*/ - $sql = new \Laminas\Db\Sql\Sql($adapter); - $select = $sql->select(); - $select->from($_pref . 'comments') - ->where(['comment_approved = ?' => 1]) - ->where(['comment_post_ID = ?' => $wordpressPostId]); + $select = $connection->select() + ->from($_pref . 'comments') + ->where('comment_approved = ?', 1) + ->where('comment_post_ID = ?', $wordpressPostId); - $resultComments = $adapter->query($sql->buildSqlString($select))->execute(); + $resultComments = $connection->fetchAll($select); $commentParents = []; From 247221c3a8b42b383c9bf0c621076403317e0916 Mon Sep 17 00:00:00 2001 From: "ivan.hrytsai" Date: Tue, 15 Oct 2024 11:36:39 +0300 Subject: [PATCH 03/11] remove breadcrumb --- Model/Import/Mageplaza.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Model/Import/Mageplaza.php b/Model/Import/Mageplaza.php index fe40229b..2a6f3817 100644 --- a/Model/Import/Mageplaza.php +++ b/Model/Import/Mageplaza.php @@ -122,7 +122,7 @@ public function execute() ]); $result = $connection->fetchAll($select); -//var_dump($result); + foreach ($result as $data) { /* Prepare tag data */ /* @@ -141,7 +141,6 @@ public function execute() /* Initial saving */ if (!isset($existingTags[$data['title']])) { $tag = $this->_tagFactory->create(); -// var_dump($data);exit(); $tag->setData($data)->save(); $this->_importedTagsCount++; $tags[$tag->getId()] = $tag; From a8952a0f5b57cd73a65f4304b637bc4beb837be8 Mon Sep 17 00:00:00 2001 From: "ivan.hrytsai" Date: Tue, 15 Oct 2024 12:28:25 +0300 Subject: [PATCH 04/11] 12016-All-import-scripts-improvements --- Model/Import/AbstractImport.php | 40 +++++---------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/Model/Import/AbstractImport.php b/Model/Import/AbstractImport.php index 8f148af4..2cd472f0 100644 --- a/Model/Import/AbstractImport.php +++ b/Model/Import/AbstractImport.php @@ -241,49 +241,21 @@ 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 getDbConnection() { $connectionConf = [ From 64b803447031a381be3370ec3239aaf9ba0fdc0e Mon Sep 17 00:00:00 2001 From: "ivan.hrytsai" Date: Tue, 22 Oct 2024 11:59:16 +0300 Subject: [PATCH 05/11] 12016-All-import-scripts-improvements --- Model/Import/AbstractImport.php | 5 ----- Model/Import/Aw.php | 2 -- Model/Import/Aw2.php | 2 -- Model/Import/Mageplaza.php | 2 -- Model/Import/Mageplaza1.php | 5 ----- Model/Import/Mirasvit.php | 3 --- Model/Import/Wordpress.php | 3 --- 7 files changed, 22 deletions(-) diff --git a/Model/Import/AbstractImport.php b/Model/Import/AbstractImport.php index 2cd472f0..8bf3ac42 100644 --- a/Model/Import/AbstractImport.php +++ b/Model/Import/AbstractImport.php @@ -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 */ diff --git a/Model/Import/Aw.php b/Model/Import/Aw.php index 3b0f61ab..f4e30183 100644 --- a/Model/Import/Aw.php +++ b/Model/Import/Aw.php @@ -19,7 +19,6 @@ class Aw extends AbstractImport public function execute() { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); @@ -268,6 +267,5 @@ public function execute() unset($post); } /* end */ - $adapter->getDriver()->getConnection()->disconnect(); } } diff --git a/Model/Import/Aw2.php b/Model/Import/Aw2.php index 915d18dc..297edc92 100644 --- a/Model/Import/Aw2.php +++ b/Model/Import/Aw2.php @@ -20,7 +20,6 @@ class Aw2 extends AbstractImport public function execute() { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); @@ -229,6 +228,5 @@ public function execute() unset($post); } /* end */ - $adapter->getDriver()->getConnection()->disconnect(); } } diff --git a/Model/Import/Mageplaza.php b/Model/Import/Mageplaza.php index 2a6f3817..aab96934 100644 --- a/Model/Import/Mageplaza.php +++ b/Model/Import/Mageplaza.php @@ -18,7 +18,6 @@ class Mageplaza extends AbstractImport public function execute() { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); @@ -274,6 +273,5 @@ public function execute() unset($post); } /* end */ - $adapter->getDriver()->getConnection()->disconnect(); } } diff --git a/Model/Import/Mageplaza1.php b/Model/Import/Mageplaza1.php index be16b921..ca20523d 100644 --- a/Model/Import/Mageplaza1.php +++ b/Model/Import/Mageplaza1.php @@ -30,7 +30,6 @@ class Mageplaza1 extends AbstractImport */ public function execute() { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); @@ -313,7 +312,6 @@ public function execute() unset($post); } /* end */ - $adapter->getDriver()->getConnection()->disconnect(); } /** @@ -324,7 +322,6 @@ public function execute() */ private function getPostAttrValue($postId, $attributeCode) { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); @@ -351,7 +348,6 @@ private function getPostAttrValue($postId, $attributeCode) private function getBlogEntityId() { if (null === $this->blogEntityId) { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); @@ -383,7 +379,6 @@ private function getBlogAttributeId($attributeCode) if (!isset($this->blogAttributes[$attributeCode])) { $this->blogAttributes[$attributeCode] = []; - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); diff --git a/Model/Import/Mirasvit.php b/Model/Import/Mirasvit.php index f69a8edc..f04b5d28 100644 --- a/Model/Import/Mirasvit.php +++ b/Model/Import/Mirasvit.php @@ -21,7 +21,6 @@ class Mirasvit extends AbstractImport public function execute() { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); @@ -293,12 +292,10 @@ public function execute() unset($post); } /* end */ - $adapter->getDriver()->getConnection()->disconnect(); } protected function getAttributValue($entitytTypeCode, $entitytId, $attributeCode) { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index 64e57cca..c1fa31c8 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -17,7 +17,6 @@ class Wordpress extends AbstractImport public function execute() { - $adapter = $this->getDbAdapter(); $connection = $this->getDbConnection(); $_pref = $this->getPrefix(); @@ -401,8 +400,6 @@ public function execute() unset($post); } /* end */ - - $adapter->getDriver()->getConnection()->disconnect(); } protected function wordpressOutoutWrap($pee, $br = true) From d298c3d2305384fd937a9d70826f8ef399d22abd Mon Sep 17 00:00:00 2001 From: Yuriy Date: Tue, 7 Jan 2025 10:42:22 +0200 Subject: [PATCH 06/11] 12458-wordpress-import-authors --- Model/Import/Wordpress.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index d1722c14..40844010 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -146,6 +146,37 @@ public function execute() } } + /* Import authors */ + $oldAuthors = []; + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + if ($objectManager->get(\Magento\Framework\Module\Manager::class)->isEnabled('Magefan_BlogAuthor')) { + $sql = 'SELECT p.ID, p.post_author, u.user_nicename as identifier, u.user_email as email, u.display_name FROM ' . $_pref . 'posts p + LEFT JOIN ' . $_pref . 'users u on p.post_author = u.ID + WHERE p.post_type = "post" GROUP BY p.post_author'; + + $result = $adapter->query($sql)->execute(); + foreach ($result as $data) { + /* Prepare author data */ + if (!empty($data['display_name'])) { + $data['display_name'] = explode(' ', $data['display_name'], 2); + $data['firstname'] = !empty($data['display_name'][0]) ? $data['display_name'][0] : ''; + $data['lastname'] = !empty($data['display_name'][1]) ? $data['display_name'][0] : ''; + } + + $data['identifier'] = $this->prepareIdentifier($data['identifier']); + $author = $this->_authorFactory->create(); + try { + /* Initial saving */ + $author->setData($data)->save(); + $this->_importedAuthorsCount++; + $oldAuthors[$data['post_author']] = $author; + } catch (\Magento\Framework\Exception\LocalizedException $e) { + unset($tag); + $this->_logger->debug('Blog Author Import [' . $data['identifier'] . ']: ' . $e->getMessage()); + } + } + } + /* Import posts */ $sql = 'SELECT * FROM '.$_pref.'posts WHERE `post_type` = "post"'; $result = $adapter->query($sql)->execute(); @@ -315,6 +346,7 @@ public function execute() 'categories' => $postCategories, 'tags' => $postTags, 'featured_img' => $data['featured_img'], + 'author_id' => (isset($data['post_author']) && isset($oldAuthors[$data['post_author']])) ? $oldAuthors[$data['post_author']]->getId() : null, ]; $data['identifier'] = $this->prepareIdentifier($data['identifier']); From bc1d9cb60abaa6a2d61710c1e8ea990220140e68 Mon Sep 17 00:00:00 2001 From: Yuriy Date: Tue, 7 Jan 2025 10:45:04 +0200 Subject: [PATCH 07/11] 12458-wordpress-import-authors-2 --- Model/Import/Wordpress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index 40844010..a9c4155a 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -171,7 +171,7 @@ public function execute() $this->_importedAuthorsCount++; $oldAuthors[$data['post_author']] = $author; } catch (\Magento\Framework\Exception\LocalizedException $e) { - unset($tag); + unset($author); $this->_logger->debug('Blog Author Import [' . $data['identifier'] . ']: ' . $e->getMessage()); } } From 16ee180fc2495201881962793866b89ac472e102 Mon Sep 17 00:00:00 2001 From: Yuriy Date: Tue, 7 Jan 2025 11:01:04 +0200 Subject: [PATCH 08/11] 12458-wordpress-import-authors-3 --- Model/Import/Wordpress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index a9c4155a..5c4447d2 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -150,7 +150,7 @@ public function execute() $oldAuthors = []; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); if ($objectManager->get(\Magento\Framework\Module\Manager::class)->isEnabled('Magefan_BlogAuthor')) { - $sql = 'SELECT p.ID, p.post_author, u.user_nicename as identifier, u.user_email as email, u.display_name FROM ' . $_pref . 'posts p + $sql = 'SELECT p.post_author, u.user_nicename as identifier, u.user_email as email, u.display_name FROM ' . $_pref . 'posts p LEFT JOIN ' . $_pref . 'users u on p.post_author = u.ID WHERE p.post_type = "post" GROUP BY p.post_author'; From 45747d47899089c438269240fb113b20cecb7c16 Mon Sep 17 00:00:00 2001 From: Yura <48569882+yuriyhamulevych@users.noreply.github.com> Date: Mon, 20 Jan 2025 14:38:35 +0200 Subject: [PATCH 09/11] Update Wordpress.php --- Model/Import/Wordpress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index 5c4447d2..8c2b8426 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -160,7 +160,7 @@ public function execute() if (!empty($data['display_name'])) { $data['display_name'] = explode(' ', $data['display_name'], 2); $data['firstname'] = !empty($data['display_name'][0]) ? $data['display_name'][0] : ''; - $data['lastname'] = !empty($data['display_name'][1]) ? $data['display_name'][0] : ''; + $data['lastname'] = !empty($data['display_name'][1]) ? $data['display_name'][1] : ''; } $data['identifier'] = $this->prepareIdentifier($data['identifier']); From 1bba1092595eebde048b28a638725acc6c7fb0ad Mon Sep 17 00:00:00 2001 From: Yuriy Date: Thu, 23 Jan 2025 11:48:48 +0200 Subject: [PATCH 10/11] 12458-wordpress-import-parse-content --- Model/Import/AbstractImport.php | 67 ++++++++++++++++++++++++++++++++- Model/Import/Wordpress.php | 2 +- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/Model/Import/AbstractImport.php b/Model/Import/AbstractImport.php index ec1c30dd..783fb14d 100644 --- a/Model/Import/AbstractImport.php +++ b/Model/Import/AbstractImport.php @@ -275,7 +275,7 @@ protected function getDbAdapter() } catch (\Exception $e) { throw new \Exception("Failed connect to the database."); } - + } return $this->dbAdapter; } @@ -302,7 +302,7 @@ protected function getFeaturedImgBySrc($src) if (!$hasFormat) { $imageName .= '.jpg'; } - + $imagePath = $mediaPath . '/' . $imageName; $imageSource = false; if (!$this->file->fileExists($imagePath)) { @@ -323,4 +323,67 @@ protected function getFeaturedImgBySrc($src) return false; } } + + protected function parseContent($content) + { + + $content = str_replace('', '', $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('/', $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; + } } diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index 8c2b8426..edcd3d16 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -318,7 +318,7 @@ public function execute() $creationTime = strtotime((string)$data['post_date_gmt']); $content = $data['post_content']; - $content = str_replace('', '', $content); + $content = $this->parseContent($content); $content = preg_replace( '/src=[\'"]((http:\/\/|https:\/\/|\/\/)(.*)|(\s|"|\')|(\/[\d\w_\-\.]*))\/wp-content\/uploads(.*)((\.jpg|\.jpeg|\.gif|\.png|\.tiff|\.tif|\.svg)|(\s|"|\'))[\'"\s]/Ui', From b57af2ba706fc0c3bf0efd49e408304a40cde8c0 Mon Sep 17 00:00:00 2001 From: Yuriy Date: Fri, 24 Jan 2025 15:10:49 +0200 Subject: [PATCH 11/11] 12458-12016-All-import-scripts-improvements --- Model/Import/AbstractImport.php | 127 ++++++++++++++++++++++++++++++++ Model/Import/Wordpress.php | 19 ++++- 2 files changed, 142 insertions(+), 4 deletions(-) diff --git a/Model/Import/AbstractImport.php b/Model/Import/AbstractImport.php index aa271683..720d87ce 100644 --- a/Model/Import/AbstractImport.php +++ b/Model/Import/AbstractImport.php @@ -113,6 +113,16 @@ abstract class AbstractImport extends \Magento\Framework\Model\AbstractModel */ protected $connectionFactory; + /** + * @var \Magento\Framework\Filesystem + */ + protected $fileSystem; + + /** + * @var \Magento\Framework\Filesystem\Io\File + */ + protected $file; + /** * AbstractImport constructor. * @param \Magento\Framework\Model\Context $context @@ -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 @@ -270,4 +283,118 @@ protected function getDbConnection() return $this->connectionFactory->create($connectionConf); } + + protected function getFeaturedImgBySrc($src) + { + $mediaPath = $this->fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath() . '/magefan_blog'; + + $this->file->mkdir($mediaPath, 0775); + + $imageName = explode('?', $src); + $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'; + } + + $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)) { + try { + $imageData = $this->file->read($src); + $this->file->write($imagePath, $imageData); + $imageSource = true; + } catch (\Exception $e) { + $imageSource = false; + } + } else { + $imageSource = true; + } + + if ($imageSource) { + return 'magefan_blog/' . $imageName; + } else { + return false; + } + } + + protected function parseContent($content) + { + $content = str_replace('', '', $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; + } } diff --git a/Model/Import/Wordpress.php b/Model/Import/Wordpress.php index 368cb716..fc60bd09 100644 --- a/Model/Import/Wordpress.php +++ b/Model/Import/Wordpress.php @@ -161,11 +161,22 @@ public function execute() $oldAuthors = []; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); if ($objectManager->get(\Magento\Framework\Module\Manager::class)->isEnabled('Magefan_BlogAuthor')) { - $sql = 'SELECT p.post_author, u.user_nicename as identifier, u.user_email as email, u.display_name FROM ' . $_pref . 'posts p - LEFT JOIN ' . $_pref . 'users u on p.post_author = u.ID - WHERE p.post_type = "post" GROUP BY p.post_author'; + $select = $connection->select() + ->from(['p' => $_pref . 'posts'], [ + 'post_author' + ]) + ->joinLeft( + ['u' => $_pref . 'users'], + 'p.post_author = u.ID', [ + 'identifier' => 'user_nicename', + 'email' => 'user_email', + 'display_name', + ] + ) + ->where('p.post_type = ?', 'post') + ->group('p.post_author'); - $result = $adapter->query($sql)->execute(); + $result = $connection->fetchAll($select); foreach ($result as $data) { /* Prepare author data */ if (!empty($data['display_name'])) {