Last active
June 27, 2016 12:33
-
-
Save jebaird/5600267 to your computer and use it in GitHub Desktop.
migrate a drupal site to jekyll using php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment