Dotclear

source: plugins/importExport/inc/class.dc.import.wp.php @ 1185:ebbe7458e43a

Revision 1185:ebbe7458e43a, 23.4 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Variable $old_cat_ids might not be initialized, thank's nicosomb

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

Sites map