Just this past week I migrated this blog over to Jekyll from Drupal. I had been using Drupal for the last couple years and I was getting frustrated with how many steps it took to post articles. So I decided to make the switch.
Looking that the sparse Jekyll documentation it appeared that migrating the content was going to be pretty easy. It was once I figured out that wouldn’t be able to get mysql plus to compile on windows. I was finally able to install jekyll on my hosting account and export the content that way.
It seemed like a big hurdle to get all of the ruby gems to work on windows just to export the content. So after I got my blog fully migrated over I decided to write this migration script in php.
<?php | |
/** | |
* php drupal to jekyll exporter | |
* Jesse Baird <jebaird.com> | |
* | |
* http://jebaird.com/2013/05/17/durpal-export-to-jekyll-via-php.html | |
* | |
* This script exports blog and page types with the tags attached. It also sets the right url alias redirects | |
* blog types are saved in _post/ | |
* page pages aee saved in pages/ | |
* | |
* | |
* node | |
* -taxomony | |
* page | |
* | |
* | |
* tables | |
* url_alias | |
* node | |
* | |
* term_data =tags | |
*/ | |
$host = 'localhost:3306'; | |
$username = 'root'; | |
$password = ''; | |
$db = 'drupal_jebaird'; | |
//add any other types you would like to export, you will have to tweak the settings below | |
$nodeTypes = array( | |
'blog', | |
'page' | |
); | |
$fileHeader =<<<J | |
--- | |
layout: {layout} | |
title: {title} | |
tags: [{tags}] | |
created: {created} | |
changed: {changed} | |
--- | |
J; | |
$redirectHeader = <<<R | |
--- | |
layout: redirect | |
refresh_to_post_id: {url} | |
--- | |
R; | |
//######################## | |
function cleanString( $str ){ | |
$str = trim($str); | |
$str = str_replace(' ', '-', $str ); | |
//remove all non word chars except - | |
$str = preg_replace('#[^\w-]#','',$str); | |
//clean up --asdfsf-----asfdsaf- | |
$str = preg_replace('#-{2,100}#', '', $str ); | |
return strtolower($str); | |
} | |
function template( $string, $array ){ | |
foreach ($array as $key => $value) { | |
$string = str_replace('{'.$key.'}', $value, $string ); | |
} | |
return $string; | |
} | |
mkdir('./_posts'); | |
mkdir('./pages'); | |
$con = mysql_connect($host,$username,$password); | |
mysql_select_db($db, $con); | |
/* | |
* Load up terms | |
* cache them so we dont have to make fancy db quries | |
*/ | |
$termsDB = array(); | |
$res = mysql_query("SELECT * from term_data"); | |
while( $term = mysql_fetch_assoc($res) ){ | |
$termsDB[ $term['tid'] ] = $term[ 'name' ]; | |
} | |
foreach ( $nodeTypes as $type ) { | |
$res = mysql_query("SELECT * FROM `node`, node_revisions WHERE node.nid = node_revisions.nid and node.type='" . $type."'", $con); | |
while( $node = mysql_fetch_assoc($res) ){ | |
/* | |
* save the file on the fs with post file format or page title | |
* setup redirects | |
*/ | |
$cleanedTitle = cleanString($node['title']); | |
$savePath = './pages/'; | |
$fileName = $cleanedTitle.'.md'; | |
$fileContents = template($fileHeader, $node); | |
$jekyllvars = array( | |
'layout' => 'default', | |
'tags' => '' | |
); | |
$newUrl = '/pages/'.$cleanedTitle.'.html'; | |
if( $type == 'blog' ){ | |
$cdate = $node['created']; | |
$year = date('Y', $cdate); | |
$month = date('m', $cdate); | |
$day = date('d', $cdate); | |
//file names are Y-M-D-title.md | |
$fileName = $year.'-'.$month.'-'.$day.'-'.$fileName; | |
$savePath ='./_posts/'; | |
//urls are /year.moth/title.html | |
$newUrl ='/'.$year.'/'.$month.'/'.$day.'/'.$cleanedTitle.'.html'; | |
$jekyllvars['layout'] = 'post'; | |
} | |
/* | |
* figure out tags are attached to this node | |
*/ | |
$res2 = mysql_query("SELECT * FROM term_node WHERE nid = ". $node['nid'] ); | |
$postTags = array(); | |
while( $t = mysql_fetch_assoc($res2) ){ | |
$postTags[] = $termsDB[ $t['tid'] ]; | |
} | |
$jekyllvars['tags'] = implode(',', $postTags ); | |
$fileContents = template($fileContents, $jekyllvars); | |
$fileContents.= $node['body']; | |
// echo $cleanedTitle."\n"; | |
// echo $fileContents; | |
//write the post | |
file_put_contents($savePath.$fileName, $fileContents); | |
/* | |
* this is the jekyll var that gets put in layouts/refrsh.html | |
* refresh_to_post_id | |
* | |
* setup redirect | |
*/ | |
$redirects = array( | |
'node/'.$node['nid'] | |
); | |
$res2= mysql_query("SELECT dst FROM url_alias WHERE src = 'node/".$node['nid']."'"); | |
while( $route = mysql_fetch_assoc($res2 ) ){ | |
$path = $route['dst']; | |
$redirects[]= $route['dst']; | |
} | |
foreach( $redirects as $path ){ | |
mkdir($path,'0777',TRUE); | |
file_put_contents($path.'/index.html', template($redirectHeader, array( | |
'url' => $newUrl | |
))); | |
} | |
} | |
} | |
//and bob is your uncle |
I hope this speeds up your migration process. Please feel free to add any tweaks.