class FeedsQueryPathParser in Feeds QueryPath Parser 6
Same name and namespace in other branches
- 7 FeedsQueryPathParser.inc \FeedsQueryPathParser
@file
Provides the class for FeedsQueryPathParser.
Hierarchy
- class \FeedsConfigurable
- class \FeedsPlugin implements FeedsSourceInterface
- class \FeedsParser
- class \FeedsQueryPathParser
- class \FeedsParser
- class \FeedsPlugin implements FeedsSourceInterface
Expanded class hierarchy of FeedsQueryPathParser
1 string reference to 'FeedsQueryPathParser'
- feeds_querypath_parser_feeds_plugins in ./
feeds_querypath_parser.module - Implementation of hook_feeds_plugins().
File
- ./
FeedsQueryPathParser.inc, line 9 - Provides the class for FeedsQueryPathParser.
View source
class FeedsQueryPathParser extends FeedsParser {
protected $debug_options = array();
protected $debug_switch = FALSE;
protected $rawXML = array();
protected $source_config = array();
/**
* Implementation of FeedsParser::parse().
*/
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;
}
/**
* Source form.
*/
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']['errors'] = array(
// '#type' => 'checkbox',
// '#title' => t('Show error messages.'),
// '#default_value' => isset($source_config['options']['errors']) ? $source_config['options']['errors'] : FALSE,
//);
//if (extension_loaded('tidy')) {
// $form['options']['tidy'] = array(
// '#type' => 'checkbox',
// '#title' => t('Use Tidy'),
// '#description' => t('The Tidy PHP extension has been detected.
// Select this to clean the markup before parsing.'),
// '#default_value' => isset($source_config['options']['tidy']) ? $source_config['options']['tidy'] : FALSE,
// );
//}
$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;
}
/**
* Override parent::configForm().
*/
public function configForm(&$form_state) {
$form = $this
->sourceForm($this->config);
$form['context']['#required'] = FALSE;
return $form;
}
/**
* Define source defaults.
*/
public function sourceDefaults() {
return $this
->configDefaults();
}
/**
* Define config defaults.
*/
public function configDefaults() {
return array(
'context' => '',
'sources' => array(),
'attrs' => array(),
'rawXML' => array(),
'options' => array(
'errors' => FALSE,
'tidy' => FALSE,
'debug' => array(),
),
);
}
/**
* Override parent::sourceFormValidate().
*
* Simply trims all values from the form. That way when testing them
* later we can be sure that there aren't any strings with spaces in them.
*
* @param &$values
* The values from the form to validate, passed by reference.
*/
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);
}
}
/**
* Override parent::getMappingSources().
*/
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();
// Older versions of Feeds return FALSE here.
if (is_array($sources)) {
return $sources + $querypath_source;
}
return $querypath_source;
}
}
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(). | |
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:: |
|
FeedsQueryPathParser:: |
protected | property | ||
FeedsQueryPathParser:: |
protected | property | ||
FeedsQueryPathParser:: |
protected | property | ||
FeedsQueryPathParser:: |
protected | property | ||
FeedsQueryPathParser:: |
public | function |
Define config defaults. Overrides FeedsConfigurable:: |
|
FeedsQueryPathParser:: |
public | function |
Override parent::configForm(). Overrides FeedsConfigurable:: |
|
FeedsQueryPathParser:: |
public | function |
Validation handler for configForm(). Overrides FeedsConfigurable:: |
|
FeedsQueryPathParser:: |
protected | function | ||
FeedsQueryPathParser:: |
public | function |
Override parent::getMappingSources(). Overrides FeedsParser:: |
|
FeedsQueryPathParser:: |
public | function |
Implementation of FeedsParser::parse(). Overrides FeedsParser:: |
|
FeedsQueryPathParser:: |
protected | function | ||
FeedsQueryPathParser:: |
public | function |
Define source defaults. Overrides FeedsPlugin:: |
|
FeedsQueryPathParser:: |
public | function |
Source form. Overrides FeedsPlugin:: |
|
FeedsQueryPathParser:: |
public | function |
Override parent::sourceFormValidate(). Overrides FeedsPlugin:: |