class FeedsJSONPathParser in Feeds JSONPath Parser 6
Same name and namespace in other branches
- 7 FeedsJSONPathParser.inc \FeedsJSONPathParser
Base class for the HTML and XML parsers.
Hierarchy
- class \FeedsConfigurable
- class \FeedsPlugin implements FeedsSourceInterface
- class \FeedsParser
- class \FeedsJSONPathParser
- class \FeedsParser
- class \FeedsPlugin implements FeedsSourceInterface
Expanded class hierarchy of FeedsJSONPathParser
2 string references to 'FeedsJSONPathParser'
- FeedsJSONPathParserTestCase::test in tests/
feeds_jsonpath_parser.test - Run tests.
- feeds_jsonpath_parser_feeds_plugins in ./
feeds_jsonpath_parser.module - Implementation of hook_feeds_plugins().
File
- ./
FeedsJSONPathParser.inc, line 12 - Provides the Class for Feeds JSONPath Parser.
View source
class FeedsJSONPathParser extends FeedsParser {
/**
* Implementation of FeedsParser::parse().
*/
public function parse(FeedsImportBatch $batch, FeedsSource $source) {
$mappings = $source->importer->processor->config['mappings'];
$mappings = $this
->filterMappings($mappings);
$source_config = $source
->getConfigFor($this);
// Allow config inheritance.
if (empty($source_config)) {
$source_config = $this->config;
}
$this->debug = array_keys(array_filter($source_config['debug']['options']));
$raw = trim($batch
->getRaw());
// Set link so we can set the result link attribute.
$fetcher_config = $source
->getConfigFor($source->importer->fetcher);
$batch->link = $fetcher_config['source'];
$array = json_decode(utf8_encode($raw), TRUE);
// Support JSON lines format.
if (!is_array($array)) {
$raw = preg_replace('/}\\s*{/', '},{', $raw);
$raw = '[' . $raw . ']';
$array = json_decode(utf8_encode($raw), TRUE);
}
if (is_array($array)) {
require_once 'jsonpath-0.8.1.php';
$all_items = $this
->jsonPath($array, $source_config['context']);
$this
->debug($all_items, 'context');
unset($array);
foreach ($all_items as $item) {
$parsed_item = $variables = array();
foreach ($source_config['sources'] as $source => $query) {
$parsed = $this
->parseSourceElement($item, $query, $source);
// Avoid null values.
if (isset($parsed)) {
// Variable sunstitution can't handle arrays.
if (!is_array($parsed)) {
$variables['{' . $mappings[$source] . '}'] = $parsed;
}
else {
$variables['{' . $mappings[$source] . '}'] = '';
}
$parsed_item[$source] = $parsed;
}
}
$batch->items[] = $parsed_item;
}
}
else {
throw new Exception(t('There was an error decoding the JSON document.'));
}
}
/**
* Utilizes the jsonPath function from jsonpath-0.8.1.php
*
* jsonPath returns false if the expression returns zero results and that will
* mess up our for loops, so return an empty array instead.
*
* @todo
* Firgure out error handling.
* @param $array
* The input array to parse
* @$expression
* The JSONPath expression.
* @return array
* Returns an array that is the output of jsonPath
*/
private function jsonPath($array, $expression) {
$result = jsonPath($array, $expression);
return $result === FALSE ? array() : $result;
}
/**
* Parses one item from the context array.
*
* @param $item
* A PHP array.
* @param $query
* A JSONPath query.
* @return array
* An array containing the results of the query.
*/
protected function parseSourceElement($item, $query, $source) {
if (empty($query)) {
return;
}
$results = $this
->jsonPath($item, $query);
$this
->debug($results, $source);
unset($item);
/**
* If there is one result, return it directly. If there are no results,
* return. Otherwise return the results.
*/
if (count($results) === 1) {
return $results[0];
}
if (count($results) === 0) {
return;
}
return $results;
}
/**
* Source form.
*/
public function sourceForm($source_config) {
if (empty($source_config)) {
$source_config = $this->config;
}
$mappings_ = feeds_importer($this->id)->processor->config['mappings'];
$uniques = $mappings = array();
foreach ($mappings_ as $mapping) {
if (strpos($mapping['source'], 'jsonpath_parser:') === 0) {
$mappings[$mapping['source']] = $mapping['target'];
if ($mapping['unique']) {
$uniques[] = $mapping['target'];
}
}
}
$form = array();
$form['jsonpath'] = array(
'#type' => 'fieldset',
'#title' => t('JSONPath Parser Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
if (empty($mappings)) {
$form['jsonpath']['error_message']['#value'] = '<div class="help">' . t('FeedsJSONPathParser: No mappings were defined.') . '</div>';
return $form;
}
$form['jsonpath']['context'] = array(
'#type' => 'textfield',
'#title' => t('Context'),
'#required' => TRUE,
'#description' => t('This is the base query, all other queries will execute in this context.'),
'#default_value' => isset($source_config['context']) ? $source_config['context'] : '',
'#maxlength' => 1024,
'#size' => 80,
);
$form['jsonpath']['sources'] = array(
'#type' => 'fieldset',
);
if (!empty($uniques)) {
$items = array(
format_plural(count($uniques), t('Field <strong>!column</strong> is mandatory and considered unique: only one item per !column value will be created.', array(
'!column' => implode(', ', $uniques),
)), t('Fields <strong>!columns</strong> are mandatory and values in these columns are considered unique: only one entry per value in one of these columns will be created.', array(
'!columns' => implode(', ', $uniques),
))),
);
$form['jsonpath']['sources']['help']['#value'] = '<div class="help">' . theme('item_list', array(
'items' => $items,
)) . '</div>';
}
$variables = array();
foreach ($mappings as $source => $target) {
$form['jsonpath']['sources'][$source] = array(
'#type' => 'textfield',
'#title' => $target,
'#description' => t('The JSONPath expression to execute.'),
'#default_value' => isset($source_config['sources'][$source]) ? $source_config['sources'][$source] : '',
'#maxlength' => 1024,
'#size' => 80,
);
if (!empty($variables)) {
$form['jsonpath']['sources'][$source]['#description'] .= '<br>' . t('The variables ' . implode(', ', $variables) . ' are availliable for replacement.');
}
$variables[] = '{' . $target . '}';
}
$form['jsonpath']['debug'] = array(
'#type' => 'fieldset',
'#title' => t('Debug'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['jsonpath']['debug']['options'] = array(
'#type' => 'checkboxes',
'#title' => t('Debug query'),
'#options' => array_merge(array(
'context' => 'context',
), $mappings),
'#default_value' => isset($source_config['debug']['options']) ? $source_config['debug']['options'] : array(),
);
return $form;
}
/**
* Override parent::configForm().
*/
public function configForm(&$form_state) {
$form = $this
->sourceForm($this->config);
$form['jsonpath']['context']['#required'] = FALSE;
$form['jsonpath']['#collapsed'] = FALSE;
return $form;
}
/**
* Override parent::getMappingSources().
*/
public function getMappingSources() {
$mappings = $this
->filterMappings(feeds_importer($this->id)->processor->config['mappings']);
$next = 0;
if (!empty($mappings)) {
$last_mapping = end(array_keys($mappings));
$next = explode(':', $last_mapping);
$next = $next[1] + 1;
}
return array(
'jsonpath_parser:' . $next => array(
'name' => t('JSONPath Expression'),
'description' => t('Allows you to configure n JSONPath expression that will populate this field.'),
),
) + parent::getMappingSources();
}
public function sourceDefaults() {
return array();
}
/**
* Define defaults.
*/
public function configDefaults() {
return array(
'context' => '',
'sources' => array(),
'debug' => array(),
);
}
/**
* Override parent::sourceFormValidate().
*
* If the values of this source are the same as the base config we set them to
* blank to that the values will be inherited from the importer defaults.
*
* @param &$values
* The values from the form to validate, passed by reference.
*/
public function sourceFormValidate(&$values) {
$values = $values['jsonpath'];
asort($values);
asort($this->config);
if ($values === $this->config) {
$values = array();
return;
}
$this
->configFormValidate($values);
}
/**
* Override parent::sourceFormValidate().
*/
public function configFormValidate(&$values) {
if (isset($values['jsonpath'])) {
$values = $values['jsonpath'];
}
$values['context'] = trim($values['context']);
foreach ($values['sources'] as &$source) {
$source = trim($source);
}
}
/**
* Filters mappings, returning the ones that belong to us.
*/
private function filterMappings($mappings) {
$our_mappings = array();
foreach ($mappings as $mapping) {
if (strpos($mapping['source'], 'jsonpath_parser:') === 0) {
$our_mappings[$mapping['source']] = $mapping['target'];
}
}
return $our_mappings;
}
private function debug($item, $source) {
if (in_array($source, $this->debug)) {
$o = '<ul>';
foreach ($item as $i) {
$o .= '<li>' . check_plain(var_export($i, TRUE)) . '</li>';
}
$o .= '</ul>';
drupal_set_message($source . ':' . $o);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedsConfigurable:: |
protected | property | ||
FeedsConfigurable:: |
protected | property | CTools export enabled status of this object. | |
FeedsConfigurable:: |
protected | property | ||
FeedsConfigurable:: |
protected | property | ||
FeedsConfigurable:: |
public | function | Similar to setConfig but adds to existing configuration. | 1 |
FeedsConfigurable:: |
public | function | Submission handler for configForm(). | 3 |
FeedsConfigurable:: |
public | function | Copy a configuration. | 1 |
FeedsConfigurable:: |
public | function | Determine whether this object is persistent and enabled. I. e. it is defined either in code or in the database and it is enabled. | 1 |
FeedsConfigurable:: |
public | function | Implementation of getConfig(). | 1 |
FeedsConfigurable:: |
public static | function | Instantiate a FeedsConfigurable object. | 1 |
FeedsConfigurable:: |
public | function | Set configuration. | 1 |
FeedsConfigurable:: |
public | function | Override magic method __get(). Make sure that $this->config goes through getConfig() | |
FeedsConfigurable:: |
public | function | Override magic method __isset(). This is needed due to overriding __get(). | |
FeedsJSONPathParser:: |
public | function |
Define defaults. Overrides FeedsConfigurable:: |
|
FeedsJSONPathParser:: |
public | function |
Override parent::configForm(). Overrides FeedsConfigurable:: |
|
FeedsJSONPathParser:: |
public | function |
Override parent::sourceFormValidate(). Overrides FeedsConfigurable:: |
|
FeedsJSONPathParser:: |
private | function | ||
FeedsJSONPathParser:: |
private | function | Filters mappings, returning the ones that belong to us. | |
FeedsJSONPathParser:: |
public | function |
Override parent::getMappingSources(). Overrides FeedsParser:: |
|
FeedsJSONPathParser:: |
private | function | Utilizes the jsonPath function from jsonpath-0.8.1.php | |
FeedsJSONPathParser:: |
public | function |
Implementation of FeedsParser::parse(). Overrides FeedsParser:: |
|
FeedsJSONPathParser:: |
protected | function | Parses one item from the context array. | |
FeedsJSONPathParser:: |
public | function |
Implementation of FeedsSourceInterface::sourceDefaults(). Overrides FeedsPlugin:: |
|
FeedsJSONPathParser:: |
public | function |
Source form. Overrides FeedsPlugin:: |
|
FeedsJSONPathParser:: |
public | function |
Override parent::sourceFormValidate(). Overrides FeedsPlugin:: |
|
FeedsParser:: |
public | function | Clear all caches for results for given source. | |
FeedsParser:: |
public | function | Get an element identified by $element_key of the given item. The element key corresponds to the values in the array returned by FeedsParser::getMappingSources(). | 1 |
FeedsPlugin:: |
public | function |
Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface:: |
|
FeedsPlugin:: |
protected static | function | Loads on-behalf implementations from mappers/ directory. | |
FeedsPlugin:: |
public | function |
Save changes to the configuration of this object.
Delegate saving to parent (= Feed) which will collect
information from this object by way of getConfig() and store it. Overrides FeedsConfigurable:: |
|
FeedsPlugin:: |
public | function |
A source is being deleted. Overrides FeedsSourceInterface:: |
1 |
FeedsPlugin:: |
public | function |
A source is being saved. Overrides FeedsSourceInterface:: |
1 |
FeedsPlugin:: |
protected | function |
Constructor. Overrides FeedsConfigurable:: |