Dotclear

source: plugins/importExport/inc/class.dc.import.wp.php @ 1559:b6601f74bcd2

Revision 1559:b6601f74bcd2, 23.4 KB checked in by Anne Kozlika <kozlika@…>, 12 years ago (diff)

Import/Export? enhancements.

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

Sites map