You are here

class FeedsQueryPathParser in Feeds QueryPath Parser 6

Same name and namespace in other branches
  1. 7 FeedsQueryPathParser.inc \FeedsQueryPathParser

@file

Provides the class for FeedsQueryPathParser.

Hierarchy

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

Namesort descending Modifiers Type Description Overrides
FeedsConfigurable::$config protected property
FeedsConfigurable::$disabled protected property CTools export enabled status of this object.
FeedsConfigurable::$export_type protected property
FeedsConfigurable::$id protected property
FeedsConfigurable::addConfig public function Similar to setConfig but adds to existing configuration. 1
FeedsConfigurable::configFormSubmit public function Submission handler for configForm(). 3
FeedsConfigurable::copy public function Copy a configuration. 1
FeedsConfigurable::existing 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::getConfig public function Implementation of getConfig(). 1
FeedsConfigurable::instance public static function Instantiate a FeedsConfigurable object. 1
FeedsConfigurable::setConfig public function Set configuration. 1
FeedsConfigurable::__get public function Override magic method __get(). Make sure that $this->config goes through getConfig()
FeedsConfigurable::__isset public function Override magic method __isset(). This is needed due to overriding __get().
FeedsParser::clear public function Clear all caches for results for given source.
FeedsParser::getSourceElement 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::hasSourceConfig public function Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface::hasSourceConfig
FeedsPlugin::loadMappers protected static function Loads on-behalf implementations from mappers/ directory.
FeedsPlugin::save 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::save
FeedsPlugin::sourceDelete public function A source is being deleted. Overrides FeedsSourceInterface::sourceDelete 1
FeedsPlugin::sourceSave public function A source is being saved. Overrides FeedsSourceInterface::sourceSave 1
FeedsPlugin::__construct protected function Constructor. Overrides FeedsConfigurable::__construct
FeedsQueryPathParser::$debug_options protected property
FeedsQueryPathParser::$debug_switch protected property
FeedsQueryPathParser::$rawXML protected property
FeedsQueryPathParser::$source_config protected property
FeedsQueryPathParser::configDefaults public function Define config defaults. Overrides FeedsConfigurable::configDefaults
FeedsQueryPathParser::configForm public function Override parent::configForm(). Overrides FeedsConfigurable::configForm
FeedsQueryPathParser::configFormValidate public function Validation handler for configForm(). Overrides FeedsConfigurable::configFormValidate
FeedsQueryPathParser::debug protected function
FeedsQueryPathParser::getMappingSources public function Override parent::getMappingSources(). Overrides FeedsParser::getMappingSources
FeedsQueryPathParser::parse public function Implementation of FeedsParser::parse(). Overrides FeedsParser::parse
FeedsQueryPathParser::parseSourceElement protected function
FeedsQueryPathParser::sourceDefaults public function Define source defaults. Overrides FeedsPlugin::sourceDefaults
FeedsQueryPathParser::sourceForm public function Source form. Overrides FeedsPlugin::sourceForm
FeedsQueryPathParser::sourceFormValidate public function Override parent::sourceFormValidate(). Overrides FeedsPlugin::sourceFormValidate