Skip to content

Commit 876f198

Browse files
committed
Improved import from Mageplaza Blog
1 parent 99e5310 commit 876f198

File tree

4 files changed

+90
-46
lines changed

4 files changed

+90
-46
lines changed

Model/Import/AbstractImport.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ abstract class AbstractImport extends \Magento\Framework\Model\AbstractModel
5858
*/
5959
protected $_importedTagsCount = 0;
6060

61+
/**
62+
* @var integer
63+
*/
64+
protected $_importedCommentsCount = 0;
65+
6166
/**
6267
* @var array
6368
*/
@@ -73,6 +78,11 @@ abstract class AbstractImport extends \Magento\Framework\Model\AbstractModel
7378
*/
7479
protected $_skippedTags = [];
7580

81+
/**
82+
* @var array
83+
*/
84+
protected $_skippedComments = [];
85+
7686
/**
7787
* @var \Magento\Store\Model\StoreManagerInterface
7888
*/
@@ -122,21 +132,23 @@ public function getImportStatistic()
122132
return new \Magento\Framework\DataObject([
123133
'imported_posts_count' => $this->_importedPostsCount,
124134
'imported_categories_count' => $this->_importedCategoriesCount,
135+
'imported_tags_count' => $this->_importedTagsCount,
136+
'imported_comments_count' => $this->_importedCommentsCount,
137+
'imported_count' => $this->_importedPostsCount + $this->_importedCategoriesCount + $this->_importedTagsCount + $this->_importedCommentsCount,
138+
125139
'skipped_posts' => $this->_skippedPosts,
126140
'skipped_categories' => $this->_skippedCategories,
127-
'imported_count' => $this->_importedPostsCount + $this->_importedCategoriesCount + $this->_importedTagsCount + $this->_importedCommentsCount,
128-
'skipped_count' => count($this->_skippedPosts) + count($this->_skippedCategories) + count($this->_skippedTags) + count($this->_skippedComments),
129-
'imported_tags_count' => $this->_importedTagsCount,
130-
'imported_comments_count' => $this->_importedCommentsCount,
131141
'skipped_tags' => $this->_skippedTags,
132-
'skipped_comments' => $this->_skippedComments,
142+
'skipped_comments' => $this->_skippedComments,
143+
'skipped_count' => count($this->_skippedPosts) + count($this->_skippedCategories) + count($this->_skippedTags) + count($this->_skippedComments),
133144
]);
134145
}
135146

136147
/**
137148
* Prepare import data
138149
* @param array $data
139150
* @return $this
151+
* @throws \Exception
140152
*/
141153
public function prepareData($data)
142154
{
@@ -150,12 +162,6 @@ public function prepareData($data)
150162
}
151163
}
152164

153-
// foreach ($data as $field => $value) {
154-
// if (!in_array($field, $this->_requiredFields)) {
155-
// unset($data[$field]);
156-
// }
157-
// }
158-
159165
$this->setData($data);
160166

161167
return $this;

Model/Import/Aw.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ public function execute()
247247
try {
248248
/* saving */
249249
$comment->setData($commentData)->save();
250+
$this->_importedCommentsCount++;
250251
} catch (\Exception $e) {
252+
$this->_skippedComments[] = $commentData['title'];
251253
unset($comment);
252254
}
253255
}

Model/Import/Mageplaza.php

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
namespace Magefan\Blog\Model\Import;
1010

1111
use Magento\Framework\Config\ConfigOptionsListConstants;
12+
1213
/**
13-
* Aw import model
14+
* Mageplaza import model
1415
*/
1516
class Mageplaza extends AbstractImport
1617
{
@@ -51,6 +52,7 @@ public function execute()
5152
$this->getData('dbname')
5253
);
5354
}
55+
5456
if (mysqli_connect_errno()) {
5557
throw new \Exception("Failed connect to magento database", 1);
5658
}
@@ -60,12 +62,7 @@ public function execute()
6062
$config->get('db/table_prefix')
6163
);
6264

