You are here

public function WineProducerNamespaceXMLPullMigration::__construct in Migrate 7.2

General initialization of a Migration object.

Overrides Migration::__construct

File

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

Class

WineProducerNamespaceXMLPullMigration
TIP: An alternative approach using MigrateSourceSQL. This uses a different XML library, which advances element-by-element through the XML file rather than reading in the whole file. This source will work better with large XML files, but is slower for…

Code

public function __construct($arguments) {
  parent::__construct($arguments);
  $this->description = t('XML feed with namespaces (pull) of wine producers of the world');
  $fields = array(
    'pr:name' => t('Producer name'),
    'pr:description' => t('Description of producer'),
    'pr:authorid' => t('Numeric ID of the author'),
    'pr: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 local file path.
  $xml_folder = DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_example') . '/xml/';
  $items_url = $xml_folder . 'producers4.xml';

  // As with MigrateSourceMultiItems, this applies where there is not a
  // separate list of IDs to process - the source XML file is entirely
  // self-contained. For the ID path, and xpath for each component, we can
  // use the full xpath syntax as usual. However, the syntax to select the
  // elements that correspond to objects to import is more limited. It must
  // be a fully-qualified path to the element (i.e.,
  // /producers/producer rather than just //producer).
  $item_xpath = '/pr:producers/pr:producer';

  // relative to document
  $item_ID_xpath = 'pr:sourceid';

  // relative to item_xpath
  $namespaces = array(
    'pr' => 'http://www.wine.org/wine-producers',
  );
  $this->source = new MigrateSourceXML($items_url, $item_xpath, $item_ID_xpath, $fields, array(), $namespaces);
  $this->destination = new MigrateDestinationNode('migrate_example_producer');
  $this->map = new MigrateSQLMap($this->machineName, array(
    'sourceid' => array(
      'type' => 'varchar',
      'length' => 4,
      'not null' => TRUE,
    ),
  ), MigrateDestinationNode::getKeySchema());
  $this
    ->addFieldMapping('title', 'pr:name')
    ->xpath('pr:name');
  $this
    ->addFieldMapping('uid', 'pr:authorid')
    ->xpath('pr:authorid')
    ->sourceMigration('WineUser')
    ->defaultValue(1);
  $this
    ->addFieldMapping('migrate_example_wine_regions', 'pr:region')
    ->xpath('pr:region');
  $this
    ->addFieldMapping('body', 'pr:description')
    ->xpath('pr: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',
    ));
  }
}