You are here

public function FeedsXPathParserBase::parse in Feeds XPath Parser 6

Same name and namespace in other branches
  1. 7 FeedsXPathParserBase.inc \FeedsXPathParserBase::parse()

Implements FeedsParser::parse().

Overrides FeedsParser::parse

File

./FeedsXPathParserBase.inc, line 38
Provides the abstract base class for FeedsXPathParserHTML and FeedsXPathParserXML.

Class

FeedsXPathParserBase
Base class for the HTML and XML parsers.

Code

public function parse(FeedsImportBatch $batch, FeedsSource $source) {
  $source_config = $source
    ->getConfigFor($this);
  if (empty($source_config)) {
    $source_config = $this
      ->getConfig();
  }
  $this->doc = $this
    ->setup($source_config, $batch);
  $mappings = $this
    ->getOwnMappings();
  $this->rawXML = array_keys(array_filter($source_config['rawXML']));

  // Set link.
  $fetcher_config = $source
    ->getConfigFor($source->importer->fetcher);
  $batch->link = $fetcher_config['source'];
  $this->xpath = new FeedsXPathParserDOMXPath($this->doc);
  $config = array();
  $config['debug'] = array_keys(array_filter($source_config['exp']['debug']));
  $config['errors'] = $source_config['exp']['errors'];
  $this->xpath
    ->setConfig($config);
  $all_nodes = $this->xpath
    ->namespacedQuery($source_config['context'], NULL, 'context');
  foreach ($all_nodes as $node) {
    $parsed_item = $variables = array();
    foreach ($source_config['sources'] as $source => $query) {

      // Variable substitution.
      $query = strtr($query, $variables);

      // Parse the item.
      $result = $this
        ->parseSourceElement($query, $node, $source);
      if (isset($result)) {
        if (!is_array($result)) {
          $variables['$' . $mappings[$source]] = $result;
        }
        else {
          $variables['$' . $mappings[$source]] = '';
        }
        $parsed_item[$source] = $result;
      }
    }
    if (!empty($parsed_item)) {
      $batch->items[] = $parsed_item;
    }
  }
  unset($this->doc);
  unset($this->xpath);
}