Last active
September 14, 2016 05:10
-
-
Save raynimmo/9a6a5d109a8d01448b4e to your computer and use it in GitHub Desktop.
Import blog posts from Serendipity to Drupal. Quick and dirty import direct from database.
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 | |
//create temporary data store | |
/* | |
CREATE TABLE blogdata ( | |
id int(11), | |
title varchar(200), | |
timestamp int(10), | |
body text, | |
done int(3) default '0' | |
); | |
*/ | |
//populate it from blog entries | |
//insert into blogdata select id, title, timestamp, concat(body, extended), 0 from serendipity_entries; | |
/*** configuration ***/ | |
/** map s9y user id to drupal id. | |
* the format is s9yID => drupalID | |
* entries not found in this map are assigned to uid 1, which is the admin user. | |
*/ | |
//$usermap = array(1=>1); | |
define('DRUPAL_ROOT', getcwd()); | |
echo "script start"; | |
require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; | |
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); | |
error_reporting(E_ALL); | |
//$mappings = ''; //hold url mapping info | |
//$q = mysql_query("select id, title, timestamp, last_modified, CONCAT(body,extended) as body, authorid, comments from serendipity_entries order by timestamp asc"); | |
//$q = mysql_query("select * from `blogdatafromimport` where done=0 order by timestamp asc"); | |
$table = 'database_table_name'; | |
$db = mysql_connect('localhost:8888','username','password') or die("Database error"); | |
mysql_select_db($table, $db); | |
//$querystring = "select * from `serendipity_entries` where done=0 order by timestamp asc;"; | |
//$querystring = "select id, title, timestamp, last_modified, CONCAT(body,extended) as body, authorid, comments from serendipity_entries order by timestamp asc"; | |
$querystring = "select * from blogdata where done=0 order by timestamp asc"; | |
//$querystring = "select * from blogdata order by timestamp asc"; | |
$result = mysql_query($querystring); | |
echo "<br />rows: " . mysql_num_rows($result); | |
$i=0; | |
while($row = mysql_fetch_assoc($result)) { | |
echo "import " . $i . "<br />"; | |
$i++; | |
$node = new stdClass(); // We create a new node object | |
$node->title = utf8_encode($row['title']); | |
$node->type = "blog"; // Or any other content type you want | |
node_object_prepare($node); // Set some default values. | |
$node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. | |
//$node->path = array('alias' => ''); // Setting a node path | |
$node->uid = 40; // Or any id you wish | |
$node->status = 1; | |
$node->promote = 0; | |
$node->comment = 2; | |
// Let's add standard body field | |
$node->body[$node->language][0]['value'] = utf8_encode($row['body']); | |
$node->body[$node->language][0]['summary'] = text_summary(utf8_encode($row['body'])); | |
// If field has a format, you need to define it. Here we define a default filtered_html format for a body field | |
$node->body[$node->language][0]['format'] = 'full_html'; //filter_default_format(); / | |
$node = node_submit($node); // Prepare node for a submit | |
$node->created = $row['timestamp']; | |
$node->changed = $row['timestamp']; | |
node_save($node); // After this call we'll get a nid | |
echo "<br />"; | |
} | |
$result = mysql_query("update blogdataforimport set done=1"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment