Ticket #4184: jaws.php

File jaws.php, 20.1 kB (added by obazavil, 1 year ago)
Line 
1 <?php
2 /*
3  * Jaws import plugin
4  * by Omar Bazavilvazo - http://OmarBazavilvazo.com/
5  */
6
7 /**
8     Add These Functions to make our lives easier
9 **/
10 if(!function_exists('get_catbynicename'))
11 {
12     function get_catbynicename($category_nicename)
13     {
14     global $wpdb;
15
16     $cat_id -= 0;     // force numeric
17     $name = $wpdb->get_var('SELECT cat_ID FROM '.$wpdb->categories.' WHERE category_nicename="'.$category_nicename.'"');
18
19     return $name;
20     }
21 }
22
23 if(!function_exists('get_comment_count'))
24 {
25     function get_comment_count($post_ID)
26     {
27         global $wpdb;
28         return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
29     }
30 }
31
32 if(!function_exists('link_exists'))
33 {
34     function link_exists($linkname)
35     {
36         global $wpdb;
37         return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$wpdb->escape($linkname).'"');
38     }
39 }
40
41 /**
42     The Main Importer Class
43 **/
44 class Jaws_Import {
45
46     function header()
47     {
48         echo '<div class="wrap">';
49         echo '<h2>'.__('Import Jaws').'</h2>';
50         echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
51     }
52
53     function footer()
54     {
55         echo '</div>';
56     }
57
58     function greet() {
59         echo '<div class="narrow">';
60         echo '<p>'.__('Howdy! This imports categories, users, posts, comments, and links from any Jaws 0.6.1+ into this blog.').'</p>';
61         echo '<p>'.__('This has not been tested on previous versions of Jaws.  Mileage may vary.').'</p>';
62         echo '<p>'.__('Your Jaws Configuration settings are as follows:').'</p>';
63         echo '<form action="admin.php?import=jaws&amp;step=1" method="post">';
64         $this->db_form();
65         echo '<p class="submit"><input type="submit" name="submit" value="'.__('Import Categories').' &raquo;" /></p>';
66         echo '</form>';
67         echo '</div>';
68     }
69
70     function get_jaws_cats()
71     {
72         global $wpdb;
73         // General Housekeeping
74         $jawsdb = new wpdb(get_option('jawsuser'), get_option('jawspass'), get_option('jawsname'), get_option('jawshost'));
75         set_magic_quotes_runtime(0);
76         $prefix = get_option('tpre');
77
78         // Get Categories
79         return $jawsdb->get_results('SELECT
80             id,
81             name
82             FROM '.$prefix.'blog_category',
83             ARRAY_A);
84     }
85
86     function get_jaws_users()
87     {
88         global $wpdb;
89         // General Housekeeping
90         $jawsdb = new wpdb(get_option('jawsuser'), get_option('jawspass'), get_option('jawsname'), get_option('jawshost'));
91         set_magic_quotes_runtime(0);
92         $prefix = get_option('tpre');
93
94         // Get Users
95
96         return $jawsdb->get_results('SELECT
97             id as user_id,
98             username,
99             passwd,
100             name,
101             email,
102             createtime
103             FROM '.$prefix.'users WHERE username != "admin"', ARRAY_A);
104     }
105
106     function get_jaws_posts()
107     {
108         // General Housekeeping
109         $jawsdb = new wpdb(get_option('jawsuser'), get_option('jawspass'), get_option('jawsname'), get_option('jawshost'));
110         set_magic_quotes_runtime(0);
111         $prefix = get_option('tpre');
112
113         // Get Posts
114         return $jawsdb->get_results('SELECT *, id as post_id FROM '.$prefix.'blog', ARRAY_A);
115     }
116     
117     function get_jaws_post_categories($id_post)
118     {
119         // General Housekeeping
120         $jawsdb = new wpdb(get_option('jawsuser'), get_option('jawspass'), get_option('jawsname'), get_option('jawshost'));
121         set_magic_quotes_runtime(0);
122         $prefix = get_option('tpre');
123
124         // Get Categories
125         return $jawsdb->get_results('SELECT category_id as cat_id FROM '.$prefix.'blog_entrycat
126             WHERE entry_id='.$id_post, ARRAY_A);
127         
128     }
129
130     function get_jaws_comments()
131     {
132         global $wpdb;
133         // General Housekeeping
134         $jawsdb = new wpdb(get_option('jawsuser'), get_option('jawspass'), get_option('jawsname'), get_option('jawshost'));
135         set_magic_quotes_runtime(0);
136         $prefix = get_option('tpre');
137
138         // Get Comments
139         return $jawsdb->get_results('SELECT *, id as comment_id FROM '.$prefix.'comments WHERE gadget="Blog"', ARRAY_A);
140     }
141
142         function get_jaws_links()
143     {
144         //General Housekeeping
145         $jawsdb = new wpdb(get_option('jawsuser'), get_option('jawspass'), get_option('jawsname'), get_option('jawshost'));
146         set_magic_quotes_runtime(0);
147         $prefix = get_option('tpre');
148
149         // Get Friends...
150         return $jawsdb->get_results('SELECT * FROM '.$prefix.'friend', ARRAY_A);
151     }
152
153     function cat2wp($categories='')
154     {
155         // General Housekeeping
156         global $wpdb;
157         $count = 0;
158         $jawscat2wpcat = array();
159         // Do the Magic
160         if(is_array($categories))
161         {
162             echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
163             foreach ($categories as $category)
164             {
165                 $count++;
166                 extract($category);
167
168
169                 // Make Nice Variables
170                 $name = $wpdb->escape($name);
171                 $title = $wpdb->escape($name);
172
173                 if($cinfo = category_exists($name))
174                 {
175                     $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
176                 }
177                 else
178                 {
179                     $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
180                 }
181                 $jawscat2wpcat[$id] = $ret_id;
182             }
183
184             // Store category translation for future use
185             add_option('jawscat2wpcat',$jawscat2wpcat);
186             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count).'<br /><br /></p>';
187             return true;
188         }
189         echo __('No Categories to Import!');
190         return false;
191     }
192
193     function users2wp($users='')
194     {
195         // General Housekeeping
196         global $wpdb;
197         $count = 0;
198         $jawsid2wpid = array();
199
200         // Midnight Mojo
201         if(is_array($users))
202         {
203             echo '<p>'.__('Importing Users...').'<br /><br /></p>';
204             foreach($users as $user)
205             {
206                 $count++;
207                 extract($user);
208
209                 // Make Nice Variables
210                 $username = $wpdb->escape($username);
211                 $name = $wpdb->escape($name);
212
213                 if($uinfo = get_userdatabylogin($username))
214                 {
215
216                     $ret_id = wp_insert_user(array(
217                                 'ID'            => $uinfo->ID,
218                                 'user_login'    => $username,
219                                 'user_nicename'    => $name,
220                                 'user_email'    => $email,
221                                 'user_url'        => 'http://',
222                                 'display_name'    => $name)
223                                 );
224                 }
225                 else
226                 {
227                     $ret_id = wp_insert_user(array(
228                                 'user_login'    => $username,
229                                 'user_nicename'    => $name,
230                                 'user_email'    => $email,
231                                 'user_url'        => 'http://',
232                                 'display_name'    => $name)
233                                 );
234                 }
235                 $jawsid2wpid[$user_id] = $ret_id;
236
237                 // Set Jaws-to-WordPress permissions translation
238                 $transperms = array(0 => '10', 1 => '9', 2 => '4');
239
240                 // Update Usermeta Data
241                 $user = new WP_User($ret_id);
242                 if('10' == $transperms[$privs]) { $user->set_role('administrator'); }
243                 if('9'  == $transperms[$privs]) { $user->set_role('editor'); }
244                 if('5'  == $transperms[$privs]) { $user->set_role('editor'); }
245                 if('4'  == $transperms[$privs]) { $user->set_role('author'); }
246                 if('3'  == $transperms[$privs]) { $user->set_role('contributor'); }
247                 if('2'  == $transperms[$privs]) { $user->set_role('contributor'); }
248                 if('0'  == $transperms[$privs]) { $user->set_role('subscriber'); }
249
250                 update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] );
251                 update_usermeta( $ret_id, 'rich_editing', 'false');
252             }// End foreach($users as $user)
253
254             // Store id translation array for future use
255             add_option('jawsid2wpid',$jawsid2wpid);
256
257             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
258             return true;
259         }// End if(is_array($users)
260
261         echo __('No Users to Import!');
262         return false;
263
264     }// End function user2wp()
265
266     function posts2wp($posts='')
267     {
268         // General Housekeeping
269         global $wpdb;
270         $count = 0;
271         $jawsposts2wpposts = array();
272         $jawscat2wpcat = get_option('jawscat2wpcat');
273         $jawsid2wpid = get_option('jawsid2wpid');
274
275         // Do the Magic
276         if(is_array($posts))
277         {
278             echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
279             foreach($posts as $post)
280             {
281                 $count++;
282                 extract($post);
283
284                 // Set Jaws-to-WordPress status translation
285                 $stattrans = array(0 => 'draft', 1 => 'publish');
286
287                 //Can we do this more efficiently?
288                 $uinfo = ( get_userdatabylogin( $user_id ) ) ? get_userdatabylogin( $user_id ) : 1;
289                 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
290
291                 $Title = $wpdb->escape($title);
292                 $Body = $wpdb->escape($text);
293                 //$Excerpt = $wpdb->escape($Excerpt);
294                 $Excerpt = "";    //TODO: calculate it somehow
295                 $post_status = $stattrans[$published];
296
297                 // Import Post data into WordPress
298
299                 if($pinfo = post_exists($Title,$Body))
300                 {
301                     $ret_id = wp_insert_post(array(
302                         'ID'                => $pinfo,
303                         'post_date'            => $createtime,
304                         'post_date_gmt'        => $createtime,
305                         'post_author'        => $jawsid2wpid[$user_id],
306                         'post_modified'        => $updatetime,
307                         'post_modified_gmt' => $updatetime,
308                         'post_title'        => $Title,
309                         'post_content'        => $Body,
310                         'post_excerpt'        => $Excerpt,
311                         'post_status'        => $post_status,
312                         'post_name'            => $fast_url,
313                         'comment_count'        => $comments)
314                         );
315                 }
316                 else
317                 {
318                     $ret_id = wp_insert_post(array(
319                         'post_date'            => $createtime,
320                         'post_date_gmt'        => $createtime,
321                         'post_author'        => $jawsid2wpid[$user_id],
322                         'post_modified'        => $updatetime,
323                         'post_modified_gmt' => $updatetime,
324                         'post_title'        => $Title,
325                         'post_content'        => $Body,
326                         'post_excerpt'        => $Excerpt,
327                         'post_status'        => $post_status,
328                         'post_name'            => $fast_url,
329                         'comment_count'        => $comments)
330                         );
331                 }
332                 $jawsposts2wpposts[$post_id] = $ret_id;
333
334                 // Make Post-to-Category associations
335                 $categories = array();
336                 $cats = array();
337                 $categories = $this->get_jaws_post_categories($post_id);
338
339                 if(!empty($categories))
340                 {
341                     $cat_index = 0;
342                     foreach ($categories as $category) {
343                         $cats[$cat_index] = $jawscat2wpcat[$category['cat_id']];
344                         $cat_index++;
345                     }               
346                     
347                     wp_set_post_categories($ret_id, $cats);
348                 }
349             }
350         }
351         // Store ID translation for later use
352         add_option('jawsposts2wpposts',$jawsposts2wpposts);
353
354         echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
355         return true;
356     }
357
358     function comments2wp($comments='')
359     {
360         // General Housekeeping
361         global $wpdb;
362         $count = 0;
363         $jawscm2wpcm = array();
364         $postarr = get_option('jawsposts2wpposts');
365
366         // Magic Mojo
367         if(is_array($comments))
368         {
369             echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
370             foreach($comments as $comment)
371             {
372                 $count++;
373                 extract($comment);
374
375                 // WordPressify Data
376                 $comment_ID = ltrim($id, '0');
377                 $comment_post_ID = $postarr[$gadget_reference];
378                 //$comment_approved = ('approved' == $status) ? 1 : 0;
379                 $comment_approved = 1;    //TODO: check
380                 $name = $wpdb->escape($name);
381                 $email = $wpdb->escape($email);
382                 $web = $wpdb->escape($url);
383                 $message = $wpdb->escape($message);
384
385                 if($cinfo = comment_exists($name, $createtime))
386                 {
387                     // Update comments
388                     $ret_id = wp_update_comment(array(
389                         'comment_ID'            => $cinfo,
390                         'comment_post_ID'        => $comment_post_ID,
391                         'comment_author'        => $name,
392                         'comment_author_email'    => $email,
393                         'comment_author_url'    => $web,
394                         'comment_date'            => $posted,
395                         'comment_content'        => $message,
396                         'comment_approved'        => $comment_approved,
397                         'comment_parent'        => $postarr[$parent])
398                         );
399                 }
400                 else
401                 {
402                     // Insert comments
403                     $ret_id = wp_insert_comment(array(
404                         'comment_post_ID'        => $comment_post_ID,
405                         'comment_author'        => $name,
406                         'comment_author_email'    => $email,
407                         'comment_author_url'    => $web,
408                         'comment_author_IP'        => $ip,
409                         'comment_date'            => $posted,
410                         'comment_content'        => $message,
411                         'comment_approved'        => $comment_approved,
412                         'comment_parent'        => $postarr[$parent])
413                         );
414                 }
415                 $jawscm2wpcm[$comment_id] = $ret_id;
416             }
417             // Store Comment ID translation for future use
418             add_option('jawscm2wpcm', $jawscm2wpcm);
419
420             // Associate newly formed categories with posts
421             get_comment_count($ret_id);
422
423
424             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
425             return true;
426         }
427         echo __('No Comments to Import!');
428         return false;
429     }
430
431     function links2wp($links='')
432     {
433         // General Housekeeping
434         global $wpdb;
435         $count = 0;
436
437         // Deal with the links
438         if(is_array($links))
439         {
440             echo '<p>'.__('Importing Links (Friends)...').'<br /><br /></p>';
441             foreach($links as $link)
442             {
443                 $count++;
444                 extract($link);
445
446                 // Make nice vars
447                 $category = $wpdb->escape("Blogroll");
448                 $linkname = $wpdb->escape($friend);
449                 $description = "";
450
451                 if($linfo = link_exists($linkname))
452                 {
453                     $ret_id = wp_insert_link(array(
454                                 'link_id'            => $linfo,
455                                 'link_url'            => $url,
456                                 'link_name'            => $linkname,
457                                 'link_category'        => $category,
458                                 'link_description'    => $description,
459                                 'link_updated'        => time())
460                                 );
461                 }
462                 else
463                 {
464                     $ret_id = wp_insert_link(array(
465                                 'link_url'            => $url,
466                                 'link_name'            => $linkname,
467                                 'link_category'        => $category,
468                                 'link_description'    => $description,
469