You are here

class FeedsOAIHTTPFetcher in Feeds OAI-PMH Fetcher and Parser 7

Same name and namespace in other branches
  1. 6 FeedsOAIHTTPFetcher.inc \FeedsOAIHTTPFetcher

Fetcher class for OAI-PMH repository webservices.

Hierarchy

Expanded class hierarchy of FeedsOAIHTTPFetcher

1 string reference to 'FeedsOAIHTTPFetcher'
feeds_oai_pmh_feeds_plugins in ./feeds_oai_pmh.module
Implementation of hook_feed_plugins().

File

./FeedsOAIHTTPFetcher.inc, line 125

View source
class FeedsOAIHTTPFetcher extends FeedsHTTPFetcher {

  /**
   * Fetch content from feed.
   */
  public function fetch(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    $from_timestamp = FALSE;
    $until_timestamp = FALSE;

    // Fetching rules:
    // Whenever there is a resumption token, use that.
    // Else
    //   if limit by date == yes
    //     issue those
    //   else
    //     start from last known record creation date (from variable)
    $resumption_token = variable_get('feeds_oai:resumptionToken:' . $source_config['set'] . ':' . $source_config['source'], '');
    if (!$resumption_token) {
      if ($source_config['use_dates']) {
        $from_timestamp = $this
          ->dateFieldToTimestamp($source_config['dates']['from']);
        $until_timestamp = $this
          ->dateFieldToTimestamp($source_config['dates']['to']);
      }
      else {
        $from_timestamp = (int) variable_get('feeds_oai:from:' . $source_config['set'] . ':' . $source_config['source'], FALSE);
        if ($from_timestamp > 0) {
          $from_timestamp = $from_timestamp + 1;
        }
      }
    }

    // The setSpec to harvest from.
    $set = $source_config['set'];
    return new FeedsOAIHTTPFetcherResult($source_config['source'], $from_timestamp, $until_timestamp, $resumption_token, $set);
  }

  /**
   * Declare defaults.
   */
  public function configDefaults() {

    // TODO: is this needed?
    return array(
      'auto_detect_feeds' => FALSE,
      'use_pubsubhubbub' => FALSE,
      'last_fetched_timestamp' => '',
      'earliest_timestamp' => '',
      'use_dates' => FALSE,
      'to' => array(),
      'from' => array(),
    );
  }

  /**
   * Add form options.
   */
  public function configForm(&$form_state) {
    $form = array();

    // TODO: Specify metadata format here?
    return $form;
  }

