From a3a92b3e58781461189881ee41724fac5b91bcf1 Mon Sep 17 00:00:00 2001 From: Allison Sherrick <67483756+alliomeria@users.noreply.github.com> Date: Tue, 28 Nov 2023 14:42:11 -0500 Subject: [PATCH 1/4] Update AuthAutocompleteController.php For https://github.com/esmero/ami/issues/185, to complement https://github.com/esmero/ami/pull/186 Update logic for processing MeSH queries: - for count > 0 (instead of 1) - make use of array_unshift to better sort and select results that more closely match original query Coded live by @DiegoPino and shared with @alliomeria for local testing. Thank you @DiegoPino! --- src/Controller/AuthAutocompleteController.php | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Controller/AuthAutocompleteController.php b/src/Controller/AuthAutocompleteController.php index 6beeb69..8e2aac1 100644 --- a/src/Controller/AuthAutocompleteController.php +++ b/src/Controller/AuthAutocompleteController.php @@ -916,24 +916,31 @@ protected function mesh($input, $vocab, $rdftype) { return $results; } - $input = rawurlencode($input); - $urlindex = "/mesh/lookup/{$vocab}?label=" . $input .'&limit=10&match=' . $rdftype; - $baseurl = 'https://id.nlm.nih.gov'; - $remoteUrl = $baseurl . $urlindex; - $options['headers'] = ['Accept' => 'application/json']; - $body = $this->getRemoteJsonData($remoteUrl, $options); - $results = []; - $jsondata = json_decode($body, TRUE); - $json_error = json_last_error(); - if ($json_error == JSON_ERROR_NONE) { - if (count($jsondata) > 1) { - foreach ($jsondata as $entry) { - $results[] = [ - 'value' => $entry['resource'], - 'label' => $entry['label'], - ]; - } +$input_encoded = rawurlencode($input); +$urlindex = "/mesh/lookup/{$vocab}?label=" . $input_encoded .'&limit=10&match=' . $rdftype; +$baseurl = 'https://id.nlm.nih.gov'; +$remoteUrl = $baseurl . $urlindex; +$options['headers'] = ['Accept' => 'application/json']; +$body = $this->getRemoteJsonData($remoteUrl, $options); +$results = []; +$jsondata = json_decode($body, TRUE); +$json_error = json_last_error(); +if ($json_error == JSON_ERROR_NONE) { + if (count($jsondata) > 0) { + foreach ($jsondata as $entry) { + if (strtolower(trim($entry['label'] ?? '')) == strtolower($input)) { + array_unshift($results, [ + 'value' => $entry['resource'], + 'label' => $entry['label'], + ]); + } else { + $results[] = [ + 'value' => $entry['resource'], + 'label' => $entry['label'], + ]; } + } + } else { $results[] = [ 'value' => NULL, From 53ef3d8b7effdb36f660cbe86a40af72f3b4bda3 Mon Sep 17 00:00:00 2001 From: Allison Sherrick <67483756+alliomeria@users.noreply.github.com> Date: Tue, 28 Nov 2023 15:47:56 -0500 Subject: [PATCH 2/4] Update AuthAutocompleteController.php --- src/Controller/AuthAutocompleteController.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Controller/AuthAutocompleteController.php b/src/Controller/AuthAutocompleteController.php index 8e2aac1..5d63982 100644 --- a/src/Controller/AuthAutocompleteController.php +++ b/src/Controller/AuthAutocompleteController.php @@ -916,18 +916,18 @@ protected function mesh($input, $vocab, $rdftype) { return $results; } -$input_encoded = rawurlencode($input); -$urlindex = "/mesh/lookup/{$vocab}?label=" . $input_encoded .'&limit=10&match=' . $rdftype; -$baseurl = 'https://id.nlm.nih.gov'; -$remoteUrl = $baseurl . $urlindex; -$options['headers'] = ['Accept' => 'application/json']; -$body = $this->getRemoteJsonData($remoteUrl, $options); -$results = []; -$jsondata = json_decode($body, TRUE); -$json_error = json_last_error(); -if ($json_error == JSON_ERROR_NONE) { - if (count($jsondata) > 0) { - foreach ($jsondata as $entry) { + $input_encoded = rawurlencode($input); + $urlindex = "/mesh/lookup/{$vocab}?label=" . $input_encoded .'&limit=10&match=' . $rdftype; + $baseurl = 'https://id.nlm.nih.gov'; + $remoteUrl = $baseurl . $urlindex; + $options['headers'] = ['Accept' => 'application/json']; + $body = $this->getRemoteJsonData($remoteUrl, $options); + $results = []; + $jsondata = json_decode($body, TRUE); + $json_error = json_last_error(); + if ($json_error == JSON_ERROR_NONE) { + if (count($jsondata) > 0) { + foreach ($jsondata as $entry) { if (strtolower(trim($entry['label'] ?? '')) == strtolower($input)) { array_unshift($results, [ 'value' => $entry['resource'], From 3715c362d37e791a3b5e2aafc0926c1455114699 Mon Sep 17 00:00:00 2001 From: Allison Sherrick <67483756+alliomeria@users.noreply.github.com> Date: Wed, 29 Nov 2023 11:54:45 -0500 Subject: [PATCH 3/4] Update AuthAutocompleteController.php Spacing adjustments --- src/Controller/AuthAutocompleteController.php | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Controller/AuthAutocompleteController.php b/src/Controller/AuthAutocompleteController.php index 5d63982..10b7136 100644 --- a/src/Controller/AuthAutocompleteController.php +++ b/src/Controller/AuthAutocompleteController.php @@ -925,26 +925,27 @@ protected function mesh($input, $vocab, $rdftype) { $results = []; $jsondata = json_decode($body, TRUE); $json_error = json_last_error(); - if ($json_error == JSON_ERROR_NONE) { + if ($json_error == JSON_ERROR_NONE) { if (count($jsondata) > 0) { - foreach ($jsondata as $entry) { - if (strtolower(trim($entry['label'] ?? '')) == strtolower($input)) { - array_unshift($results, [ - 'value' => $entry['resource'], - 'label' => $entry['label'], - ]); - } else { - $results[] = [ - 'value' => $entry['resource'], - 'label' => $entry['label'], - ]; + foreach ($jsondata as $entry) { + if (strtolower(trim($entry['label'] ?? '')) == strtolower($input)) { + array_unshift($results, [ + 'value' => $entry['resource'], + 'label' => $entry['label'], + ]); + } + else { + $results[] = [ + 'value' => $entry['resource'], + 'label' => $entry['label'], + ]; + } + } } - } - } else { $results[] = [ - 'value' => NULL, - 'label' => "Sorry no match from MeSH for {$vocab}", + 'value' => NULL, + 'label' => "Sorry no match from MeSH for {$vocab}", ]; } return $results; From dcbd1db662c22dff214996d870d61883e4b0805e Mon Sep 17 00:00:00 2001 From: Allison Sherrick <67483756+alliomeria@users.noreply.github.com> Date: Wed, 29 Nov 2023 11:56:11 -0500 Subject: [PATCH 4/4] Update AuthAutocompleteController.php Little more --- src/Controller/AuthAutocompleteController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Controller/AuthAutocompleteController.php b/src/Controller/AuthAutocompleteController.php index 10b7136..397c89a 100644 --- a/src/Controller/AuthAutocompleteController.php +++ b/src/Controller/AuthAutocompleteController.php @@ -930,22 +930,22 @@ protected function mesh($input, $vocab, $rdftype) { foreach ($jsondata as $entry) { if (strtolower(trim($entry['label'] ?? '')) == strtolower($input)) { array_unshift($results, [ - 'value' => $entry['resource'], - 'label' => $entry['label'], + 'value' => $entry['resource'], + 'label' => $entry['label'], ]); } else { $results[] = [ - 'value' => $entry['resource'], - 'label' => $entry['label'], + 'value' => $entry['resource'], + 'label' => $entry['label'], ]; } } } else { $results[] = [ - 'value' => NULL, - 'label' => "Sorry no match from MeSH for {$vocab}", + 'value' => NULL, + 'label' => "Sorry no match from MeSH for {$vocab}", ]; } return $results;