Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This code snipped with explain you to migrate Video 6.x-2 to Video 6.x-3, and you can still use this as a generic guide to migrate new video version 6.x-3
/**
* @author Heshan Wanigasooriya <heshanmw at gmail dot com>
* Migrate Video 6.x-2 to 6.x-3
*
*/
include_once('./includes/bootstrap.inc');
// disable error reporting for bootstrap process
error_reporting(E_ERROR);
// let's bootstrap: we will be able to use drupal apis
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// enable full error reporting again
error_reporting(E_ALL);
// ***** CONFIGURATION *******
// type of the node, using this you can migrate any type of video nodes to Video 6.3.x
define(NODE_TYPE, 'video');
// your source database
define(DB_FROM, 'db_from');
// your destination database
define(DB_TO, 'db_to');
// table name
define(DB_NAME, 'content_type_video');
// ***** END CONFIGURATION *******
// allow execution only from the command line!
if(empty($_SERVER['REQUEST_METHOD'])) {
migrate_video();
}
else {
print ('This script is only executable from the command line.');
die();
}
/**
* main migrating function
* this will execute batches
*/
function migrate_video() {
db_set_active(DB_FROM);
$num_users = db_result(db_query("SELECT COUNT(nid) FROM {node} WHERE type = '%s'", NODE_TYPE));
echo ('video nodes in the system are : ' . $num_users);
echo "\n";
sleep(2);
$start = 0;
$limit = 100;
while($start < $num_users) {
echo ('executing node limit from ' . $start);
echo "\n";
create_node($start, $limit);
$start = $start + $limit;;
sleep(0.1);
}
}
/**
*
* @param int $start start of the batch
* @param int $offset offset of the batch
*/
function create_node($start, $offset) {
db_set_active(DB_FROM);
$result = db_query_range('SELECT nid, title FROM {node} WHERE type = \'%s\' ORDER BY nid ASC', NODE_TYPE, $start, $offset);
// echo ('SELECT uid, name FROM {users} ORDER BY uid ASC'. $start . $end);
// echo "\n";
while ($account = db_fetch_object($result)) {
echo ('Executing node id ' . $account->nid . ' title ' . $account->title);
echo "\n";
$node = node_old_load($account->nid);
if($node->nid == 0)
continue;
// print_r($node);
// print_r($user);
db_set_active(DB_TO);
node_new_save($node);
db_set_active(DB_FROM);
}
}
/**
* load video nodes from database
* @param int $nid
* @return object node
*/
function node_old_load($nid) {
db_set_active(DB_FROM);
$node = db_fetch_object(
db_query('SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment,
n.promote, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log,
r.format, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid
INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE n.nid = %d AND n.type = \'%s\' ',
$nid, NODE_TYPE));
return $node;
}
/**
*
* @global object $user user object
* @param object $node ndoe of loaded
*/
function node_new_save($node) {
// Let modules modify the node before it is saved to the database.
global $user;
// since we are going to migrate with node ID we can set this to FALSE
// if we want to create as new ID's then we should set to TRUE
$node->is_new = FALSE;
// Set some required fields:
if (empty($node->created)) {
$node->created = time();
}
$node->timestamp = $node->revision_timestamp;
$node->format = isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT;
$old_nid = $node->nid;
// adding CCK field values
$file = file_old_load($old_nid);
if(empty($file)) {
watchdog('video_migration', 'missing node id ' . $old_nid);
return;
}
// load video deetails
$video = video_old_load($nid);
$file->uid = $node->uid;
$file->status = 1;
$file->timestamp = $node->created;
// write to files
$fid = file_save_new($file);
echo ('File id writed : ' . $fid);
echo "\n";
// Generate the node table query and the node_revisions table query.
db_set_active(DB_TO);
drupal_write_record('node_revisions', $node);
drupal_write_record('node', $node);
$nid = db_last_insert_id('node', 'nid');
$vid = db_last_insert_id('node_revisions', 'vid');
// if you do not want to update nodes then you can just create new nodes and
// skip below process
echo ('Updating node nid ' . $nid .' to ' . $old_nid);
echo "\n";
// update with id's
db_query('UPDATE {node_revisions} SET vid = %d WHERE vid = %d', $old_nid, $vid);
db_query('UPDATE {node} SET nid = %d, vid = %d WHERE nid = %d', $old_nid, $old_nid, $nid);
// Update the node access table for this node.
node_access_acquire_grants($node);
db_query('UPDATE {node_access} SET nid = %d WHERE nid = %d', $old_nid, $nid);
// use serialize data
$video_serial_data = unserialize($video->serialized_data);
$thumb_file = file_old_thumbnail_load($video_serial_data['iid']);
// add to field video
//Build the field_video_data record
$data = 'a:3:{s:11:"video_thumb";s:52:"' . $thumb_file->filepath . '";s:5:"width";s:3:"'.
empty($video->width)?$video->width:640 .'";s:15:"video_thumb_fid";N;}';
// write to content table
// make sure you have correct fields on the table
db_query('INSERT INTO {' . DB_NAME . '}
VALUES (%d, %d, %d, %d, \'%s\')',
$old_nid, $old_nid, $fid, 1, $data);
}
/**
*
* @return object video file details
*/
function video_old_load($nid){
db_set_active(DB_FROM);
return db_fetch_object(db_query('SELECT * FROM {video} WHERE nid = %d', $nid));
}
/**
* get the files from
* @param int $nid nodeid
* @return object files object
*/
function file_old_load($nid) {
db_set_active(DB_FROM);
// use this if you do not want to get only converted videos
// return db_fetch_object(db_query('SELECT * FROM {files} WHERE nid = %d', $nid));
// this will only allow you to get converted videos only or FLV only
return db_fetch_object(db_query('SELECT * FROM {files} WHERE nid = %d AND filemime = \'%s\'', $nid, 'application/octet-stream'));
}
/**
* get the files from
* @param int $nid nodeid
* @return object files object
*/
function file_old_thumbnail_load($nid) {
db_set_active(DB_FROM);
// use this if you do not want to get only converted videos
// return db_fetch_object(db_query('SELECT * FROM {files} WHERE nid = %d', $nid));
// this will only allow you to get converted videos only or FLV only
return db_fetch_object(db_query('SELECT * FROM {files} WHERE nid = %d AND filename = \'%s\'', $nid, '_original'));
}
/**
* wirte to files tables
* @param object $file
* @return int fileid
*/
function file_save_new($file) {
db_set_active(DB_TO);
drupal_write_record('files', $file);
return $fid = db_last_insert_id('files', 'fid');
}