Your SlideShare is downloading. ×
0
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Drupal content-migration
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Drupal content-migration

7,038

Published on

Learn how to use the migrate module to migrate content from various sources into Drupal.

Learn how to use the migrate module to migrate content from various sources into Drupal.

Published in: Technology
1 Comment
1 Like
Statistics
Notes
  • Hi! Thanks for this presentation, but for me as a blogger it's hard to comprehend. I've found a video by CMS2CMS which describes a way of migration to Drupal with no coding: http://www.youtube.com/watch?v=cCkW-vwzlEg

    Hope it helps someone.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
7,038
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
55
Comments
1
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×