public function FeedsQueryPathParser::parse in Feeds QueryPath Parser 7
Same name and namespace in other branches
- 6 FeedsQueryPathParser.inc \FeedsQueryPathParser::parse()
Implements FeedsParser::parse().
File
- ./
FeedsQueryPathParser.inc, line 14 - Provides the class for FeedsQueryPathParser.
Class
- FeedsQueryPathParser
- @file
Code
public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
// Setup mappings so they can be used in variable replacement.
$mappings = $this
->getOwnMappings();
// Set source config, if it's empty get config from importer.
$this->source_config = $source
->getConfigFor($this);
// Allow config inheritance.
if (empty($this->source_config)) {
$this->source_config = $this
->getConfig();
}
$this->rawXML = array_keys(array_filter($this->source_config['rawXML']));
$this->debug = array_keys(array_filter($this->source_config['debug']['options']));
$raw = trim($fetcher_result
->getRaw());
if (empty($raw)) {
throw new Exception(t('Feeds QueryPath parser: The document is empty.'));
}
$opts = array(
'ignore_parser_warnings' => TRUE,
);
$result = new FeedsParserResult();
// Set link so we can set the result link attribute.
$fetcher_config = $source
->getConfigFor($source->importer->fetcher);
$result->link = $fetcher_config['source'];
$this
->includeQueryPath();
$doc = @qp($raw, NULL, $opts);
// Convert document to UTF-8
$ContentType = qp($doc, 'meta[http-equiv="content-type"]');
if ($ContentType
->hasAttr('content') && preg_match('/charset=([-\\w]*)/i', $ContentType
->attr('content'), $matches)) {
$ContentType
->attr('content', preg_replace('/charset=([-\\w]*)/i', 'charset=utf-8', $ContentType
->attr('content')));
qp($doc, 'meta[http-equiv="content-type"]')
->remove();
qp($doc, 'head')
->prepend($ContentType
->html());
$doc = qp(drupal_convert_to_utf8(utf8_decode($doc
->html()), $matches[1]), NULL, $opts);
}
$result->title = qp($doc, 'title', $opts)
->text();
$context = qp($doc, $this->source_config['context'], $opts);
$this
->debug($context, 'context');
foreach ($context as $item) {
$parsed_item = $variables = array();
foreach ($this->source_config['sources'] as $source => $query) {
// Variable substitution.
$query = strtr($query, $variables);
$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;
}
}
if (!empty($parsed_item)) {
$result->items[] = $parsed_item;
}
}
return $result;
}