root/branches/2.0/wp-admin/import/textpattern.php

Revision 5406, 19.4 kB (checked in by ryan, 2 years ago)

Add nonces to importers

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