{"id":50880,"date":"2011-03-20T16:43:46","date_gmt":"2011-03-20T05:43:46","guid":{"rendered":"http:\/\/riscy.biz\/?p=50880"},"modified":"2011-03-20T22:47:14","modified_gmt":"2011-03-20T11:47:14","slug":"converting-from-drupal-to-wordpress","status":"publish","type":"post","link":"https:\/\/riscy.biz\/index.php\/2011\/03\/20\/converting-from-drupal-to-wordpress\/","title":{"rendered":"Converting from Drupal to WordPress"},"content":{"rendered":"<p>Well after looking at this <a href=\"http:\/\/9seeds.com\/news\/drupal-to-wordpress-migration\/\">site<\/a> I modified the existing Drupal database to remove all references to the img_assist module. What a pain in the arse that module is, there is no way to easily migrate away from it so I wrote a Perl script to change the tags to &lt;img&gt; tags instead.<\/p>\n<blockquote><p>use DBI;<br \/>\nmy $databasename = &#8220;drupal&#8221;;<br \/>\nmy $servername = &#8220;192.168.1.1&#8221;;<br \/>\nmy $port = &#8220;3306&#8221;;<br \/>\nmy $dsn = &#8220;dbi:mysql:$databasename:$servername:$port&#8221;;<\/p>\n<p># set the user and password<br \/>\nmy $user = &#8216;xxxxx&#8217;;<br \/>\nmy $pass = &#8216;xxxxx&#8217;;<\/p>\n<p># now connect and get a database handle<br \/>\nmy $dbh = DBI-&gt;connect($dsn, $user, $pass)<br \/>\nor die &#8220;Can\u2019t connect to the DB: $DBI::errstr\\n&#8221;;<\/p>\n<p>my $query1=&#8221;Select * from node_revisions LEFT JOIN node ON (node_revisions.nid=node.nid) WHERE node.type=&#8217;blog'&#8221;;<br \/>\nmy $action = $dbh-&gt;prepare($query1);<br \/>\n$action-&gt;execute();<\/p>\n<p>while (@row = $action-&gt;fetchrow()) {<br \/>\n$body=$row[4];<br \/>\nwhile ($body=~m\/\\[img_assist\\|nid=(\\d+)\\|.*\\]\/){<br \/>\n$vid=$row[1];<br \/>\n$width=450;<br \/>\nmy $query2=&#8221;Select filepath FROM files LEFT JOIN image ON (files.fid=image.fid) WHERE nid=$1 AND image_size=&#8217;_original'&#8221;;<br \/>\nmy $action2= $dbh-&gt;prepare($query2);<br \/>\n$action2-&gt;execute();<br \/>\n@row2 = $action2-&gt;fetchrow();<br \/>\n$body=~s\/\\[img_assist\\|nid=(\\d+)\\|.*\\]\/&lt;p style=&#8221;text-align: center;&#8221;&gt;&lt;img src=&#8221;$row2[0]&#8221; width=&#8221;$width&#8221;&gt;&lt;\\\/p&gt;\/;<\/p>\n<p>($escapequotes=$body)=~s\/\\&#8217;\/\\&#8217;\\&#8217;\/g;<br \/>\nmy $query3=&#8221;UPDATE node_revisions SET body=&#8217;$escapequotes&#8217;,teaser=&#8217;$escapequotes&#8217; WHERE vid=$vid&#8221;;<br \/>\nprint &#8220;$vid\\n&#8221;;<br \/>\nmy $action3 = $dbh-&gt;prepare($query3);<br \/>\n$action3-&gt;execute();<br \/>\n}<br \/>\nwhile ($body=~m\/\\&lt;img.*src\\=\\&#8221;\\?q\\=image\\\/view\\\/(\\d+).*\\&gt;\/){<\/p>\n<p>$vid=$row[1];<br \/>\n$nid=$1;<br \/>\n$width=450;<br \/>\nmy $query2=&#8221;Select filepath FROM files LEFT JOIN image ON (files.fid=image.fid) WHERE nid=$nid AND image_size=&#8217;_original'&#8221;;<br \/>\nmy $action2= $dbh-&gt;prepare($query2);<br \/>\n$action2-&gt;execute();<br \/>\n@row2 = $action2-&gt;fetchrow();<br \/>\n$body=~s\/\\&lt;img.*src\\=\\&#8221;\\?q\\=image\\\/view\\\/$nid.*\\&gt;\/&lt;img src=&#8221;$row2[0]&#8221; width=&#8221;450&#8243;&gt;\/;<\/p>\n<p>($escapequotes=$body)=~s\/\\&#8217;\/\\&#8217;\\&#8217;\/g;<br \/>\nmy $query3=&#8221;UPDATE node_revisions SET body=&#8217;$escapequotes&#8217;,teaser=&#8217;$escapequotes&#8217; WHERE vid=$vid&#8221;;<br \/>\nprint &#8220;$vid\\n&#8221;;<br \/>\nmy $action3 = $dbh-&gt;prepare($query3);<br \/>\n$action3-&gt;execute();<br \/>\n}<br \/>\n}<br \/>\nprint &#8220;Finished\\n&#8221;;<\/p><\/blockquote>\n<p>It&#8217;s a bit ugly but works, I had to go through twice because some of the tags were using older versions of img_assist, again what a dumb idea. If only the entries had been migrated as the module was updated. Anyway that was done. Then I modified the import script linked at the start of this post to this:<\/p>\n<blockquote><p># This set of instructions was updated by 9seeds.com based on<br \/>\n# a post by Mike Smullin at mikesmullin.com<br \/>\n#<br \/>\n# Mike&#8217;s original post can be found here:<br \/>\n# http:\/\/www.mikesmullin.com\/development\/migrate-convert-import-drupal-5-to-wordpress-27\/<br \/>\n#<br \/>\n# 9seeds&#8217; updated post can be found here:<br \/>\n# http:\/\/9seeds.com\/news\/drupal-to-wordpress-migration<\/p>\n<p># Clear all existing WordPress content<br \/>\nTRUNCATE TABLE wordpress.wp_comments;<br \/>\nTRUNCATE TABLE wordpress.wp_links;<br \/>\nTRUNCATE TABLE wordpress.wp_postmeta;<br \/>\nTRUNCATE TABLE wordpress.wp_posts;<br \/>\nTRUNCATE TABLE wordpress.wp_term_relationships;<br \/>\nTRUNCATE TABLE wordpress.wp_term_taxonomy;<br \/>\nTRUNCATE TABLE wordpress.wp_terms;<\/p>\n<p># Create Categories<br \/>\nINSERT INTO wordpress.wp_terms (term_id, `name`, slug, term_group)<br \/>\nSELECT<br \/>\nd.tid, d.name, REPLACE(LOWER(d.name), &#8216; &#8216;, &#8216;_&#8217;), 0<br \/>\nFROM drupal.term_data d<br \/>\nINNER JOIN drupal.term_hierarchy h<br \/>\nUSING(tid);<\/p>\n<p># Add Taxonomies<br \/>\nINSERT INTO wordpress.wp_term_taxonomy (term_id, taxonomy, description, parent)<br \/>\nSELECT<br \/>\nd.tid `term_id`,<br \/>\n&#8216;category&#8217; `taxonomy`,<br \/>\nd.description `description`,<br \/>\nh.parent `parent`<br \/>\nFROM drupal.term_data d<br \/>\nINNER JOIN drupal.term_hierarchy h<br \/>\nUSING(tid);<\/p>\n<p># Import posts\/pages<br \/>\nINSERT INTO wordpress.wp_posts (id, post_date, post_content, post_title, post_excerpt, post_name, post_modified, post_type, `post_status`)<br \/>\nSELECT DISTINCT<br \/>\nn.nid `id`,<br \/>\nFROM_UNIXTIME(n.created) `post_date`,<br \/>\nr.body `post_content`,<br \/>\nn.title `post_title`,<br \/>\nr.teaser `post_excerpt`,<br \/>\nIF(SUBSTR(a.dst, 11, 1) = &#8216;\/&#8217;, SUBSTR(a.dst, 12), a.dst) `post_name`,<br \/>\nFROM_UNIXTIME(n.changed) `post_modified`,<br \/>\nn.type `post_type`,<br \/>\nIF(n.status = 1, &#8216;publish&#8217;, &#8216;private&#8217;) `post_status`<br \/>\nFROM drupal.node n<br \/>\nINNER JOIN drupal.node_revisions r<br \/>\nUSING(vid)<br \/>\nLEFT OUTER JOIN drupal.url_alias a<br \/>\nON a.src = CONCAT(&#8216;node\/&#8217;, n.nid)<br \/>\nWHERE n.type IN (&#8216;blog&#8217;);<\/p>\n<p># Turn articles in to posts<br \/>\nupdate wordpress.wp_posts set post_type=&#8217;post&#8217; where post_type=&#8217;blog&#8217;;<\/p>\n<p># Add post to category relationships<br \/>\nINSERT INTO wordpress.wp_term_relationships (object_id, term_taxonomy_id)<br \/>\nSELECT nid, tid FROM drupal.term_node;<\/p>\n<p># Update category count<br \/>\nUPDATE wordpress.wp_term_taxonomy tt<br \/>\nSET `count` = (<br \/>\nSELECT COUNT(tr.object_id)<br \/>\nFROM wordpress.wp_term_relationships tr<br \/>\nWHERE tr.term_taxonomy_id = tt.term_taxonomy_id);<\/p>\n<p># Import comments<br \/>\nINSERT INTO wordpress.wp_comments (comment_post_ID, comment_date, comment_content, comment_parent, comment_author, comment_author_email, comment_author_url, comment_approved)<br \/>\nSELECT nid, FROM_UNIXTIME(timestamp), comment, thread, name, mail, homepage, status FROM drupal.comments;<\/p>\n<p># Update comment counts<br \/>\nuse wordpress;<br \/>\nUPDATE `wp_posts` SET `comment_count` = (SELECT COUNT(`comment_post_id`) FROM `wp_comments` WHERE `wp_posts`.`id` = `wp_comments`.`comment_post_id`);<\/p>\n<p># Fix breaks in post content<br \/>\nUPDATE wordpress.wp_posts SET post_content = REPLACE(post_content, &#8221;, &#8221;);<\/p>\n<p># fix images in post content<br \/>\nUPDATE wordpress.wp_posts SET post_content = REPLACE(post_content, &#8216;&#8221;files\/&#8217;, &#8216;&#8221;wp-content\/uploads\/&#8217;);<br \/>\nUPDATE wordpress.wp_posts SET post_content = REPLACE(post_content, &#8216;&#8221;\/files&#8217;, &#8216;&#8221;wp-content\/uploads\/&#8217;);<br \/>\nUPDATE wordpress.wp_posts SET post_content = REPLACE(post_content, &#8216;&#8221;images\/&#8217;, &#8216;&#8221;wp-content\/uploads\/images\/&#8217;);<\/p>\n<p># fix images in post_excerpt\u00a0 content<br \/>\nUPDATE wordpress.wp_posts SET post_excerpt = REPLACE(post_excerpt, &#8216;&#8221;files\/&#8217;, &#8216;&#8221;wp-content\/uploads\/&#8217;);<br \/>\nUPDATE wordpress.wp_posts SET post_excerpt = REPLACE(post_excerpt, &#8216;&#8221;\/files&#8217;, &#8216;&#8221;wp-content\/uploads\/&#8217;);<br \/>\nUPDATE wordpress.wp_posts SET post_excerpt = REPLACE(post_excerpt, &#8216;&#8221;images\/&#8217;, &#8216;&#8221;wp-content\/uploads\/images\/&#8217;);<\/p>\n<p># fix links<br \/>\nUPDATE wordpress.wp_posts SET post_content = REPLACE(post_content, &#8216;href=&#8221;?q=node\/&#8217;, &#8216;href=&#8221;?p=&#8217;);<br \/>\nUPDATE wordpress.wp_posts SET post_excerpt = REPLACE(post_excerpt, &#8216;href=&#8221;?q=node\/&#8217;, &#8216;href=&#8221;?p=&#8217;);<\/p>\n<p>#remove dud characters<br \/>\nUPDATE wordpress.wp_posts SET post_content = REPLACE(post_content, &#8216;\u00c2&#8217;, &#8221;);<br \/>\nUPDATE wordpress.wp_posts SET post_excerpt = REPLACE(post_excerpt, &#8216;\u00c2&#8217;, &#8221;);<\/p><\/blockquote>\n<p>I added some extra replacements which fixed any links to old nodes and I also had to include post type &#8216;blog&#8217; so that all the entries got moved over. I don&#8217;t care about pages or stories.<\/p>\n<p>This is by no means a completed product, it works just enough for me to have a WordPress blog running without too much effort. The migration went better on <a href=\"http:\/\/littlefishcreations.com\">littlefishcreations.com<\/a> this blog still has almost all the images failing. I might get around to fixing it one day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well after looking at this site I modified the existing Drupal database to remove all references to the img_assist module. What a pain in the arse that module is, there is no way to easily migrate away from it so &hellip; <a href=\"https:\/\/riscy.biz\/index.php\/2011\/03\/20\/converting-from-drupal-to-wordpress\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[2,3],"class_list":["post-50880","post","type-post","status-publish","format-standard","hentry","category-computers","tag-drupal","tag-wordpress"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/posts\/50880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/comments?post=50880"}],"version-history":[{"count":0,"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/posts\/50880\/revisions"}],"wp:attachment":[{"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/media?parent=50880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/categories?post=50880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/riscy.biz\/index.php\/wp-json\/wp\/v2\/tags?post=50880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}