Example #1
0
                        $match = new stdclass();
                        $match->text = $citation;
                        $match->hit = $hit_original;
                        $match->match = true;
                        $match->id = $row->id;
                        $match->score = $row->order[0];
                        $match->symdiff = $symdiff;
                        if ($symdiff > $best_hit) {
                            $result->results = array();
                        }
                        $result->results[] = $match;
                    }
                }
            }
        }
    }
    return count($result->results) > 1;
}
if (0) {
    // test
    $result = new stdclass();
    $result->results = array();
    $result->query_ok = false;
    $q = 'Monroe, R (1977) A new species of Euastacus (Decapoda: Parastacidae) from north Queensland. Memoirs of the Queensland Museum 18:65-67';
    $q = 'Clark, E (1936) The freshwater and land crayfishes of Australia. Memoirs of the Natural Museum of Victoria 10:5-58';
    find_citation($q, $result);
    print_r($result);
}
?>

Example #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 ')';
    }
}