You are hereBlogs / willhall's blog / Migration and Validation of Strings

Migration and Validation of Strings


By willhall - Posted on 21 October 2009

Just been hacking away at this to get the idea across of migrating and validating data from one database into the drupal core. Ite relys on the user having created a content type of news from CCK.


<?php
mysql_connect('localhost', 'user', 'password') or die('Cannot connect to MySql');
mysql_select_db('database') or die('Cannot connect to database');

// get the required data from the database
$sql = 'select * from tableName';
$query = mysql_query($sql);

// give it a timestamp
$unixTimestamp = 1230800640; //starting unix timestamp
$increment = 1012745; //could be replace by a rand()

//beginning of the mysql insert sql queries
$insertToNode = "INSERT INTO `node` (`nid`, `vid`, `type`, `language`, `title`, `uid`, `status`, `created`, `changed`, `comment`, `promote`, `moderate`, `sticky`, `tnid`, `translate`) VALUES ";
$insertToNodeCommentStats = "INSERT INTO `node_comment_statistics` (`nid`, `last_comment_timestamp`, `last_comment_name`, `last_comment_uid`, `comment_count`) VALUES ";
$insertToNodeRevisions = "INSERT INTO `node_revisions` (`nid`, `vid`, `uid`, `title`, `body`, `teaser`, `log`, `timestamp`, `format`) VALUES ";

//get the total number of rows
$totalRows = mysql_num_rows($query);

$startNid = 3; // the first Node id (Nid) to be added
$userUid = 1; // the id of the user (1 = admin)

// create the insert queries
while($result = mysql_fetch_assoc($query)){

// sanitise strings ready for insertion
$headline = mysql_real_escape_string($result['headline']);
$content = mysql_real_escape_string($result['content']);
$snippet = mysql_real_escape_string($result['teaser']);

// create looping additois to database
$insertToNode = $insertToNode."($startNid, $startNid, 'news', '', '".$headline."', $userUid, 1, $unixTimestamp, $unixTimestamp, 2, 1, 0, 0, 0, 0)";
$insertToNodeCommentStats = $insertToNodeCommentStats."($startNid, $unixTimestamp, NULL, 1, 0)";
$insertToNodeRevisions = $insertToNodeRevisions."($startNid, $startNid, $userUid, '".$headline."', '".$content."', '".$snippet."', '', $unixTimestamp, 2)";

$startNid++;

$unixTimestamp = $unixTimestamp + $increment;

// put a semi colon to finish query
if($startNid != $totalRows){
$insertToNode = $insertToNode.', ';
$insertToNodeCommentStats = $insertToNodeCommentStats.', ';
$insertToNodeRevisions = $insertToNodeRevisions.', ';
}
}

// if not empty run as mysql_query
if(!empty($insertToNode)){
mysql_select_db('newDatabase') or die ('Cannot connect to database');

$insertToNode = $insertToNode.';';
$insertToNodeCommentStats = $insertToNodeCommentStats.';';
$insertToNodeRevisions = $insertToNodeRevisions.';';

mysql_query($insertToNode);
mysql_query($insertToNodeCommentStats);
mysql_query($insertToNodeRevisions);
}

//echo the data that has been inserted
echo 'Inserted into tablename node: '.$insertToNode;
echo 'Inserted into tablename node_comment_statistics: '.$insertToNodeCommentStats;
echo 'Inserted into tablename node_revisions: '.$insertToNodeRevisions;
?>

@willhallonline

  • exasperated at poor connections, may remove and reinstall everything! 4 hours 15 min ago
  • Government sticking to IE6 http://bit.ly/ceMrg9 "more cost effective in many cases to continue to use IE6" (what?!) 6 hours 42 min ago
  • still the mail server continues to frustrate 6 hours 49 min ago
  • feeling positive today, gonna destroy this damned mail server 11 hours 56 min ago
  • where are the open source business analysts who can help out with the actual running of companies, not just make a website for them 11 hours 57 min ago