You are here

class MigrateItemXML in Migrate 6.2

Same name and namespace in other branches
  1. 7.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 111
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;
  public function __construct($item_url) {
    parent::__construct();
    $this->itemUrl = $item_url;

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

  /**
   * Implementors are expected to return an object representing a source item.
   *
   * @param mixed $id
   *
   * @return stdClass
   */
  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) {
      $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;
    }
  }

  /**
   * 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
   */
  protected function constructItemUrl($id) {
    return str_replace(':id', $id, $this->itemUrl);
  }

  /**
   * 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.)
   */
  protected function loadXmlUrl($item_url) {
    return simplexml_load_file($item_url);
  }

}

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::constructItemUrl protected function 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.
MigrateItemXML::getItem public function Implementors are expected to return an object representing a source item. Overrides MigrateItem::getItem
MigrateItemXML::loadXmlUrl protected function 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.)
MigrateItemXML::__construct public function Overrides MigrateItem::__construct