Created
August 28, 2011 23:09
-
-
Save gagarine/1177368 to your computer and use it in GitHub Desktop.
Migrate class for drupal migrate module
This file contains hidden or 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 SkCommonMigration extends Migration { | |
public function __construct() { | |
parent::__construct(); | |
//$this->addFieldMapping('field_tags', 'tags'); | |
} | |
/** | |
* Get the image infos | |
*/ | |
public function get_imageinfo($row, $fieldname) { | |
$field_image_query = db_select(SK_MIGRATE_DATABASE_NAME . '.content_' . $fieldname, 'fi') | |
->fields('fi') | |
->condition('fi.nid', $row->nid, '='); | |
$field_image_query->join(SK_MIGRATE_DATABASE_NAME . '.files', 'f', 'fi.' . $fieldname . '_fid = f.fid'); | |
$field_image_query->addField('f', 'filepath'); | |
$results = $field_image_query->execute(); | |
$images = array(); | |
//Data field | |
$field_data = $fieldname . '_data'; | |
foreach ($results as $result_row) { | |
$image_data = unserialize($result_row->$field_data); | |
$current_image_data = array( | |
'path' => SK_MIGRATE_FILES_DIRECTORY . '/' . $result_row->filepath, | |
'alt' => $image_data['alt'], | |
'title' => $image_data['title'], | |
); | |
$images[] = drupal_json_encode($current_image_data); | |
} | |
return $images; | |
} | |
/** | |
* Retrieve the set of terms associated with a node from the migration database. | |
*/ | |
function get_terms($row, $vid) { | |
$terms_query = db_select(SK_MIGRATE_DATABASE_NAME . '.term_node', 'n') | |
->fields('n', array('nid')) | |
->condition('n.nid', $row->nid, '='); | |
$terms_query->join(SK_MIGRATE_DATABASE_NAME . '.term_data', 'td', 'n.tid = td.tid'); | |
$terms_query->condition('td.vid', $vid, '='); | |
$terms_query->addField('td', 'name'); | |
$results = $terms_query->execute(); | |
$terms = array(); | |
foreach ($results as $result) { | |
$terms[] = $result->name; | |
} | |
return implode(',', $terms); | |
} | |
} | |
class SkArticleMigrate extends SkCommonMigration { | |
/** | |
* Class Constructor | |
*/ | |
public function __construct() { | |
parent::__construct(); | |
$this->description = t('Migrate Article (news)'); | |
$source_fields = array( | |
'nid' => t('The node ID of the article'), | |
'field_image' => t('Thumnail of the article'), | |
'field_content_images' => t('Attached image'), | |
'field_attached_file' => t('Attached Files'), | |
'article_category' => t('Article categorie'), | |
); | |
//The query to get the source data | |
$query = db_select(SK_MIGRATE_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', 'articles', '='); | |
$query->join(SK_MIGRATE_DATABASE_NAME . '.node_revisions', 'nr', 'n.vid = nr.vid'); | |
$query->addField('nr', 'body'); | |
$query->addField('nr', 'teaser'); | |
$query->join(SK_MIGRATE_DATABASE_NAME . '.users', 'u', 'n.uid = u.uid'); | |
$query->addField('u', 'name'); | |
$query->join(SK_MIGRATE_DATABASE_NAME . '.content_type_articles', 'ct', 'n.vid = ct.vid'); | |
$query->addField('ct', 'field_teaser_value'); | |
$query->orderBy('n.changed'); | |
/** | |
* Nodes will be checked for updates based on the 'changed' column | |
* from the original db defined in the sql query above. | |
*/ | |
$this->highwaterField = array( | |
'name' => 'changed', // Column to be used as highwater mark | |
'alias' => 'n', // Table alias containing that column | |
); | |
$this->source = new MigrateSourceSQL($query, $source_fields); | |
$this->destination = new MigrateDestinationNode('article'); | |
/** | |
* create a mapping to track the relationship between the source rows from the source database and their | |
* resulting Drupal objects; we pass along the schema definitions for the primary key of our source and | |
* destination databases (we have to be specific for our source). | |
*/ | |
$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() | |
); | |
// ** Make the mappings ** | |
$this->addFieldMapping('title', 'title'); | |
$this->addFieldMapping('is_new')->defaultValue(TRUE); | |
$this->addFieldMapping('uid', 'uid')->defaultValue(1); | |
$this->addFieldMapping('revision')->defaultValue(TRUE); | |
$this->addFieldMapping('revision_uid', 'uid')->defaultValue(1); | |
$this->addFieldMapping('created', 'created'); | |
$this->addFieldMapping('changed', 'changed'); | |
$this->addFieldMapping('status', 'status'); | |
$this->addFieldMapping('promote', 'promote'); | |
$this->addFieldMapping('sticky', 'sticky'); | |
$this->addFieldMapping('comment', 'comment'); | |
$this->addFieldMapping('language')->defaultValue('und'); | |
$this->addFieldMapping('path')->issueGroup(t('DNM')); | |
$this->addFieldMapping('pathauto_perform_alias')->defaultValue('1'); | |
//Taxonomy | |
$this->addFieldMapping('field_article_category', 'article_category'); | |
$body_arguments = MigrateTextFieldHandler::arguments(NULL, filter_default_format(), NULL); | |
$this->addFieldMapping('body', 'body') | |
->arguments($body_arguments); | |
// Text Fields | |
$teaser_arguments = MigrateTextFieldHandler::arguments(NULL, NULL, NULL); | |
$this->addFieldMapping('field_teaser', 'field_teaser_value') | |
->arguments($teaser_arguments); | |
//Files and Images Field | |
$associated_file_arguments = MigrateFileFieldHandler::arguments(NULL, 'file_copy', FILE_EXISTS_RENAME); | |
$this->addFieldMapping('field_thumbnail_image', 'field_image') | |
->arguments($associated_file_arguments); | |
$associated_file_arguments = MigrateFileFieldHandler::arguments(NULL, 'file_copy', FILE_EXISTS_RENAME); | |
$this->addFieldMapping('field_image', 'field_content_images') | |
->arguments($associated_file_arguments); | |
$associated_file_arguments = MigrateFileFieldHandler::arguments(NULL, 'file_copy', FILE_EXISTS_RENAME); | |
$this->addFieldMapping('field_files', 'field_attached_file') | |
->arguments($associated_file_arguments); | |
} | |
/** | |
* Add any additional data / clean up data for the current row / node that will be added/updated. | |
*/ | |
public function prepareRow($row) { | |
//Assign the fred or richard | |
switch ($row->uid) { | |
//richard | |
case 35: | |
$row->uid = 57; | |
break; | |
//fred | |
case 34: | |
$row->uid = 58; | |
break; | |
default: | |
$row->uid = 1; | |
break; | |
} | |
/** | |
* Category article | |
* | |
* vid 7 | |
*/ | |
$row->article_category = $this->get_terms($row, 7); | |
/** | |
* field_image (Thumnail of the article) | |
*/ | |
$row->field_image = $this->get_imageinfo($row, 'field_image'); | |
/** | |
* field_content_images' => t('Attached image') | |
* | |
* node with mutiple images | |
*/ | |
$row->field_content_images = $this->get_imageinfo($row, 'field_content_images'); | |
//we move file in an other place so we need to change the body for inline image | |
$search = 'src="/sites/default/files/'; | |
$replace = 'src="/sites/default/files/article/field/image/'; | |
$row->body = str_replace($search, $replace, $row->body); | |
/** | |
* file field | |
* | |
* @todo | |
* | |
*/ | |
//return true to import the row | |
return TRUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment