You are here

FeedsQueryPathParser.inc in Feeds QueryPath Parser 6

Same filename and directory in other branches
  1. 7 FeedsQueryPathParser.inc

Provides the class for FeedsQueryPathParser.

File

FeedsQueryPathParser.inc
View source
<?php

/**
 * @file
 *
 * Provides the class for FeedsQueryPathParser.
 */
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;
  }

}

/**
 * Implementation of hook_form_feeds_ui_mapping_form_alter().
 *
 * This is an interesting bit of work. Each source name has to be unique,
 * but we have no idea how many to create with getMappingSources() because we
 * don't know how many targets there are going to be.
 *
 * Solution is to keep track in the form how many have been added.
 */
function feeds_querypath_parser_form_feeds_ui_mapping_form_alter(&$form, &$form_state) {
  $newest_querypath_mapping = array();
  foreach ($form['#mappings'] as $mapping) {
    if (strpos($mapping['source'], 'querypathparser:') === 0) {
      $newest_querypath_mapping = $mapping;
    }
  }
  if (!empty($newest_querypath_mapping)) {
    list($a, $count) = explode(':', $newest_querypath_mapping['source']);
    $default_source = $a . ':' . '0';
    $label = $form['source']['#options'][$default_source];
    unset($form['source']['#options'][$default_source]);
    $form['source']['#options'][$a . ':' . ++$count] = $label;
  }
}

Functions

Namesort descending Description
feeds_querypath_parser_form_feeds_ui_mapping_form_alter Implementation of hook_form_feeds_ui_mapping_form_alter().

Classes

Namesort descending Description
FeedsQueryPathParser @file