Exemplo n.º 1
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 $debug;
    global $format;
    $id = 0;
    $callback = '';
    // If no query parameters
    if (count($_GET) == 0) {
        display_form();
        exit(0);
    }
    if (isset($_GET['format'])) {
        switch ($_GET['format']) {
            case 'html':
                $format = 'html';
                break;
            case 'json':
                $format = 'json';
                break;
            default:
                $format = 'html';
                break;
        }
    }
    if (isset($_GET['callback'])) {
        $callback = $_GET['callback'];
    }
    $debug = false;
    if (isset($_GET['debug'])) {
        $debug = true;
    }
    // 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
    $referent = new stdclass();
    parse_openurl($params, $referent);
    // Flesh it out
    // If we are looking for an article we need an ISSN, or at least an OCLC
    // Ask whether have this in our database (assumes we have ISSN)
    if (!isset($referent->issn)) {
        // Try and get ISSN from bioGUID
        $issn = issn_from_title($referent->secondary_title);
        if ($issn != '') {
            $referent->issn = $issn;
        } else {
            // No luck with ISSN, look for OCLC
            if (!isset($referent->oclc)) {
                $oclc = oclc_for_title($referent->secondary_title);
                if ($oclc != 0) {
                    $referent->oclc = $oclc;
                }
            }
        }
    }
    if ($debug) {
        echo '<h1>Referent</h1>';
        echo '<pre>';
        print_r($referent);
        echo '</pre>';
    }
    // Handle identifiers
    if (isset($referent->url)) {
        // BHL URL, for example if we have already mapped article to BHL
        // in Zotero,
        if (preg_match('/^http:\\/\\/(www\\.)?biodiversitylibrary.org\\/page\\/(?<pageid>[0-9]+)/', $referent->url, $matches)) {
            //print_r($matches);
            $PageID = $matches['pageid'];
            $references = bhl_reference_from_pageid($PageID);
            //print_r($references);
            if (count($references) == 0) {
                // We don't have an article for this PageID
                $search_hit = bhl_score_page($PageID, $referent->title);
                // Store
                $id = db_store_article($referent, $PageID);
            } else {
                // Have a reference with this PageID already
                // Will need to handle case where > 1 article on same page, e.g.
                // http://www.biodiversitylibrary.org/page/3336598
                $id = $references[0];
            }
            // Did we get a hit?
            if ($id != 0) {
                // We have this reference in our database
                switch ($format) {
                    case 'json':
                        // Display object
                        $reference = db_retrieve_reference($id);
                        header("Content-type: text/plain; charset=utf-8\n\n");
                        if ($callback != '') {
                            echo $callback . '(';
                        }
                        echo json_format(json_encode($reference));
                        if ($callback != '') {
                            echo ')';
                        }
                        break;
                    case 'html':
                    default:
                        // Redirect to reference display
                        header('Location: ' . $config['web_root'] . 'reference/' . $id . "\n\n");
                        break;
                }
                exit;
            }
        }
    }
    // OK, we're not forcing a match to BHL, so do we have this article?
    $id = db_find_article($referent);
    //echo "<b>id=$id</b><br/>";
    if ($id != 0) {
        // We have this reference in our database
        switch ($format) {
            case 'json':
                // Display object
                $reference = db_retrieve_reference($id);
                header("Content-type: text/plain; charset=utf-8\n\n");
                if ($callback != '') {
                    echo $callback . '(';
                }
                echo json_format(json_encode($reference));
                if ($callback != '') {
                    echo ')';
                }
                break;
            case 'html':
            default:
                // Twitter as log
                if ($config['twitter']) {
                    $tweet_this = false;
                    $tweet_this = isset($_GET['rfr_id']);
                    if ($tweet_this) {
                        $url = $config['web_root'] . 'reference/' . $id . ' ';
                        //  . '#openurl'; // url + hashtag
                        $url = $id;
                        $url_len = strlen($url);
                        $status = '';
                        //$text = $_GET['rfr_id'];
                        $text = '#openurl ' . $_SERVER["HTTP_REFERER"];
                        //$text .= ' @rdmpage';
                        if (isset($article->title)) {
                        }
                        $status = $text;
                        $status_len = strlen($status);
                        $extra = 140 - $status_len - $url_len - 1;
                        if ($extra < 0) {
                            $status_len += $extra;
                            $status_len -= 1;
                            $status = substr($status, 0, $status_len);
                            $status .= '…';
                        }
                        $status .= ' ' . $url;
                        tweet($status);
                    }
                }
                // Redirect to reference display
                header('Location: reference/' . $id . "\n\n");
                break;
        }
        exit;
    }
    // OK, not found, so let's go look for it...
    // Search BHL
    $atitle = '';
    if (isset($referent->title)) {
        $atitle = $referent->title;
    }
    $search_hits = bhl_find_article($atitle, $referent->secondary_title, $referent->volume, isset($referent->spage) ? $referent->spage : $referent->pages, isset($referent->series) ? $referent->series : '', isset($referent->date) ? $referent->date : '', isset($referent->issn) ? $referent->issn : '');
    if (count($search_hits) == 0) {
        // try alternative way of searching using article title
        $search_hits = bhl_find_article_from_article_title($referent->title, $referent->secondary_title, $referent->volume, isset($referent->spage) ? $referent->spage : $referent->pages, isset($referent->series) ? $referent->series : '', isset($referent->issn) ? $referent->issn : '');
    }
    // At this point if we haven't found it in BHL we could go elsewhere, e.g. bioGUID,
    // in which case we'd need to take this into account when displaying HTML and JSON
    if ($debug) {
        echo '<h3>Search hits</h3>';
        echo '<pre>';
        print_r($search_hits);
        echo '</pre>';
    }
    if (1) {
        // Check whether we already have an article that starts on this
        foreach ($search_hits as $hit) {
            $references = bhl_reference_from_pageid($hit->PageID);
            //print_r($references);
            if (count($references) != 0) {
                // We have this reference in our database
                switch ($format) {
                    case 'json':
                        // Display object
                        $reference = db_retrieve_reference($references[0]);
                        header("Content-type: text/plain; charset=utf-8\n\n");
                        if ($callback != '') {
                            echo $callback . '(';
                        }
                        echo json_format(json_encode($reference));
                        if ($callback != '') {
                            echo ')';
                        }
                        break;
                    case 'html':
                    default:
                        // Redirect to reference display
                        header('Location: reference/' . $references[0] . "\n\n");
                        break;
                }
                exit;
            }
        }
    }
    // Output search results in various formats...
    switch ($format) {
        case 'json':
            display_bhl_result_json($referent, $search_hits, $callback);
            break;
        case 'html':
        default:
            display_bhl_result_html($referent, $search_hits);
            break;
    }
}
Exemplo 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 ')';
    }
}
Exemplo n.º 3
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 $debug;
    $webhook = '';
    // If no query parameters
    if (count($_GET) == 0) {
        //display_form();
        exit(0);
    }
    if (isset($_GET['webhook'])) {
        $webhook = $_GET['webhook'];
    }
    $debug = false;
    if (isset($_GET['debug'])) {
        $debug = true;
    }
    // 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>';
    }
    // Display form...
    display_form($context_object, $webhook);
}
Exemplo n.º 4
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) {
                if (!$found) {
                    //echo $identifier->type;
                    //echo $identifier->id;
                    switch ($identifier->type) {
                        case 'lsid':
                            // Look up directly using AFD id
                            if (preg_match('/urn:lsid:biodiversity.org.au:afd.publication:(?<id>.*)$/', $identifier->id, $m)) {
                                $resp = $couch->send("GET", "/" . $config['couchdb'] . "/" . urlencode($m['id']));
                                $reference = json_decode($resp);
                                if (!isset($reference->error)) {
                                    $found = true;
                                    $match = new stdclass();
                                    $match->match = true;
                                    $match->id = $reference->_id;
                                    $openurl_result->results[] = $match;
                                    $openurl_result->query_ok = true;
                                }
                            }
                            break;
                        case 'doi':
                            $url = $config['couchdb'] . "/_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;
                    }
                }
            }
        }
    }
    //print_r($openurl_result->results);
    // classical OpenURL lookup using [ISSN,volume,spage] triple
    // Use cleaned journal name (we don't have ISSNs for most AFD references
    // classical OpenURL lookup using [journal,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)
            if (0) {
                $openurl_result->query_ok = true;
                // build triple [journal, volume, spage]
                $keys = array($journal, $volume, $spage);
                //print_r($keys);
                $url = $config['couchdb'] . "/_design/openurl/_view/triple_journal?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'] . "/" . urlencode($openurl_result->results[$i]->id));
        $reference = json_decode($resp);
        if (!isset($reference->error)) {
            $openurl_result->results[$i]->reference = $reference;
        }
    }
    // decide what to do...
    $location = '';
    if ($num_results == 1) {
        // default location is AFD CouchDB
        $location = $config['web_server'] . $config['web_root'] . 'id/' . $openurl_result->results[0]->id;
        // if we have an external identifier send user there...
        if (isset($openurl_result->results[0]->reference->identifiers)) {
            foreach ($openurl_result->results[0]->reference->identifiers as $k => $v) {
                switch ($k) {
                    case 'doi':
                        $location = 'http://dx.doi.org/' . $v;
                        break;
                    case 'hdl':
                        $location = 'http://hdl.handle.net/' . $v;
                        break;
                    case 'biostor':
                        $location = 'http://biostor.org/reference/' . $v;
                        break;
                    default:
                        break;
                }
            }
        }
    }
    if ($location != '') {
        echo "<b>Location={$location}</b><br/>";
        header("Location: " . $location);
    } else {
        echo '<p>Not found :(</p>';
        /*
        // for now just return JSON	
        if ($callback != '')
        {
        	echo $callback . '(';
        }
        echo json_encode($openurl_result);
        if ($callback != '')
        {
        	echo ')';
        }
        */
    }
}
Exemplo n.º 5
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;
    $debug_openurl = 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_openurl = 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));
    }
    // This is what we got from user
    $context_object = new stdclass();
    parse_openurl($params, $context_object);
    //print_r($context_object);
    // OK, can we find this?
    // result object
    $openurl_result = new stdclass();
    $openurl_result->status = 404;
    $openurl_result->context_object = $context_object;
    $openurl_result->results = array();
    if ($debug_openurl) {
        $openurl_result->debug = new stdclass();
    }
    // 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) {
                if ($debug_openurl) {
                    $openurl_result->debug->identifiers[] = $identifier;
                }
                if (!$found) {
                    switch ($identifier->type) {
                        case 'doi':
                            $url = $config['couchdb_options']['database'] . "/_design/identifier/_view/doi?key=" . urlencode('"' . $identifier->id . '"');
                            //echo $url . '<br />';
                            if ($config['stale']) {
                                $url .= '&stale=ok';
                            }
                            $resp = $couch->send("GET", "/" . $url . '&limit=1');
                            $result = json_decode($resp);
                            if (count($result->rows) == 1) {
                                $openurl_result->debug->found_from_identifiers = true;
                                $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->status = 200;
                            }
                            break;
                        case 'pmid':
                            $url = $config['couchdb_options']['database'] . "/_design/identifier/_view/pmid?key=" . $identifier->id;
                            //echo $url . '<br />';
                            if ($config['stale']) {
                                $url .= '&stale=ok';
                            }
                            $resp = $couch->send("GET", "/" . $url . '&limit=1');
                            $result = json_decode($resp);
                            if (count($result->rows) == 1) {
                                $openurl_result->debug->found_from_identifiers = true;
                                $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->status = 200;
                            }
                            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;
            // Convert accented characters
            $journal = strtr(utf8_decode($journal), utf8_decode("ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž"), "aaaaaaaaaaaaaaaaaaccccccccccddddddeeeeeeeeeeeeeeeeeegggggggghhhhiiiiiiiiiiiiiiiiiijjkkkllllllllllnnnnnnnnnnnoooooooooooooooooorrrrrrsssssssssttttttuuuuuuuuuuuuuuuuuuuuwwyyyyyyzzzzzz");
            $journal = utf8_encode($journal);
            $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);
            $journal = preg_replace('/\\bde\\b/', '', $journal);
            $journal = preg_replace('/\\bla\\b/', '', $journal);
            $journal = preg_replace('/\\bet\\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) {
                if ($debug_openurl) {
                    $openurl_result->debug->triple = $triple;
                    $openurl_result->debug->journal = $journal;
                }
                $openurl_result->status = 200;
                // i. get ISSN
                $url = $config['couchdb_options']['database'] . "/_design/openurl/_view/journal_to_issn?key=" . urlencode('"' . $journal . '"');
                //echo $url;
                if ($config['stale']) {
                    $url .= '&stale=ok';
                }
                //echo $url;
                $resp = $couch->send("GET", "/" . $url . '&limit=1');
                $result = json_decode($resp);
                //print_r($resp);
                if (count($result->rows) == 1) {
                    // we have an ISSN for this journal
                    $issn = $result->rows[0]->value;
                    if ($debug_openurl) {
                        $openurl_result->debug->issn = $issn;
                    }
                    // build triple [ISSN, volume, spage]
                    $keys = array($issn, $volume, $spage);
                    if ($debug_openurl) {
                        $openurl_result->debug->keys = $keys;
                    }
                    $url = $config['couchdb_options']['database'] . "/_design/openurl/_view/issn_triple?key=" . urlencode(json_encode($keys));
                    //echo $url . '<br />';
                    if ($config['stale']) {
                        $url .= '&stale=ok';
                    }
                    $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;
                        }
                    } else {
                        // No hit, try converting volume to/from Roman
                        if (is_numeric($volume)) {
                            $volume = strtolower(roman($volume));
                        } else {
                            $volume = arabic($volume);
                        }
                        $keys = array($issn, $volume, $spage);
                        if ($debug_openurl) {
                            $openurl_result->debug->keys = $keys;
                        }
                        $url = $config['couchdb_options']['database'] . "/_design/openurl/_view/triple?key=" . urlencode(json_encode($keys));
                        //echo $url . '<br />';
                        if ($config['stale']) {
                            $url .= '&stale=ok';
                        }
                        $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)
    {			
    	// Has user supplied an unparsed string?
    	if (isset($context_object->referent->dat))
    	{
    		find_citation($context_object->referent->dat, $openurl_result, 0.7);
    	}
    	else
    	{
    		find_citation(reference_to_citation_string($context_object->referent), $openurl_result, 0.7);
    	}
    }	
    */
    // ok, if we have one or more results we return these and let user/agent decide what to do
    $num_results = count($openurl_result->results);
    if ($context_object->redirect) {
        $id = 0;
        if ($num_results == 1) {
            if ($openurl_result->results[0]->match) {
                // I'm feeling lucky
                $id = $openurl_result->results[0]->id;
            }
        }
        // Redirect to reference display, if not found then id is 0 and so we get default error message
        header('Location: reference/' . $id . "\n\n");
    } else {
        // API call return, populated with details
        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;
            }
        }
        api_output($openurl_result, $callback);
    }
}