summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Ryan2011-03-31 01:39:02 (GMT)
committer Mike Ryan2011-03-31 01:39:02 (GMT)
commit0e30a0d655b2182f7a801f72f9b8cd0d2cfbfc6d (patch)
tree70d810c09cb990b854aebcada2e3c1764c3ce1d0
parent0af4ad577d095808a51ac14745b6fd7c54f842bf (diff)
Issue #1101586 by mikeryan: Add shortcut methods for adding several field mappings at once
-rw-r--r--CHANGELOG.txt1
-rw-r--r--includes/migration.inc53
-rw-r--r--migrate_example/beer.inc76
-rw-r--r--migrate_example/wine.inc78
4 files changed, 83 insertions, 125 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index a6ed6d1..4687596 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -3,6 +3,7 @@ Next release
============
Features and enhancements
+- #1101586 - Add shortcut methods for adding several field mappings at once.
- #1101592 - Replace --itemlimit with --limit, supporting time limits as well.
Bug fixes
diff --git a/includes/migration.inc b/includes/migration.inc
index 824c026..e7582ea 100644
--- a/includes/migration.inc
+++ b/includes/migration.inc
@@ -205,6 +205,59 @@ abstract class Migration extends MigrationBase {
}
/**
+ * Shortcut for adding several fields which have the same name on both source
+ * and destination sides.
+ *
+ * @param array $fields
+ * List of field names to map.
+ */
+ protected function addSimpleMappings(array $fields) {
+ foreach ($fields as $field) {
+ $this->addFieldMapping($field, $field);
+ }
+ }
+
+ /**
+ * Shortcut for adding several destination fields which are to be explicitly
+ * not migrated.
+ *
+ * @param array $fields
+ * List of fields to mark as not for migration.
+ *
+ * @param string $issue_group
+ * Issue group name to apply to the generated mappings (defaults to 'DNM').
+ */
+ protected function addUnmigratedDestinations(array $fields, $issue_group = NULL) {
+ if (!$issue_group) {
+ $issue_group = t('DNM');
+ }
+ foreach ($fields as $field) {
+ $this->addFieldMapping($field)
+ ->issueGroup($issue_group);
+ }
+ }
+
+ /**
+ * Shortcut for adding several source fields which are to be explicitly
+ * not migrated.
+ *
+ * @param array $fields
+ * List of fields to mark as not for migration.
+ *
+ * @param string $issue_group
+ * Issue group name to apply to the generated mappings (defaults to 'DNM').
+ */
+ protected function addUnmigratedSources(array $fields, $issue_group = NULL) {
+ if (!$issue_group) {
+ $issue_group = t('DNM');
+ }
+ foreach ($fields as $field) {
+ $this->addFieldMapping(NULL, $field)
+ ->issueGroup($issue_group);
+ }
+ }
+
+ /**
* Reports whether this migration process is complete (i.e., all available
* source rows have been processed).
*/
diff --git a/migrate_example/beer.inc b/migrate_example/beer.inc
index 3357a82..870bcea 100644
--- a/migrate_example/beer.inc
+++ b/migrate_example/beer.inc
@@ -177,26 +177,21 @@ class BeerUserMigration extends BasicExampleMigration {
// unmapped source fields, and unmapped destination fields
// Mapped fields
- $this->addFieldMapping('status', 'status');
+
+ // This is a shortcut you can use when the source and destination fields are
+ // identical
+ $this->addSimpleMappings(array('status', 'mail'));
// Dedupe assures that value is unique. Use it when source data is non-unique.
// Pass the Drupal table and column for determining uniqueness.
$this->addFieldMapping('name', 'name')
->dedupe('users', 'name');
- // Here we have a special case - we want to transform a date/time string
- // into a UNIX timestamp, and also apply a specific timezone. The
- // mapping of posted to created here is primarily for the sake of
- // documentation - the prepare method below actually populates the field.
- $this->addFieldMapping('created', 'posted')
- ->description('See prepare method');
- // We set the timezone here for efficiency - it will be used in the prepare
- // method
- // TODO: Don't set site timezone, affects lastupdatetime
-// date_default_timezone_set('US/Mountain');
+ // The migrate module automatically converts date/time strings to UNIX timestamps.
+ $this->addFieldMapping('created', 'posted');
// TODO Add dedupe with custom replacement pattern.
- $this->addFieldMapping('mail', 'mail');
+// $this->addFieldMapping('mail', 'mail');
$this->addFieldMapping('pass', 'password');
// Instead of mapping a source field to a destination field, you can
@@ -211,20 +206,11 @@ class BeerUserMigration extends BasicExampleMigration {
->issueGroup(t('DNM'));
// Unmapped destination fields
- $this->addFieldMapping('theme')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('signature')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('access')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('login')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('timezone')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('picture')
- ->issueGroup(t('DNM'));
+
+ // This is a shortcut you can use to mark several destination fields as DNM
+ // at once
+ $this->addUnmigratedDestinations(array('theme', 'signature', 'access', 'login',
+ 'timezone', 'language', 'picture'));
// Oops, we made a typo - this should have been 'init'! If you have
// migrate_ui enabled, look at the BeerUser info page - you'll see that it
@@ -324,20 +310,8 @@ class BeerNodeMigration extends BasicExampleMigration {
// No unmapped source fields
// Unmapped destination fields
- $this->addFieldMapping('name')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('created')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('changed')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('status')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('promote')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('revision')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('name', 'created', 'changed', 'status',
+ 'promote', 'revision', 'language'));
}
}
@@ -361,9 +335,7 @@ class BeerCommentMigration extends BasicExampleMigration {
$this->destination = new MigrateDestinationComment('comment_node_migrate_example_beer');
// Mapped fields
- $this->addFieldMapping('name', 'name');
- $this->addFieldMapping('subject', 'subject');
- $this->addFieldMapping('mail', 'mail');
+ $this->addSimpleMappings(array('name', 'subject', 'mail'));
$this->addFieldMapping('status')
->defaultValue(COMMENT_PUBLISHED);
@@ -383,21 +355,7 @@ class BeerCommentMigration extends BasicExampleMigration {
// No unmapped source fields
// Unmapped destination fields
- $this->addFieldMapping('user_name')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('user_email')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('hostname')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('created')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('changed')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('thread')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('homepage')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('user_name', 'user_email', 'hostname',
+ 'created', 'changed', 'thread', 'homepage', 'language'));
}
}
diff --git a/migrate_example/wine.inc b/migrate_example/wine.inc
index b628e6e..e235f4f 100644
--- a/migrate_example/wine.inc
+++ b/migrate_example/wine.inc
@@ -172,12 +172,10 @@ class WineUserMigration extends AdvancedExampleMigration {
$this->destination = new MigrateDestinationUser();
// Mapped fields
- $this->addFieldMapping('name', 'name');
- $this->addFieldMapping('status', 'status');
+ $this->addSimpleMappings(array('name', 'status', 'mail'));
$this->addFieldMapping('created', 'posted');
$this->addFieldMapping('access', 'last_access');
$this->addFieldMapping('login', 'last_login');
- $this->addFieldMapping('mail', 'mail');
$this->addFieldMapping('pass', 'password');
$this->addFieldMapping('roles')
->defaultValue(drupal_map_assoc(array(2)));
@@ -189,14 +187,7 @@ class WineUserMigration extends AdvancedExampleMigration {
// Unmapped source fields
// Unmapped destination fields
- $this->addFieldMapping('theme')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('timezone')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('picture')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('theme', 'timezone', 'language', 'picture'));
}
}
@@ -245,22 +236,8 @@ class WineProducerMigration extends AdvancedExampleMigration {
// No unmapped source fields
// Unmapped destination fields
- $this->addFieldMapping('is_new')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('name')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('created')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('changed')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('status')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('promote')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('revision')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('is_new', 'name', 'created', 'changed',
+ 'status', 'promote', 'revision', 'language'));
}
}
@@ -432,16 +409,8 @@ class WineWineMigration extends AdvancedExampleMigration {
// No unmapped source fields
// Unmapped destination fields
- $this->addFieldMapping('is_new')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('status')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('promote')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('revision')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('is_new', 'status', 'promote',
+ 'revision', 'language'));
}
// TIP: Implement a prepareRow() method to manipulate the source row between
@@ -488,9 +457,7 @@ class WineCommentMigration extends AdvancedExampleMigration {
$this->destination = new MigrateDestinationComment('comment_node_migrate_example_wine');
// Mapped fields
- $this->addFieldMapping('name', 'name');
- $this->addFieldMapping('subject', 'subject');
- $this->addFieldMapping('mail', 'mail');
+ $this->addSimpleMappings(array('name', 'subject', 'mail'));
$this->addFieldMapping('status')
->defaultValue(COMMENT_PUBLISHED);
$this->addFieldMapping('nid', 'wineid')
@@ -509,10 +476,7 @@ class WineCommentMigration extends AdvancedExampleMigration {
// No unmapped source fields
// Unmapped destination fields
- $this->addFieldMapping('thread')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('thread', 'language'));
}
}
@@ -603,16 +567,8 @@ class WineUpdatesMigration extends AdvancedExampleMigration {
$this->addFieldMapping('sticky');
$this->addFieldMapping('created');
$this->addFieldMapping('changed');
- $this->addFieldMapping('is_new')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('status')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('promote')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('revision')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('is_new', 'status', 'promote',
+ 'revision', 'language'));
}
}
@@ -656,10 +612,7 @@ class WineCommentUpdatesMigration extends AdvancedExampleMigration {
$this->addFieldMapping('created');
$this->addFieldMapping('changed');
$this->addFieldMapping('homepage');
- $this->addFieldMapping('thread')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('thread', 'language'));
}
}
@@ -743,13 +696,6 @@ class WineUserUpdatesMigration extends AdvancedExampleMigration {
$this->addFieldMapping('signature');
$this->addFieldMapping('signature_format');
$this->addFieldMapping('init');
- $this->addFieldMapping('theme')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('timezone')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('language')
- ->issueGroup(t('DNM'));
- $this->addFieldMapping('picture')
- ->issueGroup(t('DNM'));
+ $this->addUnmigratedDestinations(array('theme', 'timezone', 'language', 'picture'));
}
}