Guest User

PRMigration

a guest
Feb 6th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.10 KB | None | 0 0
  1. class PRMigration extends Migration {
  2.   public function __construct() {
  3.     parent::__construct();
  4.     $this->map = new MigrateSQLMap($this->machineName,
  5.         array(
  6.           'id' => array(
  7.             'type' => 'int',
  8.             'not null' => TRUE,
  9.             'description' => 'Press Release ID',
  10.           )
  11.         ),
  12.         MigrateDestinationNode::getKeySchema()
  13.       );
  14.  
  15.     $query = Database::getConnection('default', 'secondary')
  16.              ->select('releases', 'r')
  17.              ->fields('r', array('id', 'title', 'released_at', 'synopsis', 'body',
  18.                  'never_post', 'position', 'writer', 'created_by', 'updated_by',
  19.                  'created_at', 'updated_at', 'subtitle', 'contact_id',
  20.                  'version', 'deleted_at'));
  21.    
  22.     $source_fields = array(
  23.       'links' => t('Related links'),
  24.       'source_images' => t('Image assets attached ot this press release.'),
  25.     );
  26.     $this->source = new MigrateSourceSQL($query, $source_fields, NULL, array('map_joinable' => FALSE));
  27.     // Using the default Node destination class.
  28.     $this->destination = new MigrateDestinationNode('press_release');
  29.    
  30.     ///////////////////////////////////////////////////////
  31.     // Mapped fields
  32.     ///////////////////////////////////////////////////////
  33.     $file_args = MigrateFileFieldHandler::arguments(NULL, 'file_blob', FILE_EXISTS_RENAME);
  34.     $this->addFieldMapping('field_image', 'source_images')
  35.          ->arguments($file_args);
  36.          
  37.   }
  38.   public function prepareRow($row) {
  39.     // Pull in the associated images for this release.
  40.     $source_db = Database::getConnection('default', 'secondary');
  41.     $query = $source_db
  42.              ->select('assets', 'a')
  43.              ->fields('a', array('id', 'filename', 'content_type', 'alt', 'caption',
  44.                  'photo_credit', 'size', 'height', 'width', 'parent_id', 'thumbnail',
  45.                  'db_file_id', 'created_by', 'updated_by', 'created_at', 'updated_at'));
  46.     $query->innerJoin('db_files', 'dbf', 'a.db_file_id = dbf.id');
  47.     $query->addField('dbf', 'data');
  48.     $query->leftJoin('assignments', 'assign', 'a.id = assign.asset_id');
  49.     $query->condition('assign.category_type', 'Release', '=');
  50.     $query->condition('assign.category_id', $row->id, '=');
  51.     $results = $query->execute();
  52.    
  53.     $images = array();
  54.    
  55.     foreach ($results as $row) {
  56.       $image_information = array(
  57.         'path' => $row->filename,
  58.         'alt' => $row->alt,
  59.         'title' => $row->caption,
  60.       );
  61.       $images[] = drupal_json_encode($image_information);
  62.      
  63.     }
  64.    
  65.     $row->source_images = $images;
  66.     return TRUE;
  67.   }
  68. }
  69.  
  70. class PRAssetMigration extends Migration {
  71.   public function __construct() {
  72.     parent::__construct();
  73.    
  74.     $this->map = new MigrateSQLMap($this->machineName,
  75.         array(
  76.           'id' => array(
  77.             'type' => 'int',
  78.             'not null' => TRUE,
  79.             'description' => 'Asset ID',
  80.           )
  81.         ),
  82.         MigrateDestinationNode::getKeySchema()
  83.       );
  84.      
  85.     // Select all the fields from the 'assets' table, and join it with the related 'data' field in db_files.
  86.     $source_db = Database::getConnection('default', 'secondary');
  87.     $query = $source_db
  88.              ->select('assets', 'a')
  89.              ->fields('a', array('id', 'filename', 'content_type', 'alt', 'caption',
  90.                  'photo_credit', 'size', 'height', 'width', 'parent_id', 'thumbnail',
  91.                  'db_file_id', 'created_by', 'updated_by', 'created_at', 'updated_at'));
  92.     $query->innerJoin('db_files', 'dbf', 'a.db_file_id = dbf.id');
  93.     $query->addField('dbf', 'data');
  94.     $count_query = $source_db->select('assets', 'a');
  95.     $count_query->addExpression('COUNT(id)', 'cnt');
  96.    
  97.     // User the simplified count query so it doesn't have to do the join to get the count.
  98.     $this->source = new MigrateSourceSQL($query, array(), $count_query, array('map_joinable' => FALSE));
  99.     $this->destination = new MigrateDestinationFile(MigrateDestinationFile::options(FALSE, NULL, NULL, NULL, FALSE, TRUE));
  100.    
  101.     ///////////////////////////////////////////////////////
  102.     // Mapped fields
  103.     ///////////////////////////////////////////////////////
  104.     $this->addSimpleMappings(array());
  105.  
  106.     $this->addFieldMapping('contents', 'data');
  107.     $this->addFieldMapping('filename', 'filename')
  108.          ->dedupe('file_managed', 'filename');
  109.     $this->addFieldMapping('filemime', 'content_type');
  110.     // Since we don't care about the history of the source files, use updated_at instead of created_at.
  111.     $this->addFieldMapping('timestamp', 'updated_at');
  112.    
  113.    
  114.     ///////////////////////////////////////////////////////
  115.     // Unmapped Source fields
  116.     ///////////////////////////////////////////////////////
  117.     $this->addUnmigratedSources(array('db_file_id', 'created_by', 'updated_by', 'created_at'));
  118.    
  119.     ///////////////////////////////////////////////////////
  120.     // Unmapped destination fields
  121.     ///////////////////////////////////////////////////////
  122.     $this->addUnmigratedDestinations(array('uri', 'path', 'uid'));
  123.    
  124.          
  125.   }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment