1 | <?php |
---|
2 | /** |
---|
3 | * @brief importExport, a plugin for Dotclear 2 |
---|
4 | * |
---|
5 | * @package Dotclear |
---|
6 | * @subpackage Plugins |
---|
7 | * |
---|
8 | * @copyright Olivier Meunier & Association Dotclear |
---|
9 | * @copyright GPL-2.0-only |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('DC_RC_PATH')) {return;} |
---|
13 | |
---|
14 | class dcImportWP extends dcIeModule |
---|
15 | { |
---|
16 | protected $con; |
---|
17 | protected $prefix; |
---|
18 | protected $blog_id; |
---|
19 | |
---|
20 | protected $action = null; |
---|
21 | protected $step = 1; |
---|
22 | |
---|
23 | protected $post_offset = 0; |
---|
24 | protected $post_limit = 20; |
---|
25 | protected $post_count = 0; |
---|
26 | |
---|
27 | protected $has_table = array(); |
---|
28 | |
---|
29 | protected $vars; |
---|
30 | protected $base_vars = array( |
---|
31 | 'db_host' => '', |
---|
32 | 'db_name' => '', |
---|
33 | 'db_user' => '', |
---|
34 | 'db_pwd' => '', |
---|
35 | 'db_prefix' => 'wp_', |
---|
36 | 'ignore_first_cat' => 1, |
---|
37 | 'cat_import' => 1, |
---|
38 | 'cat_as_tags' => '', |
---|
39 | 'cat_tags_prefix' => 'cat: ', |
---|
40 | 'post_limit' => 20, |
---|
41 | 'post_formater' => 'xhtml', |
---|
42 | 'comment_formater' => 'xhtml', |
---|
43 | 'user_ids' => array(), |
---|
44 | 'cat_ids' => array(), |
---|
45 | 'permalink_template' => 'p=%post_id%', |
---|
46 | 'permalink_tags' => array( |
---|
47 | '%year%', |
---|
48 | '%monthnum%', |
---|
49 | '%day%', |
---|
50 | '%hour%', |
---|
51 | '%minute%', |
---|
52 | '%second%', |
---|
53 | '%postname%', |
---|
54 | '%post_id%', |
---|
55 | '%category%', |
---|
56 | '%author%' |
---|
57 | ) |
---|
58 | ); |
---|
59 | protected $formaters; |
---|
60 | |
---|
61 | protected function setInfo() |
---|
62 | { |
---|
63 | $this->type = 'import'; |
---|
64 | $this->name = __('WordPress import'); |
---|
65 | $this->description = __('Import a WordPress installation into your current blog.'); |
---|
66 | } |
---|
67 | |
---|
68 | public function init() |
---|
69 | { |
---|
70 | $this->con = &$this->core->con; |
---|
71 | $this->prefix = $this->core->prefix; |
---|
72 | $this->blog_id = $this->core->blog->id; |
---|
73 | |
---|
74 | if (!isset($_SESSION['wp_import_vars'])) { |
---|
75 | $_SESSION['wp_import_vars'] = $this->base_vars; |
---|
76 | } |
---|
77 | $this->vars = &$_SESSION['wp_import_vars']; |
---|
78 | |
---|
79 | if ($this->vars['post_limit'] > 0) { |
---|
80 | $this->post_limit = $this->vars['post_limit']; |
---|
81 | } |
---|
82 | |
---|
83 | $this->formaters = dcAdminCombos::getFormatersCombo(); |
---|
84 | } |
---|
85 | |
---|
86 | public function resetVars() |
---|
87 | { |
---|
88 | $this->vars = $this->base_vars; |
---|
89 | unset($_SESSION['wp_import_vars']); |
---|
90 | } |
---|
91 | |
---|
92 | public function process($do) |
---|
93 | { |
---|
94 | $this->action = $do; |
---|
95 | } |
---|
96 | |
---|
97 | # We handle process in another way to always display something to |
---|
98 | # user |
---|
99 | protected function guiprocess($do) |
---|
100 | { |
---|
101 | switch ($do) { |
---|
102 | case 'step1': |
---|
103 | $this->vars['db_host'] = $_POST['db_host']; |
---|
104 | $this->vars['db_name'] = $_POST['db_name']; |
---|
105 | $this->vars['db_user'] = $_POST['db_user']; |
---|
106 | $this->vars['db_pwd'] = $_POST['db_pwd']; |
---|
107 | $this->vars['db_prefix'] = $_POST['db_prefix']; |
---|
108 | $this->vars['ignore_first_cat'] = isset($_POST['ignore_first_cat']); |
---|
109 | $this->vars['cat_import'] = isset($_POST['cat_import']); |
---|
110 | $this->vars['cat_as_tags'] = isset($_POST['cat_as_tags']); |
---|
111 | $this->vars['cat_tags_prefix'] = $_POST['cat_tags_prefix']; |
---|
112 | $this->vars['post_limit'] = abs((integer) $_POST['post_limit']) > 0 ? $_POST['post_limit'] : 0; |
---|
113 | $this->vars['post_formater'] = isset($this->formaters[$_POST['post_formater']]) ? $_POST['post_formater'] : 'xhtml'; |
---|
114 | $this->vars['comment_formater'] = isset($this->formaters[$_POST['comment_formater']]) ? $_POST['comment_formater'] : 'xhtml'; |
---|
115 | $db = $this->db(); |
---|
116 | $db->close(); |
---|
117 | $this->step = 2; |
---|
118 | echo $this->progressBar(1); |
---|
119 | break; |
---|
120 | case 'step2': |
---|
121 | $this->step = 2; |
---|
122 | $this->importUsers(); |
---|
123 | $this->step = 3; |
---|
124 | echo $this->progressBar(3); |
---|
125 | break; |
---|
126 | case 'step3': |
---|
127 | $this->step = 3; |
---|
128 | $this->importCategories(); |
---|
129 | if ($this->core->plugins->moduleExists('blogroll')) { |
---|
130 | $this->step = 4; |
---|
131 | echo $this->progressBar(5); |
---|
132 | } else { |
---|
133 | $this->step = 5; |
---|
134 | echo $this->progressBar(7); |
---|
135 | } |
---|
136 | break; |
---|
137 | case 'step4': |
---|
138 | $this->step = 4; |
---|
139 | $this->importLinks(); |
---|
140 | $this->step = 5; |
---|
141 | echo $this->progressBar(7); |
---|
142 | break; |
---|
143 | case 'step5': |
---|
144 | $this->step = 5; |
---|
145 | $this->post_offset = !empty($_REQUEST['offset']) ? abs((integer) $_REQUEST['offset']) : 0; |
---|
146 | if ($this->importPosts($percent) === -1) { |
---|
147 | http::redirect($this->getURL() . '&do=ok'); |
---|
148 | } else { |
---|
149 | echo $this->progressBar(ceil($percent * 0.93) + 7); |
---|
150 | } |
---|
151 | break; |
---|
152 | case 'ok': |
---|
153 | $this->resetVars(); |
---|
154 | $this->core->blog->triggerBlog(); |
---|
155 | $this->step = 6; |
---|
156 | echo $this->progressBar(100); |
---|
157 | break; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | public function gui() |
---|
162 | { |
---|
163 | try { |
---|
164 | $this->guiprocess($this->action); |
---|
165 | } catch (Exception $e) { |
---|
166 | $this->error($e); |
---|
167 | } |
---|
168 | |
---|
169 | switch ($this->step) { |
---|
170 | case 1: |
---|
171 | echo |
---|
172 | '<p>' . sprintf(__('This will import your WordPress content as new content in the current blog: %s.'), |
---|
173 | '<strong>' . html::escapeHTML($this->core->blog->name) . '</strong>') . '</p>' . |
---|
174 | '<p class="warning">' . __('Please note that this process ' . |
---|
175 | 'will empty your categories, blogroll, entries and comments on the current blog.') . '</p>'; |
---|
176 | |
---|
177 | printf($this->imForm(1, __('General information'), __('Import my blog now')), |
---|
178 | '<p>' . __('We first need some information about your old WordPress installation.') . '</p>' . |
---|
179 | '<p><label for="db_host">' . __('Database Host Name:') . '</label> ' . |
---|
180 | form::field('db_host', 30, 255, html::escapeHTML($this->vars['db_host'])) . '</p>' . |
---|
181 | '<p><label for="db_name">' . __('Database Name:', html::escapeHTML($this->vars['db_name'])) . '</label> ' . |
---|
182 | form::field('db_name', 30, 255, html::escapeHTML($this->vars['db_name'])) . '</p>' . |
---|
183 | '<p><label for="db_user">' . __('Database User Name:') . '</label> ' . |
---|
184 | form::field('db_user', 30, 255, html::escapeHTML($this->vars['db_user'])) . '</p>' . |
---|
185 | '<p><label for="db_pwd">' . __('Database Password:') . '</label> ' . |
---|
186 | form::password('db_pwd', 30, 255) . '</p>' . |
---|
187 | '<p><label for="db_prefix">' . __('Database Tables Prefix:') . '</label> ' . |
---|
188 | form::field('db_prefix', 30, 255, html::escapeHTML($this->vars['db_prefix'])) . '</p>' . |
---|
189 | |
---|
190 | '<h3 class="vertical-separator">' . __('Entries import options') . '</h3>' . |
---|
191 | '<div class="two-cols">' . |
---|
192 | |
---|
193 | '<div class="col">' . |
---|
194 | '<p>' . __('WordPress and Dotclear\'s handling of categories are quite different. ' . |
---|
195 | 'You can assign several categories to a single post in WordPress. In the Dotclear world, ' . |
---|
196 | 'we see it more like "One category, several tags." Therefore Dotclear can only import one ' . |
---|
197 | 'category per post and will chose the lowest numbered one. If you want to keep a trace of ' . |
---|
198 | 'every category, you can import them as tags, with an optional prefix.') . '</p>' . |
---|
199 | '<p>' . __('On the other hand, in WordPress, a post can not be uncategorized, and a ' . |
---|
200 | 'default installation has a first category labelised <i>"Uncategorized"</i>.' . |
---|
201 | 'If you did not change that category, you can just ignore it while ' . |
---|
202 | 'importing your blog, as Dotclear allows you to actually keep your posts ' . |
---|
203 | 'uncategorized.') . '</p>' . |
---|
204 | '</div>' . |
---|
205 | |
---|
206 | '<div class="col">' . |
---|
207 | '<p><label for="ignore_first_cat" class="classic">' . form::checkbox('ignore_first_cat', 1, $this->vars['ignore_first_cat']) . ' ' . |
---|
208 | __('Ignore the first category:') . '</label></p>' . |
---|
209 | '<p><label for="cat_import" class="classic">' . form::checkbox('cat_import', 1, $this->vars['cat_import']) . ' ' . |
---|
210 | __('Import lowest numbered category on posts:') . '</label></p>' . |
---|
211 | '<p><label for="cat_as_tags" class="classic">' . form::checkbox('cat_as_tags', 1, $this->vars['cat_as_tags']) . ' ' . |
---|
212 | __('Import all categories as tags:') . '</label></p>' . |
---|
213 | '<p><label for="cat_tags_prefix">' . __('Prefix such tags with:') . '</label> ' . |
---|
214 | form::field('cat_tags_prefix', 10, 20, html::escapeHTML($this->vars['cat_tags_prefix'])) . '</p>' . |
---|
215 | '<p><label for="post_limit">' . __('Number of entries to import at once:') . '</label> ' . |
---|
216 | form::number('post_limit', 0, 999, html::escapeHTML($this->vars['post_limit'])) . '</p>' . |
---|
217 | '</div>' . |
---|
218 | |
---|
219 | '</div>' . |
---|
220 | |
---|
221 | '<h3 class="clear vertical-separator">' . __('Content filters') . '</h3>' . |
---|
222 | '<p>' . __('You may want to process your post and/or comment content with the following filters.') . '</p>' . |
---|
223 | '<p><label for="post_formater">' . __('Post content formatter:') . '</label> ' . |
---|
224 | form::combo('post_formater', $this->formaters, $this->vars['post_formater']) . '</p>' . |
---|
225 | '<p><label for="comment_formater">' . __('Comment content formatter:') . '</label> ' |
---|
226 | . form::combo('comment_formater', $this->formaters, $this->vars['comment_formater']) . '</p>' |
---|
227 | ); |
---|
228 | break; |
---|
229 | case 2: |
---|
230 | printf($this->imForm(2, __('Importing users')), |
---|
231 | $this->autoSubmit() |
---|
232 | ); |
---|
233 | break; |
---|
234 | case 3: |
---|
235 | printf($this->imForm(3, __('Importing categories')), |
---|
236 | $this->autoSubmit() |
---|
237 | ); |
---|
238 | break; |
---|
239 | case 4: |
---|
240 | printf($this->imForm(4, __('Importing blogroll')), |
---|
241 | $this->autoSubmit() |
---|
242 | ); |
---|
243 | break; |
---|
244 | case 5: |
---|
245 | $t = sprintf(__('Importing entries from %d to %d / %d'), $this->post_offset, |
---|
246 | min(array($this->post_offset + $this->post_limit, $this->post_count)), $this->post_count); |
---|
247 | printf($this->imForm(5, $t), |
---|
248 | form::hidden(array('offset'), $this->post_offset) . |
---|
249 | $this->autoSubmit() |
---|
250 | ); |
---|
251 | break; |
---|
252 | case 6: |
---|
253 | echo |
---|
254 | '<p class="message">' . __('Every newly imported user has received a random password ' . |
---|
255 | 'and will need to ask for a new one by following the "I forgot my password" link on the login page ' . |
---|
256 | '(Their registered email address has to be valid.)') . '</p>' . |
---|
257 | $this->congratMessage(); |
---|
258 | break; |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | # Simple form for step by step process |
---|
263 | protected function imForm($step, $legend, $submit_value = null) |
---|
264 | { |
---|
265 | if (!$submit_value) { |
---|
266 | $submit_value = __('next step') . ' >'; |
---|
267 | } |
---|
268 | |
---|
269 | return |
---|
270 | '<form action="' . $this->getURL(true) . '" method="post">' . |
---|
271 | '<h3 class="vertical-separator">' . $legend . '</h3>' . |
---|
272 | '<div>' . $this->core->formNonce() . |
---|
273 | form::hidden(array('do'), 'step' . $step) . |
---|
274 | '%s' . '</div>' . |
---|
275 | '<p><input type="submit" value="' . $submit_value . '" /></p>' . |
---|
276 | '<p class="form-note info">' . __('Depending on the size of your blog, it could take a few minutes.') . '</p>' . |
---|
277 | '</form>'; |
---|
278 | } |
---|
279 | |
---|
280 | # Error display |
---|
281 | protected function error($e) |
---|
282 | { |
---|
283 | echo '<div class="error"><strong>' . __('Errors:') . '</strong>' . |
---|
284 | '<p>' . $e->getMessage() . '</p></div>'; |
---|
285 | } |
---|
286 | |
---|
287 | # Database init |
---|
288 | protected function db() |
---|
289 | { |
---|
290 | $db = dbLayer::init('mysql', $this->vars['db_host'], $this->vars['db_name'], $this->vars['db_user'], $this->vars['db_pwd']); |
---|
291 | |
---|
292 | $rs = $db->select("SHOW TABLES LIKE '" . $this->vars['db_prefix'] . "%'"); |
---|
293 | if ($rs->isEmpty()) { |
---|
294 | throw new Exception(__('WordPress tables not found')); |
---|
295 | } |
---|
296 | |
---|
297 | while ($rs->fetch()) { |
---|
298 | $this->has_table[$rs->f(0)] = true; |
---|
299 | } |
---|
300 | |
---|
301 | # Set this to read data as they were written |
---|
302 | try { |
---|
303 | $db->execute('SET NAMES DEFAULT'); |
---|
304 | } catch (Exception $e) {} |
---|
305 | |
---|
306 | $db->execute('SET CHARACTER SET DEFAULT'); |
---|
307 | $db->execute("SET COLLATION_CONNECTION = DEFAULT"); |
---|
308 | $db->execute("SET COLLATION_SERVER = DEFAULT"); |
---|
309 | $db->execute("SET CHARACTER_SET_SERVER = DEFAULT"); |
---|
310 | $db->execute("SET CHARACTER_SET_DATABASE = DEFAULT"); |
---|
311 | |
---|
312 | $this->post_count = $db->select( |
---|
313 | 'SELECT COUNT(ID) FROM ' . $this->vars['db_prefix'] . 'posts ' . |
---|
314 | 'WHERE post_type = \'post\' OR post_type = \'page\'' |
---|
315 | )->f(0); |
---|
316 | |
---|
317 | return $db; |
---|
318 | } |
---|
319 | |
---|
320 | protected function cleanStr($str) |
---|
321 | { |
---|
322 | return text::cleanUTF8(@text::toUTF8($str)); |
---|
323 | } |
---|
324 | |
---|
325 | # Users import |
---|
326 | protected function importUsers() |
---|
327 | { |
---|
328 | $db = $this->db(); |
---|
329 | $prefix = $this->vars['db_prefix']; |
---|
330 | $rs = $db->select('SELECT * FROM ' . $prefix . 'users'); |
---|
331 | |
---|
332 | try |
---|
333 | { |
---|
334 | $this->con->begin(); |
---|
335 | |
---|
336 | while ($rs->fetch()) { |
---|
337 | $user_login = preg_replace('/[^A-Za-z0-9@._-]/', '-', $rs->user_login); |
---|
338 | $this->vars['user_ids'][$rs->ID] = $user_login; |
---|
339 | if (!$this->core->userExists($user_login)) { |
---|
340 | $cur = $this->con->openCursor($this->prefix . 'user'); |
---|
341 | $cur->user_id = $user_login; |
---|
342 | $cur->user_pwd = crypt::createPassword(); |
---|
343 | $cur->user_displayname = $rs->user_nicename; |
---|
344 | $cur->user_email = $rs->user_email; |
---|
345 | $cur->user_url = $rs->user_url; |
---|
346 | $cur->user_creadt = $rs->user_registered; |
---|
347 | $cur->user_lang = $this->core->blog->settings->system->lang; |
---|
348 | $cur->user_tz = $this->core->blog->settings->system->blog_timezone; |
---|
349 | $permissions = array(); |
---|
350 | |
---|
351 | $rs_meta = $db->select('SELECT * FROM ' . $prefix . 'usermeta WHERE user_id = ' . $rs->ID); |
---|
352 | while ($rs_meta->fetch()) { |
---|
353 | switch ($rs_meta->meta_key) { |
---|
354 | case 'first_name': |
---|
355 | $cur->user_firstname = $this->cleanStr($rs_meta->meta_value); |
---|
356 | break; |
---|
357 | case 'last_name': |
---|
358 | $cur->user_name = $this->cleanStr($rs_meta->meta_value); |
---|
359 | break; |
---|
360 | case 'description': |
---|
361 | $cur->user_desc = $this->cleanStr($rs_meta->meta_value); |
---|
362 | break; |
---|
363 | case 'rich_editing': |
---|
364 | $cur->user_options = new ArrayObject(array( |
---|
365 | 'enable_wysiwyg' => $rs_meta->meta_value == 'true' ? true : false |
---|
366 | )); |
---|
367 | break; |
---|
368 | case 'wp_user_level': |
---|
369 | switch ($rs_meta->meta_value) { |
---|
370 | case '0': # Subscriber |
---|
371 | $cur->user_status = 0; |
---|
372 | break; |
---|
373 | case '1': # Contributor |
---|
374 | $permissions['usage'] = true; |
---|
375 | $permissions['publish'] = true; |
---|
376 | $permissions['delete'] = true; |
---|
377 | break; |
---|
378 | case '2': # Author |
---|
379 | case '3': |
---|
380 | case '4': |
---|
381 | $permissions['contentadmin'] = true; |
---|
382 | $permissions['media'] = true; |
---|
383 | break; |
---|
384 | case '5': # Editor |
---|
385 | case '6': |
---|
386 | case '7': |
---|
387 | $permissions['contentadmin'] = true; |
---|
388 | $permissions['categories'] = true; |
---|
389 | $permissions['media_admin'] = true; |
---|
390 | $permissions['pages'] = true; |
---|
391 | $permissions['blogroll'] = true; |
---|
392 | break; |
---|
393 | case '8': # Administrator |
---|
394 | case '9': |
---|
395 | case '10': |
---|
396 | $permissions['admin'] = true; |
---|
397 | break; |
---|
398 | } |
---|
399 | break; |
---|
400 | } |
---|
401 | } |
---|
402 | $this->core->addUser($cur); |
---|
403 | $this->core->setUserBlogPermissions( |
---|
404 | $cur->user_id, |
---|
405 | $this->blog_id, |
---|
406 | $permissions |
---|
407 | ); |
---|
408 | } |
---|
409 | } |
---|
410 | $this->con->commit(); |
---|
411 | $db->close(); |
---|
412 | } catch (Exception $e) { |
---|
413 | $this->con->rollback(); |
---|
414 | $db->close(); |
---|
415 | throw $e; |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | # Categories import |
---|
420 | protected function importCategories() |
---|
421 | { |
---|
422 | $db = $this->db(); |
---|
423 | $prefix = $this->vars['db_prefix']; |
---|
424 | $rs = $db->select( |
---|
425 | 'SELECT * FROM ' . $prefix . 'terms AS t, ' . $prefix . 'term_taxonomy AS x ' . |
---|
426 | 'WHERE x.taxonomy = \'category\' ' . |
---|
427 | 'AND t.term_id = x.term_id ' . |
---|
428 | ($this->vars['ignore_first_cat'] ? 'AND t.term_id <> 1 ' : '') . |
---|
429 | 'ORDER BY t.term_id ASC' |
---|
430 | ); |
---|
431 | |
---|
432 | try |
---|
433 | { |
---|
434 | $this->con->execute( |
---|
435 | 'DELETE FROM ' . $this->prefix . 'category ' . |
---|
436 | "WHERE blog_id = '" . $this->con->escape($this->blog_id) . "' " |
---|
437 | ); |
---|
438 | |
---|
439 | $ord = 2; |
---|
440 | while ($rs->fetch()) { |
---|
441 | $cur = $this->con->openCursor($this->prefix . 'category'); |
---|
442 | $cur->blog_id = $this->blog_id; |
---|
443 | $cur->cat_title = $this->cleanStr($rs->name); |
---|
444 | $cur->cat_desc = $this->cleanStr($rs->description); |
---|
445 | $cur->cat_url = $this->cleanStr($rs->slug); |
---|
446 | $cur->cat_lft = $ord++; |
---|
447 | $cur->cat_rgt = $ord++; |
---|
448 | |
---|
449 | $cur->cat_id = $this->con->select( |
---|
450 | 'SELECT MAX(cat_id) FROM ' . $this->prefix . 'category' |
---|
451 | )->f(0) + 1; |
---|
452 | $this->vars['cat_ids'][$rs->term_id] = $cur->cat_id; |
---|
453 | $cur->insert(); |
---|
454 | } |
---|
455 | |
---|
456 | $db->close(); |
---|
457 | } catch (Exception $e) { |
---|
458 | $db->close(); |
---|
459 | throw $e; |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | # Blogroll import |
---|
464 | protected function importLinks() |
---|
465 | { |
---|
466 | $db = $this->db(); |
---|
467 | $prefix = $this->vars['db_prefix']; |
---|
468 | $rs = $db->select('SELECT * FROM ' . $prefix . 'links ORDER BY link_id ASC'); |
---|
469 | |
---|
470 | try |
---|
471 | { |
---|
472 | $this->con->execute( |
---|
473 | 'DELETE FROM ' . $this->prefix . 'link ' . |
---|
474 | "WHERE blog_id = '" . $this->con->escape($this->blog_id) . "' " |
---|
475 | ); |
---|
476 | |
---|
477 | while ($rs->fetch()) { |
---|
478 | $cur = $this->con->openCursor($this->prefix . 'link'); |
---|
479 | $cur->blog_id = $this->blog_id; |
---|
480 | $cur->link_href = $this->cleanStr($rs->link_url); |
---|
481 | $cur->link_title = $this->cleanStr($rs->link_name); |
---|
482 | $cur->link_desc = $this->cleanStr($rs->link_description); |
---|
483 | $cur->link_xfn = $this->cleanStr($rs->link_rel); |
---|
484 | |
---|
485 | $cur->link_id = $this->con->select( |
---|
486 | 'SELECT MAX(link_id) FROM ' . $this->prefix . 'link' |
---|
487 | )->f(0) + 1; |
---|
488 | $cur->insert(); |
---|
489 | } |
---|
490 | |
---|
491 | $db->close(); |
---|
492 | } catch (Exception $e) { |
---|
493 | $db->close(); |
---|
494 | throw $e; |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | # Entries import |
---|
499 | protected function importPosts(&$percent) |
---|
500 | { |
---|
501 | $db = $this->db(); |
---|
502 | $prefix = $this->vars['db_prefix']; |
---|
503 | |
---|
504 | $plink = $db->select( |
---|
505 | 'SELECT option_value FROM ' . $prefix . 'options ' . |
---|
506 | "WHERE option_name = 'permalink_structure'" |
---|
507 | )->option_value; |
---|
508 | if ($plink) { |
---|
509 | $this->vars['permalink_template'] = substr($plink, 1); |
---|
510 | } |
---|
511 | |
---|
512 | $rs = $db->select( |
---|
513 | 'SELECT * FROM ' . $prefix . 'posts ' . |
---|
514 | 'WHERE post_type = \'post\' OR post_type = \'page\' ' . |
---|
515 | 'ORDER BY ID ASC ' . |
---|
516 | $db->limit($this->post_offset, $this->post_limit) |
---|
517 | ); |
---|
518 | |
---|
519 | try |
---|
520 | { |
---|
521 | if ($this->post_offset == 0) { |
---|
522 | $this->con->execute( |
---|
523 | 'DELETE FROM ' . $this->prefix . 'post ' . |
---|
524 | "WHERE blog_id = '" . $this->con->escape($this->blog_id) . "' " |
---|
525 | ); |
---|
526 | } |
---|
527 | |
---|
528 | while ($rs->fetch()) { |
---|
529 | $this->importPost($rs, $db); |
---|
530 | } |
---|
531 | |
---|
532 | $db->close(); |
---|
533 | } catch (Exception $e) { |
---|
534 | $db->close(); |
---|
535 | throw $e; |
---|
536 | } |
---|
537 | |
---|
538 | if ($rs->count() < $this->post_limit) { |
---|
539 | return -1; |
---|
540 | } else { |
---|
541 | $this->post_offset += $this->post_limit; |
---|
542 | } |
---|
543 | |
---|
544 | if ($this->post_offset > $this->post_count) { |
---|
545 | $percent = 100; |
---|
546 | } else { |
---|
547 | $percent = $this->post_offset * 100 / $this->post_count; |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | protected function importPost($rs, $db) |
---|
552 | { |
---|
553 | $post_date = !@strtotime($rs->post_date) ? '1970-01-01 00:00' : $rs->post_date; |
---|
554 | if (!isset($this->vars['user_ids'][$rs->post_author])) { |
---|
555 | $user_id = $this->core->auth->userID(); |
---|
556 | } else { |
---|
557 | $user_id = $this->vars['user_ids'][$rs->post_author]; |
---|
558 | } |
---|
559 | |
---|
560 | $cur = $this->con->openCursor($this->prefix . 'post'); |
---|
561 | $cur->blog_id = $this->blog_id; |
---|
562 | $cur->user_id = $user_id; |
---|
563 | $cur->post_dt = $post_date; |
---|
564 | $cur->post_creadt = $post_date; |
---|
565 | $cur->post_upddt = $rs->post_modified; |
---|
566 | $cur->post_title = $this->cleanStr($rs->post_title); |
---|
567 | |
---|
568 | if (!$cur->post_title) { |
---|
569 | $cur->post_title = 'No title'; |
---|
570 | } |
---|
571 | |
---|
572 | if ($this->vars['cat_import'] || $this->vars['cat_as_tags']) { |
---|
573 | $old_cat_ids = $db->select( |
---|
574 | 'SELECT * FROM ' . $this->vars['db_prefix'] . 'terms AS t, ' . |
---|
575 | $this->vars['db_prefix'] . 'term_taxonomy AS x, ' . |
---|
576 | $this->vars['db_prefix'] . 'term_relationships AS r ' . |
---|
577 | 'WHERE t.term_id = x.term_id ' . |
---|
578 | ($this->vars['ignore_first_cat'] ? 'AND t.term_id <> 1 ' : '') . |
---|
579 | 'AND x.taxonomy = \'category\' ' . |
---|
580 | 'AND t.term_id = r.term_taxonomy_id ' . |
---|
581 | 'AND r.object_id =' . $rs->ID . |
---|
582 | ' ORDER BY t.term_id ASC ' |
---|
583 | ); |
---|
584 | if (!$old_cat_ids->isEmpty() && $this->vars['cat_import']) { |
---|
585 | $cur->cat_id = $this->vars['cat_ids'][(integer) $old_cat_ids->term_id]; |
---|
586 | } |
---|
587 | } |
---|
588 | |
---|
589 | $permalink_infos = array( |
---|
590 | date('Y', strtotime($cur->post_dt)), |
---|
591 | date('m', strtotime($cur->post_dt)), |
---|
592 | date('d', strtotime($cur->post_dt)), |
---|
593 | date('H', strtotime($cur->post_dt)), |
---|
594 | date('i', strtotime($cur->post_dt)), |
---|
595 | date('s', strtotime($cur->post_dt)), |
---|
596 | $rs->post_name, |
---|
597 | $rs->ID, |
---|
598 | $cur->cat_id, |
---|
599 | $cur->user_id |
---|
600 | ); |
---|
601 | $cur->post_url = str_replace( |
---|
602 | $this->vars['permalink_tags'], |
---|
603 | $permalink_infos, |
---|
604 | $rs->post_type == 'post' ? $this->vars['permalink_template'] : '%postname%' |
---|
605 | ); |
---|
606 | $cur->post_url = substr($cur->post_url, 0, 255); |
---|
607 | |
---|
608 | if (!$cur->post_url) { |
---|
609 | $cur->post_url = $rs->ID; |
---|
610 | } |
---|
611 | |
---|
612 | $cur->post_format = $this->vars['post_formater']; |
---|
613 | $_post_content = explode('<!--more-->', $rs->post_content, 2); |
---|
614 | if (count($_post_content) == 1) { |
---|
615 | $cur->post_excerpt = null; |
---|
616 | $cur->post_content = $this->cleanStr(array_shift($_post_content)); |
---|
617 | } else { |
---|
618 | $cur->post_excerpt = $this->cleanStr(array_shift($_post_content)); |
---|
619 | $cur->post_content = $this->cleanStr(array_shift($_post_content)); |
---|
620 | } |
---|
621 | |
---|
622 | $cur->post_content_xhtml = $this->core->callFormater($this->vars['post_formater'], $cur->post_content); |
---|
623 | $cur->post_excerpt_xhtml = $this->core->callFormater($this->vars['post_formater'], $cur->post_excerpt); |
---|
624 | |
---|
625 | switch ($rs->post_status) { |
---|
626 | case 'publish': |
---|
627 | $cur->post_status = 1; |
---|
628 | break; |
---|
629 | case 'draft': |
---|
630 | $cur->post_status = 0; |
---|
631 | break; |
---|
632 | case 'pending': |
---|
633 | $cur->post_status = -2; |
---|
634 | break; |
---|
635 | default: |
---|
636 | $cur->post_status = -2; |
---|
637 | } |
---|
638 | $cur->post_type = $rs->post_type; |
---|
639 | $cur->post_password = $rs->post_password ?: null; |
---|
640 | $cur->post_open_comment = $rs->comment_status == 'open' ? 1 : 0; |
---|
641 | $cur->post_open_tb = $rs->ping_status == 'open' ? 1 : 0; |
---|
642 | |
---|
643 | $cur->post_words = implode(' ', text::splitWords( |
---|
644 | $cur->post_title . ' ' . |
---|
645 | $cur->post_excerpt_xhtml . ' ' . |
---|
646 | $cur->post_content_xhtml |
---|
647 | )); |
---|
648 | |
---|
649 | $cur->post_id = $this->con->select( |
---|
650 | 'SELECT MAX(post_id) FROM ' . $this->prefix . 'post' |
---|
651 | )->f(0) + 1; |
---|
652 | |
---|
653 | $cur->post_url = $this->core->blog->getPostURL($cur->post_url, $cur->post_dt, $cur->post_title, $cur->post_id); |
---|
654 | |
---|
655 | $cur->insert(); |
---|
656 | $this->importComments($rs->ID, $cur->post_id, $db); |
---|
657 | $this->importPings($rs->ID, $cur->post_id, $db); |
---|
658 | |
---|
659 | # Create tags |
---|
660 | $this->importTags($rs->ID, $cur->post_id, $db); |
---|
661 | |
---|
662 | if (isset($old_cat_ids)) { |
---|
663 | if (!$old_cat_ids->isEmpty() && $this->vars['cat_as_tags']) { |
---|
664 | $old_cat_ids->moveStart(); |
---|
665 | while ($old_cat_ids->fetch()) { |
---|
666 | $this->core->meta->setPostMeta($cur->post_id, 'tag', $this->cleanStr($this->vars['cat_tags_prefix'] . $old_cat_ids->name)); |
---|
667 | } |
---|
668 | } |
---|
669 | } |
---|
670 | } |
---|
671 | |
---|
672 | # Comments import |
---|
673 | protected function importComments($post_id, $new_post_id, $db) |
---|
674 | { |
---|
675 | $count_c = $count_t = 0; |
---|
676 | |
---|
677 | $rs = $db->select( |
---|
678 | 'SELECT * FROM ' . $this->vars['db_prefix'] . 'comments ' . |
---|
679 | 'WHERE comment_post_ID = ' . (integer) $post_id . ' ' |
---|
680 | ); |
---|
681 | |
---|
682 | while ($rs->fetch()) { |
---|
683 | $cur = $this->con->openCursor($this->prefix . 'comment'); |
---|
684 | $cur->post_id = (integer) $new_post_id; |
---|
685 | $cur->comment_author = $this->cleanStr($rs->comment_author); |
---|
686 | $cur->comment_status = (integer) $rs->comment_approved; |
---|
687 | $cur->comment_dt = $rs->comment_date; |
---|
688 | $cur->comment_email = $this->cleanStr($rs->comment_author_email); |
---|
689 | $cur->comment_content = $this->core->callFormater($this->vars['comment_formater'], $this->cleanStr($rs->comment_content)); |
---|
690 | $cur->comment_ip = $rs->comment_author_IP; |
---|
691 | $cur->comment_trackback = $rs->comment_type == 'trackback' ? 1 : 0; |
---|
692 | $cur->comment_site = substr($this->cleanStr($rs->comment_author_url), 0, 255); |
---|
693 | if ($cur->comment_site == '') { |
---|
694 | $cur->comment_site = null; |
---|
695 | } |
---|
696 | |
---|
697 | if ($rs->comment_approved == 'spam') { |
---|
698 | $cur->comment_status = -2; |
---|
699 | } |
---|
700 | |
---|
701 | $cur->comment_words = implode(' ', text::splitWords($cur->comment_content)); |
---|
702 | |
---|
703 | $cur->comment_id = $this->con->select( |
---|
704 | 'SELECT MAX(comment_id) FROM ' . $this->prefix . 'comment' |
---|
705 | )->f(0) + 1; |
---|
706 | |
---|
707 | $cur->insert(); |
---|
708 | |
---|
709 | if ($cur->comment_trackback && $cur->comment_status == 1) { |
---|
710 | $count_t++; |
---|
711 | } elseif ($cur->comment_status == 1) { |
---|
712 | $count_c++; |
---|
713 | } |
---|
714 | } |
---|
715 | |
---|
716 | if ($count_t > 0 || $count_c > 0) { |
---|
717 | $this->con->execute( |
---|
718 | 'UPDATE ' . $this->prefix . 'post SET ' . |
---|
719 | 'nb_comment = ' . $count_c . ', ' . |
---|
720 | 'nb_trackback = ' . $count_t . ' ' . |
---|
721 | 'WHERE post_id = ' . (integer) $new_post_id . ' ' |
---|
722 | ); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | # Pings import |
---|
727 | protected function importPings($post_id, $new_post_id, $db) |
---|
728 | { |
---|
729 | $urls = array(); |
---|
730 | $pings = array(); |
---|
731 | |
---|
732 | $rs = $db->select( |
---|
733 | 'SELECT pinged FROM ' . $this->vars['db_prefix'] . 'posts ' . |
---|
734 | 'WHERE ID = ' . (integer) $post_id |
---|
735 | ); |
---|
736 | $pings = explode("\n", $rs->pinged); |
---|
737 | unset($pings[0]); |
---|
738 | |
---|
739 | foreach ($pings as $ping_url) { |
---|
740 | $url = $this->cleanStr($ping_url); |
---|
741 | if (isset($urls[$url])) { |
---|
742 | continue; |
---|
743 | } |
---|
744 | |
---|
745 | $cur = $this->con->openCursor($this->prefix . 'ping'); |
---|
746 | $cur->post_id = (integer) $new_post_id; |
---|
747 | $cur->ping_url = $url; |
---|
748 | $cur->insert(); |
---|
749 | |
---|
750 | $urls[$url] = true; |
---|
751 | } |
---|
752 | } |
---|
753 | |
---|
754 | # Meta import |
---|
755 | protected function importTags($post_id, $new_post_id, $db) |
---|
756 | { |
---|
757 | $rs = $db->select( |
---|
758 | 'SELECT * FROM ' . $this->vars['db_prefix'] . 'terms AS t, ' . |
---|
759 | $this->vars['db_prefix'] . 'term_taxonomy AS x, ' . |
---|
760 | $this->vars['db_prefix'] . 'term_relationships AS r ' . |
---|
761 | 'WHERE t.term_id = x.term_id ' . |
---|
762 | 'AND x.taxonomy = \'post_tag\' ' . |
---|
763 | 'AND t.term_id = r.term_taxonomy_id ' . |
---|
764 | 'AND r.object_id =' . $post_id . |
---|
765 | ' ORDER BY t.term_id ASC' |
---|
766 | ); |
---|
767 | |
---|
768 | if ($rs->isEmpty()) { |
---|
769 | return; |
---|
770 | } |
---|
771 | |
---|
772 | while ($rs->fetch()) { |
---|
773 | $this->core->meta->setPostMeta($new_post_id, 'tag', $this->cleanStr($rs->name)); |
---|
774 | } |
---|
775 | } |
---|
776 | } |
---|