You are here

class MigrateListJSON in Migrate 6.2

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

Implementation of MigrateList, for retrieving a list of IDs to be migrated from a JSON object.

Hierarchy

Expanded class hierarchy of MigrateListJSON

File

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

View source
class MigrateListJSON extends MigrateList {

  /**
   * A URL pointing to an JSON object containing a list of IDs to be processed.
   *
   * @var string
   */
  protected $listUrl;
  protected $httpOptions;
  public function __construct($list_url, $http_options = array()) {
    parent::__construct();
    $this->listUrl = $list_url;
    $this->httpOptions = $http_options;
  }

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

  /**
   * Load the JSON at the given URL, and return an array of the IDs found within it.
   *
   * @return array
   */
  public function getIdList() {
    migrate_instrument_start("Retrieve {$this->listUrl}");
    if (empty($this->httpOptions)) {
      $json = file_get_contents($this->listUrl);
    }
    else {
      $response = drupal_http_request($this->listUrl, $this->httpOptions);
      $json = $response->data;
    }
    migrate_instrument_stop("Retrieve {$this->listUrl}");
    if ($json) {
      $data = drupal_json_decode($json);
      if ($data) {
        return $this
          ->getIDsFromJSON($data);
      }
    }
    Migration::displayMessage(t('Loading of !listurl failed:', array(
      '!listurl' => $this->listUrl,
    )));
    return NULL;
  }

  /**
   * Given an array generated from JSON, 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 JSON structure.
   *
   * @param array $data
   *
   * @return array
   */
  protected function getIDsFromJSON(array $data) {
    return $data;
  }

  /**
   * 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 JSON structure.
   */
  public function computeCount() {
    $count = 0;
    if (empty($this->httpOptions)) {
      $json = file_get_contents($this->listUrl);
    }
    else {
      $response = drupal_http_request($this->listUrl, $this->httpOptions);
      $json = $response->data;
    }
    if ($json) {
      $data = drupal_json_decode($json);
      if ($data) {
        $count = count($data);
      }
    }
    return $count;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateListJSON::$httpOptions protected property
MigrateListJSON::$listUrl protected property A URL pointing to an JSON object containing a list of IDs to be processed.
MigrateListJSON::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 JSON structure. Overrides MigrateList::computeCount
MigrateListJSON::getIdList public function Load the JSON at the given URL, and return an array of the IDs found within it. Overrides MigrateList::getIdList
MigrateListJSON::getIDsFromJSON protected function Given an array generated from JSON, 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…
MigrateListJSON::__construct public function Overrides MigrateList::__construct
MigrateListJSON::__toString public function Our public face is the URL we're getting items from Overrides MigrateList::__toString