You are here

class FeedsOAIHTTPFetcher in Feeds OAI-PMH Fetcher and Parser 6

Same name and namespace in other branches
  1. 7 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 FeedsOAIHTTPBatch($source_config['source'], $from_timestamp, $until_timestamp, $resumption_token, $set);
  }

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

    // TODO: is this needed?
    return array(
      '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) {
    ctools_include('dependent');
    $form = parent::sourceForm($source_config);

    // 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'],
        )));
      }
    }
    if (isset($result)) {

      // 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 AHAH event handler.
    $form['source']['#ahah'] = array(
      'path' => 'feeds_oai_pmh/set_ahah',
      'wrapper' => 'ahah-element',
      // ID of div element to update.
      'method' => 'replace',
      'effect' => 'fade',
      'event' => 'change',
    );
    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']);
      }
      else {
        $sets_options = feeds_oai_pmh_sets_options($result['repository']['sets']);
      }
    }
    $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(),
      '#ahah' => array(
        'path' => 'feeds_oai_pmh/set_ahah',
        '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'),
      // Add Ctools processing to make this fieldset dependant on above checkbox
      '#process' => array(
        'ctools_dependent_process',
      ),
      // Form element IDs are edit-feeds-[feeds-object]-[form-element-id]
      '#dependency' => array(
        'edit-feeds-FeedsOAIHTTPFetcher-use-dates' => array(
          1,
        ),
      ),
      // Surrounding fieldsets with a div is necessary for Ctools to work
      '#prefix' => '<div id="edit-feeds-FeedsOAIHTTPFetcher-dates-wrapper">',
      '#suffix' => '</div></div>',
      // This is also needed by Ctools to make fieldsets work
      '#input' => TRUE,
    );
    if (isset($source_config['earliest_timestamp'])) {
      $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.'),
    );
    return $form;
  }

  /**
   * Override parent::sourceFormValidate().
   */
  public function sourceFormValidate(&$values) {

    // 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
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::configFormValidate public function Validation 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().
FeedsFetcher::menuItem public function Menu item definition for fetchers of this class. Note how the path component in the item definition matches the return value of FeedsFetcher::path();
FeedsFetcher::path public function Construct a path for a concrete fetcher/source combination. The result of this method matches up with the general path definition in FeedsFetcher::menuItem(). For example usage look at FeedsHTTPFetcher.
FeedsHTTPFetcher::clear public function Clear caches. Overrides FeedsFetcher::clear
FeedsHTTPFetcher::importPeriod public function Implement FeedsFetcher::importPeriod(). Overrides FeedsFetcher::importPeriod
FeedsHTTPFetcher::request public function Implements FeedsFetcher::request(). Overrides FeedsFetcher::request
FeedsHTTPFetcher::sourceDelete public function Override sourceDelete() - unsubscribe from hub. Overrides FeedsPlugin::sourceDelete
FeedsHTTPFetcher::sourceSave public function Override sourceSave() - subscribe to hub. Overrides FeedsPlugin::sourceSave
FeedsHTTPFetcher::subscribe public function Implement FeedsFetcher::subscribe() - subscribe to hub. Overrides FeedsFetcher::subscribe
FeedsHTTPFetcher::subscriber protected function Convenience method for instantiating a subscriber object.
FeedsHTTPFetcher::unsubscribe public function Implement FeedsFetcher::unsubscribe() - unsubscribe from hub. Overrides FeedsFetcher::unsubscribe
FeedsOAIHTTPFetcher::configDefaults public function Declare defaults. Overrides FeedsHTTPFetcher::configDefaults
FeedsOAIHTTPFetcher::configForm public function Add form options. Overrides FeedsHTTPFetcher::configForm
FeedsOAIHTTPFetcher::dateFieldToTimestamp protected function
FeedsOAIHTTPFetcher::fetch public function Fetch content from feed. Overrides FeedsHTTPFetcher::fetch
FeedsOAIHTTPFetcher::sourceForm public function Expose source form. Overrides FeedsHTTPFetcher::sourceForm
FeedsOAIHTTPFetcher::sourceFormValidate public function Override parent::sourceFormValidate(). Overrides FeedsHTTPFetcher::sourceFormValidate
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::sourceDefaults public function Implementation of FeedsSourceInterface::sourceDefaults(). Overrides FeedsSourceInterface::sourceDefaults 1
FeedsPlugin::__construct protected function Constructor. Overrides FeedsConfigurable::__construct