瀏覽代碼

New feature to retrieve 10 random titles

main
Simon Bowie 3 年之前
父節點
當前提交
669db56b5b
共有 4 個文件被更改,包括 112 次插入74 次删除
  1. +4
    -0
      site/public/index.html
  2. +1
    -1
      site/public/random.php
  3. +41
    -0
      site/public/titles.php
  4. +66
    -73
      site/solr.php

+ 4
- 0
site/public/index.html 查看文件

@@ -31,6 +31,10 @@
<a href="./compare.php">compare two random records</a>
</div>

<div class="centre">
<a href="./titles.php">ten random titles</a>
</div>

<div class="centre">
<form action="search.php" method="POST">
<input type="hidden" name="search" value="leak">

+ 1
- 1
site/public/random.php 查看文件

@@ -10,7 +10,7 @@
include '../solr.php';
include '../ops_api.php';

$random_record = random_record();
$random_record = one_random_record();

?>


+ 41
- 0
site/public/titles.php 查看文件

@@ -0,0 +1,41 @@
<?php

# @name: titles.php
# @version: 0.1
# @creation_date: 2021-09-24
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: Displays ten random titles

include '../solr.php';

$random_titles = ten_random_titles();

?>

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>

<?php

foreach ($random_titles as $title):

?>

<?
echo $title;
?>

<br><br>

<hr>

<?php
endforeach;
?>

</body>
</html>

+ 66
- 73
site/solr.php 查看文件

@@ -23,109 +23,102 @@ function solr_search($search){
$output = 'No results found';
}
else{
$output = parse_results($json);
foreach ($json->response->docs as $result){
$content = $result->content;
$result_output = parse_result($content);
$results[] = $result_output;
}
}

return $output;

return $results;
}

function parse_results($json_response){

foreach ($json_response->response->docs as $result){

$content = $result->content;

//Set document reference number (used for OPS API)
preg_match('/=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))/', $content, $doc_ref);
$result_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/', $content, $application_id);
$result_output['application_id'] = $application_id[1];
function parse_result($input){

// Search for the EPO publication URL in the content element and display it
preg_match('/Publication.*\n(.*)\n/', $content, $epo_publication);
$result_output['epo_publication_url'] = $epo_publication[1];
//Set document reference number (used for OPS API)
preg_match('/=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))/', $input, $doc_ref);
$output['doc_ref'] = str_replace(' ','',$doc_ref[1]);

// Search for the IPC publication URL in the content element and display it
preg_match('/IPC.*\n(.*)\n/', $content, $ipc_publication);
$result_output['ipc_publication_url'] = $ipc_publication[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 title in the content element and display it
preg_match('/Title.*\n(.*)\n/', $content, $title);
$result_output['title'] = $title[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 abstract in the content element and display it
preg_match('/Abstract.*\n(.*)\n/', $content, $abstract);
$result_output['abstract'] = $abstract[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];

$results[] = $result_output;
// Search for the title in the content element and display it
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];
}
else {
preg_match('/\(.\) \\n\\n(.*)\\n/', $input, $abstract);
$output['abstract'] = $abstract[1];
}

return $results;
return $output;

}

function random_record (){
function get_random_record(){

// Generate a random number for sorting by random
$random = rand();
// 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/' . $_ENV["SOLR_CORE"] . '/select?q.op=OR&q=*%3A*&wt=json&sort=random_' . $random . '%20asc';
// 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/' . $_ENV["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);
// 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);
// 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);
// Pick a random key out of the docs array
$random = array_rand($json->response->docs);

//Set content variable
$content = $json->response->docs[$random]->content;
//Set content variable
$content = $json->response->docs[$random]->content;

//Set document reference number (used for OPS API)
preg_match('/=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))/', $content, $doc_ref);
$output['doc_ref'] = str_replace(' ','',$doc_ref[1]);
return $content;
}

// Search for the application ID in the content element and display it
preg_match('/Application.*\n(.*)\n/', $content, $application_id);
$output['application_id'] = $application_id[1];
function one_random_record (){

// Search for the EPO publication URL in the content element and display it
preg_match('/Publication.*\n(.*)\n/', $content, $epo_publication);
$output['epo_publication_url'] = $epo_publication[1];
$content = get_random_record();

// Search for the IPC publication URL in the content element and display it
preg_match('/IPC.*\n(.*)\n/', $content, $ipc_publication);
$output['ipc_publication_url'] = $ipc_publication[1];
$output = parse_result($content);

// Search for the title in the content element and display it
preg_match('/Title.*\n(.*)\n/', $content, $title);
$output['title'] = $title[1];
return $output;

// Search for the abstract in the content element and display it
if (preg_match('/Abstract.*\n(.*)\n/', $content, $abstract)){
$output['abstract'] = $abstract[1];
}
else {
preg_match('/\(.\) \\n\\n(.*)\\n/', $content, $abstract);
$output['abstract'] = $abstract[1];
}

function ten_random_titles (){

for ($x=0; $x <= 9; $x++) {
$random = get_random_record();
// Search for the title in the content element and display it
preg_match('/Title.*\n(.*)\n/', $random, $title);
$output[$x] = $title[1];
}

return $output;

}


?>

Loading…
取消
儲存