Created
January 10, 2018 17:29
-
-
Save amoutiers/991da535cc2afef8c1bb0c56326a378e to your computer and use it in GitHub Desktop.
A basic php script to migrate from Drupal to Hugo, needs "html2markdown" installed on the server
This file contains 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 | |
define('DRUPAL_ROOT', __DIR__); | |
include_once(DRUPAL_ROOT . '/includes/bootstrap.inc'); | |
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); | |
$nids = db_query('SELECT DISTINCT(nid) FROM {node}') | |
->fetchCol(); | |
$nodes = node_load_multiple($nids); | |
foreach($nodes as $node) { | |
$front_matter = array( | |
'title' => $node->title, | |
'date' => date('c', $node->created), | |
'lastmod' => date('c', $node->changed), | |
'draft' => 'false', | |
); | |
if (count($node->taxonomy_vocabulary_2[LANGUAGE_NONE])) { | |
$tags = taxonomy_term_load_multiple( | |
array_column( | |
$node->taxonomy_vocabulary_2[LANGUAGE_NONE], | |
'tid' | |
) | |
); | |
$front_matter['tags'] = array_column($tags, 'name'); | |
} | |
if (count($node->taxonomy_vocabulary_1[LANGUAGE_NONE])) { | |
$cat = taxonomy_term_load_multiple( | |
array_column( | |
$node->taxonomy_vocabulary_1[LANGUAGE_NONE], | |
'tid' | |
) | |
); | |
$front_matter['categories'] = array_column($cat, 'name'); | |
} | |
$path = drupal_get_path_alias('node/'.$node->nid); | |
if ($path != 'node/'.$node->nid) { | |
$front_matter['url'] = '/'.$path; | |
$content_dir = explode('/', $path); | |
$content_dir = end($content_dir); | |
} | |
else { | |
$content_dir = $node->nid; | |
} | |
$content = json_encode( | |
$front_matter, | |
JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE | |
); | |
$content .= "\n\n"; | |
$tmp_file = '/tmp/node.html'; | |
file_put_contents($tmp_file, $node->body['fr'][0]['value']); | |
$body = shell_exec('html2markdown '.$tmp_file); | |
unlink($tmp_file); | |
//$body = $node->body['fr'][0]['value']; | |
$content .= $body; | |
$dir_name = '/tmp/hugo/content/'.$node->type.'/'.$content_dir; | |
mkdir($dir_name, 0777, true); | |
file_put_contents($dir_name.'/index.md', $content); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same problem as rickysarraf - language was undefined on nodes, plus script hard codes 'fr' on line 60. Switch 'fr' to 'und' and node content is exported where possible.
Was also migrating a site that was previously hosted in Aegir. Needed to run the script with drush 'drush php-script drupal7_to_hugo.php'.
With those changes it worked well. Thank you @amoutiers for sharing.