root/trunk/wp-admin/import/dotclear.php

Revision 7645, 22.7 kB (checked in by ryan, 3 months ago)

Prepare DB queries in more places. Props filosofo. see #6644

  • Property svn:eol-style set to native
Line 
1 <?php
2 /*
3  * DotClear import plugin
4  * by Thomas Quinot - http://thomas.quinot.org/
5  */
6
7 /**
8     Add These Functions to make our lives easier
9 **/
10
11 if(!function_exists('get_comment_count'))
12 {
13     function get_comment_count($post_ID)
14     {
15         global $wpdb;
16         return $wpdb->get_var( $wpdb->prepare("SELECT count(*) FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
17     }
18 }
19
20 if(!function_exists('link_exists'))
21 {
22     function link_exists($linkname)
23     {
24         global $wpdb;
25         return $wpdb->get_var( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_name = %s", $linkname) );
26     }
27 }
28
29 /*
30  Identify UTF-8 text
31  Taken from http://www.php.net/manual/fr/function.mb-detect-encoding.php#50087
32 */
33 //
34 //    utf8 encoding validation developed based on Wikipedia entry at:
35 //    http://en.wikipedia.org/wiki/UTF-8
36 //
37 //    Implemented as a recursive descent parser based on a simple state machine
38 //    copyright 2005 Maarten Meijer
39 //
40 //    This cries out for a C-implementation to be included in PHP core
41 //
42
43 function valid_1byte($char) {
44     if(!is_int($char)) return false;
45         return ($char & 0x80) == 0x00;
46 }
47
48 function valid_2byte($char) {
49     if(!is_int($char)) return false;
50         return ($char & 0xE0) == 0xC0;
51 }
52
53 function valid_3byte($char) {
54     if(!is_int($char)) return false;
55         return ($char & 0xF0) == 0xE0;
56 }
57
58 function valid_4byte($char) {
59     if(!is_int($char)) return false;
60         return ($char & 0xF8) == 0xF0;
61 }
62
63 function valid_nextbyte($char) {
64     if(!is_int($char)) return false;
65         return ($char & 0xC0) == 0x80;
66 }
67
68 function valid_utf8($string) {
69     $len = strlen($string);
70     $i = 0;
71     while( $i < $len ) {
72         $char = ord(substr($string, $i++, 1));
73         if(valid_1byte($char)) {    // continue
74             continue;
75         } else if(valid_2byte($char)) { // check 1 byte
76             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
77                 return false;
78         } else if(valid_3byte($char)) { // check 2 bytes
79             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
80                 return false;
81             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
82                 return false;
83         } else if(valid_4byte($char)) { // check 3 bytes
84             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
85                 return false;
86             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
87                 return false;
88             if(!valid_nextbyte(ord(substr($string, $i++, 1))))
89                 return false;
90         } // goto next char
91     }
92     return true; // done
93 }
94
95 function csc ($s) {
96     if (valid_utf8 ($s)) {
97         return $s;
98     } else {
99         return iconv(get_option ("dccharset"),"UTF-8",$s);
100     }
101 }
102
103 function textconv ($s) {
104     return csc (preg_replace ('|(?<!<br />)\s*\n|', ' ', $s));
105 }
106
107 /**
108     The Main Importer Class
109 **/
110 class Dotclear_Import {
111
112     function header()
113     {
114         echo '<div class="wrap">';
115         echo '<h2>'.__('Import DotClear').'</h2>';
116         echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
117     }
118
119     function footer()
120     {
121         echo '</div>';
122     }
123
124     function greet()
125     {
126         echo '<div class="narrow"><p>'.__('Howdy! This importer allows you to extract posts from a DotClear database into your blog.  Mileage may vary.').'</p>';
127         echo '<p>'.__('Your DotClear Configuration settings are as follows:').'</p>';
128         echo '<form action="admin.php?import=dotclear&amp;step=1" method="post">';
129         wp_nonce_field('import-dotclear');
130         $this->db_form();
131         echo '<p class="submit"><input type="submit" name="submit" value="'.attribute_escape(__('Import Categories')).'" /></p>';
132         echo '</form></div>';
133     }
134
135     function get_dc_cats()
136     {
137         global $wpdb;
138         // General Housekeeping
139         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
140         set_magic_quotes_runtime(0);
141         $dbprefix = get_option('dcdbprefix');
142
143         // Get Categories
144         return $dcdb->get_results('SELECT * FROM '.$dbprefix.'categorie', ARRAY_A);
145     }
146
147     function get_dc_users()
148     {
149         global $wpdb;
150         // General Housekeeping
151         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
152         set_magic_quotes_runtime(0);
153         $dbprefix = get_option('dcdbprefix');
154
155         // Get Users
156
157         return $dcdb->get_results('SELECT * FROM '.$dbprefix.'user', ARRAY_A);
158     }
159
160     function get_dc_posts()
161     {
162         // General Housekeeping
163         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
164         set_magic_quotes_runtime(0);
165         $dbprefix = get_option('dcdbprefix');
166
167         // Get Posts
168         return $dcdb->get_results('SELECT '.$dbprefix.'post.*, '.$dbprefix.'categorie.cat_libelle_url AS post_cat_name
169                         FROM '.$dbprefix.'post INNER JOIN '.$dbprefix.'categorie
170                         ON '.$dbprefix.'post.cat_id = '.$dbprefix.'categorie.cat_id', ARRAY_A);
171     }
172
173     function get_dc_comments()
174     {
175         global $wpdb;
176         // General Housekeeping
177         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
178         set_magic_quotes_runtime(0);
179         $dbprefix = get_option('dcdbprefix');
180
181         // Get Comments
182         return $dcdb->get_results('SELECT * FROM '.$dbprefix.'comment', ARRAY_A);
183     }
184
185     function get_dc_links()
186     {
187         //General Housekeeping
188         $dcdb = new wpdb(get_option('dcuser'), get_option('dcpass'), get_option('dcname'), get_option('dchost'));
189         set_magic_quotes_runtime(0);
190         $dbprefix = get_option('dcdbprefix');
191
192         return $dcdb->get_results('SELECT * FROM '.$dbprefix.'link ORDER BY position', ARRAY_A);
193     }
194
195     function cat2wp($categories='')
196     {
197         // General Housekeeping
198         global $wpdb;
199         $count = 0;
200         $dccat2wpcat = array();
201         // Do the Magic
202         if(is_array($categories))
203         {
204             echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
205             foreach ($categories as $category)
206             {
207                 $count++;
208                 extract($category);
209
210                 // Make Nice Variables
211                 $name = $wpdb->escape($cat_libelle_url);
212                 $title = $wpdb->escape(csc ($cat_libelle));
213                 $desc = $wpdb->escape(csc ($cat_desc));
214
215                 if($cinfo = category_exists($name))
216                 {
217                     $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
218                 }
219                 else
220                 {
221                     $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
222                 }
223                 $dccat2wpcat[$id] = $ret_id;
224             }
225
226             // Store category translation for future use
227             add_option('dccat2wpcat',$dccat2wpcat);
228             echo '<p>'.sprintf(__ngettext('Done! <strong>%1$s</strong> category imported.', 'Done! <strong>%1$s</strong> categories imported.', $count), $count).'<br /><br /></p>';
229             return true;
230         }
231         echo __('No Categories to Import!');
232         return false;
233     }
234
235     function users2wp($users='')
236     {
237         // General Housekeeping
238         global $wpdb;
239         $count = 0;
240         $dcid2wpid = array();
241
242         // Midnight Mojo
243         if(is_array($users))
244         {
245             echo '<p>'.__('Importing Users...').'<br /><br /></p>';
246             foreach($users as $user)
247             {
248                 $count++;
249                 extract($user);
250
251                 // Make Nice Variables
252                 $name = $wpdb->escape(csc ($name));
253                 $RealName = $wpdb->escape(csc ($user_pseudo));
254
255                 if($uinfo = get_userdatabylogin($name))
256                 {
257
258                     $ret_id = wp_insert_user(array(
259                                 'ID'        => $uinfo->ID,
260                                 'user_login'    => $user_id,
261                                 'user_nicename'    => $Realname,
262                                 'user_email'    => $user_email,
263                                 'user_url'    => 'http://',
264                                 'display_name'    => $Realname)
265                                 );
266                 }
267                 else
268                 {
269                     $ret_id = wp_insert_user(array(
270                                 'user_login'    => $user_id,
271                                 'user_nicename'    => csc ($user_pseudo),
272                                 'user_email'    => $user_email,
273                                 'user_url'    => 'http://',
274                                 'display_name'    => $Realname)
275                                 );
276                 }
277                 $dcid2wpid[$user_id] = $ret_id;
278
279                 // Set DotClear-to-WordPress permissions translation
280
281                 // Update Usermeta Data
282                 $user = new WP_User($ret_id);
283                 $wp_perms = $user_level + 1;
284                 if(10 == $wp_perms) { $user->set_role('administrator'); }
285                 else if(== $wp_perms) { $user->set_role('editor'); }
286                 else if(<= $wp_perms) { $user->set_role('editor'); }
287                 else if(<= $wp_perms) { $user->set_role('author'); }
288                 else if(<= $wp_perms) { $user->set_role('contributor'); }
289                 else if(<= $wp_perms) { $user->set_role('contributor'); }
290                 else                     { $user->set_role('subscriber'); }
291
292                 update_usermeta( $ret_id, 'wp_user_level', $wp_perms);
293                 update_usermeta( $ret_id, 'rich_editing', 'false');
294                 update_usermeta( $ret_id, 'first_name', csc ($user_prenom));
295                 update_usermeta( $ret_id, 'last_name', csc ($user_nom));
296             }// End foreach($users as $user)
297
298             // Store id translation array for future use
299             add_option('dcid2wpid',$dcid2wpid);
300
301
302             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
303             return true;
304         }// End if(is_array($users)
305
306         echo __('No Users to Import!');
307         return false;
308
309     }// End function user2wp()
310
311     function posts2wp($posts='')
312     {
313         // General Housekeeping
314         global $wpdb;
315         $count = 0;
316         $dcposts2wpposts = array();
317         $cats = array();
318
319         // Do the Magic
320         if(is_array($posts))
321         {
322             echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
323             foreach($posts as $post)
324             {
325                 $count++;
326                 extract($post);
327
328                 // Set DotClear-to-WordPress status translation
329                 $stattrans = array(0 => 'draft', 1 => 'publish');
330                 $comment_status_map = array (0 => 'closed', 1 => 'open');
331
332                 //Can we do this more efficiently?
333                 $uinfo = ( get_userdatabylogin( $user_id ) ) ? get_userdatabylogin( $user_id ) : 1;
334                 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
335
336                 $Title = $wpdb->escape(csc ($post_titre));
337                 $post_content = textconv ($post_content);
338                 $post_excerpt = "";
339                 if ($post_chapo != "") {
340                     $post_excerpt = textconv ($post_chapo);
341                     $post_content = $post_excerpt ."\n<!--more-->\n".$post_content;
342                 }
343                 $post_excerpt = $wpdb->escape ($post_excerpt);
344                 $post_content = $wpdb->escape ($post_content);
345                 $post_status = $stattrans[$post_pub];
346
347                 // Import Post data into WordPress
348
349                 if($pinfo = post_exists($Title,$post_content))
350                 {
351                     $ret_id = wp_insert_post(array(
352                             'ID'            => $pinfo,
353                             'post_author'        => $authorid,
354                             'post_date'        => $post_dt,
355                             'post_date_gmt'        => $post_dt,
356                             'post_modified'        => $post_upddt,
357                             'post_modified_gmt'    => $post_upddt,
358                             'post_title'        => $Title,
359                             'post_content'        => $post_content,
360                             'post_excerpt'        => $post_excerpt,
361                             'post_status'        => $post_status,
362                             'post_name'        => $post_titre_url,
363                             'comment_status'    => $comment_status_map[$post_open_comment],
364                             'ping_status'        => $comment_status_map[$post_open_tb],
365                             'comment_count'        => $post_nb_comment + $post_nb_trackback)
366                             );
367                     if ( is_wp_error( $ret_id ) )
368                         return $ret_id;
369                 }
370                 else
371                 {
372                     $ret_id = wp_insert_post(array(
373                             'post_author'        => $authorid,
374                             'post_date'        => $post_dt,
375                             'post_date_gmt'        => $post_dt,
376                             'post_modified'        => $post_modified_gmt,
377                             'post_modified_gmt'    => $post_modified_gmt,
378                             'post_title'        => $Title,
379                             'post_content'        => $post_content,
380                             'post_excerpt'        => $post_excerpt,
381                             'post_status'        => $post_status,
382                             'post_name'        => $post_titre_url,
383                             'comment_status'    => $comment_status_map[$post_open_comment],
384                             'ping_status'        => $comment_status_map[$post_open_tb],
385                             'comment_count'        => $post_nb_comment + $post_nb_trackback)
386                             );
387                     if ( is_wp_error( $ret_id ) )
388                         return $ret_id;
389                 }
390                 $dcposts2wpposts[$post_id] = $ret_id;
391
392                 // Make Post-to-Category associations
393                 $cats = array();
394                 $category1 = get_category_by_slug($post_cat_name);
395                 $category1 = $category1->term_id;
396
397                 if($cat1 = $category1) { $cats[1] = $cat1; }
398
399                 if(!empty($cats)) { wp_set_post_categories($ret_id, $cats); }
400             }
401         }
402         // Store ID translation for later use
403         add_option('dcposts2wpposts',$dcposts2wpposts);
404
405         echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
406         return true;
407     }
408
409     function comments2wp($comments='')
410     {
411         // General Housekeeping
412         global $wpdb;
413         $count = 0;
414         $dccm2wpcm = array();
415         $postarr = get_option('dcposts2wpposts');
416
417         // Magic Mojo
418         if(is_array($comments))
419         {
420             echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
421             foreach($comments as $comment)
422             {
423                 $count++;
424                 extract($comment);
425
426                 // WordPressify Data
427                 $comment_ID = (int) ltrim($comment_id, '0');
428                 $comment_post_ID = (int) $postarr[$post_id];
429                 $comment_approved = "$comment_pub";
430                 $name = $wpdb->escape(csc ($comment_auteur));
431                 $email = $wpdb->escape($comment_email);
432                 $web = "http://".$wpdb->escape($comment_site);
433                 $message = $wpdb->escape(textconv ($comment_content));
434
435                 if($cinfo = comment_exists($name, $comment_dt))
436                 {
437                     // Update comments
438                     $ret_id = wp_update_comment(array(
439                             'comment_ID'        => $cinfo,
440                             'comment_post_ID'    => $comment_post_ID,
441                             'comment_author'    => $name,
442                             'comment_author_email'    => $email,
443                             'comment_author_url'    => $web,
444                             'comment_author_IP'    => $comment_ip,
445                             'comment_date'        => $comment_dt,
446                             'comment_date_gmt'    => $comment_dt,
447                             'comment_content'    => $message,
448                             'comment_approved'    => $comment_approved)
449                             );
450                 }
451                 else
452                 {
453                     // Insert comments
454                     $ret_id = wp_insert_comment(array(
455                             'comment_post_ID'    =