You are here

class MigrateItemJSON in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 plugins/sources/json.inc \MigrateItemJSON

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

Hierarchy

Expanded class hierarchy of MigrateItemJSON

File

plugins/sources/json.inc, line 106
Support for migration from JSON sources.

View source
class MigrateItemJSON extends MigrateItem {

  /**
   * A URL pointing to a JSON object containing the data for one item to be
   * migrated.
   *
   * @var string
   */
  protected $itemUrl;
  protected $httpOptions;
  public function __construct($item_url, $http_options) {
    parent::__construct();
    $this->itemUrl = $item_url;
    $this->httpOptions = $http_options;
  }

  /**
   * Implementors are expected to return an object representing a source item.
   *
   * @param mixed $id
   *
   * @return stdClass
   */
  public function getItem($id) {
    $item_url = $this
      ->constructItemUrl($id);

    // Get the JSON object at the specified URL
    $json = $this
      ->loadJSONUrl($item_url);
    if ($json) {
      return $json;
    }
    else {
      $migration = Migration::currentMigration();
      $message = t('Loading of !objecturl failed:', array(
        '!objecturl' => $item_url,
      ));
      $migration
        ->getMap()
        ->saveMessage(array(
        $id,
      ), $message, MigrationBase::MESSAGE_ERROR);
      return NULL;
    }
  }

  /**
   * The default implementation simply replaces the :id token in the URL with
   * the ID obtained from MigrateListJSON. 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 JSON loader - just pull and decode. This can be overridden for
   * preprocessing of JSON (removal of unwanted elements, caching of JSON if the
   * source service is slow, etc.)
   */
  protected function loadJSONUrl($item_url) {
    if (empty($this->httpOptions)) {
      $json = file_get_contents($item_url);
    }
    else {
      $response = drupal_http_request($item_url, $this->httpOptions);
      $json = $response->data;
    }
    return json_decode($json);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateItemJSON::$httpOptions protected property
MigrateItemJSON::$itemUrl protected property A URL pointing to a JSON object containing the data for one item to be migrated.
MigrateItemJSON::constructItemUrl protected function The default implementation simply replaces the :id token in the URL with the ID obtained from MigrateListJSON. Override if the item URL is not so easily expressed from the ID.
MigrateItemJSON::getItem public function Implementors are expected to return an object representing a source item. Overrides MigrateItem::getItem
MigrateItemJSON::loadJSONUrl protected function Default JSON loader - just pull and decode. This can be overridden for preprocessing of JSON (removal of unwanted elements, caching of JSON if the source service is slow, etc.)
MigrateItemJSON::__construct public function Overrides MigrateItem::__construct