You are here

public function WineProducerXMLMigration::__construct in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 migrate_example/wine.inc \WineProducerXMLMigration::__construct()

General initialization of a Migration object.

Overrides Migration::__construct

File

migrate_example/wine.inc, line 524
Advanced migration examples. These serve two purposes:

Class

WineProducerXMLMigration
TIP: An example of importing from an XML feed. See the files in the xml directory - index.xml contains a list of IDs to import, and <id>.xml is the data for a given producer.

Code

public function __construct($arguments) {
  parent::__construct($arguments);
  $this->description = t('XML feed of wine producers of the world');

  // There isn't a consistent way to automatically identify appropriate
  // "fields" from an XML feed, so we pass an explicit list of source fields.
  $fields = array(
    'name' => t('Producer name'),
    'description' => t('Description of producer'),
    'authorid' => t('Numeric ID of the author'),
    'region' => t('Name of region'),
  );

  // IMPORTANT: Do not try this at home! We have included importable files
  // with the migrate_example module so it can be very simply installed and
  // run, but you should never include any data you want to keep private
  // (especially user data like email addresses, phone numbers, etc.) in the
  // module directory. Your source data should be outside of the webroot, and
  // should not be anywhere where it may get committed into a revision control
  // system.
  // This can also be an URL instead of a file path.
  $xml_folder = DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_example') . '/xml/';
  $list_url = $xml_folder . 'index.xml';

  // Each ID retrieved from the list URL will be plugged into :id in the
  // item URL to fetch the specific objects.
  $item_url = $xml_folder . ':id.xml';

  // We use the MigrateSourceList class for any source where we obtain the
  // list of IDs to process separately from the data for each item. The
  // listing and item are represented by separate classes, so for example we
  // could replace the XML listing with a file directory listing, or the XML
  // item with a JSON item.
  $this->source = new MigrateSourceList(new MigrateListXML($list_url), new MigrateItemXML($item_url), $fields);
  $this->destination = new MigrateDestinationNode('migrate_example_producer');

  // The source ID here is the one retrieved from the XML listing file, and
  // used to identify the specific item's file
  $this->map = new MigrateSQLMap($this->machineName, array(
    'sourceid' => array(
      'type' => 'varchar',
      'length' => 4,
      'not null' => TRUE,
    ),
  ), MigrateDestinationNode::getKeySchema());

  // TIP: Note that for XML sources, in addition to the source field passed to
  // addFieldMapping (the name under which it will be saved in the data row
  // passed through the migration process) we specify the Xpath used to
  // retrieve the value from the XML.
  $this
    ->addFieldMapping('title', 'name')
    ->xpath('/producer/name');
  $this
    ->addFieldMapping('uid', 'authorid')
    ->xpath('/producer/authorid')
    ->sourceMigration('WineUser')
    ->defaultValue(1);
  $this
    ->addFieldMapping('migrate_example_wine_regions', 'region')
    ->xpath('/producer/region');
  $this
    ->addFieldMapping('body', 'description')
    ->xpath('/producer/description');
  $this
    ->addUnmigratedDestinations(array(
    'body:summary',
    'body:format',
    'changed',
    'comment',
    'created',
    'is_new',
    'language',
    'log',
    'migrate_example_wine_regions:create_term',
    'migrate_example_wine_regions:ignore_case',
    'migrate_example_wine_regions:source_type',
    'promote',
    'revision',
    'revision_uid',
    'status',
    'sticky',
    'tnid',
    'translate',
  ));
  $destination_fields = $this->destination
    ->fields();
  if (isset($destination_fields['path'])) {
    $this
      ->addFieldMapping('path')
      ->issueGroup(t('DNM'));
    if (isset($destination_fields['pathauto'])) {
      $this
        ->addFieldMapping('pathauto')
        ->issueGroup(t('DNM'));
    }
  }
  if (module_exists('statistics')) {
    $this
      ->addUnmigratedDestinations(array(
      'totalcount',
      'daycount',
      'timestamp',
    ));
  }
}