You are here

function feeds_oai_pmh_identify in Feeds OAI-PMH Fetcher and Parser 6

Same name and namespace in other branches
  1. 7 feeds_oai_pmh.inc \feeds_oai_pmh_identify()

Returns an array of information returned by the OAI-PMH Identify verb.

4 calls to feeds_oai_pmh_identify()
FeedsOAIHTTPBatch::__construct in ./FeedsOAIHTTPFetcher.inc
Constructor.
FeedsOAIHTTPFetcher::sourceForm in ./FeedsOAIHTTPFetcher.inc
Expose source form.
FeedsOAIParser::parse in ./FeedsOAIParser.inc
Implementation of FeedsParser::parse().
feeds_oai_pmh_set_ahah in ./feeds_oai_pmh.module
Callback function for AHAH setSpec element in form.

File

./feeds_oai_pmh.inc, line 12

Code

function feeds_oai_pmh_identify($baseurl) {
  static $cache = array();
  if (isset($cache[$baseurl])) {
    return $cache[$baseurl];
  }

  // Use Drupal cache
  $cid = 'feeds_oai_pmh:' . str_replace('http://', '', $baseurl);
  if ($cached = cache_get($cid)) {

    // If cached data is not yet stale, return it.
    if ($cached->expire > time()) {
      return $cached->data;
    }
  }
  $output = array();
  $url = "{$baseurl}?verb=Identify";
  $repository = array(
    'deleted_record' => '',
    'compression' => FALSE,
    'compression_gzip' => FALSE,
    'compression_deflate' => FALSE,
    'earliest_timestamp' => '',
    'sets' => array(),
  );
  $result = drupal_http_request($url);
  if ($result->code != 200) {
    $message = 'OAI repository %repo is not avaliable, please check the base URL %url is correct.';
    $args = array(
      '%repo' => $baseurl,
      '%url' => $baseurl,
    );
    watchdog('feeds_oai_pmh', $message, $args, WATCHDOG_ERROR);
    return array(
      'output' => t($message, $args),
      'status' => 1,
    );
  }

  // Returns FALSE on error
  $xml = @simplexml_load_string($result->data);
  if (!$xml) {
    $message = t('OAI repository %repo returns invalid XML upon identify.', array(
      '%repo' => $baseurl,
    ));
    watchdog('feeds_oai_pmh', $message, '', WATCHDOG_ERROR);
    return array(
      'output' => $message,
      'status' => 1,
    );
  }
  $ident = $xml->Identify;

  // Things which must come back, or die
  // Protocool Version
  if ($ident->protocolVersion != '2.0') {
    $message = t('OAI repository %repo: Incorrect Identify Response -- Unsupported Protcool Version "@version"', array(
      '%repo' => $baseurl,
      '@version' => $ident->protocolVersion,
    ));
    watchdog('feeds_oai_pmh', $message, '', WATCHDOG_ERROR);
    return array(
      'output' => $message,
      'status' => 1,
    );
  }
  else {
    $repository["protocol_version"] = (string) $ident->protocolVersion;
  }

  // DeleteRecord
  if (!isset($ident->deletedRecord)) {
    $message = t('OAI repository %repo: Incorrect Identify Response -- No deleteRecord', array(
      '%repo' => $baseurl,
    ));
    watchdog('feeds_oai_pmh', $message, '', WATCHDOG_ERROR);
    return array(
      'output' => $message,
      'status' => 1,
    );
  }
  else {
    $repository['deleted_record'] = (string) $ident->deletedRecord;
  }

  // earliest Datestamp
  if (!isset($ident->earliestDatestamp)) {
    $message = t('OAI repository %repo: Incorrect Identify Response -- No earliest Datestamp', array(
      '%repo' => $baseurl,
    ));
    watchdog('feeds_oai_pmh', $message, '', WATCHDOG_ERROR);
    return array(
      'output' => $message,
      'status' => 1,
    );
  }
  else {

    #$repository['earliest_datestamp'] = (string)$ident->earliestDatestamp;
    $repository['earliest_timestamp'] = strtotime((string) $ident->earliestDatestamp);
  }

  // Granularity
  if (!isset($ident->granularity)) {
    $message = t('OAI repository %repo: Incorrect Identify Response -- No Granularity', array(
      '%repo' => $baseurl,
    ));
    watchdog('feeds_oai_pmh', $message, '', WATCHDOG_ERROR);
    return array(
      'output' => $message,
      'status' => 1,
    );
  }
  else {

    // Granularty is only in days
    // Magic number from strlen(YYYY-MM-DD)
    if (strlen($ident->granularity) == 10) {
      $repository['granularity'] = 'days';
    }
    elseif (strlen($ident->granularity) == 20) {
      $repository['granularity'] = 'seconds';
    }
    else {
      $message = t('OAI repository %repo: Incorrect Identify Response -- Invalid granularity', array(
        '%repo' => $baseurl,
      ));
      watchdog('feeds_oai_pmh', $message, '', WATCHDOG_ERROR);
      return array(
        'output' => $message,
        'status' => 1,
      );
    }
  }

  // Optional things, which are nice to have
  if (isset($ident->compression)) {

    // According to HTTP 1.1 RFC 2616 there is also the Lempel-Ziv-Welch
    // compression, which in theory could be supported. However, PHP doesn't
    // seem to play nice with it, and I haven't seen a repo with it. It is also
    // 14 years old.
    $repository['compression'] = TRUE;
    foreach ($ident->compression as $encoding) {
      if ($encoding == 'gzip') {
        $repository['compression_gzip'] = TRUE;
      }
      elseif ($encoding == 'deflate') {
        $repository['compression_deflate'] = TRUE;
      }
    }
  }

  // Get and assign sets information
  if ($sets = feeds_oai_pmh_get_sets($baseurl)) {
    $repository['sets'] = $sets;
  }
  $return = array(
    'output' => $output,
    'status' => 0,
    'repository' => $repository,
  );

  // Store in static cache
  $cache[$baseurl] = $return;

  // Cache in the DB for 60 minutes
  cache_set($cid, $return, 'cache', time() + 3600 * 60);
  return $return;
}