View source
<?php
class FeedsQueryPathParser extends FeedsParser {
protected $debug_options = array();
protected $debug_switch = FALSE;
protected $rawXML = array();
protected $source_config = array();
public function parse(FeedsImportBatch $batch, FeedsSource $source) {
$this->source_config = $source
->getConfigFor($this);
if (isset($this->source_config['rawXML']) && is_array($this->source_config['rawXML'])) {
$this->rawXML = array_keys(array_filter($this->source_config['rawXML']));
}
if (isset($this->source_config['options']['debug']) && is_array($this->source_config['options']['debug'])) {
$this->debug_options = array_keys(array_filter($this->source_config['options']['debug']));
}
if (!empty($this->debug_options)) {
$this->debug_switch = TRUE;
}
$mappings = feeds_importer($this->id)->processor->config['mappings'];
$this->mappings = array();
foreach ($mappings as $mapping) {
if (strpos($mapping['source'], 'querypathparser:') === 0) {
$this->mappings[$mapping['source']] = $mapping['target'];
}
}
$batch
->setTitle(trim(qp($batch
->getRaw(), 'title')
->text()));
$context = qp($batch
->getRaw(), $this->source_config['context']);
foreach ($context as $child) {
$parsed_item = $variables = array();
foreach ($this->source_config['sources'] as $source => $query) {
$query = strtr($query, $variables);
$result = $this
->parseSourceElement($child, $query, $source);
if (!is_array($result)) {
$variables['$' . $this->mappings[$source]] = $result;
}
$parsed_item[$source] = $result;
}
$batch
->addItem($parsed_item);
}
}
protected function parseSourceElement($item, $query, $source) {
$attr = $this->source_config['attrs'][$source];
if ($query == '' && $attr == '') {
return;
}
if ($query != '') {
$item = qp($item, $query);
}
$values = array();
foreach ($item as $i) {
if ($attr != '') {
$values[] = $i
->attr($attr);
}
elseif (in_array($source, $this->rawXML)) {
$values[] = $i
->html();
}
else {
$values[] = $i
->text();
}
}
$this
->debug($values, $source);
return count($values) <= 1 ? reset($values) : $values;
}
public function sourceForm($source_config) {
$form = array();
$form['#weight'] = -10;
$form['#tree'] = TRUE;
$mappings_ = feeds_importer($this->id)->processor->config['mappings'];
$uniques = $mappings = array();
foreach ($mappings_ as $mapping) {
if (strpos($mapping['source'], 'querypathparser:') === 0) {
$mappings[$mapping['source']] = $mapping['target'];
if ($mapping['unique']) {
$uniques[] = $mapping['target'];
}
}
}
if (empty($mappings)) {
$form['error_message']['#value'] = 'FeedsQueryPathParser: No mappings were defined.';
return $form;
}
$form['context'] = array(
'#type' => 'textfield',
'#title' => t('Context'),
'#required' => TRUE,
'#description' => t('This is the base query, all other queries will run in this context.'),
'#default_value' => isset($source_config['context']) ? $source_config['context'] : '',
'#maxlength' => 1024,
);
$form['sources'] = array(
'#type' => 'fieldset',
);
$form['attrs'] = array(
'#title' => t('Attributes'),
'#type' => 'fieldset',
'#description' => t('If you want an attribute returned for a field, type its name here.'),
);
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['sources']['help']['#value'] = '<div class="help">' . theme('item_list', $items) . '</div>';
}
$variables = array();
foreach ($mappings as $source => $target) {
$form['sources'][$source] = array(
'#type' => 'textfield',
'#title' => $target,
'#description' => t('The CSS selector for this field.'),
'#default_value' => isset($source_config['sources'][$source]) ? $source_config['sources'][$source] : '',
'#maxlength' => 1024,
);
if (!empty($variables)) {
$form['sources'][$source]['#description'] .= '<br>' . t('The variables ' . implode(', ', $variables) . ' are availliable for replacement.');
}
$variables[] = '$' . $target;
$form['attrs'][$source] = array(
'#type' => 'textfield',
'#title' => $target,
'#description' => t('The attribute to return.'),
'#default_value' => isset($source_config['attrs'][$source]) ? $source_config['attrs'][$source] : '',
'#maxlength' => 1024,
);
}
$form['rawXML'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the queries you would like to return raw XML or HTML'),
'#options' => $mappings,
'#default_value' => isset($source_config['rawXML']) ? $source_config['rawXML'] : array(),
);
$form['options'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => 'QueryPath Parser Options',
);
$form['options']['debug'] = array(
'#type' => 'checkboxes',
'#title' => t('Debug Query'),
'#options' => array_merge(array(
'context' => 'context',
), $mappings),
'#default_value' => isset($source_config['options']['debug']) ? $source_config['options']['debug'] : array(),
);
return $form;
}
public function configForm(&$form_state) {
$form = $this
->sourceForm($this->config);
$form['context']['#required'] = FALSE;
return $form;
}
public function sourceDefaults() {
return $this
->configDefaults();
}
public function configDefaults() {
return array(
'context' => '',
'sources' => array(),
'attrs' => array(),
'rawXML' => array(),
'options' => array(
'errors' => FALSE,
'tidy' => FALSE,
'debug' => array(),
),
);
}
public function sourceFormValidate(&$values) {
$values['context'] = trim($values['context']);
foreach ($values['sources'] as &$query) {
$query = trim($query);
}
foreach ($values['attrs'] as &$attr) {
$attr = trim($attr);
}
}
public function configFormValidate(&$values) {
$this
->sourceFormValidate($values);
}
protected function debug($item, $source) {
if ($this->debug_switch && in_array($source, $this->debug_options)) {
$o = '<ul>';
foreach ($item as $i) {
$o .= '<li>' . check_plain($i
->asXML()) . '</li>';
}
$o .= '</ul>';
drupal_set_message($source . ':' . $o);
}
}
public function getMappingSources() {
$querypath_source = array(
'querypathparser:0' => array(
'name' => t('QueryPath Expression'),
'description' => t('Allows you to configure an CSS expression that will populate this field.'),
),
);
$sources = parent::getMappingSources();
if (is_array($sources)) {
return $sources + $querypath_source;
}
return $querypath_source;
}
}
function feeds_querypath_parser_form_feeds_ui_mapping_form_alter(&$form, &$form_state) {
$newest_querypath_mapping = array();
foreach ($form['#mappings'] as $mapping) {
if (strpos($mapping['source'], 'querypathparser:') === 0) {
$newest_querypath_mapping = $mapping;
}
}
if (!empty($newest_querypath_mapping)) {
list($a, $count) = explode(':', $newest_querypath_mapping['source']);
$default_source = $a . ':' . '0';
$label = $form['source']['#options'][$default_source];
unset($form['source']['#options'][$default_source]);
$form['source']['#options'][$a . ':' . ++$count] = $label;
}
}