Ticket #3398: geeklog.php

File geeklog.php, 18.2 kB (added by justdave, 2 years ago)

geeklog importer

Line 
1 <?php
2 /**
3     Add These Functions to make our lives easier
4 **/
5
6 set_time_limit(300);
7 ini_set('memory_limit', '16M');
8
9 if(!function_exists('get_catbynicename'))
10 {
11     function get_catbynicename($category_nicename)
12     {
13     global $wpdb;
14     
15     $cat_id -= 0;     // force numeric
16     $name = $wpdb->get_var('SELECT cat_ID FROM '.$wpdb->categories.' WHERE category_nicename="'.$category_nicename.'"');
17     
18     return $name;
19     }
20 }
21
22 if(!function_exists('get_comment_count'))
23 {
24     function get_comment_count($post_ID)
25     {
26         global $wpdb;
27         return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
28     }
29 }
30
31 if(!function_exists('link_exists'))
32 {
33     function link_exists($linkname)
34     {
35         global $wpdb;
36         return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$wpdb->escape($linkname).'"');
37     }
38 }
39
40 /**
41     The Main Importer Class
42 **/
43 class Geeklog_Import {
44
45     function header()
46     {
47         echo '<div class="wrap">';
48         echo '<h2>'.__('Import GeekLog').'</h2>';
49         echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
50     }
51
52     function footer()
53     {
54         echo '</div>';
55     }
56     
57     function greet()
58     {
59         echo '<p>'.__('This importer allows you to extract topics, posts, users, and comments from GeekLog 1.3.9 into your blog. This has not been tested on any other versions of GeekLog.  Mileage may vary.').'</p>';
60         echo '<p>'.__('Please enter your GeekLog database onfiguration settings:').'</p>';
61         echo '<form action="admin.php?import=geeklog&amp;step=1" method="post">';
62         $this->db_form();
63         echo '<input type="submit" name="submit" value="'.__('Import Categories').'" />';
64         echo '</form>';
65     }
66
67     function get_gl_cats()
68     {
69         global $wpdb;
70         // General Housekeeping
71         $gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
72         set_magic_quotes_runtime(0);
73         $prefix = get_option('tpre');
74         
75         // Get Categories
76         return $gldb->get_results('SELECT
77                                         tid,
78                                         topic
79                                         FROM '.$prefix.'topics',
80                                      ARRAY_A);
81     }
82     
83     function get_gl_users()
84     {
85         global $wpdb;
86         // General Housekeeping
87         $gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
88         set_magic_quotes_runtime(0);
89         $prefix = get_option('tpre');
90         
91         // Get Users
92         
93         return $gldb->get_results('SELECT
94                                         uid,
95                                         username,
96                                         passwd,
97                                         fullname,
98                                         email,
99                                         homepage,
100                                         regdate
101                                        FROM '.$prefix.'users', ARRAY_A);
102     }
103     
104     function get_gl_posts()
105     {
106         // General Housekeeping
107         $gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
108         set_magic_quotes_runtime(0);
109         $prefix = get_option('tpre');
110         
111         // Get Posts
112         return $gldb->get_results('SELECT
113                                         sid,
114                                         uid,
115                                         tid,
116                                         date,
117                                         title,
118                                         introtext,
119                                         bodytext,
120                                         comments,
121                                         commentcode,
122                                         postmode,
123                                         draft_flag,
124                                         hits
125                                        FROM '.$prefix.'stories', ARRAY_A);
126     }
127     
128     function get_gl_comments()
129     {
130         global $wpdb;
131         // General Housekeeping
132         $gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
133         set_magic_quotes_runtime(0);
134         $prefix = get_option('tpre');
135         
136         // Get Comments (only ones belonging to stories, not polls or anything)
137         return $gldb->get_results('SELECT * FROM '.$prefix.'comments WHERE type="article"', ARRAY_A);
138     }
139     
140         function get_gl_links()
141     {
142         //General Housekeeping
143         $gldb = new wpdb(get_option('gluser'), get_option('glpass'), get_option('glname'), get_option('glhost'));
144         set_magic_quotes_runtime(0);
145         $prefix = get_option('tpre');
146         
147         return $gldb->get_results('SELECT
148                                         id,
149                                         date,
150                                         category,
151                                         url,
152                                         linkname,
153                                         description
154                                       FROM '.$prefix.'gl_link',
155                                       ARRAY_A);                         
156     }
157     
158     function cat2wp($categories='')
159     {
160         // General Housekeeping
161         global $wpdb;
162         $count = 0;
163         $glcat2wpcat = array();
164         // Do the Magic
165         if(is_array($categories))
166         {
167             echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
168             foreach ($categories as $category)
169             {
170                 $count++;
171                 extract($category);
172                 
173                 
174                 // Make Nice Variables
175                 $name = $wpdb->escape(strtolower($topic));
176                 $title = $wpdb->escape($topic);
177                 
178                 if($cinfo = category_exists($name))
179                 {
180                     $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
181                 }
182                 else
183                 {
184                     $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
185                 }
186                 $glcat2wpcat[$tid] = $ret_id;
187             }
188             
189             // Store category translation for future use
190             add_option('glcat2wpcat',$glcat2wpcat);
191             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count).'<br /><br /></p>';
192             return true;
193         }
194         echo __('No Categories to Import!');
195         return false;
196     }
197     
198     function users2wp($users='')
199     {
200         // General Housekeeping
201         global $wpdb;
202         $count = 0;
203         $glid2wpid = array();
204         
205         // Midnight Mojo
206         if(is_array($users))
207         {
208             echo '<p>'.__('Importing Users...').'<br /><br /></p>';
209             foreach($users as $user)
210             {
211                 $count++;
212                 extract($user);
213                 
214                 // Make Nice Variables
215                 $username = $wpdb->escape($username);
216                 $fullname = $wpdb->escape($fullname);
217                 $homepage = $wpdb->escape($homepage);
218                 
219                 if($uinfo = get_userdatabylogin($name))
220                 {
221                     
222                     $ret_id = wp_insert_user(array(
223                                 'ID'        => $uinfo->ID,
224                                 'user_login'    => $username,
225                                 'user_nicename'    => $fullname,
226                                 'user_email'    => $email,
227                                 'user_url'    => $homepage,
228                                 'user_registered' => $regdate,
229                                 'display_name'    => $name)
230                                 );
231                 }
232                 else
233                 {
234                     $ret_id = wp_insert_user(array(
235                                 'user_login'    => $username,
236                                 'user_nicename'    => $fullname,
237                                 'user_email'    => $email,
238                                 'user_url'    => $homepage,
239                                 'user_registered' => $regdate,
240                                 'display_name'    => $name)
241                                 );
242                 }
243                 $glid2wpid[$uid] = $ret_id;
244                 
245                 // We only have the encrypted password, but wp_insert_user will encrypt it
246                 // again, so we have to set it manually.
247                 $wpdb->query("UPDATE $wpdb->users SET user_pass='$passwd' WHERE ID=$ret_id");
248                 update_usermeta( $ret_id, 'rich_editing', 'false');
249             }// End foreach($users as $user)
250             
251             // Store id translation array for future use
252             add_option('glid2wpid',$glid2wpid);
253             
254             
255             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
256             return true;
257         }// End if(is_array($users)
258         
259         echo __('No Users to Import!');
260         return false;
261         
262     }// End function user2wp()
263     
264     function posts2wp($posts='')
265     {
266         // General Housekeeping
267         global $wpdb;
268         $count = 0;
269         $glposts2wpposts = array();
270         $glcat2wpcat = get_option('glcat2wpcat');
271                 $glid2wpid = get_option('glid2wpid');
272         $cats = array();
273
274         // Do the Magic
275         if(is_array($posts))
276         {
277             echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
278             foreach($posts as $post)
279             {
280                 $count++;
281                 extract($post);
282                 
283                 $authorid = $glid2wpid[$uid] ;
284
285                 $Title = $title;
286                 $Body = $introtext.$bodytext;
287                 $Excerpt = $introtext;
288                 $post_status = $draft_flag ? 'draft' : 'publish';
289                 $comment_status = ($commentcode == 0) ? 'open' : 'closed';
290                 if ($postmode == 'plaintext') {
291                     // For some screwy reason, Geeklog double-escapes plaintext posts.
292                     // So if it's plaintext, we need to unescape it one more time.
293                     $Title = stripslashes($Title);
294                     $Body = stripslashes($Body);
295                     $Excerpt = stripslashes($Excerpt);
296                 }
297                 
298                 // Import Post data into WordPress
299                 
300                 if($pinfo = post_exists($Title,$Body,$date))
301                 {
302                     $ret_id = wp_insert_post(array(
303                             'ID'            => $pinfo,
304                             'post_date'        => $date,
305                             'post_author'        => $authorid,
306                             'post_modified'        => $date,
307                             'post_title'        => $wpdb->escape($Title),
308                             'post_content'        => $wpdb->escape($Body),
309                             'post_excerpt'        => $wpdb->escape($Excerpt),
310                             'post_status'        => $post_status,
311                             'comment_status'    => $comment_status,
312                             'comment_count'        => $comments)
313                             );
314                 }
315                 else
316                 {
317                     $ret_id = wp_insert_post(array(
318                             'post_date'        => $date,
319                             'post_author'        => $authorid,
320                             'post_modified'        => $date,
321                             'post_title'        => $wpdb->escape($Title),
322                             'post_content'        => $wpdb->escape($Body),
323                             'post_excerpt'        => $wpdb->escape($Excerpt),
324                             'post_status'        => $post_status,
325                             'comment_status'    => $comment_status,
326                             'comment_count'        => $comments)
327                             );
328                 }
329                 $glposts2wpposts[$sid] = $ret_id;
330                 
331                 // Make Post-to-Category associations
332                 wp_set_post_cats('', $ret_id, array($glcat2wpcat[$tid]));
333                                 add_post_meta($ret_id, '_gl_sid', $sid);
334
335             }
336         }
337         // Store ID translation for later use
338         add_option('glposts2wpposts',$glposts2wpposts);
339         
340         echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
341         return true;   
342     }
343     
344     function comments2wp($comments='')
345     {
346         // General Housekeeping
347         global $wpdb;
348         $count = 0;
349         $glcm2wpcm = array();
350         $glposts2wpposts = get_option('glposts2wpposts');
351                 $glid2wpid = get_option('glid2wpid');
352         
353         // Magic Mojo
354         if(is_array($comments))
355         {
356             echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
357             foreach($comments as $thecomment)
358             {
359                 $count++;
360                 extract($thecomment);
361                 
362                 // WordPressify Data
363                 $comment_ID = ltrim($cid, '0');
364                 $comment_post_ID = $glposts2wpposts[$sid];
365                 $comment_approved = 1;
366                 $name = $wpdb->escape($title);
367                 $comment_parent = $glcm2wpcm[$pid] ? $glcm2wpcm[$pid] : 0;
368                 $user_id = $glid2wpid[$uid] ? $glid2wpid[$uid] : 0;
369                 $user_info = get_userdata($user_id);
370                 
371                 if($cinfo = comment_exists($name, $date))
372                 {
373                     // Update comments
374                     $ret_id = wp_update_comment(array(
375                             'comment_ID'            => $cinfo,
376                             'comment_post_ID'        => $comment_post_ID,
377                             'comment_date'            => $date,
378                             'comment_content'        => $wpdb->escape($comment),
379                             'comment_parent'        => $comment_parent,
380                             'comment_author'        => $user_info->display_name,
381                             'comment_author_url'        => $user_info->user_url,
382                             'comment_auther_email'        => $user_info->user_email,
383                             'user_id'            => $user_id,
384                             'comment_approved'        => $comment_approved)
385                             );
386                 }
387                 else
388                 {
389                     // Insert comments
390                     $ret_id = wp_insert_comment(array(
391                             'comment_post_ID'        => $comment_post_ID,
392                             'comment_date'            => $date,
393                             'comment_content'        => $wpdb->escape($comment),
394                             'comment_parent'        => $comment_parent,
395                             'comment_author'        => $user_info->display_name,
396                             'comment_author_url'        => $user_info->user_url,
397                             'comment_auther_email'        => $user_info->user_email,
398                             'user_id'            => $user_id,
399                             'comment_approved'        => $comment_approved)
400                             );
401                 }
402                 $glcm2wpcm[$cid] = $ret_id;
403             }
404             // Store Comment ID translation for future use
405             add_option('glcm2wpcm', $glcm2wpcm);           
406             
407             // Associate newly formed categories with posts
408             get_comment_count($ret_id);
409             
410             
411             echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
412             return true;
413         }
414         echo __('No Comments to Import!');
415         return false;
416     }
417     
418     function links2wp($links='')
419     {
420         // General Housekeeping
421         global $wpdb;
422         $count = 0;
423         
424         // Deal with the links
425         if(is_array($links))
426         {
427             echo '<p>'.__('Importing Links...').'<br /><br /></p>';
428             foreach($links as $link)
429             {
430                 $count++;
431                 extract($link);
432                 
433                 // Make nice vars
434                 $category = $wpdb->escape($category);
435                 $linkname = $wpdb->escape($linkname);
436                 $description = $wpdb->escape($description);
437                 
438                 if($linfo = link_exists($linkname))
439                 {
440                     $ret_id = wp_insert_link(array(
441                                 'link_id'            => $linfo,
442                                 'link_url'            => $url,
443                                 'link_name'            => $linkname,
444                                 'link_category'        => $category,
445                                 'link_description'    => $description,
446                                 'link_updated'        => $date)
447                                 );
448                 }
449                 else
450                 {
451                     $ret_id = wp_insert_link(array(
452                                 'link_url'            => $url,
453                                 'link_name'            => $linkname,
454                                 'link_category'        => $category,
455                                 'link_description'    => $description,
456                                 'link_updated'        => $date)
457                                 );
458                 }
459                 $gllinks2wplinks[$link_id] = $ret_id;
460             }
461             add_option<