You are here

class FeedsOAIHTTPFetcherResult in Feeds OAI-PMH Fetcher and Parser 7

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

Hierarchy

Expanded class hierarchy of FeedsOAIHTTPFetcherResult

File

./FeedsOAIHTTPFetcher.inc, line 8

View source
class FeedsOAIHTTPFetcherResult extends FeedsFetcherResult {
  protected $oai_endpoint_url;
  protected $from_timestamp;
  protected $until_timestamp;
  public $repository;

  /**
   * Constructor.
   */
  public function __construct($oai_endpoint_url, $from_timestamp, $until_timestamp, $resumption_token, $set) {
    $this->oai_endpoint_url = $oai_endpoint_url;
    $this->from_timestamp = $from_timestamp;
    $this->until_timestamp = $until_timestamp;
    $this->set = $set;
    $this->resumption_token = $resumption_token;

    // Run identify request to fill this repository's information.
    require_once drupal_get_path('module', 'feeds_oai_pmh') . '/feeds_oai_pmh.inc';
    $repository = feeds_oai_pmh_identify($oai_endpoint_url);
    $this->repository = $repository['repository'];
    parent::__construct('');
  }

  /**
   * Implementation of FeedsResult::getRaw();
   */
  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;
  }
  protected function setResumptionToken($resumption_token) {
    $this->resumption_token = $resumption_token;
    variable_set('feeds_oai:resumptionToken:' . $this->set . ':' . $this->oai_endpoint_url, $resumption_token);
  }
  protected function setLastDate($timestamp) {
    variable_set('feeds_oai:from:' . $this->set . ':' . $this->oai_endpoint_url, $timestamp);
  }
  protected function formatDate($timestamp) {
    $granularity = $this->repository['granularity'];
    if ('seconds' == $granularity) {
      $date_format = 'Y-m-d\\TH:m:s\\Z';
    }
    elseif ('days' == $granularity) {
      $date_format = 'Y-m-d';
    }
    return date($date_format, $timestamp);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsFetcherResult::$file_path protected property The path to a file where the raw data is stored.
FeedsFetcherResult::$raw protected property The raw fetched data.
FeedsFetcherResult::checkFile protected function Checks that a file exists and is readable.
FeedsFetcherResult::constructFilePath public function Constructs file name for saving the raw data.
FeedsFetcherResult::fileExists public function Returns if the file to parse exists.
FeedsFetcherResult::getFeedsInProgressDir public function Returns directory for storing files that are in progress of import.
FeedsFetcherResult::getFileContents public function Returns the contents of a file, if it exists.
FeedsFetcherResult::getFilePath public function Get a path to a temporary file containing the resource provided by the fetcher. 1
FeedsFetcherResult::rawExists public function Returns if raw data exists.
FeedsFetcherResult::sanitizeFile public function Sanitize the file in place.
FeedsFetcherResult::sanitizeRaw public function Sanitize the raw content string.
FeedsFetcherResult::sanitizeRawOptimized public function Sanitize the raw content string.
FeedsFetcherResult::saveRawToFile public function Saves the raw fetcher result to a file.
FeedsFetcherResult::__sleep public function Prevent saving the raw result when serializing object.
FeedsOAIHTTPFetcherResult::$from_timestamp protected property
FeedsOAIHTTPFetcherResult::$oai_endpoint_url protected property
FeedsOAIHTTPFetcherResult::$repository public property
FeedsOAIHTTPFetcherResult::$until_timestamp protected property
FeedsOAIHTTPFetcherResult::formatDate protected function
FeedsOAIHTTPFetcherResult::getRaw public function Implementation of FeedsResult::getRaw(); Overrides FeedsFetcherResult::getRaw
FeedsOAIHTTPFetcherResult::setLastDate protected function
FeedsOAIHTTPFetcherResult::setResumptionToken protected function
FeedsOAIHTTPFetcherResult::__construct public function Constructor. Overrides FeedsFetcherResult::__construct