class MigrateSourceJSON in Migrate 7.2
Implementation of MigrateSource, to handle imports from stand-alone JSON files.
Hierarchy
- class \MigrateSource implements \Iterator
- class \MigrateSourceJSON
Expanded class hierarchy of MigrateSourceJSON
File
- plugins/
sources/ json.inc, line 402 - Support for migration from JSON sources.
View source
class MigrateSourceJSON extends MigrateSource {
/**
* The MigrateJSONReader object serving as a cursor over the JSON source file.
*
* @var MigrateJSONReader
*/
protected $reader;
/**
* Name of the field within the JSON object holding the ID value.
*
* @var string
*/
protected $idField;
/**
* The source URLs to load JSON from
*
* @var array
*/
protected $sourceUrls = array();
/**
* Holds our current position within the $source_urls array
*
* @var int
*/
protected $activeUrl = NULL;
/**
* Store the reader class used to query JSON so we can create reader objects
* on the fly.
*
* @var string
*/
protected $readerClass = '';
/**
* List of available source fields.
*
* @var array
*/
protected $fields = array();
/**
* Source constructor.
*
* @param string or array $url
* URL(s) of the JSON source data.
* @param string $id_field
* Name of the field within the JSON object holding the ID value.
* @param array $fields
* Optional - keys are field names, values are descriptions. Use to override
* the default descriptions, or to add additional source fields which the
* migration will add via other means (e.g., prepareRow()).
* @param boolean $options
* Options applied to this source. In addition to the standard MigrateSource
* options, we support:
* - reader_class: The reader class to instantiate for traversing the JSON -
* defaults to MigrateJSONReader (any substitutions must be derived from
* MigrateJSONReader).
*/
public function __construct($urls, $id_field, array $fields = array(), array $options = array()) {
parent::__construct($options);
$this->idField = $id_field;
if (empty($options['reader_class'])) {
$reader_class = 'MigrateJSONReader';
}
else {
$reader_class = $options['reader_class'];
}
if (!is_array($urls)) {
$urls = array(
$urls,
);
}
$this->sourceUrls = $urls;
$active_url = variable_get('migrate_source_json_active_url', NULL);
if (isset($active_url)) {
$active_url--;
}
$this->activeUrl = $active_url;
$this->readerClass = $reader_class;
$this->fields = $fields;
}
/**
* Return a string representing the source query.
*
* @return string
*/
public function __toString() {
// Clump the urls into a string
// This could cause a problem when using a lot of urls, may need to hash
$urls = implode(', ', $this->sourceUrls);
return 'urls = ' . $urls;
}
/**
* Returns a list of fields available to be mapped from the source query.
*
* @return array
* Keys: machine names of the fields (to be passed to addFieldMapping)
* Values: Human-friendly descriptions of the fields.
*/
public function fields() {
return $this->fields;
}
/**
* Returns the active Url.
*
* @return string
*/
public function activeUrl() {
if (isset($this->activeUrl)) {
return $this->sourceUrls[$this->activeUrl];
}
}
/**
* Return a count of all available source records.
*/
public function computeCount() {
$count = 0;
foreach ($this->sourceUrls as $url) {
$reader = new $this->readerClass($url, $this->idField);
foreach ($reader as $element) {
$count++;
}
}
return $count;
}
/**
* Implementation of MigrateSource::performRewind().
*/
public function performRewind() {
// Set the reader back to the beginning of the file (positioned to the
// first matching element), then apply our logic to make sure we have the
// first element fulfilling our logic (idlist/map/prepareRow()).
$active_url = variable_get('migrate_source_json_active_url', NULL);
if (isset($active_url)) {
$active_url--;
}
$this->activeUrl = $active_url;
if ($this->reader) {
$this->reader
->rewind();
$this->reader = NULL;
}
}
/**
* Implementation of MigrationSource::getNextRow().
*
* @return stdClass
* data for the next row from the JSON source files
*/
public function getNextRow() {
migrate_instrument_start('MigrateSourceJSON::next');
$row = NULL;
// The reader is lazy loaded, so it may not be defined yet, need to test if set
if ($this->reader) {
// attempt to load the next row
$this->reader
->next();
}
// Test the reader for a valid row
if (isset($this->reader) && $this->reader
->valid()) {
$row = $this->reader
->current();
}
else {
// The current source is at the end, try to load the next source
if ($this
->getNextSource()) {
$row = $this->reader
->current();
}
}
migrate_instrument_stop('MigrateSourceJSON::next');
return $row;
}
/**
* Advances the reader to the next source from source_urls
*
* @return bool
* TRUE if a valid source was loaded
*/
public function getNextSource() {
migrate_instrument_start('MigrateSourceJSON::nextSource');
// Return value
$status = FALSE;
while ($this->activeUrl === NULL || count($this->sourceUrls) - 1 > $this->activeUrl) {
if (is_null($this->activeUrl)) {
$this->activeUrl = 0;
}
else {
// Increment the activeUrl so we try to load the next source
$this->activeUrl = $this->activeUrl + 1;
}
variable_set('migrate_source_json_active_url', $this->activeUrl);
$this->reader = new $this->readerClass($this->sourceUrls[$this->activeUrl], $this->idField);
$this->reader
->rewind();
if ($this->reader
->valid()) {
// We have a valid source
$status = TRUE;
break;
}
}
if (count($this->sourceUrls) - 1 == $this->activeUrl) {
variable_del('migrate_source_json_active_url');
}
migrate_instrument_stop('MigrateSourceJSON::nextSource');
return $status;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrateSource:: |
protected | property | The MigrateMap class for the current migration. | |
MigrateSource:: |
protected | property | The Migration class currently invoking us, during rewind() and next(). | |
MigrateSource:: |
protected | property | Whether this instance should cache the source count. | |
MigrateSource:: |
protected | property | Key to use for caching counts. | |
MigrateSource:: |
protected | property | The primary key of the current row | |
MigrateSource:: |
protected | property | The current row from the quey | |
MigrateSource:: |
protected | property | Information on the highwater mark for the current migration, if any. | |
MigrateSource:: |
protected | property | List of source IDs to process. | |
MigrateSource:: |
protected | property | By default, next() will directly read the map row and add it to the data row. A source plugin implementation may do this itself (in particular, the SQL source can incorporate the map table into the query) - if so, it should set this TRUE so we… | |
MigrateSource:: |
protected | property | Used in the case of multiple key sources that need to use idlist. | |
MigrateSource:: |
protected | property | Number of rows intentionally ignored (prepareRow() returned FALSE) | |
MigrateSource:: |
protected | property | Number of rows we've at least looked at. | 1 |
MigrateSource:: |
protected | property | The highwater mark at the beginning of the import operation. | |
MigrateSource:: |
protected | property | Whether this instance should not attempt to count the source. | |
MigrateSource:: |
protected | property | If TRUE, we will maintain hashed source rows to determine whether incoming data has changed. | |
MigrateSource:: |
public | function | Return a count of available source records, from the cache if appropriate. Returns -1 if the source is not countable. | |
MigrateSource:: |
public | function | Implementation of Iterator::current() - called when entering a loop iteration, returning the current row | |
MigrateSource:: |
protected | function | Determine whether this row has changed, and therefore whether it should be processed. | |
MigrateSource:: |
public | function | ||
MigrateSource:: |
public | function | ||
MigrateSource:: |
public | function | ||
MigrateSource:: |
protected | function | Generate a hash of the source row. | 3 |
MigrateSource:: |
public | function | Implementation of Iterator::key - called when entering a loop iteration, returning the key of the current row. It must be a scalar - we will serialize to fulfill the requirement, but using getCurrentKey() is preferable. | |
MigrateSource:: |
public | function | Implementation of Iterator::next() - subclasses of MigrateSource should implement getNextRow() to retrieve the next valid source rocord to process. | |
MigrateSource:: |
protected | function | Give the calling migration a shot at manipulating, and possibly rejecting, the source row. | |
MigrateSource:: |
public | function | Reset numIgnored back to 0. | |
MigrateSource:: |
public | function | Implementation of Iterator::rewind() - subclasses of MigrateSource should implement performRewind() to do any class-specific setup for iterating source records. | |
MigrateSource:: |
public | function | Implementation of Iterator::valid() - called at the top of the loop, returning TRUE to process the loop and FALSE to terminate it | |
MigrateSourceJSON:: |
protected | property | Holds our current position within the $source_urls array | |
MigrateSourceJSON:: |
protected | property | List of available source fields. | |
MigrateSourceJSON:: |
protected | property | Name of the field within the JSON object holding the ID value. | |
MigrateSourceJSON:: |
protected | property | The MigrateJSONReader object serving as a cursor over the JSON source file. | |
MigrateSourceJSON:: |
protected | property | Store the reader class used to query JSON so we can create reader objects on the fly. | |
MigrateSourceJSON:: |
protected | property | The source URLs to load JSON from | |
MigrateSourceJSON:: |
public | function | Returns the active Url. | |
MigrateSourceJSON:: |
public | function | Return a count of all available source records. | |
MigrateSourceJSON:: |
public | function |
Returns a list of fields available to be mapped from the source query. Overrides MigrateSource:: |
|
MigrateSourceJSON:: |
public | function | Implementation of MigrationSource::getNextRow(). | |
MigrateSourceJSON:: |
public | function | Advances the reader to the next source from source_urls | |
MigrateSourceJSON:: |
public | function | Implementation of MigrateSource::performRewind(). | |
MigrateSourceJSON:: |
public | function |
Source constructor. Overrides MigrateSource:: |
|
MigrateSourceJSON:: |
public | function | Return a string representing the source query. |