Skip to content

Instantly share code, notes, and snippets.

@rickysarraf
Forked from amoutiers/drupal7_to_hugo.php
Created November 5, 2018 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickysarraf/21c23557cad543603c9419d8123f75b5 to your computer and use it in GitHub Desktop.
Save rickysarraf/21c23557cad543603c9419d8123f75b5 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
<?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