63-
64-
65-
66-
67-
$sql = 'SELECT * FROM '.$_pref.'mageplaza_blog_category LIMIT 1';
68-
65+
$sql = 'SELECT * FROM ' . $_pref . 'mageplaza_blog_category LIMIT 1';
6966
try {
7067
$this->_mysqliQuery($sql);
7168
} catch (\Exception $e) {
@@ -74,30 +71,34 @@ public function execute()
7471

7572
$categories = [];
7673
$oldCategories = [];
74+
7775
/* Import categories */
7876
$sql = 'SELECT
7977
t.category_id as old_id,
8078
t.name as title,
8179
t.url_key as identifier,
8280
t.position as position,
81+
t.meta_title as meta_title,
8382
t.meta_keywords as meta_keywords,
8483
t.meta_description as meta_description,
8584
t.description as content,
86-
t.parent_id as path,
85+
t.parent_id as parent_id,
86+
t.position as position,
8787
t.enabled as is_active,
8888
t.store_ids as store_ids
89-
90-
91-
FROM '.$_pref.'mageplaza_blog_category t';
89+
FROM ' . $_pref . 'mageplaza_blog_category t';
9290
$result = $this->_mysqliQuery($sql);
9391
while ($data = mysqli_fetch_assoc($result)) {
9492
/* Prepare category data */
9593

9694
$data['store_ids'] = explode(',', $data['store_ids']);
95+
$data['path'] = 0;
96+
/*
9797
$data['identifier'] = trim(strtolower($data['identifier']));
9898
if (strlen($data['identifier']) == 1) {
9999
$data['identifier'] .= $data['identifier'];
100100
}
101+
*/
101102
$category = $this->_categoryFactory->create();
102103
try {
103104
/* Initial saving */
@@ -112,6 +113,41 @@ public function execute()
112113
}
113114
}
114115

116+
/* Reindexing parent categories */
117+
foreach ($categories as $ct) {
118+
if ($oldParentId = $ct->getData('parent_id')) {
119+
if (isset($oldCategories[$oldParentId])) {
120+
$ct->setPath(
121+
$parentId = $oldCategories[$oldParentId]->getId()
122+
);
123+
}
124+
}
125+
}
126+
127+
for ($i = 0; $i < 4; $i++) {
128+
$changed = false;
129+
foreach ($categories as $ct) {
130+
if ($ct->getPath()) {
131+
$parentId = explode('/', $ct->getPath())[0];
132+
$pt = $categories[$parentId];
133+
if ($pt->getPath()) {
134+
$ct->setPath($pt->getPath() . '/'. $ct->getPath());
135+
$changed = true;
136+
}
137+
}
138+
}
139+
140+
if (!$changed) {
141+
break;
142+
}
143+
}
144+
/* end*/
145+
146+
foreach ($categories as $ct) {
147+
/* Final saving */
148+
$ct->save();
149+
}
150+
115151
/* Import tags */
116152
$tags = [];
117153
$oldTags = [];
@@ -121,16 +157,21 @@ public function execute()
121157
t.tag_id as old_id,
122158
t.name as title,
123159
t.url_key as identifier,
124-
t.description as content,
160+
t.description as content,
161+
t.meta_title as meta_title,
162+
t.meta_description as meta_description,
163+
t.meta_keywords as meta_keywords,
125164
t.enabled as is_active
126-
FROM '.$_pref.'mageplaza_blog_tag t';
165+
FROM ' . $_pref . 'mageplaza_blog_tag t';
127166

128167
$result = $this->_mysqliQuery($sql);
129168
while ($data = mysqli_fetch_assoc($result)) {
130169
/* Prepare tag data */
170+
/*
131171
foreach (['title'] as $key) {
132172
$data[$key] = mb_convert_encoding($data[$key], 'HTML-ENTITIES', 'UTF-8');
133173
}
174+
*/
134175

135176
if (!$data['title']) {
136177
continue;
@@ -159,33 +200,25 @@ public function execute()
159200
}
160201

161202

162-
163-
164203
/* Import posts */
165-
$postCategories = [];
166-
$data['store_ids'] = [];
167-
$sql = 'SELECT * FROM '.$_pref.'mageplaza_blog_post';
204+
$sql = 'SELECT * FROM ' . $_pref . 'mageplaza_blog_post';
168205
$result = $this->_mysqliQuery($sql);
169206
while ($data = mysqli_fetch_assoc($result)) {
170207
/* Find post categories*/
171-
$c_sql = 'SELECT category_id FROM '.$_pref.'mageplaza_blog_post_category WHERE post_id = "'.$data['post_id'].'"';
172-
208+
$postCategories = [];
209+
$c_sql = 'SELECT category_id FROM ' . $_pref . 'mageplaza_blog_post_category WHERE post_id = "'.$data['post_id'].'"';
173210
$c_result = $this->_mysqliQuery($c_sql);
174-
175211
while ($c_data = mysqli_fetch_assoc($c_result)) {
176-
177212
$oldId = $c_data['category_id'];
178213
if (isset($oldCategories[$oldId])) {
179-
180214
$id = $oldCategories[$oldId]->getId();
181215
$postCategories[$id] = $id;
182216
}
183217
}
184218

185-
186-
/* find post tags*/
219+
/* Find post tags*/
187220
$postTags = [];
188-
$t_sql = 'SELECT tag_id FROM '.$_pref.'mageplaza_blog_post_tag WHERE post_id = "'.$data['post_id'].'"';
221+
$t_sql = 'SELECT tag_id FROM ' . $_pref . 'mageplaza_blog_post_tag WHERE post_id = "'.$data['post_id'].'"';
189222

190223
$t_result = $this->_mysqliQuery($t_sql);
191224

@@ -199,30 +232,30 @@ public function execute()
199232
}
200233
}
201234

202-
203-
235+
/* Find store ids */
204236
$data['store_ids'] = explode(',', $data['store_ids']);
205237

238+
206239
/* Prepare post data */
207240
$data = [
208241
'old_id' => $data['post_id'],
209242
'store_ids' => $data['store_ids'],
210243
'title' => $data['name'],
244+
'meta_title' => $data['meta_title'],
211245
'meta_keywords' => $data['meta_keywords'],
212246
'meta_description' => $data['meta_description'],
213247
'identifier' => $data['url_key'],
214-
'content_heading' => $data['name'],
248+
'content_heading' => '',
215249
'content' => $data['post_content'],
216250
'short_content' => $data['short_description'],
217251
'creation_time' => strtotime($data['created_at']),
218252
'update_time' => strtotime($data['updated_at']),
219253
'publish_time' => strtotime($data['publish_date']),
220254
'is_active' => $data['enabled'],
221255
'categories' => $postCategories,
256+
'tags' => $postTags,
222257
'featured_img' => !empty($data['image']) ? 'magefan_blog/' . $data['image'] : '',
223-
'media_gallery' => !empty($data['image']) ? 'magefan_blog/' . $data['image'] : '',
224-
'tags' => $postTags
225-
258+
'author_id' => '',
226259
];
227260

228261

@@ -233,7 +266,7 @@ public function execute()
233266

234267

235268
/* find post comment s*/
236-
$sql = 'SELECT * FROM '.$_pref.'mageplaza_blog_comment WHERE `post_id` = ' . $post->getOldId();
269+
$sql = 'SELECT * FROM ' . $_pref . 'mageplaza_blog_comment WHERE `post_id` = ' . $post->getOldId();
237270
$resultComments = $this->_mysqliQuery($sql);
238271

239272
while ($comments = mysqli_fetch_assoc($resultComments)) {
@@ -249,10 +282,11 @@ public function execute()
249282
'text' => $comments['content'],
250283
'creation_time' => $comments['created_at'],
251284
];
252-
285+
/*
253286
foreach (['text'] as $key) {
254287
$commentData[$key] = mb_convert_encoding($commentData[$key], 'HTML-ENTITIES', 'UTF-8');
255288
}
289+
*/
256290

257291
if (!$commentData['text']) {
258292
continue;
@@ -265,7 +299,7 @@ public function execute()
265299
$comment->setData($commentData)->save();
266300
$this->_importedCommentsCount++;
267301
} catch (\Exception $e) {
268-
$this->_skippedComments[] = $data['title'];
302+
$this->_skippedComments[] = $commentData['title'];
269303
unset($comment);
270304
}
271305
}

Model/Import/Wordpress.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,10 @@ public function execute()
285285
try {
286286
/* Initial saving */
287287
$comment->setData($commentData)->save();
288+
$this->_importedCommentsCount++;
288289
$commentParents[$comments["comment_ID"]] = $comment->getCommentId();
289290
} catch (\Exception $e) {
291+
$this->_skippedComments[] = $commentData['title'];
290292
unset($comment);
291293
}
292294
}

0 commit comments

Comments
 (0)