Developing a new website or upgrading an existing one: both are challenges. And in most cases there is already content that needs to be copied to the new website.
Meet the Migrate module! A splendid tool that already has proven itself, it is even included in Drupal 8 core (http://www.acquia.com/blog/d8migrate). In this talk you can expect:
A short introduction on the migration process - ETL.
Overview of the Migrate module
Features of the migrate module
During this presentation, all covered topics will be illustrated with experiences from two projects.
The first is the migration of content of a legacy FAQ site to a brand new Drupal site. The original website was a completely customized web application where students can submit questions. These questions were then answered by the didactical team. The new site had to offer the same features while the content was conserved.
The second and smaller project is the upgrade of an existing Drupal 6 site to a newly designed website in Drupal 7. As there was not that much content, migration was easier.
As the design of Drupal 8 is Object-Oriented, there will also be some attention to the nice design of the Migrate module which results in an easy plugin system.
35. USER IMPORT
class UserMigration extends Migration {
! public function __construct() {
// Always call the parent constructor first for basic setup
parent::__construct();
$this->description = t('Migrate users from the source database to drupal users');
//map table
$this->map=new MigrateSQLMap($this->machineName,
! array(
! ! 'unr' =>array(
! ! ! 'type' => 'varchar',
! ! ! 'length' => 8,
! ! ! 'not null' => TRUE,
! ! ! 'description' => 'The KU Leuven unumber',
! ! ),!
! ),
! MigrateDestinationUser::getKeySchema()
);
//¡
36. USER IMPORT ! //source definition
$columns = array(
! 0 => array('name','Name of the user'),
! 1 => array('unr','Unumber of the user'),
! 2 => array('email','Email of the user'),
! 3 => array('course','The course the user is involved'),
! 4 => array('monitor','Is this user a monitor?'),
! 5 => array('id','Database id'),
! 6 => array('roles','The courses involved (will be mapped to roles)'),
);
//Make sure file is UTF encoded with BOM and has MAC linefeeds
$file_path = ! DRUPAL_ROOT.'/'.drupal_get_path('module', 'vtmigrate').'/personeel.csv';
//drupal_set_message(fopen($file_path,'r'));
//drupal_set_message($file_path);
$this->source = new MigrateSourceCSV($file_path,
! $columns,
! array(
! ! 'header_rows' => 1,
! ! 'delimiter' => ';',
! )
);
//destination $this->destination = new MigrateDestinationUser(); //¡
38. USER MAPPING - PREPARE
public function prepare($user,stdClass $row){
! //add all the role ids to the user account
! $role_names = explode(',',$row->roles);
!
! //check if role didactisch team bestaat
! $new_role = array('didactisch team');
! foreach($new_role as $r){
! ! if(!user_role_load_by_name($r)){
! ! ! //create it
! ! ! $role = new stdClass();
! ! ! $role->name = $r;
! ! ! user_role_save($role);
! ! }
! }
! $role = user_role_load_by_name('didactisch team');
! //$user role is a mapping from role id to role name
! $user->roles[$role->rid] = $role->name;
}
39. DATE
//as suggested in https://drupal.org/node/1806296#comment-6589830
public function prepareRow($row) {
! //for some reason, 2 hours have to be added
! $row->startdate = strtotime("+2 hours",$row->startdate);
! $row->enddate = strtotime("+2 hours",$row->enddate);
! $row->datespan = drupal_json_encode(array(
! ! 'from' !=> $row->startdate,
! ! 'to' ! => $row->enddate
! ));
! //change linebreaks back to newlines
! $row->body = str_replace('<br/>',"n",$row->body);
}
40. REFERENCE
public function prepareRow($entity,stdClass $row) {
! //¡
! $loc_query = db_select('node','n')
! ! ->fields('n')
! ! ->condition('n.type','location')
//MYSQL's like is case insensitive
! ! ->condition('n.title','%'.$key.'%','LIKE');
! $locations = $loc_query->execute();
! ! ! !
! if(!empty($locations)){
! ! foreach($locations as $loc){
! ! //target_id is specific for entity reference
! ! // cf https://drupal.org/node/1845986
! ! $entity->field_location['und'][0]['target_id'] = $loc->nid;
! }
}
44. FILES & IMAGES
$files = array();
$nr_matches = preg_match_all('/<a href="(S+)"/i',$row->answer,$matches,PREG_SET_ORDER);
//drupal_set_message("#match: ".print_r($nr_matches,true),'warning');
if($nr_matches > 0){
drupal_set_message("#filematch: ".print_r($matches,true),'warning');
! $base_url = 'https://mirw.kuleuven.be/vragentrommel/trommelroot/';
! //all matches are stored in an array at $matches[0]
foreach($matches as $match){
! $files[] = $base_url .$match[1];
! }
}
//add the URI of the files to $row->files and separate them by hashtag
$row->files = implode('#',$files);
45. FILES & IMAGES
$images = array();
$nr_matches = preg_match_all('/<img src=/slideshow/add-the-migrate-module-to-your-toolbox/41309300/([^s>]+)>/i&
$matches,PREG_SET_ORDER);
//drupal_set_message("#match: ".print_r($nr_matches,true),'warning');
if($nr_matches > 0){
drupal_set_message("#image match: ".print_r($matches,true),'warning');
! $base_url = 'https://mirw.kuleuven.be/vragentrommel/trommelroot/';
! //all matches are stored in an array at $matches[0]
! foreach($matches as $match){
! $file_url = $base_url . $match[1];
! ! $images[] = $file_url;
! }
}
//add the URI of the images to $row->images, separated with #
$row->images = implode('#',$images);