  /**
   * Expose source form.
   */
  public function sourceForm($source_config) {
    $form = parent::sourceForm($source_config);
    $error = FALSE;

    // If earliest_timestamp is not set, and source is, then get info from
    // repository to populate settings.
    if (isset($source_config['source']) && !empty($source_config['source'])) {
      require_once drupal_get_path('module', 'feeds_oai_pmh') . '/feeds_oai_pmh.inc';
      $result = feeds_oai_pmh_identify($source_config['source']);

      #dpm($result);
      if ($result['status'] == 0) {
        $source_config = array_merge($source_config, $result['repository']);
      }
      else {
        drupal_set_message(t('There was a problem fetching repository information: !list', array(
          '!list' => $result['output'],
        )));
        $error = TRUE;
      }
    }
    if (isset($result) && $error == FALSE) {

      // Build options array for sets available in repository.
      $sets_options = feeds_oai_pmh_sets_options($result['repository']['sets']);
    }

    // Override the default "source" element provided by Feeds.
    // Clearer label and description.
    $form['source']['#title'] = t('URL of OAI-PMH endpoint');
    $form['source']['#description'] = t('You can use services like http://www.opendoar.org/ to get a list of repository OAI-PMH endpoints.');

    // Add AJAX event handler.
    $form['source']['#ajax'] = array(
      'callback' => 'feeds_oai_pmh_ajax_callback',
      'wrapper' => 'ahah-element',
      // ID of div element to update.
      'method' => 'replace',
      'effect' => 'fade',
      'event' => 'change',
    );

    // A set wrapper to handle replacement by AJAX callback
    $form['source']['#prefix'] = '<div class="clear-block" id="ahah-element">';
    if ($form['source']['#default_value']) {
      require_once drupal_get_path('module', 'feeds_oai_pmh') . '/feeds_oai_pmh.inc';
      $result = feeds_oai_pmh_identify($form['source']['#default_value']);
      if ($result['status'] == 0) {
        $source_config = array_merge($source_config, $result['repository']);
      }
      elseif (isset($result['repository'])) {
        $sets_options = feeds_oai_pmh_sets_options($result['repository']['sets']);
      }
      else {
        $sets_options = feeds_oai_pmh_sets_options(array());
      }
    }
    $form['set'] = array(
      '#type' => 'select',
      '#title' => t('Set to fetch'),
      '#default_value' => isset($source_config['set']) ? $source_config['set'] : NULL,
      '#options' => isset($sets_options) ? $sets_options : array(),
      '#suffix' => '',
      '#ajax' => array(
        'callback' => 'feeds_oai_pmh_ajax_callback',
        'wrapper' => 'ahah-element',
        // ID of div element to update.
        'method' => 'replace',
        'effect' => 'fade',
        'event' => 'change',
      ),
    );
    if (isset($source_config['source']) && isset($source_config['set'])) {
      $msg = feeds_oai_pmh_current_status_msg($source_config['source'], $source_config['set']);
      if ($msg) {
        $form['status'] = array(
          '#value' => '<div class="messages status">' . $msg . '</div>',
        );
      }
    }
    $form['use_dates'] = array(
      '#type' => 'checkbox',
      '#title' => 'Limit fetch by record creation date',
      '#default_value' => isset($source_config['use_dates']) ? $source_config['use_dates'] : NULL,
    );
    $form['dates'] = array(
      '#type' => 'fieldset',
      '#title' => t('Record creation dates to fetch'),
      // Form element IDs are edit-feeds-[feeds-object]-[form-element-id]
      '#states' => array(
        'visible' => array(
          '#edit-feeds-feedsoaihttpfetcher-use-dates' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    if (isset($source_config['earliest_timestamp']) && $source_config['earliest_timestamp'] > 0) {
      $date = format_date($source_config['earliest_timestamp'], 'custom', 'M d, Y');
      $form['dates']['#description'] = t('Note: earliest record reported by repository is @date', array(
        '@date' => $date,
      ));
    }
    $form['dates']['from'] = array(
      '#type' => 'date',
      '#title' => t('Starting date'),
      '#default_value' => isset($source_config['dates']['from']) ? $source_config['dates']['from'] : NULL,
    );
    $form['dates']['to'] = array(
      '#type' => 'date',
      '#title' => t('Ending date'),
      '#default_value' => isset($source_config['dates']['to']) ? $source_config['dates']['to'] : NULL,
    );
    $form['restart'] = array(
      '#type' => 'checkbox',
      '#title' => t('Reset import for this repository/set to above settings'),
      '#description' => t('This forces any imports that are currently underway
          for the chosen repository/set to start over from the beginning.
          Normally, all imports that have already begun will only try to fetch
          new items until this option is checked, or if the "Delete items"
          option is used.'),
      '#suffix' => '</div>',
    );
    return $form;
  }

  /**
   * Override parent::sourceFormValidate().
   */
  public function sourceFormValidate(&$values) {
    require_once drupal_get_path('module', 'feeds_oai_pmh') . '/feeds_oai_pmh.inc';
    $result = feeds_oai_pmh_identify($values['source']);
    if ($result['status'] != 0) {
      return;
    }

    // TODO: Check that start date <= repository's reported earliest_timestamp
    // Check that start date <= end date
    if ($values['use_dates']) {
      $from_timestamp = $this
        ->dateFieldToTimestamp($values['dates']['from']);
      $until_timestamp = $this
        ->dateFieldToTimestamp($values['dates']['to']);
      if ($from_timestamp > $until_timestamp) {
        form_set_error('feeds][source', t('The ending date must be later than the starting date'));
      }
    }

    // Check for restart option.
    if ($values['restart']) {
      variable_del('feeds_oai:resumptionToken:' . $values['set'] . ':' . $values['source']);
      variable_del('feeds_oai:from:' . $values['set'] . ':' . $values['source']);
      unset($values['restart']);
      drupal_set_message(t('Import for this repository/set has been reset, ignoring any previous imports.'));
    }
  }
  protected function dateFieldToTimestamp($field_value) {
    return mktime(NULL, NULL, NULL, $field_value['month'], $field_value['day'], $field_value['year']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsOAIHTTPFetcher::configDefaults public function Declare defaults.
FeedsOAIHTTPFetcher::configForm public function Add form options.
FeedsOAIHTTPFetcher::dateFieldToTimestamp protected function
FeedsOAIHTTPFetcher::fetch public function Fetch content from feed.
FeedsOAIHTTPFetcher::sourceForm public function Expose source form.
FeedsOAIHTTPFetcher::sourceFormValidate public function Override parent::sourceFormValidate().