Ticket #1944: textpattern.php

File textpattern.php, 15.9 KB (added by technosailor, 6 years ago)

textpattern import script

Line 
1<?php
2
3/*
4This Import Script is written by Aaron Brazell of Technosailor.com
5
6It was developed using a large blog running Textpattern 4.0.2 and
7  successfully imported nearly 3000 records at a time.  Higher
8  scalability is uncertain.
9 
10  BACKUP YOUR DATABASE PRIOR TO RUNNING THIS IMPORT SCRIPT
11*/
12
13// BEGIN EDITING
14
15// $txpconfig options can be found in the Textpattern %textpattern%/config.php file
16$txpcfg['db'] = 'wordpress';
17$txpcfg['user'] = 'root';
18$txpcfg['pass'] = '';
19$txpcfg['host'] = 'localhost';
20$txpcfg['table_prefix'] = '';
21
22// STOP EDITING
23
24add_option('tpre',$txpcfg['table_prefix']);
25
26class Textpattern_Import {
27
28        var $posts = array ();
29        var $file;
30
31        function header() 
32        {
33                echo '<div class="wrap">';
34                echo '<h2>'.__('Import Textpattern').'</h2>';
35        }
36
37        function footer() 
38        {
39                echo '</div>';
40        }
41       
42        function greet() 
43        {
44                global $txpcfg;
45               
46                _e('<p>Howdy! This importer allows you to extract posts from any Textpattern 4.0.2+ into your blog. This has not been tested on previous versions of Textpattern.  Mileage may vary.</p>');
47                _e('<p>Your Textpattern Configuration settings are as follows:</p>');
48                _e('<ul><li><strong>Textpattern Database Name:</strong> '.$txpcfg['db'].'</li>');
49                _e('<li><strong>Textpattern Database User:</strong> '.$txpcfg['user'].'</li>');
50                _e('<li><strong>Textpattern Database Password:</strong> '.$txpcfg['pass'].'</li>');
51                _e('<li><strong>Textpattern Database Host:</strong> '.$txpcfg['host'].'</li>');
52                _e('</ul>');
53                _e('<p>If this is incorrect, please modify settings in wp-admin/import/textpattern.php</p>');
54                _e('<form action="admin.php?import=textpattern&amp;step=1" method="post">');
55                _e('<input type="submit" name="submit" value="Next Step" />');
56                _e('</form>');
57        }
58
59        function get_txp_cats() 
60        {
61               
62                // General Housekeeping
63                global $txpdb;
64                set_magic_quotes_runtime(0);
65                $prefix = get_option('tpre');
66               
67                // Get Categories
68                $cats = $txpdb->get_results('SELECT id,
69                                                                                name,
70                                                                                title
71                                                                        FROM '.$prefix.'txp_category
72                                                                        WHERE type = "article"
73                                                                        ', ARRAY_A);
74                return $cats;
75        }
76       
77        function get_txp_posts()
78        {
79                // General Housekeeping
80                global $txpdb;
81                set_magic_quotes_runtime(0);
82                $prefix = get_option('tpre');
83               
84                // Get Posts
85                $posts = $txpdb->get_results('SELECT
86                                                                                ID,
87                                                                                Posted,
88                                                                                AuthorID,
89                                                                                LastMod,
90                                                                                Title,
91                                                                                Body,
92                                                                                Excerpt,
93                                                                                Category1,
94                                                                                Category2,
95                                                                                Status,
96                                                                                Keywords,
97                                                                                url_title,
98                                                                                comments_count,
99                                                                                ADDDATE(Posted, "INTERVAL '.get_settings('gmt_offset').' HOURS") AS post_date_gmt,
100                                                                                ADDDATE(LastMod, "INTERVAL '.get_settings('gmt_offset').' HOURS") AS post_modified_gmt
101                                                                        FROM '.$prefix.'textpattern
102                                                                        ', ARRAY_A);
103                return $posts;
104        }
105       
106        function get_txp_comments()
107        {
108                // General Housekeeping
109                global $txpdb;
110                set_magic_quotes_runtime(0);
111                $prefix = get_option('tpre');
112               
113                // Get Comments
114                $comments = $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A);
115                return $comments;
116        }
117       
118        function get_txp_users()
119        {
120                // General Housekeeping
121                global $txpdb;
122                set_magic_quotes_runtime(0);
123                $prefix = get_option('tpre');
124               
125                // Get Users
126                $users = $txpdb->get_results('SELECT
127                                                                                user_id,
128                                                                                name,
129                                                                                RealName,
130                                                                                email,
131                                                                                privs
132                                                                        FROM '.$prefix.'txp_users', ARRAY_A);
133                return $users;
134        }
135       
136        function users2wp($users='')
137        {
138                // General Housekeeping
139                global $wpdb;
140                // Clear WordPress User table
141                $wpdb->query('DELETE FROM '.$wpdb->users);
142                $wpdb->query('DELETE FROM '.$wpdb->usermeta);
143               
144                // Midnight Mojo
145                if(is_array($users))
146                {
147                        echo __('<p>Importing Users...<br /><br /></p>');
148                        foreach($users as $user)
149                        {
150                                $count++;
151                                extract($user);
152                                // Do some Role Stuff
153                                $user = new WP_User($user_id);
154                               
155                                switch ( $privs )
156                                {
157                                        case 1 :               
158                                                $user->set_role('administrator');                               
159                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
160                                                                                        user_id         = '.$user_id.',
161                                                                                        meta_key        = "wp_user_level",
162                                                                                        meta_value      ="10"');
163                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
164                                                                                        user_id         = '.$user_id.',
165                                                                                        meta_key        = "rich_editing",
166                                                                                        meta_value      = "false"');
167                                                break;
168                                        case 2 :
169                                                $user->set_role('editor');
170                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
171                                                                                        user_id         = '.$user_id.',
172                                                                                        meta_key        = "wp_user_level",
173                                                                                        meta_value      = "9"');
174                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
175                                                                                        user_id         = '.$user_id.',
176                                                                                        meta_key        = "rich_editing",
177                                                                                        meta_value      = "false"');
178                                                break;
179                                        case 3 :
180                                                $user->set_role('editor');
181                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
182                                                                                        user_id         = '.$user_id.',
183                                                                                        meta_key        = "wp_user_level",
184                                                                                        meta_value      =  "5"');
185                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
186                                                                                        user_id         = '.$user_id.',
187                                                                                        meta_key        = "rich_editing",
188                                                                                        meta_value      = "false"');
189                                                break;
190                                        case 4 :
191                                                $user->set_role('author');
192                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
193                                                                                        user_id         = '.$user_id.',
194                                                                                        meta_key        = "wp_user_level",
195                                                                                        meta_value      = "4"');
196                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
197                                                                                        user_id         = '.$user_id.',
198                                                                                        meta_key        = "rich_editing",
199                                                                                        meta_value      = "false"');
200                                                break;
201                                        case 5 :
202                                                $user->set_role('contributor');
203                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
204                                                                                        user_id         = '.$user_id.',
205                                                                                        meta_key        = "wp_user_level",
206                                                                                        meta_value      =  "3"');
207                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
208                                                                                        user_id         = '.$user_id.',
209                                                                                        meta_key        = "rich_editing",
210                                                                                        meta_value      = "false"');
211                                                break;
212                                        case 6 :
213                                                $user->set_role('contributor');
214                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
215                                                                                        user_id         = '.$user_id.',
216                                                                                        meta_key        = "wp_user_level",
217                                                                                        meta_value      = "2"');
218                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
219                                                                                        user_id         = '.$user_id.',
220                                                                                        meta_key        = "rich_editing",
221                                                                                        meta_value      = "false"');
222                                                break;
223                                        default :
224                                                $user->set_role('contributor');
225                                                $wpdb->query('INSERT INTO '.$wpdb->usermeta.' SET
226                                                                                        user_id         = '.$user_id.',
227                                                                                        meta_key        = "wp_user_level",
228                                                                                        meta_value      = "0"');
229                                                break;
230                                } 
231                               
232                                // Insert
233
234                                $wpdb->query('INSERT INTO '.$wpdb->users.' SET
235                                                                        ID                              = '.$user_id.',
236                                                                        user_login              = "'.$wpdb->escape($name).'",
237                                                                        user_pass               = "'.md5('password123').'",
238                                                                        user_nicename   = "'.$wpdb->escape($RealName).'",
239                                                                        user_email              = "$email",
240                                                                        user_url                = "http://",
241                                                                        display_name    = "'.$wpdb->escape($name).'"
242                                                        ');
243                        }// End foreach($users as $user)
244                        echo __('<p>Done! <strong>'.$count.'</strong> users imported.<br /><br /></p>');
245                       
246                }// End if(is_array($users)
247                return false;
248               
249        }// End function user2wp()
250       
251        function cat2wp($categories='') 
252        {
253                // General Housekeeping
254                global $wpdb;
255                $wpdb->query('DELETE FROM '.$wpdb->categories);
256               
257                // Do the Magic
258                if(is_array($categories))
259                {
260                        echo __('<p>Importing Categories...<br /><br /></p>');
261                        foreach ($categories as $category) 
262                        {
263                                $count++;
264                                extract($category);
265                               
266                                if(category_exists($name))
267                                        echo __('Category already Imported');
268                                else
269                                {
270                                        $wpdb->query('INSERT INTO '.$wpdb->categories.' SET
271                                                                                cat_ID                                  = '.$id.',
272                                                                                category_nicename               = "'.$name.'",
273                                                                                cat_name                                = "'.$title.'",
274                                                                                category_parent                 = 0,
275                                                                                category_description    = ""
276                                                                ');
277                       
278                                }
279                        }
280                        echo __('<p>Done! <strong>'.$count.'</strong> categories imported.<br /><br /></p>');
281                        return true;
282                }
283                else 
284                {
285                        return false;
286                }
287        }
288       
289        function posts2wp($posts='')
290        {
291                // General Housekeeping
292                global $wpdb;
293                // Empty the Tables for new data
294                $wpdb->query('DELETE FROM '.$wpdb->post2cat);                           
295                $wpdb->query('DELETE FROM '.$wpdb->posts);
296                       
297                $catarr = get_option('txp_cats');
298               
299                // Do the Magic
300                if(is_array($posts))
301                {
302                        echo __('<p>Importing Posts...<br /><br /></p>');
303                        foreach($posts as $post)
304                        {
305                                $count++;
306                                extract($post);
307                               
308                                if(post_exists($wpdb->escape($Title)))
309                                        echo __('Post Already Imported');
310                                else 
311                                {
312                                        // WordPressify Textpattern data
313                                        $cat1 = (in_array($post['Category1'], $catarr)) ? array_search($post['Category1'], $catarr) : '';
314                                        $cat2 = (in_array($post['Category2'], $catarr)) ? array_search($post['Category2'], $catarr) : '';
315                                        $author = $wpdb->get_row('SELECT * FROM '.$wpdb->users.' WHERE user_nicename = "$AuthorID" OR user_login = "$AuthorID"',ARRAY_A);
316                                        $authorid = ($author) ? $author['ID'] : 1;
317                               
318                                        switch ( $Status )
319                                        {
320                                                case 1 :                                                        # TXP Status: Draft
321                                                case 3 :                                                        # TXP Status: Pending
322                                                        $post_status = 'draft';
323                                                        break;
324                                                case 2 :                                                        # TXP Status: Hidden
325                                                        $post_status = 'private';
326                                                        break;
327                                                case 4 :                                                        # TXP Status: Live
328                                                case 5 :                                                        # TXP Status: Static
329                                                default :
330                                                        $post_status = 'publish';
331                                                        break;
332                                        }
333                                       
334                                       
335                                        // Make Post-to-Category associations
336                                       
337                                        if($cat1 != '')
338                                        {
339                                                $wpdb->query('INSERT INTO '.$wpdb->post2cat.' SET
340                                                                                        post_id                         = $ID,
341                                                                                        category_id             = $cat1
342                                                                         ');
343                                        }
344                                        if($cat2 != '')
345                                        {
346                                                $wpdb->query('INSERT INTO '.$wpdb->post2cat.' SET
347                                                                                        post_id                         = $ID,
348                                                                                        category_id             = $cat2
349                                                                         ');
350                                        }
351                                        // Import Post data into WordPress
352                                        $wpdb->query('INSERT INTO '.$wpdb->posts.' SET
353                                                                                ID                                      = '.$ID.',
354                                                                                post_date                       = "'.$Posted.'",
355                                                                                post_date_gmt           = "'.$post_date_gmt.'",
356                                                                                post_author                     = '.$authorid.',
357                                                                                post_modified           = "'.$LastMod.'",
358                                                                                post_modified_gmt       = "'.$post_modified_gmt.'",
359                                                                                post_title                      = "'.$wpdb->escape($Title).'",
360                                                                                post_content            = "'.$wpdb->escape($Body).'",
361                                                                                post_excerpt            = "'.$wpdb->escape($Excerpt).'",
362                                                                                post_status                     = "'.$post_status.'",
363                                                                                post_name                       = "'.$url_title.'",
364                                                                                comment_count           = '.$comments_count);
365                                }
366                        }
367                        echo __('<p>Done! <strong>'.$count.'</strong> posts imported.<br /><br /></p>');
368                        return true;   
369                }
370                else 
371                {
372                        return false;
373                }
374        }
375       
376        function comments2wp($comments='')
377        {
378                // General Housekeeping
379                global $wpdb;
380                // Clear Comments table
381                $wpdb->query('DELETE FROM '.$wpdb->comments);
382               
383                // Magic Mojo
384                if(is_array($comments))
385                {
386                        echo __('<p>Importing Comments...<br /><br /></p>');
387                        foreach($comments as $comment)
388                        {
389                                $count++;
390                                extract($comment);
391                               
392                                if(comment_exists($wpdb->escape($comment['name']), $wpdb->escape($comment['posted'])))
393                                {
394                                        echo __('Comment Already Imported...');
395                                }
396                               
397                                // WordPressify Data
398                                $comment_ID = ltrim($discussid, '0');
399                                $comment_approved = (1 == $visible) ? 1 : 0;
400                               
401                                // The query
402                                $wpdb->query('INSERT INTO '.$wpdb->comments.' SET
403                                                                        comment_ID                              = '.$comment_ID.',
404                                                                        comment_post_ID                 = '.$parentid.',
405                                                                        comment_author                  = "'.$wpdb->escape($name).'",
406                                                                        comment_author_email    = "'.$wpdb->escape($email).'",
407                                                                        comment_author_url              = "'.$wpdb->escape($web).'",
408                                                                        comment_author_ip               = "'.$ip.'",
409                                                                        comment_date                    = "'.$posted.'",
410                                                                        comment_content                 = "'.$wpdb->escape($message).'",
411                                                                        comment_approved                = "'.$comment_approved.'"
412                                                         ');
413                        }
414                        echo __('<p>Done! <strong>'.$count.'</strong> comments imported.<br /><br /></p>');
415                        return true;
416                }
417                else 
418                {
419                        return false;
420                }
421               
422        }
423
424        function import_categories() 
425        {       
426                // Category Import     
427                $cats = $this->get_txp_cats();
428                $this->cat2wp($cats);
429                add_option('txp_cats', $cats);
430                       
431                _e('<form action="admin.php?import=textpattern&amp;step=3" method="post">');
432                _e('<input type="submit" name="submit" value="Next Step" />');
433                _e('</form>');
434
435        }
436       
437        function import_posts()
438        {
439                // Post Import
440                $posts = $this->get_txp_posts();
441                $this->posts2wp($posts);
442               
443                _e('<form action="admin.php?import=textpattern&amp;step=4" method="post">');
444                _e('<input type="submit" name="submit" value="Next Step" />');
445                _e('</form>');
446        }
447
448        function import_comments()
449        {
450                // Comment Import
451                $comments = $this->get_txp_comments();
452                $this->comments2wp($comments);
453               
454                _e('<form action="admin.php?import=textpattern&amp;step=5" method="post">');
455                _e('<input type="submit" name="submit" value="Finish" />');
456                _e('</form>');
457        }
458
459        function import_users()
460        {
461                // User Import
462                $users = $this->get_txp_users(); 
463                $this->users2wp($users);
464               
465                _e('<form action="admin.php?import=textpattern&amp;step=2" method="post">');
466                _e('<input type="submit" name="submit" value="Next Step" />');
467                _e('</form>');
468               
469
470        }
471       
472        function cleanup_txpimport()
473        {
474                delete_option('tpre');
475                delete_option('txp_cats');
476                $this->tips();
477        }
478       
479        function tips()
480        {
481                echo'<p>Welcome to WordPress.  We hope (and expect!) that you will find this platform incredibly rewarding!  As a new WordPress user coming from Textpattern, there are some things that we would like to point out.  Hopefully, they will help your transition go as smoothly as possible.</p>';
482                echo'<h3>Users</h3>';
483                echo'<p>You have already setup WordPress and have been assigned an administrative login and password.  Forget it.  You didn\'t have that login in Textpattern, why should you have it here?  Instead we have taken care to import all of your users into our system.  Unfortunately there is one downside.  Because both WordPress and Textpattern uses a strong encryption hash with passwords, it is impossible to decrypt it and we are forced to assign temporary passwords to all your users.  <strong>Every user has the same username, but their passwords are reset to password123.</strong>  This includes you.  So <a href="/wp-login.php">Login</a> and change it.</p>';
484                echo'<h3>Preserving Authors</h3>';
485                echo'<p>Secondly, we have attempted to preserve post authors.  If you are the only author or contributor to your blog, then you are safe.  In most cases, we are successful in this preservation endeavor.  However, if we cannot ascertain the name of the writer due to discrepancies between database tables, we assign it to you, the administrative user.</p>';
486                echo'<h3>Textile</h3>';
487                echo'<p>Also, since you\'re coming from Textpattern, you probably have been using Textile to format your comments and posts.  If this is the case, we recommend downloading and installing <a href="http://www.huddledmasses.org/2004/04/19/wordpress-plugin-textile-20/">Textile for WordPress</a>.  Trust me... You\'ll want it.</p>';
488                echo'<h3>WordPress Resources</h3>';
489                echo'<p>Finally, there are numerous WordPress resources around the internet.  Some of them are:</p>';
490                echo'<ul>';
491                echo'<li><a href="http://www.wordpress.org">The official WordPress site</a></li>';
492                echo'<li><a href="http://wordpress.org/support/">The WordPress support forums</li>';
493                echo'<li><a href="http://codex.wordpress.org">The Codex (In other words, the WordPress Bible)</a></li>';
494                echo'</ul>';
495                echo'<p>That\'s it! What are you waiting for? Go <a href="/wp-login.php">login</a>!</p>';
496        }
497       
498        function dispatch() 
499        {
500                if (empty ($_GET['step']))
501                        $step = 0;
502                else
503                        $step = (int) $_GET['step'];
504
505                $this->header();
506               
507                switch ($step) 
508                {
509                        default:
510                        case 0 :
511                                $this->greet();
512                                break;
513                        case 1 :
514                                $this->import_users();
515                                break;
516                        case 2 :
517                                $this->import_categories();
518                                break;
519                        case 3 :
520                                $this->import_posts();
521                                break;
522                        case 4 :
523                                $this->import_comments();
524                                break;
525                        case 5 :
526                                $this->cleanup_txpimport();
527                                break;
528                }
529               
530                $this->footer();
531        }
532
533        function Textpattern_Import() 
534        {
535                // Nothing.     
536        }
537}
538
539$txpdb = new wpdb($txpcfg['user'], $txpcfg['pass'], $txpcfg['db'], $txpcfg['host']);
540
541$txp_import = new Textpattern_Import();
542register_importer('textpattern', 'Textpattern', __('Import posts from a Textpattern Blog'), array ($txp_import, 'dispatch'));
543?>