|                                                                                                                    | 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 | <?php
function solr_search($search, $core, $sort){
  if ($sort == 'relevance'){
    // Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
    $solrurl = 'http://' . $_ENV["SOLR_HOSTNAME"] . ':' . $_ENV["SOLR_PORT"] . '/solr/' . $core . '/select?q.op=OR&q=content%3A' . $search . '&wt=json';
  }
  else{
    // Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
    $solrurl = 'http://' . $_ENV["SOLR_HOSTNAME"] . ':' . $_ENV["SOLR_PORT"] . '/solr/' . $core . '/select?q.op=OR&q=content%3A' . $search . '&wt=json&sort=' . $sort . '%20asc';
  }
  // Perform Curl request on the Solr API
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $solrurl);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  $response = curl_exec($ch);
  curl_close($ch);
  // Turn the API response into useful Json
  $json = json_decode($response);
  // If no results are found, display a message
  if ($json->response->numFound == '0'){
    $output = 'no results found';
  }
  else{
    foreach ($json->response->docs as $result){
      $id = $result->id;
      $content = $result->content;
      $result_output = parse_result($id, $content);
      $output[] = $result_output;
     }
  }
  return $output;
}
function solr_search_id($id, $core){
  // URL encode the ID string
  $id = urlencode($id);
  // Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
  $solrurl = 'http://' . $_ENV["SOLR_HOSTNAME"] . ':' . $_ENV["SOLR_PORT"] . '/solr/' . $core . '/select?q.op=OR&q=id%3A"' . $id . '"&wt=json';
  // Perform Curl request on the Solr API
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $solrurl);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  $response = curl_exec($ch);
  curl_close($ch);
  // Turn the API response into useful Json
  $json = json_decode($response);
  // If no results are found, display a message
  if ($json->response->numFound == '0'){
    $output = 'no results found';
  }
  else{
    foreach ($json->response->docs as $result){
      $id = $result->id;
      $content = $result->content;
      $result_output = parse_result($id, $content);
      $output[] = $result_output;
    }
  }
  return $output;
}
function parse_result($id, $input){
  $output['id'] = $id;
  //Set document reference number (used for OPS API)
  if (preg_match('/=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))/', $input, $doc_ref)){
    $output['doc_ref'] = str_replace(' ','',$doc_ref[1]);
  }
  // Search for the application ID in the content element and display it
  preg_match('/Application.*\n(.*)\n/', $input, $application_id);
  $output['application_id'] = $application_id[1];
  // Search for the EPO publication URL in the content element and display it
  preg_match('/Publication.*\n(.*)\n/', $input, $epo_publication);
  $output['epo_publication_url'] = $epo_publication[1];
  // Search for the IPC publication URL in the content element and display it
  preg_match('/IPC.*\n(.*)\n/', $input, $ipc_publication);
  $output['ipc_publication_url'] = $ipc_publication[1];
  // Search for the title in the content element and display it
  if (preg_match('/Title.*\n(.*)\n/', $input, $title)){
    $output['title'] = $title[1];
  }
  // Search for the abstract in the content element and display it
  if (preg_match('/Abstract.*\n(.*)\n/', $input, $abstract)){
    $output['abstract'] = $abstract[1];
  }
  elseif (preg_match('/\(.\) \\n\\n(.*)\\n/', $input, $abstract)) {
    $output['abstract'] = $abstract[1];
  }
  // Search for the year in the content element and display it
  if (preg_match('/=D[^\s]*\s[^\s]*\s[^\s]*\s[^\s]*\s(\d{4})/', $input, $year)){
    $output['year'] = $year[1];
  }
  return $output;
}
function get_random_record($core){
    // Generate a random number for sorting by random
    $random = rand();
    // Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
    // This query retrieves only the bib identifier field for records which satisfy the search query
    $solrurl = 'http://' . $_ENV["SOLR_HOSTNAME"] . ':' . $_ENV["SOLR_PORT"] . '/solr/' . $core . '/select?q.op=OR&q=*%3A*&wt=json&sort=random_' . $random . '%20asc';
    // Perform Curl request on the Solr API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $solrurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    $response = curl_exec($ch);
    curl_close($ch);
    // Turn the API response into useful Json
    $json = json_decode($response);
    // Pick a random key out of the docs array
    $random = array_rand($json->response->docs);
    //Set ID variable
    $id = $json->response->docs[$random]->id;
    //Set content variable
    $content = $json->response->docs[$random]->content;
    //Construct associative array with ID and content
    $result_array = array($id=>$content);
    return $result_array;
}
function one_random_record ($core){
  $random = get_random_record($core);
  foreach ($random as $id => $content){
    $output = parse_result($id, $content);
  }
  return $output;
}
function ten_random_titles ($core){
  for ($x=0; $x <= 9; $x++) {
    $random = get_random_record($core);
    foreach($random as $id => $content){
      // Search for the title in the content element and display it
      if (preg_match('/Title.*\n(.*)\n/', $content, $title)){
        $output[$x] = array($id=>$title[1]);
      }
    }
  }
  return $output;
}
function ten_random_abstracts ($core){
  for ($x=0; $x <= 9; $x++) {
    $random = get_random_record($core);
    foreach($random as $id => $content){
      // Search for the abstract in the content element and display it
      if (preg_match('/Abstract.*\n(.*)\n/', $content, $abstract)){
        $output[$x] = array($id=>$abstract[1]);
      }
      elseif (preg_match('/\(.\) \\n\\n(.*)\\n/', $content, $abstract)) {
        $output[$x] = array($id=>$abstract[1]);
      }
    }
  }
  return $output;
}
function ten_random_doc_refs ($core){
  $x = 0;
  while ($x < 9) {
    $random = get_random_record($core);
    foreach($random as $id => $content){
      //Set document reference number (used for OPS API)
      preg_match('/=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))/', $content, $doc_ref);
      $doc_ref = str_replace(' ','',$doc_ref[1]);
      if (check_for_images($doc_ref)){
        $output[$x] = $doc_ref;
        ++$x;
      }
    }
  }
  return $output;
}
?>
 |