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

Revision 8645, 24.0 kB (checked in by westi, 2 weeks ago)

phpdoc for wp-admin. See #7496 props santosj.

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