You are here

class MigrateItemXML in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/sources/xml.inc \MigrateItemXML

Implementation of MigrateItem, for retrieving a parsed XML document given an ID provided by a MigrateList class.

Hierarchy

Expanded class hierarchy of MigrateItemXML

File

plugins/sources/xml.inc, line 161
Support for migration from XML sources.

View source
class MigrateItemXML extends MigrateItem {

  /**
   * A URL pointing to an XML document containing the data for one item to be
   * migrated.
   *
   * @var string
   */
  protected $itemUrl;

  /**
   * An array of namespaces to explicitly register before Xpath queries.
   *
   * @var array
   */
  protected $namespaces;

  /**
   * {@inheritdoc}
   */
  public function __construct($item_url, array $namespaces = array()) {
    parent::__construct();
    $this->itemUrl = $item_url;
    $this->namespaces = $namespaces;

    // Suppress errors during parsing, so we can pick them up after.
    libxml_use_internal_errors(TRUE);
  }

  /**
   * Explicitly register namespaces on an XML element.
   *
   * @param SimpleXMLElement $xml
   *   A SimpleXMLElement to register the namespaces on.
   */
  protected function registerNamespaces(SimpleXMLElement &$xml) {
    foreach ($this->namespaces as $prefix => $namespace) {
      $xml
        ->registerXPathNamespace($prefix, $namespace);
    }
  }

  /**
   * {@inheritdoc}
   *
   * Implementors are expected to return an object representing a source item.
   */
  public function getItem($id) {

    // Make sure we actually have an ID.
    if (empty($id)) {
      return NULL;
    }
    $item_url = $this
      ->constructItemUrl($id);

    // And make sure we actually got a URL to fetch.
    if (empty($item_url)) {
      return NULL;
    }

    // Get the XML object at the specified URL.
    $xml = $this
      ->loadXmlUrl($item_url);
    if ($xml !== FALSE) {
      $this
        ->registerNamespaces($xml);
      $return = new stdclass();
      $return->xml = $xml;
      return $return;
    }
    else {
      $migration = Migration::currentMigration();
      $message = t('Loading of !objecturl failed:', array(
        '!objecturl' => $item_url,
      ));
      foreach (libxml_get_errors() as $error) {
        $message .= "\n" . $error->message;
      }
      $migration
        ->getMap()
        ->saveMessage(array(
        $id,
      ), $message, MigrationBase::MESSAGE_ERROR);
      libxml_clear_errors();
      return NULL;
    }
  }

  /**
   * Creates a valid URL pointing to current item.
   *
   * The default implementation simply replaces the :id token in the URL with
   * the ID obtained from MigrateListXML. Override if the item URL is not so
   * easily expressed from the ID.
   *
   * @param mixed $id
   *   XML item ID
   *
   * @return string
   *   Formatted string with replaced tokens
   */
  protected function constructItemUrl($id) {
    return str_replace(':id', $id, $this->itemUrl);
  }

  /**
   * Loads the XML.
   *
   * Default XML loader - just use Simplexml directly. This can be overridden
   * for preprocessing of XML (removal of unwanted elements, caching of XML if
   * the source service is slow, etc.)
   *
   * @param string $item_url
   *   URL to the XML file
   *
   * @return SimpleXMLElement
   *   Loaded XML
   */
  protected function loadXmlUrl($item_url) {
    return simplexml_load_file($item_url);
  }

  /**
   * Implments MigrateItem::hash().
   */
  public function hash($row) {

    // $row->xml is a SimpleXMLElement. Temporarily set it as an XML string
    // to prevent parent::hash() failing when try to create the hash.
    migrate_instrument_start('MigrateItemXML::hash');
    $hash = md5(serialize($row->xml
      ->asXML()));
    migrate_instrument_stop('MigrateItemXML::hash');
    return $hash;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateItemXML::$itemUrl protected property A URL pointing to an XML document containing the data for one item to be migrated.
MigrateItemXML::$namespaces protected property An array of namespaces to explicitly register before Xpath queries.
MigrateItemXML::constructItemUrl protected function Creates a valid URL pointing to current item.
MigrateItemXML::getItem public function Implementors are expected to return an object representing a source item. Overrides MigrateItem::getItem
MigrateItemXML::hash public function Implments MigrateItem::hash().
MigrateItemXML::loadXmlUrl protected function Loads the XML.
MigrateItemXML::registerNamespaces protected function Explicitly register namespaces on an XML element.
MigrateItemXML::__construct public function Overrides MigrateItem::__construct