You are here

class FeedsExQueryPathXml in Feeds extensible parsers 7

Same name and namespace in other branches
  1. 7.2 src/FeedsExQueryPathXml.inc \FeedsExQueryPathXml

Parses XML documents with QueryPath.

Hierarchy

Expanded class hierarchy of FeedsExQueryPathXml

2 string references to 'FeedsExQueryPathXml'
FeedsExQueryPathXml.test in src/Tests/FeedsExQueryPathXml.test
feeds_ex_feeds_plugins in ./feeds_ex.feeds.inc
Implements hook_feeds_plugins().

File

src/FeedsExQueryPathXml.inc, line 11
Contains FeedsExXml.

View source
class FeedsExQueryPathXml extends FeedsExXml {

  /**
   * Options passed to QueryPath.
   *
   * @var array
   */
  protected $queryPathOptions = array(
    'ignore_parser_warnings' => TRUE,
    'use_parser' => 'xml',
    'strip_low_ascii' => FALSE,
    'replace_entities' => FALSE,
    'omit_xml_declaration' => TRUE,
    'encoding' => 'UTF-8',
  );

  /**
   * {@inheritdoc}
   */
  protected function executeContext(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
    $document = $this
      ->prepareDocument($source, $fetcher_result);
    $parser = new QueryPath($document, NULL, $this->queryPathOptions);
    $query_path = $parser
      ->find($this->config['context']['value']);
    $state = $source
      ->state(FEEDS_PARSE);
    if (!$state->total) {
      $state->total = $query_path
        ->size();
    }
    $start = (int) $state->pointer;
    $state->pointer = $start + $source->importer
      ->getLimit();
    $state
      ->progress($state->total, $state->pointer);
    return $query_path
      ->slice($start, $source->importer
      ->getLimit());
  }

  /**
   * {@inheritdoc}
   */
  protected function executeSourceExpression($machine_name, $expression, $row) {
    $result = new QueryPath($row, $expression, $this->queryPathOptions);
    if ($result
      ->size() == 0) {
      return;
    }
    $config = $this->config['sources'][$machine_name];
    $return = array();
    if (strlen($config['attribute'])) {
      foreach ($result as $node) {
        $return[] = $node
          ->attr($config['attribute']);
      }
    }
    elseif (!empty($config['inner'])) {
      foreach ($result as $node) {
        $return[] = $node
          ->innerXML();
      }
    }
    elseif (!empty($config['raw'])) {
      foreach ($result as $node) {
        $return[] = $this
          ->getRawValue($node);
      }
    }
    else {
      foreach ($result as $node) {
        $return[] = $node
          ->text();
      }
    }

    // Return a single value if there's only one value.
    return count($return) === 1 ? reset($return) : $return;
  }

  /**
   * Returns the raw value.
   *
   * @param QueryPath $node
   *   The QueryPath object to return a raw value for.
   *
   * @return string
   *   A raw string value.
   */
  protected function getRawValue(QueryPath $node) {
    return $node
      ->xml();
  }

