View source
<?php
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',
'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;
}
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;
}
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;
}
function feeds_oai_pmh_set_ahah() {
if ($form = form_get_cache($_POST['form_build_id'], $form_state)) {
$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) {
$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 {
$form['feeds']['FeedsOAIHTTPFetcher']['set']['#options'] = feeds_oai_pmh_sets_options(array());
$form['feeds']['FeedsOAIHTTPFetcher']['dates']['#description'] = '';
drupal_set_message($result['output'], 'error');
}
form_set_cache($_POST['form_build_id'], $form, $form_state);
$form_state = array();
$form['#post'] = array();
$form = form_builder($form['form_id']['#value'], $form, $form_state);
$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;
}
function feeds_oai_pmh_current_status_msg($source, $set) {
$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;
}