Ejemplo n.º 1
0
 $json = get($biostor . '/reference/' . $id . '.bibjson');
 if ($json != '') {
     $reference = json_decode($json);
     if ($reference) {
         // ignore stuff tnot linked to BHL
         $go = true;
         if ($reference->link) {
             foreach ($reference->link as $link) {
                 if ($link->url == 'http://www.biodiversitylibrary.org/page/0') {
                     $go = false;
                 }
             }
         }
         if ($go) {
             $reference->_id = 'biostor/' . $id;
             $reference->citation = reference_to_citation_string($reference);
             // thumbnail
             $url = $biostor . '/reference/' . $id . '.json';
             $json = get($url);
             if ($json != '') {
                 $obj = json_decode($json);
                 // thumbnail
                 $reference->thumbnail = $obj->thumbnails[0];
                 // date
                 if (isset($obj->date)) {
                     $reference->date = array();
                     if (preg_match('/(?<year>[0-9]{4})/', $obj->date, $m)) {
                         $reference->date[] = (int) $m['year'];
                     }
                     if (preg_match('/(?<year>[0-9]{4})-(?<month>\\d+)/', $obj->date, $m)) {
                         $month = preg_replace('/^0+/', '', $m['month']);
Ejemplo n.º 2
0
/**
 * @brief Handle OpenURL request
 *
 * We may have more than one parameter with same name, so need to access QUERY_STRING, not _GET
 * http://stackoverflow.com/questions/353379/how-to-get-multiple-parameters-with-same-name-from-a-url-in-php
 *
 */
function main()
{
    global $config;
    global $couch;
    global $debug;
    $debug = false;
    $webhook = '';
    $callback = '';
    // If no query parameters
    if (count($_GET) == 0) {
        //display_form();
        exit(0);
    }
    if (isset($_GET['webhook'])) {
        $webhook = $_GET['webhook'];
    }
    if (isset($_GET['debug'])) {
        $debug = true;
    }
    if (isset($_GET['callback'])) {
        $callback = $_GET['callback'];
    }
    // Handle query and display results.
    $query = explode('&', html_entity_decode($_SERVER['QUERY_STRING']));
    $params = array();
    foreach ($query as $param) {
        list($key, $value) = explode('=', $param);
        $key = preg_replace('/^\\?/', '', urldecode($key));
        $params[$key][] = trim(urldecode($value));
    }
    if ($debug) {
        echo '<h1>Params</h1>';
        echo '<pre>';
        print_r($params);
        echo '</pre>';
    }
    // This is what we got from user
    $context_object = new stdclass();
    parse_openurl($params, $context_object);
    if ($debug) {
        echo '<h1>Referent</h1>';
        echo '<pre>';
        print_r($context_object);
        echo '</pre>';
    }
    // OK, can we find this?
    // result object
    $openurl_result = new stdclass();
    $openurl_result->results = array();
    $openurl_result->query_ok = false;
    // via DOI or other identifier
    if (count($openurl_result->results) == 0) {
        // identifier search
        if (isset($context_object->referent->identifier)) {
            //print_r($context_object->referent->identifier);
            $found = false;
            foreach ($context_object->referent->identifier as $identifier) {
                //print_r($identifier);
                if (!$found) {
                    switch ($identifier->type) {
                        case 'doi':
                            $url = $config['couchdb_options']['database'] . "/_design/identifier/_view/doi?key=" . urlencode('"' . $identifier->id . '"');
                            //echo $url . '<br />';
                            $resp = $couch->send("GET", "/" . $url . '&limit=1');
                            $result = json_decode($resp);
                            if (count($result->rows) == 1) {
                                $found = true;
                                $match = new stdclass();
                                $match->match = true;
                                $match->id = $result->rows[0]->id;
                                $match->{$identifier->type} = $identifier->id;
                                $openurl_result->results[] = $match;
                                $openurl_result->query_ok = true;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
    // classical OpenURL lookup using [ISSN,volume,spage] triple
    if (count($openurl_result->results) == 0) {
        $triple = false;
        if (isset($context_object->referent->journal->name)) {
            $journal = $context_object->referent->journal->name;
            $journal = strtolower($journal);
            $journal = preg_replace('/\\bfor\\b/', '', $journal);
            $journal = preg_replace('/\\band\\b/', '', $journal);
            $journal = preg_replace('/\\bof\\b/', '', $journal);
            $journal = preg_replace('/\\bthe\\b/', '', $journal);
            // whitespace
            $journal = preg_replace('/\\s+/', '', $journal);
            // punctuation
            $journal = preg_replace('/[\\.|,|\'|\\(|\\)]+/', '', $journal);
            if (isset($context_object->referent->journal->volume)) {
                $volume = $context_object->referent->journal->volume;
                if (isset($context_object->referent->journal->pages)) {
                    if (is_numeric($context_object->referent->journal->pages)) {
                        $spage = $context_object->referent->journal->pages;
                        $triple = true;
                    } else {
                        if (preg_match('/^(?<spage>\\d+)--(?<epage>\\d+)$/', $context_object->referent->journal->pages, $m)) {
                            $spage = $m['spage'];
                            $triple = true;
                        }
                    }
                }
            }
            if ($triple) {
                $openurl_result->query_ok = true;
                // i. get ISSN
                $url = $config['couchdb_options']['database'] . "/_design/openurl/_view/journal_to_issn?key=" . urlencode('"' . $journal . '"');
                //echo $url;
                $resp = $couch->send("GET", "/" . $url . '&limit=1');
                $result = json_decode($resp);
                if (count($result->rows) == 1) {
                    // we have an ISSN for this journal
                    $issn = $result->rows[0]->value;
                    // build triple [ISSN, volume, spage]
                    $keys = array($issn, $volume, $spage);
                    $url = $config['couchdb_options']['database'] . "/_design/openurl/_view/triple?key=" . urlencode(json_encode($keys));
                    //echo $url . '<br />';
                    $resp = $couch->send("GET", "/" . $url);
                    $r = json_decode($resp);
                    //print_r($r);
                    if (count($r->rows) > 0) {
                        foreach ($r->rows as $row) {
                            $match = new stdclass();
                            $match->match = true;
                            $match->triple = $keys;
                            $match->id = $row->id;
                            $openurl_result->results[] = $match;
                        }
                    }
                }
            }
        }
    }
    // full text search
    if (count($openurl_result->results) == 0) {
        find_citation(reference_to_citation_string($context_object->referent), $openurl_result);
    }
    // ok, if we have one or more results we return these and let user/agent decide what to do
    // populate with details
    $num_results = count($openurl_result->results);
    for ($i = 0; $i < $num_results; $i++) {
        $resp = $couch->send("GET", "/" . $config['couchdb_options']['database'] . "/" . urlencode($openurl_result->results[$i]->id));
        $reference = json_decode($resp);
        if (!isset($reference->error)) {
            $openurl_result->results[$i]->reference = $reference;
        }
    }
    // for now just return JSON
    if ($callback != '') {
        echo $callback . '(';
    }
    echo json_encode($openurl_result);
    if ($callback != '') {
        echo ')';
    }
}