  /**
   * {@inheritdoc}
   */
  protected function validateExpression(&$expression) {
    $expression = trim($expression);
    if (!$expression) {
      return;
    }
    try {
      $parser = new QueryPath(NULL, $expression);
    } catch (CSSParseException $e) {
      return check_plain($e
        ->getMessage());
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function configFormTableHeader() {
    return array(
      'attribute' => t('Attribute'),
    ) + parent::configFormTableHeader();
  }

  /**
   * {@inheritdoc}
   */
  protected function configFormTableColumn(array &$form_state, array $values, $column, $machine_name) {
    switch ($column) {
      case 'attribute':
        return array(
          '#type' => 'textfield',
          '#title' => t('Attribute name'),
          '#title_display' => 'invisible',
          '#default_value' => !empty($values['attribute']) ? $values['attribute'] : '',
          '#size' => 10,
          '#maxlength' => 1024,
        );
      default:
        return parent::configFormTableColumn($form_state, $values, $column, $machine_name);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsExBase::$encoder protected property The encoder used to convert encodings.
FeedsExBase::$messenger protected property The object used to display messages to the user.
FeedsExBase::debug protected function Renders our debug messages into a list.
FeedsExBase::executeSources protected function Executes the source expressions.
FeedsExBase::getEncoder public function Returns the encoder.
FeedsExBase::getFormHeader protected function Returns the configuration form table header.
FeedsExBase::getMappingSources public function
FeedsExBase::getMessenger public function Returns the messenger.
FeedsExBase::hasConfigForm public function
FeedsExBase::hasConfigurableContext protected function Returns whether or not this parser uses a context query. 2
FeedsExBase::hasSourceConfig public function
FeedsExBase::loadLibrary protected function Loads the necessary library. 2
FeedsExBase::logErrors protected function Logs errors.
FeedsExBase::parse public function
FeedsExBase::parseItems protected function Performs the actual parsing. 2
FeedsExBase::prepareExpressions protected function Prepares the expressions for parsing.
FeedsExBase::prepareRaw protected function Prepares the raw string for parsing.
FeedsExBase::prepareVariables protected function Prepares the variable map used to substitution.
FeedsExBase::printErrors protected function Prints errors to the screen.
FeedsExBase::setEncoder public function Sets the encoder.
FeedsExBase::setMessenger public function Sets the messenger to be used to display messages.
FeedsExBase::sourceDefaults public function
FeedsExBase::sourceForm public function
FeedsExBase::sourceFormValidate public function
FeedsExBase::sourceSave public function
FeedsExQueryPathXml::$queryPathOptions protected property Options passed to QueryPath.
FeedsExQueryPathXml::configFormTableColumn protected function Returns a form element for a specific column. Overrides FeedsExXml::configFormTableColumn
FeedsExQueryPathXml::configFormTableHeader protected function Reuturns the list of table headers. Overrides FeedsExXml::configFormTableHeader
FeedsExQueryPathXml::executeContext protected function Returns rows to be parsed. Overrides FeedsExXml::executeContext
FeedsExQueryPathXml::executeSourceExpression protected function Executes a single source expression. Overrides FeedsExXml::executeSourceExpression
FeedsExQueryPathXml::getRawValue protected function Returns the raw value. 1
FeedsExQueryPathXml::validateExpression protected function Validates an expression. Overrides FeedsExXml::validateExpression
FeedsExXml::$encoderClass protected property The class used as the text encoder. Overrides FeedsExBase::$encoderClass 2
FeedsExXml::$entityLoader protected property The previous value for the entity loader.
FeedsExXml::$handleXmlErrors protected property The previous value for XML error handling.
FeedsExXml::$xpath protected property The FeedsExXpathDomXpath object used for parsing.
FeedsExXml::cleanUp protected function Allows subclasses to cleanup after parsing. Overrides FeedsExBase::cleanUp
FeedsExXml::configDefaults public function Overrides FeedsExBase::configDefaults
FeedsExXml::configForm public function Overrides FeedsExBase::configForm
FeedsExXml::configFormValidate public function Overrides FeedsExBase::configFormValidate
FeedsExXml::getErrors protected function Returns the errors after parsing. Overrides FeedsExBase::getErrors
FeedsExXml::getInnerXml protected function Returns the inner XML of a DOM node.
FeedsExXml::getRaw protected function Returns the raw XML of a DOM node. 1
FeedsExXml::getTidyConfig protected function Returns the options for phptidy. 2
FeedsExXml::prepareDocument protected function Prepares the DOM document. 2
FeedsExXml::setUp protected function Allows subclasses to prepare for parsing. Overrides FeedsExBase::setUp 1
FeedsExXml::startErrorHandling protected function Starts internal error handling. Overrides FeedsExBase::startErrorHandling
FeedsExXml::stopErrorHandling protected function Stops internal error handling. Overrides FeedsExBase::stopErrorHandling