Check this article . It describes how to use migrate module and it's API.
Code from this article:
First of all we need to create module by adding folder with name my_migration adding my_migration.info and my_migration.module files. So lets start, code below will have a lot of comments:
//my_migration.info file
name = "User Migrate"
description = "Migration of data from Drupal 6 to Drupal 7"
package = "Custom Migrate"
core = 7.x
dependencies[] = migrate
dependencies[] = migrate_extras
dependencies[] = field
dependencies[] = file
dependencies[] = text
dependencies[] = path
dependencies[] = taxonomy
files[] = includes/users.inc
//my_migration.module file
// this file can be empty but we will define some constants
<!--?php /*
* Define some constants to use in class
*/
define("SOURCE_DATABASE", 'd6_latest'); // Put drupal 6 db name you should also add settings to settings.php
define("CONNECT_NAME", 'migrate');// name of connection to drupal 6 db in settings.php
define('SOURCE_TERM_GARDEN_CAT', 3); // When we will migrate terms we will use it.
define('SOURCE_URL', 'http://path_to_drupal6.site');
define('MIGRATE67_SITE_DIRECTORY', 'public://');
//my_migrate_migrate.inc you should name this file as module_name_migrate.inc
// if not your migration won't be autoloaded.
<?php
/**
* Implements hook_migrate_api().
*/
function my_migrate_migrate_api() {
$api = array(
'api' =?--> 2,
'migrations' => array(
'MfUserMigrate' => array('class_name' => 'MfUserMigrate'),
),
);
return $api;
}
//my_migration/users.inc
<!--?php /**
* User migration class to migrate users.
*/
class UserMigrate extends Migration {
public function __construct() {
parent::__construct(MigrateGroup::getInstance('UserMigrate'));
// Select fields from the Drupal 6 user table.
$query = Database::getConnection('default', CONNECT_NAME)
-?-->select('users', 'u')
->fields('u', array('uid', 'name', 'pass', 'mail', 'created', 'access',
'login', 'status', 'picture', 'init'));
// This is supposed to get roles not sure how.
$source_fields = array(
'uid' => t('User ID'),
'roles' => t('The set of roles assigned to a user.'),
);
// Set source and destination.
$this->source = new MigrateSourceSQL($query, $source_fields);
$this->destination = new MigrateDestinationUser(array('md5_passwords' => TRUE));
// Set up database maping.
$this->map = new MigrateSQLMap($this->machineName,
array(
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'D6 Unique User ID',
'alias' => 'u',
)
),
MigrateDestinationUser::getKeySchema()
);
// Add simple field mappings
$this->addFieldMapping('is_new')
->defaultValue(TRUE);
$this->addFieldMapping('uid', 'id_to_preserve');
$this->addFieldMapping('name', 'name')->dedupe('users', 'name');
$this->addFieldMapping('pass', 'pass');
$this->addFieldMapping('mail', 'mail')->dedupe('users', 'mail');
$this->addFieldMapping('language')->defaultValue('');
$this->addFieldMapping('theme')->defaultValue('');
$this->addFieldMapping('signature')->defaultValue('');
$this->addFieldMapping('signature_format')->defaultValue('filtered_html');
$this->addFieldMapping('created', 'created');
$this->addFieldMapping('access', 'access');
$this->addFieldMapping('login', 'login');
$this->addFieldMapping('status', 'status');
$this->addFieldMapping('picture')->defaultValue(0);
$this->addFieldMapping('init', 'init');
$this->addFieldMapping('timezone')->defaultValue(NULL);
$this->addFieldMapping('roles', 'roles');
}
// Set up Roles.
// massage the user roles before saving
public function prepareRow($current_row) {
$source_id = $current_row->uid;
$query = Database::getConnection('default', CONNECT_NAME)
->select('users_roles', 'r')
->fields('r', array('uid', 'rid'))
->havingCondition('r.uid', $source_id, '=');
$results = $query->execute();
// add the anonymous role for everyone
$roles = array('1' => '1');
foreach ($results as $row) {
// adjust the old to new role id's
// Editors (rid 7) need role id 4
if ($row->rid == 7) {
$roles['4'] = '4';
}
elseif ($row->rid == 4) {
// producer from 4 to 5
$roles['5'] = '5';
}
elseif ($row->rid == 5) {
// blogger from 5 to 6
$roles['6'] = '6';
}
elseif ($row->rid == 8) {
// freelancer 8 to 7
$roles['7'] = '7';
}
else {
// Collaboraters are role id 5 in both old and new dbs
$roles[$row->rid] = $row->rid;
}
}
$current_row->roles = $roles;
return TRUE;
}
} // close the ourUserMigration class