You are here

public function FeedsOAIHTTPFetcherResult::getRaw in Feeds OAI-PMH Fetcher and Parser 7

Implementation of FeedsResult::getRaw();

Overrides FeedsFetcherResult::getRaw

File

./FeedsOAIHTTPFetcher.inc, line 35

Class

FeedsOAIHTTPFetcherResult
Definition of the import batch object created on the fetching stage by FeedsOAIHTTPFetcher.

Code

public function getRaw() {

  // TODO: Move the URL building and data fetching to feeds_oai_pmh.inc
  // Build the request URL
  $url = $this->oai_endpoint_url;
  $url .= '?verb=ListRecords';
  if ($this->resumption_token) {
    $url .= "&resumptionToken=" . rawurlencode($this->resumption_token);
  }
  else {

    // When a resumptionToken is issued, there can't be any other arguments
    // in the request.
    $url .= '&metadataPrefix=oai_dc';
    if ($this->from_timestamp > 0) {
      $url .= '&from=' . rawurlencode($this
        ->formatDate($this->from_timestamp));
    }
    if ($this->until_timestamp > 0) {
      $url .= '&until=' . rawurlencode($this
        ->formatDate($this->until_timestamp));
    }
    if ($this->set && $this->set != '*') {
      $url .= '&set=' . rawurlencode($this->set);
    }
  }

  #dsm("URL for OAI request: $url");

  // Fetch the feed's contents
  $result = drupal_http_request($url);
  if ($result->code == 200) {
    $resumption_token = '';

    // TODO: Use simpleXML instead of regexp
    // Try to get resumptionToken. Example:
    // <resumptionToken completeListSize="478" cursor="0">0/300/478/oai_dc/eserev/null/null</resumptionToken>
    $ok = preg_match_all('/<resumptionToken.*?>([^<]+)<\\/resumptionToken>/s', $result->data, $matches);
    if ($ok) {
      $resumption_token = array_pop($matches[1]);

      #dsm("Resumption token: $resumption_token");
      $this
        ->setLastDate(0);
    }
    else {

      // No resumption token in response.
      if ($this->until_timestamp > 0) {

        // Since specific dates were requested, set the last date to 0.
        $this
          ->setLastDate(0);
      }
      else {

        // Store current system timestamp so next request limits items returned.
        $resumption_token = "";
        $this
          ->setLastDate(time());
      }
    }
    $this
      ->setResumptionToken($resumption_token);
  }
  else {

    // OAI fetch failed
    $msg = 'OAI-PMH request failed: @error';
    $args = array(
      '@error' => $result->error,
    );
    drupal_set_message(t($msg, $args), 'error');
    watchdog('feeds_oai_pmh', $msg, $args, WATCHDOG_ERROR, $url);
    return FALSE;
  }

  // Return the feed's contents
  return $result->data;
}