oai_endpoint_url = $oai_endpoint_url; $this->from_timestamp = $from_timestamp; $this->until_timestamp = $until_timestamp; $this->set = $set; $this->resumption_token = $resumption_token; $this->metadata_prefix = $metadata_prefix; // 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'; $url .= '&metadataPrefix=' . $this->metadata_prefix; 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"); //watchdog("FeedsOAI","URL for OAI request: $url", WATCHDOG_DEBUG); // Fetch the feed's contents $result = drupal_http_request($url); //watchdog("FeedsOAI","$result", WATCHDOG_DEBUG); //exit(krumo($result)); if ($result->code == 200) { //watchdog_log("FeedsOAI","succès", WATCHDOG_DEBUG); $resumption_token = ''; // TODO: Use simpleXML instead of regexp // Try to get resumptionToken. Example: // 0/300/478/oai_dc/eserev/null/null $ok = preg_match_all('/([^<]+)<\/resumptionToken>/s', $result->data, $matches); if ($ok) { $resumption_token = array_pop($matches[1]); #dsm("Resumption token: $resumption_token"); $this->setLastDate(0); } elseif (preg_match('/badResumptionToken/', $result->data)) { $msg = 'OAI-PMH request failed: La variable ResumptionToken est invalide (probablement expire).'; drupal_set_message($msg, 'error'); watchdog('feeds_oai_pmh', $msg, array(), WATCHDOG_ERROR, $url); return FALSE; } 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 { //watchdog_log("FeedsOAI", "echec", WATCHDOG_DEBUG); // 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); } } /** * Fetcher class for OAI-PMH repository webservices. */ 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 FeedsOAIHTTPFetcherResult( $source_config['source'], $from_timestamp, $until_timestamp, $resumption_token, $set, $this->config['metadata_prefix'] ); } /** * Declare defaults. */ public function configDefaults() { // TODO: is this needed? return array( 'auto_detect_feeds' => FALSE, 'use_pubsubhubbub' => FALSE, 'last_fetched_timestamp' => '', 'earliest_timestamp' => '', 'use_dates' => FALSE, 'to' => array(), 'from' => array(), 'metadata_prefix' => 'oai_dc', ); } /** * Override parent::configForm(). * * Admin can specify a metadata format to request from the repository. */ public function configForm(&$form_state) { $form = array(); $form['help'] = array( '#value' => t('Repositories might offer different metadata formats other than the requisite OAI_DC; records might only be available in some formats. To find out the metadata formats available in your OAI repository, issue the ListMetadataFormats verb to the OAI repository.'), ); $form['metadata_prefix'] = array( '#type' => 'textfield', '#title' => t('Metadata prefix'), '#default_value' => $this->config['metadata_prefix'], '#description' => t('Specify OAI_DC if you are using the OAI_DC Parser in your feed importer.'), ); return $form; } /** * Expose source form. */ public function sourceForm($source_config) { $form = parent::sourceForm($source_config); $error = FALSE; // 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']))); $error = TRUE; } } if (isset($result) && $error == FALSE) { // 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 AJAX event handler. $form['source']['#ajax'] = array( 'callback' => 'feeds_oai_pmh_ajax_callback', 'wrapper' => 'ahah-element', // ID of div element to update. 'method' => 'replace', 'effect' => 'fade', 'event' => 'change', ); // A set wrapper to handle replacement by AJAX callback $form['source']['#prefix'] = '
'; 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']); } elseif (isset($result['repository'])) { $sets_options = feeds_oai_pmh_sets_options($result['repository']['sets']); } else { $sets_options = feeds_oai_pmh_sets_options(array()); } } $form['set'] = array( '#type' => 'select', '#title' => t('Set to fetch'), //Patch : https://drupal.org/files/issues/feeds_oai_pmh-1208926-6-D7.patch //'#default_value' => isset($source_config['set']) ? $source_config['set'] : NULL, //'#options' => isset($sets_options) ? $sets_options : array(), '#default_value' => isset($source_config['set']) ? $source_config['set'] : 0, '#options' => isset($sets_options) ? $sets_options : array(0 => 'all sets'), '#suffix' => '', '#validated' => TRUE, '#ajax' => array( 'callback' => 'feeds_oai_pmh_ajax_callback', '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' => '
' . $msg . '
', ); } } $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'), // Form element IDs are edit-feeds-[feeds-object]-[form-element-id] '#states' => array( 'visible' => array( '#edit-feeds-feedsoaihttpfetcher-use-dates' => array('checked' => TRUE), ), ), ); if (isset($source_config['earliest_timestamp']) && $source_config['earliest_timestamp'] > 0) { $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.'), '#suffix' => '
', ); return $form; } /** * Override parent::sourceFormValidate(). */ public function sourceFormValidate(&$values) { require_once(drupal_get_path('module', 'feeds_oai_pmh') . '/feeds_oai_pmh.inc'); $result = feeds_oai_pmh_identify($values['source']); if ($result['status'] != 0) { return; } // 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']); } }