SlideShare a Scribd company logo
1 of 23
Ashok Modi (BTMash) – Drupal LA – March 2011 Migrating content to drupal – Migrate Module
Agenda Different Methods Steps to Work with Migrate Hooks Build a Class Description Source Adding Mapping Additional Data drush commands Q & A
Disclaimer Code Heavy! You may fall asleep. New territory! Talk about one *small* aspect of migration (migrating nodes) Not talking about creating own DestinationHandlers Possibly not talking about creating own FieldHandlers (depends on time) Can walk through an example migration that I did if preferred. Ask questions? It will make the presentation less terrible 
Possible method – by hand Should be accurate Get all files Mapping / everything works Time consuming Not feasible if you have a lot of content. Good way to test interns / punish coworkers (?)
Possible methods – Node Export Node Export (http://drupal.org/project/node_export) Has 7.x branch But no way to update content from 6.x -> 7.x No way to go back *easy* to set up (requires exact setup between source and destination in field names, etc)
Possible Methods - Feeds Really good method Map fields from source to destination Can import RSS / Atom / Various types of feeds Also flat files such as CSV Well documented Other than flat files, requires a feed source Might run into issues if content is updated in source *might be tricky in another cms*
Method demonstrated - Migrate Already defined many possible import sources XML, JSON, CSV, Databases (any currently supported by Drupal!) Can import many different types of content Users, Nodes, Comments, Taxonomy, Files … all core entities Can define your own import handler (not covered in presentation) Can define own method for importing custom fields Current already defined for all core field types Has support for Media Module importing Patch underway for getting date import Can define your own field handler (possibly not covered in presentation) Drush integration Rollback, Status Updates, Limited import. Caveat – Confusing documentation Only a status UI – all mapping must be done in code.
Assumptions made for presentation Migrating from a database Files directory for source are on same machine as the destination site directory
Steps to work with Migrate Let Migrate know about your module (1 hook!) Build a Migration Class Give it a description Let Migrate know what you’re getting content from. Let Migrate know about the type of content. Map the fields the migrate class it going to fill. (Optional) Massage / Add any fields you couldn’t get in the initial mapping (query).
Step 1: Hook Implement one hook – hook_migrate_api Provide the api version number (currently at version 2) That’s it! function mymodule_migrate_api() {   return array(     ‘api’ => 2,   ); }
Step 2: Build a Class Implement classes Class defines type of content that will be imported class NodeContentTypeMigration extends Migration { public function __construct() {   parent::__construct();   … } public function prepareRow($current_row) {   … } }
Step 2: Build a Class (functions inside) public function __construct() {…} Constructor for the class Allows migrate to know content type (user, node, tags) Where content is mapped from (db, csv, xml, etc) All the mappings coming in (fields) (optional)public function prepareRow($current_row) {…} Any extra data (things that cannot be pulled in a single query(?)) Massage any of the data that was pulled in (clean up text, clean up links, etc)
Step 2a: Create a description Create a description Class Description Any additional source fields (not found in initial query) Initial source -> destination mapping (what is the key in the source db?) $this->description = t(“Import all nodes of type PAGE”); Define Source Fields Fields that may not be getting pulled in via your query or in the flat file data but will be getting migrated somehow $source_fields = array(     'nid' => t('The node ID of the page'),     ’my_files' => t(’The set of files in a field for this node'), );
Off course: query source database Set up query (if need be, switch DBs using Database::getConnection) $query = Database::getConnection('for_migration', 'default'); Then write out rest of the query Alternatively, if source db is on same machine as destination db, use mysql db shortcut db_select(MY_MIGRATION_DATABASE_NAME .’.table_name’, ‘t’)
Step 2b: Call to grab data NOTE: This is only for migrations from databases Set up query (if need be, switch DBs using Database::getConnection) $query = db_select(MY_MIGRATION_DATABASE_NAME .'.node', 'n’) 	->fields('n', array('nid', 'vid', 'type', 'language', 'title', 'uid', 'status', 'created', 'changed', 'comment', 'promote', 'moderate', 'sticky', 'tnid', 'translate')) 	->condition('n.type', 'page', '=');     $query->join(MY_MIGRATION_DATABASE_NAME .'.node_revisions', 'nr', 'n.vid = nr.vid');     $query->addField('nr', 'body'); $query->addField('nr', 'teaser'); $query->join(MY_MIGRATION_DATABASE_NAME .'.users', 'u', 'n.uid = u.uid');     $query->addField('u', 'name');     $query->orderBy('n.changed');
Step 2b: Why the orderby? Migrate module has a feature called ‘highwater’ It is a key to designate and figure out if a piece of content needs to be updated rather than inserted. Means content can be updated! $this->highwaterField = array( 	'name' => 'changed',  	'alias' => 'n’, );
Step 2c: Mappings Add a ‘mapping’ (this is for tracking relationships between the rows from the source db and the rows that will come in the destination site) – essentially key of source DB.  $this->map = new MigrateSQLMap( 	$this->machineName,  	array(         		'nid' => array( 			'type' => 'int’, 			'unsigned' => TRUE,  			'not null' => TRUE, 			'description' => 'D6 Unique Node ID’, 			'alias' => 'n',  		) 	), 	MigrateDestinationNode::getKeySchema());
Step 2c: Mappings (cont’d) Now let the migrate module know what kind of mapping is being performed. $this->source = new MigrateSourceSQL($query, $source_fields); Along with the type of content $this->destination = new MigrateDestinationNode('page'); .
Step 2d: Map Fields Usually follows the form $this->addFieldMapping(‘destination_field_name’, ‘source_field_name’);  $this->addFieldMapping('revision_uid', 'uid'); Can provide default values  $this->addFieldMapping('pathauto_perform_alias')->defaultValue('1'); Can provide no value $this->addFieldMapping('path')->issueGroup(t('DNM')); Can provide arguments and separators for certain field types (body, file, etc require this methodology)
Step 3: Additional data / cleanup Optional public function prepareRow($current_row) Use it to add any additional data / cleanup any fields that were mapped in. // Get the correct uid based on username & set author id for node to uid $user_query = db_select('users', 'u’) 	->fields('u', array('uid'))     	->condition('u.name', $username, '=');   $results = $user_query->execute();   foreach ($results as $row) {     	$current_row->uid = $current_row->revision_uid = $row->uid; 	break; }
Drush Commands (the important ones) drush ms – List various migration import classes drush mi <importclass> - Import content drush mr <importclass> - Rollback content Options --idlist=id1,id2,… - Import content with specific IDs --itemlimit=n – Only import up to ‘n’ items --feedback=“n seconds” – Show status report every ‘n’ seconds --feedback=“n items” – Show status report every ‘n’ items
Resources http://drupal.org/project/migrate http://drupal.org/node/415260 Look at the example modules  http://drupal.org/project/migrate_extras http://drupal.org/project/wordpress_migrate http://cyrve.com/import (drush documentation)  http://goo.gl/3e1Jm(additional documentation to be added to core project) http://goo.gl/2qDLh (another example module)
Questions? Thank you 

More Related Content

What's hot

Enterprise workflow with Apps Script
Enterprise workflow with Apps ScriptEnterprise workflow with Apps Script
Enterprise workflow with Apps Script
ccherubino
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
Phúc Đỗ
 
Rupy2012 ArangoDB Workshop Part1
Rupy2012 ArangoDB Workshop Part1Rupy2012 ArangoDB Workshop Part1
Rupy2012 ArangoDB Workshop Part1
ArangoDB Database
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
phanleson
 
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Cloudera, Inc.
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String Functions
Avanitrambadiya
 

What's hot (20)

Enterprise workflow with Apps Script
Enterprise workflow with Apps ScriptEnterprise workflow with Apps Script
Enterprise workflow with Apps Script
 
Hive - SerDe and LazySerde
Hive - SerDe and LazySerdeHive - SerDe and LazySerde
Hive - SerDe and LazySerde
 
Scoobi - Scala for Startups
Scoobi - Scala for StartupsScoobi - Scala for Startups
Scoobi - Scala for Startups
 
Scalding for Hadoop
Scalding for HadoopScalding for Hadoop
Scalding for Hadoop
 
Restful App Engine
Restful App EngineRestful App Engine
Restful App Engine
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
Scalding - Hadoop Word Count in LESS than 70 lines of code
Scalding - Hadoop Word Count in LESS than 70 lines of codeScalding - Hadoop Word Count in LESS than 70 lines of code
Scalding - Hadoop Word Count in LESS than 70 lines of code
 
Field api.From d7 to d8
Field api.From d7 to d8Field api.From d7 to d8
Field api.From d7 to d8
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
Tutorial Solution
Tutorial SolutionTutorial Solution
Tutorial Solution
 
Rails 3 ActiveRecord
Rails 3 ActiveRecordRails 3 ActiveRecord
Rails 3 ActiveRecord
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Rupy2012 ArangoDB Workshop Part1
Rupy2012 ArangoDB Workshop Part1Rupy2012 ArangoDB Workshop Part1
Rupy2012 ArangoDB Workshop Part1
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
 
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06
 
Why you should be using structured logs
Why you should be using structured logsWhy you should be using structured logs
Why you should be using structured logs
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String Functions
 
Pig
PigPig
Pig
 

Viewers also liked

Drupal 6 to 7 migration guide
Drupal 6 to 7 migration guideDrupal 6 to 7 migration guide
Drupal 6 to 7 migration guide
Ebizon
 
JIIT PORTAL based on Drupal
JIIT PORTAL based on DrupalJIIT PORTAL based on Drupal
JIIT PORTAL based on Drupal
Prashant Saini
 
Best Practices for Migrating a Legacy-Based CMS to Drupal
Best Practices for Migrating a Legacy-Based CMS to DrupalBest Practices for Migrating a Legacy-Based CMS to Drupal
Best Practices for Migrating a Legacy-Based CMS to Drupal
Acquia
 

Viewers also liked (14)

Migration from Legacy CMS to Drupal
Migration from Legacy CMS to DrupalMigration from Legacy CMS to Drupal
Migration from Legacy CMS to Drupal
 
Drupal 6 to 7 migration guide
Drupal 6 to 7 migration guideDrupal 6 to 7 migration guide
Drupal 6 to 7 migration guide
 
Drupalcampchicago2010.rachel.datamigration.
Drupalcampchicago2010.rachel.datamigration.Drupalcampchicago2010.rachel.datamigration.
Drupalcampchicago2010.rachel.datamigration.
 
JIIT PORTAL based on Drupal
JIIT PORTAL based on DrupalJIIT PORTAL based on Drupal
JIIT PORTAL based on Drupal
 
Drupal for Non-Developers
Drupal for Non-DevelopersDrupal for Non-Developers
Drupal for Non-Developers
 
Cms
CmsCms
Cms
 
Best Practices for Migrating a Legacy-Based CMS to Drupal
Best Practices for Migrating a Legacy-Based CMS to DrupalBest Practices for Migrating a Legacy-Based CMS to Drupal
Best Practices for Migrating a Legacy-Based CMS to Drupal
 
Content Migration to Drupal 8
Content Migration to Drupal 8Content Migration to Drupal 8
Content Migration to Drupal 8
 
Cms an overview
Cms an overviewCms an overview
Cms an overview
 
Migrating data to drupal 8
Migrating data to drupal 8Migrating data to drupal 8
Migrating data to drupal 8
 
Out With the Old, in With the Open-source: Brainshark's Complete CMS Migration
Out With the Old, in With the Open-source: Brainshark's Complete CMS MigrationOut With the Old, in With the Open-source: Brainshark's Complete CMS Migration
Out With the Old, in With the Open-source: Brainshark's Complete CMS Migration
 
Migrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Migrating to Drupal 8: How to Migrate Your Content and Minimize the RisksMigrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
Migrating to Drupal 8: How to Migrate Your Content and Minimize the Risks
 
Content migration - CSV to Drupal 8
Content migration -  CSV to Drupal 8Content migration -  CSV to Drupal 8
Content migration - CSV to Drupal 8
 
T44u 2015, content migration
T44u 2015, content migrationT44u 2015, content migration
T44u 2015, content migration
 

Similar to Drupal content-migration

Apache Hadoop India Summit 2011 talk "Hive Evolution" by Namit Jain
Apache Hadoop India Summit 2011 talk "Hive Evolution" by Namit JainApache Hadoop India Summit 2011 talk "Hive Evolution" by Namit Jain
Apache Hadoop India Summit 2011 talk "Hive Evolution" by Namit Jain
Yahoo Developer Network
 

Similar to Drupal content-migration (20)

Compass Framework
Compass FrameworkCompass Framework
Compass Framework
 
Migrating data into Drupal using the migrate module
Migrating data into Drupal using the migrate moduleMigrating data into Drupal using the migrate module
Migrating data into Drupal using the migrate module
 
This upload requires better support for ODP format
This upload requires better support for ODP formatThis upload requires better support for ODP format
This upload requires better support for ODP format
 
Data migration to Drupal using Migrate Module
Data migration to Drupal using Migrate ModuleData migration to Drupal using Migrate Module
Data migration to Drupal using Migrate Module
 
Mongo-Drupal
Mongo-DrupalMongo-Drupal
Mongo-Drupal
 
Allura - an Open Source MongoDB Based Document Oriented SourceForge
Allura - an Open Source MongoDB Based Document Oriented SourceForgeAllura - an Open Source MongoDB Based Document Oriented SourceForge
Allura - an Open Source MongoDB Based Document Oriented SourceForge
 
Hands on Mahout!
Hands on Mahout!Hands on Mahout!
Hands on Mahout!
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and MingRapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
 
Migrate
MigrateMigrate
Migrate
 
Hadoop ecosystem
Hadoop ecosystemHadoop ecosystem
Hadoop ecosystem
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
 
Doctrine and NoSQL
Doctrine and NoSQLDoctrine and NoSQL
Doctrine and NoSQL
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Architecture | Busy Java Developers Guide to NoSQL | Ted Neward
Architecture | Busy Java Developers Guide to NoSQL | Ted NewardArchitecture | Busy Java Developers Guide to NoSQL | Ted Neward
Architecture | Busy Java Developers Guide to NoSQL | Ted Neward
 
UNO based ODF Toolkit API
UNO based ODF Toolkit APIUNO based ODF Toolkit API
UNO based ODF Toolkit API
 
Go Faster With Native Compilation
Go Faster With Native CompilationGo Faster With Native Compilation
Go Faster With Native Compilation
 
Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2
 
Couchbas for dummies
Couchbas for dummiesCouchbas for dummies
Couchbas for dummies
 
Apache Hadoop India Summit 2011 talk "Hive Evolution" by Namit Jain
Apache Hadoop India Summit 2011 talk "Hive Evolution" by Namit JainApache Hadoop India Summit 2011 talk "Hive Evolution" by Namit Jain
Apache Hadoop India Summit 2011 talk "Hive Evolution" by Namit Jain
 
Hadoop ecosystem
Hadoop ecosystemHadoop ecosystem
Hadoop ecosystem
 

More from Ashok Modi

Drupal Camp LA 2010: Moderating Content in Drupal
Drupal Camp LA 2010: Moderating Content in DrupalDrupal Camp LA 2010: Moderating Content in Drupal
Drupal Camp LA 2010: Moderating Content in Drupal
Ashok Modi
 
Drupal Backend Performance and Scalability
Drupal Backend Performance and ScalabilityDrupal Backend Performance and Scalability
Drupal Backend Performance and Scalability
Ashok Modi
 
Drupal Frontend Performance and Scalability
Drupal Frontend Performance and ScalabilityDrupal Frontend Performance and Scalability
Drupal Frontend Performance and Scalability
Ashok Modi
 
Zimmertwins Presentation
Zimmertwins PresentationZimmertwins Presentation
Zimmertwins Presentation
Ashok Modi
 

More from Ashok Modi (10)

Drupal Camp LA 2011: Typography modules for Drupal
Drupal Camp LA 2011: Typography modules for DrupalDrupal Camp LA 2011: Typography modules for Drupal
Drupal Camp LA 2011: Typography modules for Drupal
 
DrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performanceDrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performance
 
DrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizingDrupalCampLA 2011 - Drupal frontend-optimizing
DrupalCampLA 2011 - Drupal frontend-optimizing
 
Entity cache
Entity cacheEntity cache
Entity cache
 
Hacking core
Hacking coreHacking core
Hacking core
 
CalArts presentation
CalArts presentationCalArts presentation
CalArts presentation
 
Drupal Camp LA 2010: Moderating Content in Drupal
Drupal Camp LA 2010: Moderating Content in DrupalDrupal Camp LA 2010: Moderating Content in Drupal
Drupal Camp LA 2010: Moderating Content in Drupal
 
Drupal Backend Performance and Scalability
Drupal Backend Performance and ScalabilityDrupal Backend Performance and Scalability
Drupal Backend Performance and Scalability
 
Drupal Frontend Performance and Scalability
Drupal Frontend Performance and ScalabilityDrupal Frontend Performance and Scalability
Drupal Frontend Performance and Scalability
 
Zimmertwins Presentation
Zimmertwins PresentationZimmertwins Presentation
Zimmertwins Presentation
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Drupal content-migration

  • 1. Ashok Modi (BTMash) – Drupal LA – March 2011 Migrating content to drupal – Migrate Module
  • 2. Agenda Different Methods Steps to Work with Migrate Hooks Build a Class Description Source Adding Mapping Additional Data drush commands Q & A
  • 3. Disclaimer Code Heavy! You may fall asleep. New territory! Talk about one *small* aspect of migration (migrating nodes) Not talking about creating own DestinationHandlers Possibly not talking about creating own FieldHandlers (depends on time) Can walk through an example migration that I did if preferred. Ask questions? It will make the presentation less terrible 
  • 4. Possible method – by hand Should be accurate Get all files Mapping / everything works Time consuming Not feasible if you have a lot of content. Good way to test interns / punish coworkers (?)
  • 5. Possible methods – Node Export Node Export (http://drupal.org/project/node_export) Has 7.x branch But no way to update content from 6.x -> 7.x No way to go back *easy* to set up (requires exact setup between source and destination in field names, etc)
  • 6. Possible Methods - Feeds Really good method Map fields from source to destination Can import RSS / Atom / Various types of feeds Also flat files such as CSV Well documented Other than flat files, requires a feed source Might run into issues if content is updated in source *might be tricky in another cms*
  • 7. Method demonstrated - Migrate Already defined many possible import sources XML, JSON, CSV, Databases (any currently supported by Drupal!) Can import many different types of content Users, Nodes, Comments, Taxonomy, Files … all core entities Can define your own import handler (not covered in presentation) Can define own method for importing custom fields Current already defined for all core field types Has support for Media Module importing Patch underway for getting date import Can define your own field handler (possibly not covered in presentation) Drush integration Rollback, Status Updates, Limited import. Caveat – Confusing documentation Only a status UI – all mapping must be done in code.
  • 8. Assumptions made for presentation Migrating from a database Files directory for source are on same machine as the destination site directory
  • 9. Steps to work with Migrate Let Migrate know about your module (1 hook!) Build a Migration Class Give it a description Let Migrate know what you’re getting content from. Let Migrate know about the type of content. Map the fields the migrate class it going to fill. (Optional) Massage / Add any fields you couldn’t get in the initial mapping (query).
  • 10. Step 1: Hook Implement one hook – hook_migrate_api Provide the api version number (currently at version 2) That’s it! function mymodule_migrate_api() { return array( ‘api’ => 2, ); }
  • 11. Step 2: Build a Class Implement classes Class defines type of content that will be imported class NodeContentTypeMigration extends Migration { public function __construct() { parent::__construct(); … } public function prepareRow($current_row) { … } }
  • 12. Step 2: Build a Class (functions inside) public function __construct() {…} Constructor for the class Allows migrate to know content type (user, node, tags) Where content is mapped from (db, csv, xml, etc) All the mappings coming in (fields) (optional)public function prepareRow($current_row) {…} Any extra data (things that cannot be pulled in a single query(?)) Massage any of the data that was pulled in (clean up text, clean up links, etc)
  • 13. Step 2a: Create a description Create a description Class Description Any additional source fields (not found in initial query) Initial source -> destination mapping (what is the key in the source db?) $this->description = t(“Import all nodes of type PAGE”); Define Source Fields Fields that may not be getting pulled in via your query or in the flat file data but will be getting migrated somehow $source_fields = array( 'nid' => t('The node ID of the page'), ’my_files' => t(’The set of files in a field for this node'), );
  • 14. Off course: query source database Set up query (if need be, switch DBs using Database::getConnection) $query = Database::getConnection('for_migration', 'default'); Then write out rest of the query Alternatively, if source db is on same machine as destination db, use mysql db shortcut db_select(MY_MIGRATION_DATABASE_NAME .’.table_name’, ‘t’)
  • 15. Step 2b: Call to grab data NOTE: This is only for migrations from databases Set up query (if need be, switch DBs using Database::getConnection) $query = db_select(MY_MIGRATION_DATABASE_NAME .'.node', 'n’) ->fields('n', array('nid', 'vid', 'type', 'language', 'title', 'uid', 'status', 'created', 'changed', 'comment', 'promote', 'moderate', 'sticky', 'tnid', 'translate')) ->condition('n.type', 'page', '='); $query->join(MY_MIGRATION_DATABASE_NAME .'.node_revisions', 'nr', 'n.vid = nr.vid'); $query->addField('nr', 'body'); $query->addField('nr', 'teaser'); $query->join(MY_MIGRATION_DATABASE_NAME .'.users', 'u', 'n.uid = u.uid'); $query->addField('u', 'name'); $query->orderBy('n.changed');
  • 16. Step 2b: Why the orderby? Migrate module has a feature called ‘highwater’ It is a key to designate and figure out if a piece of content needs to be updated rather than inserted. Means content can be updated! $this->highwaterField = array( 'name' => 'changed', 'alias' => 'n’, );
  • 17. Step 2c: Mappings Add a ‘mapping’ (this is for tracking relationships between the rows from the source db and the rows that will come in the destination site) – essentially key of source DB. $this->map = new MigrateSQLMap( $this->machineName, array( 'nid' => array( 'type' => 'int’, 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'D6 Unique Node ID’, 'alias' => 'n', ) ), MigrateDestinationNode::getKeySchema());
  • 18. Step 2c: Mappings (cont’d) Now let the migrate module know what kind of mapping is being performed. $this->source = new MigrateSourceSQL($query, $source_fields); Along with the type of content $this->destination = new MigrateDestinationNode('page'); .
  • 19. Step 2d: Map Fields Usually follows the form $this->addFieldMapping(‘destination_field_name’, ‘source_field_name’); $this->addFieldMapping('revision_uid', 'uid'); Can provide default values $this->addFieldMapping('pathauto_perform_alias')->defaultValue('1'); Can provide no value $this->addFieldMapping('path')->issueGroup(t('DNM')); Can provide arguments and separators for certain field types (body, file, etc require this methodology)
  • 20. Step 3: Additional data / cleanup Optional public function prepareRow($current_row) Use it to add any additional data / cleanup any fields that were mapped in. // Get the correct uid based on username & set author id for node to uid $user_query = db_select('users', 'u’) ->fields('u', array('uid')) ->condition('u.name', $username, '='); $results = $user_query->execute(); foreach ($results as $row) { $current_row->uid = $current_row->revision_uid = $row->uid; break; }
  • 21. Drush Commands (the important ones) drush ms – List various migration import classes drush mi <importclass> - Import content drush mr <importclass> - Rollback content Options --idlist=id1,id2,… - Import content with specific IDs --itemlimit=n – Only import up to ‘n’ items --feedback=“n seconds” – Show status report every ‘n’ seconds --feedback=“n items” – Show status report every ‘n’ items
  • 22. Resources http://drupal.org/project/migrate http://drupal.org/node/415260 Look at the example modules http://drupal.org/project/migrate_extras http://drupal.org/project/wordpress_migrate http://cyrve.com/import (drush documentation) http://goo.gl/3e1Jm(additional documentation to be added to core project) http://goo.gl/2qDLh (another example module)