You are here

feeds_oai_pmh.module in Feeds OAI-PMH Fetcher and Parser 6

Same filename and directory in other branches
  1. 7 feeds_oai_pmh.module

File

feeds_oai_pmh.module
View source
<?php

/**
 * Implementation of hook_feed_plugins().
 */
function feeds_oai_pmh_feeds_plugins() {
  $info = array();
  $info['FeedsOAIHTTPFetcher'] = array(
    'name' => 'HTTP OAI-PMH Fetcher',
    'description' => 'Download content from an OAI-PMH repository.',
    'help' => 'Queries an OAI-PMH endpoint of an online repository for data, starting after the last-imported record.',
    'handler' => array(
      'parent' => 'FeedsHTTPFetcher',
      // This is the key name, not the class name.
      'class' => 'FeedsOAIHTTPFetcher',
      'file' => 'FeedsOAIHTTPFetcher.inc',
      'path' => drupal_get_path('module', 'feeds_oai_pmh'),
    ),
  );
  $info['FeedsOAIParser'] = array(
    'name' => 'OAI parser',
    'description' => 'Parse Dublin Core (oai_dc) metadata from OAI-PMH repositories.',
    'handler' => array(
      'parent' => 'FeedsParser',
      'class' => 'FeedsOAIParser',
      'file' => 'FeedsOAIParser.inc',
      'path' => drupal_get_path('module', 'feeds_oai_pmh'),
    ),
  );
  return $info;
}

/**
 * Return a "drupalized" hierarchic options form element for the sets in a repository.
 */
function feeds_oai_pmh_sets_options($sets) {
  $options = array(
    '*' => t('[all sets]'),
  );
  if (is_array($sets)) {
    ksort($sets);
    $last_depth = 0;
    foreach ($sets as $set_spec => $set_data) {
      $levels = explode(':', $set_spec);
      $label = str_repeat('--', sizeof($levels) - 1) . $set_data['name'];
      $options[$set_spec] = "[{$set_spec}] {$label}";
    }
  }
  return $options;
}

/**
 * Implementation of hook_menu().
 */
function feeds_oai_pmh_menu() {
  $items = array();
  $items['feeds_oai_pmh/set_ahah'] = array(
    'title' => 'Javascript Choice Form',
    'page callback' => 'feeds_oai_pmh_set_ahah',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Callback function for AHAH setSpec element in form.
 */
function feeds_oai_pmh_set_ahah() {
  if ($form = form_get_cache($_POST['form_build_id'], $form_state)) {

    // Validate the repository.
    $oai_source_url = $_POST['feeds']['FeedsOAIHTTPFetcher']['source'];
    require_once drupal_get_path('module', 'feeds_oai_pmh') . '/feeds_oai_pmh.inc';
    $result = feeds_oai_pmh_identify($oai_source_url);
    $set = '*';
    if ($result['status'] == 0) {

      // OAI-PMH Identify verb returned OK.
      // Change the form's set element.
      $form['feeds']['FeedsOAIHTTPFetcher']['set']['#options'] = feeds_oai_pmh_sets_options($result['repository']['sets']);
      $set = $_POST['feeds']['FeedsOAIHTTPFetcher']['set'];
      if ($set) {
        $form['feeds']['FeedsOAIHTTPFetcher']['set']['#default_value'] = $set;
      }
      if (isset($result['repository']['earliest_timestamp']) && $result['repository']['earliest_timestamp'] > 0) {
        $date = format_date($result['repository']['earliest_timestamp'], 'custom', 'M d, Y');
        $form['feeds']['FeedsOAIHTTPFetcher']['dates']['#description'] = t('Note: earliest record reported by repository is @date', array(
          '@date' => $date,
        ));
      }
    }
    else {

      // Empty sets.
      $form['feeds']['FeedsOAIHTTPFetcher']['set']['#options'] = feeds_oai_pmh_sets_options(array());
      $form['feeds']['FeedsOAIHTTPFetcher']['dates']['#description'] = '';
      drupal_set_message($result['output'], 'error');
    }

    // Cache the form.
    form_set_cache($_POST['form_build_id'], $form, $form_state);

    // Build and render the new select element, then return it in JSON format.
    $form_state = array();
    $form['#post'] = array();
    $form = form_builder($form['form_id']['#value'], $form, $form_state);

    // Update the status message
    $msg = feeds_oai_pmh_current_status_msg($oai_source_url, $set);
    if ($msg) {
      $msg = '<div class="messages status">' . $msg . '</div>';
    }
    $output = theme('status_messages') . drupal_render($form['feeds']['FeedsOAIHTTPFetcher']['set']) . $msg . drupal_render($form['feeds']['FeedsOAIHTTPFetcher']['use_dates']) . drupal_render($form['feeds']['FeedsOAIHTTPFetcher']['dates']);
    drupal_json(array(
      'status' => FALSE,
      'data' => $output,
    ));
  }
  exit;
}

/**
 * Generate a status message for the current status of this repository/set.
 */
function feeds_oai_pmh_current_status_msg($source, $set) {

  // If applicable, add status line indicating current status.
  $resumption_token = variable_get('feeds_oai:resumptionToken:' . $set . ':' . $source, NULL);
  $from_timestamp = variable_get('feeds_oai:from:' . $set . ':' . $source, NULL);
  $msg = '';
  if ($resumption_token) {
    $msg = t('The next import attempt from this repository/set will continue importing records, since a resumptionToken was returned by the repository during the last attempt.');
  }
  elseif ($from_timestamp > 0) {
    $msg = t('The next import attempt from this repository/set will only import any records created after @date, since all available records have been fetched by the last import attempt(s).', array(
      '@date' => format_date($from_timestamp, 'medium'),
    ));
  }
  return $msg;
}

Functions

Namesort descending Description
feeds_oai_pmh_current_status_msg Generate a status message for the current status of this repository/set.
feeds_oai_pmh_feeds_plugins Implementation of hook_feed_plugins().
feeds_oai_pmh_menu Implementation of hook_menu().
feeds_oai_pmh_sets_options Return a "drupalized" hierarchic options form element for the sets in a repository.
feeds_oai_pmh_set_ahah Callback function for AHAH setSpec element in form.