-
-
Save amoutiers/991da535cc2afef8c1bb0c56326a378e to your computer and use it in GitHub Desktop.
<?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); | |
} |
I finally figured it out. I was lazy to look up the php manual but as is the case always, I need to eat my own dog food.
The problem was with:
Array
(
[und] => Array
(
[0] => Array
(
[value] => <p>sg3-utils, version 1.44, was recently <a href="https://tracker.debian.org/pkg/sg3-utils">uploaded</a> to Debian. This new upstream release has happened almost 2.5 years after the last release. One important feature to emphasize about is some support for NVMe disks, which are now getting more common on latest range of laptops.</p>
The language part was reported und
for whatever reason. And I don't want to investigate it. After I marked it accordingly, I have been able to import all my data from Drupal 7 to Hugo.
Thank you so much for writing this script. Me, A web n00b, was able to use it and migrate away from the painful platform that Drupal has become now.
Now, next in my list is, to look for a self-hosted commenting system.
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.
I put some more print statements to just show what exact problem I'm running into: