Created
October 29, 2011 19:11
-
-
Save lsolesen/1324944 to your computer and use it in GitHub Desktop.
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 | |
abstract class BasicUsersMigration extends Migration | |
{ | |
public function __construct() | |
{ | |
parent::__construct(); | |
// With migrate_ui enabled, migration pages will indicate people involved in | |
// the particular migration, with their role and contact info. We default the | |
// list in the shared class; it can be overridden for specific migrations. | |
$this->team = array( | |
new MigrateTeamMember('Lars Olesen', '[email protected]', t('Webmaster')), | |
new MigrateTeamMember('Jeppe Bjørn Vejlø', '[email protected]', t('Elevforeningen')), | |
); | |
} | |
} | |
class UserTermMigration extends BasicUsersMigration { | |
public function __construct() { | |
parent::__construct(); | |
// Human-friendly description of your migration process. Be as detailed as you | |
// like. | |
$this->description = t('Migrate year from the source database to taxonomy terms'); | |
// Create a map object for tracking the relationships between source rows | |
// and their resulting Drupal objects. Usually, you'll use the MigrateSQLMap | |
// class, which uses database tables for tracking. Pass the machine name | |
// (BeerTerm) of this migration to use in generating map and message tables. | |
// And, pass schema definitions for the primary keys of the source and | |
// destination - we need to be explicit for our source, but the destination | |
// class knows its schema already. | |
$this->map = new MigrateSQLMap($this->machineName, | |
array( | |
'keyword' => array('type' => 'varchar', | |
'length' => 255, | |
'not null' => TRUE, | |
'description' => 'Term', | |
) | |
), | |
MigrateDestinationTerm::getKeySchema() | |
); | |
// We are getting data from tables in the Drupal default database - first, | |
// set up a query for this data. | |
$query = db_select('keyword', 'keyword'); | |
$query->distinct('keyword'); | |
$query->innerJoin('keyword_x_object', 'x', 'keyword.id = x.keyword_id'); //and join in a table | |
$query->innerJoin('contact', 'contact', 'contact.id = x.belong_to'); //and join in a table | |
$query | |
->fields('keyword', array('keyword')) | |
// This sort assures that parents are saved before children. | |
->condition('keyword.type', 'contact') //add a condition - the third argument defaults to '=' | |
->condition('x.intranet_id', 9); //add a condition - the third argument defaults to '=' | |
$query->orderBy('keyword.keyword', 'ASC'); | |
// Create a MigrateSource object, which manages retrieving the input data. | |
$this->source = new MigrateSourceSQL($query); | |
// Set up our destination - terms in the migrate_example_beer_styles vocabulary | |
$this->destination = new MigrateDestinationTerm('aargang'); | |
// Assign mappings TO destination fields FROM source fields. To discover | |
// the names used in these calls, use the drush commands | |
// drush migrate-fields-destination BeerTerm | |
// drush migrate-fields-source BeerTerm | |
$this->addFieldMapping('name', 'keyword') | |
->description(t('Keyword is the name')); | |
// Mappings are assigned issue groups, by which they are grouped on the | |
// migration info page when the migrate_ui module is enabled. The default | |
// is 'Done', indicating active mappings which need no attention. A | |
// suggested practice is to use groups of: | |
// Do Not Migrate (or DNM) to indicate source fields which are not being used, | |
// or destination fields not to be populated by migration. | |
// Client Issues to indicate input from the client is needed to determine | |
// how a given field is to be migrated. | |
// Implementor Issues to indicate that the client has provided all the | |
// necessary information, and now the implementor needs to complete the work. | |
/* | |
$this->addFieldMapping(NULL, 'hoppiness') | |
->description(t('This info will not be maintained in Drupal')) | |
->issueGroup(t('DNM')); | |
*/ | |
// It is good practice to account for all source and destination fields | |
// explicitly - this makes sure that everyone understands exactly what is | |
// being migrated and what is not. Also, migrate_ui highlights unmapped | |
// fields, or mappings involving fields not in the source and destination, | |
// so if (for example) a new field is added to the destination field it's | |
// immediately visible, and you can find out if anything needs to be | |
// migrated into it. | |
$this->addFieldMapping('description') | |
->issueGroup(t('DNM')); | |
} | |
} | |
class UsersMigration extends BasicUsersMigration | |
{ | |
function __construct() | |
{ | |
parent::__construct(); | |
//$this->dependencies = array('UserTerm'); | |
$this->description = t('Migrate users from intraface'); | |
$this->map = new MigrateSQLMap($this->machineName, | |
array('number' => array( | |
'type' => 'int', | |
'not null' => TRUE, | |
'description' => 'User ID.' | |
), | |
), | |
MigrateDestinationTerm::getKeySchema() | |
); | |
$query = db_select('contact', 'contact'); //bring in a table $query | |
$query->innerJoin('address', 'address', 'contact.id = address.belong_to_id'); //and join in a table | |
$query->leftJoin('keyword_x_object', 'x', 'x.belong_to = contact.id'); //and join in a table | |
$query->leftJoin('keyword', 'keyword', 'keyword.id = x.keyword_id'); //and join in a table | |
$query | |
->fields('contact', array('number', 'code', 'date_created')) //get some fields | |
->fields('address', array('name', 'address', 'postcode', 'city', 'country', 'email', 'phone')) //get some fields | |
->condition('contact.active', '1') //add a condition - the third argument defaults to '=' | |
// ->condition('address.type', 1) //add a condition - the third argument defaults to '=' | |
->condition('address.active', '1') //add a condition - the third argument defaults to '=' | |
->condition('address.email', '', '<>'); //add a condition - the third argument defaults to '=' | |
$query->orderBy('address.name', 'ASC'); | |
$query->groupBy('contact.id'); | |
$query->addExpression('GROUP_CONCAT(keyword.keyword)', 'keywords'); | |
$this->source = new MigrateSourceSQL($query); | |
$this->destination = new MigrateDestinationUser(); // Drupal user | |
$this->addFieldMapping('field_aargang', 'keywords') | |
->separator(','); | |
// Required fields that we will map | |
$this->addFieldMapping('pass', 'code'); | |
$this->addFieldMapping('mail', 'email') | |
->defaultValue('[email protected]'); | |
$this->addFieldMapping('name', 'number'); | |
$this->addFieldMapping('created', 'date_created'); | |
$this->addFieldMapping('status') | |
->defaultValue(1); | |
// Adress fields to be added | |
$arguments = array( | |
'name_line' => array('source_field' => 'name'), | |
'firstname' => array('source_field' => 'first_name'), | |
'lastname' => array('source_field' => 'last_name'), | |
'thoroughfare' => array('source_field' => 'address'), | |
'locality' => array('source_field' => 'city'), | |
'postal_code' => array('source_field' => 'postcode'), | |
); | |
$this->addFieldMapping('field_address', 'country', $arguments) | |
->description('Address field'); | |
// Custom fields to be mapped | |
//$this->addFieldMapping('field_phone', 'phone'); | |
// Addressfield - Since the excerpt is mapped via an argument, add a null mapping so it's | |
// not flagged as unmapped. | |
$this->addFieldMapping(NULL, 'address'); | |
$this->addFieldMapping(NULL, 'city'); | |
$this->addFieldMapping(NULL, 'postcode'); | |
// These fields will not be mapped in destination | |
$this->addFieldMapping('last_login_date') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('access') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('signature') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('init') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('picture') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('language') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('timezone') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('signature_format') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('theme') | |
->issueGroup(t('DNM')); | |
$this->addFieldMapping('roles') | |
->issueGroup(t('DNM')); | |
} | |
function prepareRow($row) | |
{ | |
$row->date_created = strtotime($row->date_created); | |
$row->first_name = $row->name; | |
$row->last_name = $row->name; | |
return TRUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment