You are here

class MigrateListXML in Migrate 7.2

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

Implementation of MigrateList, for retrieving a list of IDs to be migrated from an XML document.

Hierarchy

Expanded class hierarchy of MigrateListXML

File

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

View source
class MigrateListXML extends MigrateList {

  /**
   * A URL pointing to an XML document containing a list of IDs to be processed.
   *
   * @var string
   */
  protected $listUrl;

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

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

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

  /**
   * {@inheritdoc}
   *
   * Our public face is the URL we're getting items from
   */
  public function __toString() {
    return $this->listUrl;
  }

  /**
   * {@inheritdoc}
   *
   * Load the XML at the given URL, and return an array of the IDs found
   * within it.
   */
  public function getIdList() {
    migrate_instrument_start("Retrieve {$this->listUrl}");
    $xml = simplexml_load_file($this->listUrl);
    migrate_instrument_stop("Retrieve {$this->listUrl}");
    if ($xml !== FALSE) {
      $this
        ->registerNamespaces($xml);
      return $this
        ->getIDsFromXML($xml);
    }
    else {
      Migration::displayMessage(t('Loading of !listUrl failed:', array(
        '!listUrl' => $this->listUrl,
      )));
      foreach (libxml_get_errors() as $error) {
        Migration::displayMessage(MigrateItemsXML::parseLibXMLError($error));
      }
      return NULL;
    }
  }

  /**
   * Gets an array of the IDs found in a XML.
   *
   * Given an XML object, parse out the IDs for processing and return them as an
   * array. The default implementation assumes the IDs are simply the values of
   * the top-level elements - in most cases, you will need to override this to
   * reflect your particular XML structure.
   *
   * @param SimpleXMLElement $xml
   *   Object from we get the ID's
   *
   * @return array
   *   Extracted ID's
   */
  protected function getIDsFromXML(SimpleXMLElement $xml) {
    $ids = array();
    foreach ($xml as $element) {
      $ids[] = (string) $element;
    }

    // Additionally, if there are any namespaces registered, try to parse
    // elements with namespaces as well.
    if ($namespaces = $xml
      ->getNamespaces()) {
      foreach ($namespaces as $prefix => $url) {
        foreach ($xml
          ->children($url) as $element) {
          $ids[] = (string) $element;
        }
      }
    }
    return array_unique($ids);
  }

  /**
   * {@inheritdoc}
   *
   * Return a count of all available IDs from the source listing.
   * The default implementation assumes the count of top-level elements
   * reflects the number of IDs available - in many cases, you will need
   * to override this to reflect your particular XML structure.
   */
  public function computeCount() {
    $xml = simplexml_load_file($this->listUrl);
    $this
      ->registerNamespaces($xml);

    // Number of sourceid elements beneath the top-level element.
    $count = count($xml);

    // Additionally, if there are any namespaces registered, try to count
    // elements with namespaces as well.
    if ($namespaces = $xml
      ->getNamespaces()) {
      foreach ($namespaces as $prefix => $url) {
        $count += count($xml
          ->children($url));
      }
    }
    return $count;
  }

  /**
   * 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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateListXML::$listUrl protected property A URL pointing to an XML document containing a list of IDs to be processed.
MigrateListXML::$namespaces protected property An array of namespaces to explicitly register before Xpath queries.
MigrateListXML::computeCount public function Return a count of all available IDs from the source listing. The default implementation assumes the count of top-level elements reflects the number of IDs available - in many cases, you will need to override this to reflect your particular XML structure. Overrides MigrateList::computeCount
MigrateListXML::getIdList public function Load the XML at the given URL, and return an array of the IDs found within it. Overrides MigrateList::getIdList
MigrateListXML::getIDsFromXML protected function Gets an array of the IDs found in a XML.
MigrateListXML::registerNamespaces protected function Explicitly register namespaces on an XML element.
MigrateListXML::__construct public function Overrides MigrateList::__construct
MigrateListXML::__toString public function Our public face is the URL we're getting items from Overrides MigrateList::__toString